极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

楼主: zcbzjx

基于18B20+enc28j60+arduino+yeelink的远程温度监控

[复制链接]
发表于 2012-11-20 21:31:23 | 显示全部楼层
问题是,此贴的网卡怎么修改为5100?

或者,下面这个官方例程,怎么把传感器部分,修改为单个18b20或者多个

物联网跟我动手做系列教程—第三篇 实验二
如何用arduino+ethernet shield与yeelink结合5分钟实现传感器数据web上传
http://blog.yeelink.net/?p=34

///////////////////////////////////////////////////////////////////////////
// get data from light sensor
// you can replace this code for your sensor
int readLightSensor()
{
  uint16_t val=0;
  BH1750_Init(BH1750address);
  delay(200);
  if(2==BH1750_Read(BH1750address))
  {
    val=((buff[0]<<8)|buff[1])/1.2;
  }

  Serial.print("Sensor value is: ");
  Serial.println((int)val);

  return val;
}

int BH1750_Read(int address) //
{
  int i=0;
  Wire.beginTransmission(address);
  Wire.requestFrom(address, 2);
  while(Wire.available()) //
  {
    buff = Wire.read();  // receive one byte
    i++;
  }
  Wire.endTransmission();
  return i;
}

void BH1750_Init(int address)
{
  Wire.beginTransmission(address);
  Wire.write(0x10);//1lx reolution 120ms
  Wire.endTransmission();
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-20 21:54:56 | 显示全部楼层
家里没arduino,没法调试,明天我到办公室给你弄吧
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-21 10:27:46 | 显示全部楼层
本帖最后由 zcbzjx 于 2012-11-21 10:34 编辑
  1. /*
  2. Yeelink sensor client example
  3. */

  4. #include <SPI.h>
  5. #include <Ethernet.h>
  6. #include <OneWire.h>

  7. #define ONEWIRE_PIN A0

  8. OneWire ds(ONEWIRE_PIN);


  9. byte buff[2];

  10. // for yeelink api
  11. #define APIKEY         "xxxxxxxxxxxxxxxxxxxxxxxxxxx" // replace your yeelink api key here
  12. #define DEVICEID       xx // replace your device ID
  13. #define SENSORID       xxx // replace your sensor ID

  14. // assign a MAC address for the ethernet controller.
  15. byte mac[] = {
  16.   0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
  17. IPAddress myip(10,21,0,195);
  18. IPAddress mydns(10,11,5,25);
  19. IPAddress mygateway(10,21,0,1);

  20. // initialize the library instance:
  21. EthernetClient client;
  22. char server[] = "api.yeelink.net";   // name address for yeelink API

  23. unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
  24. boolean lastConnected = false;                 // state of the connection last time through the main loop
  25. const unsigned long postingInterval = 15*1000; // delay between 2 datapoints, 30s

  26. // Some global variables
  27. char sensorData[20];
  28. uint8_t dataLength;

  29. void setup() {
  30.   // Start up the dallas library

  31.   // start serial port:
  32.   Serial.begin(57600);
  33.   // start the Ethernet connection with DHCP:
  34.   Ethernet.begin(mac,myip,mydns,mygateway);
  35. }

  36. void loop() {
  37.   // if there's incoming data from the net connection.
  38.   // send it out the serial port.  This is for debugging
  39.   // purposes only:
  40.   if (client.available()) {
  41.     char c = client.read();
  42.     Serial.print(c);
  43.   }

  44.   // if there's no net connection, but there was one last time
  45.   // through the loop, then stop the client:
  46.   if (!client.connected() && lastConnected) {
  47.     Serial.println();
  48.     Serial.println("disconnecting.");
  49.     client.stop();
  50.   }

  51.   // if you're not connected, and ten seconds have passed since
  52.   // your last connection, then connect again and send data:
  53.   if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
  54.     // read sensor data, replace with your code
  55.     get_Send_String();
  56.     Serial.println(sensorData);
  57.     //send data to server

  58.     sendData(sensorData,dataLength);
  59.   }
  60.   // store the state of the connection for next time through
  61.   // the loop:
  62.   lastConnected = client.connected();
  63. }

  64. // this method makes a HTTP connection to the server:
  65. void sendData(char* thisData,byte thisLength) {
  66.   // if there's a successful connection:
  67.   if (client.connect(server, 80)) {
  68.     Serial.println("connecting...");
  69.     // send the HTTP PUT request:
  70.     client.print("POST /v1.0/device/");
  71.     client.print(DEVICEID);
  72.     client.print("/sensor/");
  73.     client.print(SENSORID);
  74.     client.print("/datapoints");
  75.     client.println(" HTTP/1.1");
  76.     client.println("Host: api.yeelink.net");
  77.     client.print("Accept: *");
  78.     client.print("/");
  79.     client.println("*");
  80.     client.print("U-ApiKey: ");
  81.     client.println(APIKEY);
  82.     client.print("Content-Length: ");


  83.     client.println(thisLength);

  84.     client.println("Content-Type: application/x-www-form-urlencoded");
  85.     client.println("Connection: close");
  86.     client.println();

  87.     // here's the actual content of the PUT request:
  88.     client.print(thisData);
  89.   }
  90.   else {
  91.     // if you couldn't make a connection:
  92.     Serial.println("connection failed");
  93.     Serial.println();
  94.     Serial.println("disconnecting.");
  95.     client.stop();
  96.   }
  97.   // note the time that the connection was made or attempted:
  98.   lastConnectionTime = millis();
  99. }


  100. ///////////////////////////////////////////////////////////////////////////
  101. // get data from light sensor
  102. // you can replace this code for your sensor
  103. ///////////////////////////////////////////////////////////////////////////
  104. // get data from temperature sensor
  105. // you can replace this code for your sensor
  106. void get_Send_String(){
  107.   uint16_t Tc_100 = get_Current_Temp();
  108.   uint8_t i,whole, fract;
  109.   whole = Tc_100/10 ;  // separate off the whole and fractional portions
  110.   fract = Tc_100 % 10;
  111.   dataLength = sprintf(sensorData,"{"value":%d.%d}",whole,fract);  
  112. }

  113. uint16_t get_Current_Temp(){ //0.1C
  114.   //returns the temperature from one DS18S20 in DEG Celsius
  115.   byte data[12];
  116.   byte addr[8];
  117.   if ( !ds.search(addr)) {
  118.     //no more sensors on chain, reset search
  119.     ds.reset_search();
  120.     return 0;
  121.   }
  122.   if ( OneWire::crc8( addr, 7) != addr[7]) {
  123.     return 1000;
  124.   }
  125.   if ( addr[0] != 0x28) {
  126.     return 1000;
  127.   }
  128.   ds.reset();
  129.   ds.select(addr);
  130.   ds.write(0x44,1); // start conversion, with parasite power on at the end
  131.   delay(1000);
  132.   ds.reset();
  133.   ds.select(addr);  
  134.   ds.write(0xBE); // Read Scratchpad

  135.   for (int i = 0; i < 9; i++) { // we need 9 bytes
  136.     data[i] = ds.read();
  137.   }
  138.   ds.reset_search();
  139.   uint16_t tempRead = (data[1] << 8) | data[0]; //using two's compliment
  140.   return tempRead*10/16;
  141. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2012-11-21 10:48:00 | 显示全部楼层
哥们,W5100的使用,远远比enc的那个简单多啦!
回复 支持 反对

使用道具 举报

发表于 2012-11-21 11:14:24 | 显示全部楼层
收到,谢谢

回家就试,传感器A0,模拟输入么?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-21 11:40:02 | 显示全部楼层
A0-A5也可以是数字,A6-A7是纯模拟
回复 支持 反对

使用道具 举报

发表于 2012-11-21 20:57:43 | 显示全部楼层
程序很棒,上传成功,感谢支持!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2012-11-21 21:04:33 | 显示全部楼层
erjiang 发表于 2012-11-21 10:48
哥们,W5100的使用,远远比enc的那个简单多啦!

主家真别笑话,菜手和麻瓜多得是,不一定都是合格水准。硬件既然能搭积木,软件为什么不可以呢,其实发布几个不同硬件配置的软件例程,对于上手很有意义。
回复 支持 反对

使用道具 举报

发表于 2012-11-21 21:22:57 | 显示全部楼层
zcbzjx 发表于 2012-11-21 10:27

老师,程序装在成功,已经运行中。

我先装在2560,不知道为什么会报错,但是运行正常。而放入UNO就一切正常,没有报错,不知道什么原因?
回复 支持 反对

使用道具 举报

发表于 2012-11-21 21:23:59 | 显示全部楼层
zcbzjx 发表于 2012-11-21 10:27

老师,是不是可以重新发布一个“基于18B20+w5100+arduino+yeelink的远程温度监控”的参考例程给大家呢?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-21 21:48:45 | 显示全部楼层
能不能把问题补充完整啊。大哥。。。
回复 支持 反对

使用道具 举报

发表于 2012-11-21 22:09:46 | 显示全部楼层
我错了,没问题了,换个USB口,就好了,谢谢老师。

不过,原来的错误信息如下图

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2012-11-27 00:15:56 | 显示全部楼层
zcbzjx 发表于 2012-11-21 10:27

张老师,我在参考COSM例程,想使用更方便的DallasTemperature.h库,但是转换时候遇到一些问题。

void get_Send_String(){
  sensors.requestTemperatures(); // 发送命令获取温度
  char SensorReadOut = sensors.getTempCByIndex(0);
  Serial.println(SensorReadingOut );  
  dataLength = sprintf(sensorData,"{\"value\":%d}",SensorReadOut);  
}

用%d能得到结果,但是只有整数,失去了精度,能保留小数点以后两位小数就满足啦。现在的问题是,是不是一定要用sprintf函数,如何驾驭呢?

我的进一步想法,读取多个18B20,并且上传yeelink。Pachube/COSM有一个PachubeClientString,不是道在yeelink上面可否实现。
回复 支持 反对

使用道具 举报

发表于 2012-11-27 00:35:15 | 显示全部楼层
这是一个测试数据点,但是只有整数,应该是转换问题。
http://www.yeelink.net/devices/1255
回复 支持 反对

使用道具 举报

发表于 2012-11-27 08:51:50 | 显示全部楼层
muggle 发表于 2012-11-27 00:15
张老师,我在参考COSM例程,想使用更方便的DallasTemperature.h库,但是转换时候遇到一些问题。

void  ...

可以实现,只要演示发送就行了,在主程序中,设置一个timer,然后根据timer让传感器标志位自增,发送函数根据当前需要发送的传感器值,读取传感器数据并完成发送即可。

这个是客户端程序逻辑的设计问题,Yeelink本身功能是支持的。

如果你有时间看看张老师的传感器,在一个device下面,都会有数个sensor同时上传的,逻辑类似
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-4 06:40 , Processed in 0.093209 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表