极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 88595|回复: 84

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

[复制链接]
发表于 2012-7-30 13:28:21 | 显示全部楼层 |阅读模式
本帖最后由 zcbzjx 于 2013-7-17 11:35 编辑

2013年7月17日,新版本库已经没有dhcpValid()方法,请各位留意。
2012.31 更新代码,改为自动获取ip地址,以及服务器用域名访问(dns)。


硬件看图。。。估计只有我看的清楚

代码
arduino1.01
请下载EtherCARD库
  1. /*
  2. Yeelink sensor client
  3. by zcb
  4. 2012.07.17
  5. */
  6. #include <EtherCard.h>
  7. #include <OneWire.h>

  8. //
  9. //18B20
  10. #define ONEWIRE_PIN A0

  11. OneWire ds(ONEWIRE_PIN);
  12. ///////////////////////////////////////////////////////////
  13. ///////////////////////////////////////////////////////////
  14. char SensorID[] PROGMEM = "XXX"; // replace your sensor ID
  15. // ethernet interface mac address
  16. static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
  17. // remote website name
  18. char website[] PROGMEM = "api.yeelink.net";
  19. ////////////////////////////////////////////////////////////

  20. // for yeelink api
  21. char APIKey[] PROGMEM = "XXXXXXXXXXXXXXXXX"; // replace your yeelink api key here
  22. char DeviceID[] PROGMEM = "XXX"; // replace your device ID


  23. // assign a MAC IP gateway dns for the ethernet controller.



  24. const uint16_t PostingInterval = 15000;// delay between 2 datapoints, 30s

  25. // Some global variables
  26. char sensorData[20];
  27. uint8_t dataLength;
  28. uint8_t Ethernet::buffer[300];
  29. uint32_t lastConnectionTime = 0;          // last time you connected to the server, in milliseconds

  30. Stash stash;



  31. void setup() {
  32. Serial.begin(57600);
  33. Serial.println("\n[yeelink client]");
  34.   
  35.   if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
  36.     Serial.println( "Failed to access Ethernet controller");

  37.   if (!ether.dhcpSetup())
  38.     Serial.println("DHCP failed");
  39.   
  40.   ether.printIp("My IP: ", ether.myip);
  41.   // ether.printIp("Netmask: ", ether.mymask);
  42.   ether.printIp("GW IP: ", ether.gwip);
  43.   ether.printIp("DNS IP: ", ether.dnsip);

  44.   if (!ether.dnsLookup(website))
  45.     Serial.println("DNS failed");
  46.   ether.printIp("Server: ", ether.hisip);
  47.   
  48.   

  49. }

  50. void loop() {
  51. if (!ether.dhcpValid() && !ether.dhcpSetup())
  52.     Serial.println("DHCP failed");

  53.   ether.packetLoop(ether.packetReceive());

  54.   if(millis() - lastConnectionTime > PostingInterval){
  55.     get_Send_String();   
  56.     send_Data();
  57.   }

  58. }

  59. void send_Data() {
  60.   // Create a Post for yeelink server,
  61.   // and send request saving sessionID
  62.   Stash::prepare(PSTR("POST /v1.0/device/$F/sensor/$F/datapoints HTTP/1.1" "\r\n"
  63.     "Host: api.yeelink.net" "\r\n"
  64.     "U-ApiKey: $F" "\r\n"
  65.     "Content-Length: $D" "\r\n"
  66.     "Content-Type: application/x-www-form-urlencoded" "\r\n" "\r\n"                       
  67.     "$S"),
  68.   DeviceID,SensorID,APIKey,dataLength,sensorData);
  69.   ether.tcpSend();
  70.   lastConnectionTime = millis();
  71. }
复制代码

  1. ///////////////////////////////////////////////////////////////////////////
  2. // get data from temperature sensor
  3. // you can replace this code for your sensor
  4. void get_Send_String(){
  5.   uint16_t Tc_100 = get_Current_Temp();
  6.   uint8_t i,whole, fract;
  7.   whole = Tc_100/10 ;  // separate off the whole and fractional portions
  8.   fract = Tc_100 % 10;
  9.   dataLength = sprintf(sensorData,"{"value":%d.%d}",whole,fract);  
  10. }

  11. uint16_t get_Current_Temp(){ //0.1C
  12.   //returns the temperature from one DS18S20 in DEG Celsius
  13.   byte data[12];
  14.   byte addr[8];
  15.   if ( !ds.search(addr)) {
  16.     //no more sensors on chain, reset search
  17.     ds.reset_search();
  18.     return 0;
  19.   }
  20.   if ( OneWire::crc8( addr, 7) != addr[7]) {
  21.     return 1000;
  22.   }
  23.   if ( addr[0] != 0x28) {
  24.     return 1000;
  25.   }
  26.   ds.reset();
  27.   ds.select(addr);
  28.   ds.write(0x44,1); // start conversion, with parasite power on at the end
  29.   delay(1000);
  30.   ds.reset();
  31.   ds.select(addr);  
  32.   ds.write(0xBE); // Read Scratchpad

  33.   for (int i = 0; i < 9; i++) { // we need 9 bytes
  34.     data[i] = ds.read();
  35.   }
  36.   ds.reset_search();
  37.   uint16_t tempRead = (data[1] << 8) | data[0]; //using two's compliment
  38.   return tempRead*10/16;
  39. }
复制代码
图片在最后,所以这儿对硬件简单说下,左边的2*5排针是接enc28j60的;
右边的8*4排针是atmega168pa 32脚转接的。第一脚在右下脚,逆时针数;
下边的5个排针(忘记画了,不过反正是5根线)是tjc3座,左边三个接18b20(Vcc,DQ,GND),右边2个接电源(5-12V直流)(-,+)。
那个贴片是asm1117-3.3没有画电容,输入输出都是接了100uf,0.1uf2个电容,mcu电源2端加接了个0.1uf电容。

还有4个圆的母杜邦座,哪个是铜柱,固定enc28J60的。

本帖子中包含更多资源

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

x
回复

使用道具 举报

发表于 2012-7-30 13:35:51 | 显示全部楼层
首先支持一下,然后麻烦一下楼主是否能把该写的东西写清楚点呢?特别是硬件连接图等等,给大家来养养眼!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-7-30 13:42:40 | 显示全部楼层
简单解释了下,目前已经做了6套了,还要做2套,运行良好,大家可以看http://www.yeelink.net/devices/197
回复 支持 反对

使用道具 举报

发表于 2012-7-30 13:58:58 | 显示全部楼层
zcbzjx 发表于 2012-7-30 13:42
简单解释了下,目前已经做了6套了,还要做2套,运行良好,大家可以看http://www.yeelink.net/devices/197

做产品???
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-7-30 14:06:55 | 显示全部楼层
本帖最后由 zcbzjx 于 2012-7-30 14:08 编辑

也不算,就是单位自己用的。8个机房,一个机房一个
回复 支持 反对

使用道具 举报

发表于 2012-7-30 16:49:06 | 显示全部楼层
我看到可耻的关键时刻都开空调了。。
回复 支持 反对

使用道具 举报

发表于 2012-7-30 21:10:00 | 显示全部楼层
应该上原理图....洞洞板确实难看懂
回复 支持 反对

使用道具 举报

发表于 2012-7-31 08:08:32 | 显示全部楼层
监控机房的温度,这个还挺实用的,话说我一直没想清楚yeelink到底怎么应用到实际中,楼主提供了一个很好的例子
回复 支持 反对

使用道具 举报

发表于 2012-7-31 16:38:33 | 显示全部楼层
楼主你太牛逼了,一天的PV到200多,是不是全北京人都在点击你的设备看?

另外我给你发邮件没回应,能不能加我QQ?  6032410,谢谢!
回复 支持 反对

使用道具 举报

发表于 2012-7-31 16:44:16 | 显示全部楼层
对了,有两个传感器的上下波动比较规律,锯齿型的,是不是有些传感器的问题?
回复 支持 反对

使用道具 举报

发表于 2012-7-31 16:48:11 | 显示全部楼层
教学楼空调开得太大了哈,22度要冻死人啦...低碳,低碳...
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-7-31 18:54:36 | 显示全部楼层
不是,是正常的,请看这个链接
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-7-31 18:56:45 | 显示全部楼层
erjiang 发表于 2012-7-31 16:48
教学楼空调开得太大了哈,22度要冻死人啦...低碳,低碳...

你说的是综合楼的吧,空调调的是27度,由于传感器放得比较低,而空调的传感器位置可能比较高,所以。。
回复 支持 反对

使用道具 举报

发表于 2012-8-1 13:12:49 | 显示全部楼层
zcbzjx 发表于 2012-7-31 18:56
你说的是综合楼的吧,空调调的是27度,由于传感器放得比较低,而空调的传感器位置可能比较高,所以。。

我突然觉得,通过你发布在yeelink的这个图表,能够最真实的表达变频空调对开关空调的明显优势,估计那这个图放给买空调的客户一看,他们买那个就会立即决定了。
回复 支持 反对

使用道具 举报

发表于 2012-8-1 21:45:28 | 显示全部楼层
楼主可不可以对代码进行下解释么?谢谢撒
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-10 01:00 , Processed in 0.066604 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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