极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13677|回复: 7

新人贴,dht11+enc28j60+yeelink

[复制链接]
发表于 2013-6-22 23:01:22 | 显示全部楼层 |阅读模式
参考了很多谈内外的资料
回复

使用道具 举报

 楼主| 发表于 2013-6-22 23:03:39 | 显示全部楼层
本帖最后由 blueleafxp 于 2013-6-22 23:06 编辑
  1. //#include <OneWire.h>
  2. #include <EtherCard.h>
  3. //#include <DallasTemperature.h>
  4. #define OUT
  5. // Data wire is plugged into port A5 on the Arduino
  6. #define ONE_WIRE_BUS A5
  7. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  8. //OneWire oneWire(ONE_WIRE_BUS);
  9. // Pass our oneWire reference to Dallas Temperature.
  10. //DallasTemperature sensors(&oneWire);

  11. #include <dht11.h>
  12. dht11 DHT11;
  13. #define DHT11PIN 3 //DHT11 PIN 3 连接UNO 3


  14. #define REQUEST_RATE 15000 // milliseconds
  15. // ethernet interface mac address
  16. static byte mymac[] = {
  17.   0x74,0x69,0x69,0x2D,0x30,0x31 };
  18. // ethernet interface ip address
  19. static byte myip[] = {
  20.   192,168,101,139 };
  21. // gateway ip address
  22. static byte gwip[] = {
  23.   192,168,101,1 };
  24. // remote dns ip address
  25. static byte dnsip[] = {
  26.   192,168,101,1 };
  27. int mode_id=0;
  28. // remote website name
  29. char website[] PROGMEM = "api.yeelink.net";
  30. char urlBuf[] PROGMEM = "/v1.0/device/xxxx/sensor/xxxx/datapoints";// 用自己的id
  31. char apiKey[] PROGMEM = "U-ApiKey: xxxxx";

  32. char urlBuf1[] PROGMEM = "/v1.0/device/xxxx/sensor/xxxx/datapoints";//用自己的key



  33. byte Ethernet::buffer[400];   // a very small tcp/ip buffer is enough here
  34. static long timer;

  35. // called when the client request is complete
  36. static void my_result_cb (byte status, word off, word len) {
  37.   Serial.print("<<< reply ");
  38.   Serial.print(millis() - timer);
  39.   Serial.println(" ms");
  40.   Serial.println((const char*) Ethernet::buffer + off);
  41. }

  42. void setup () {
  43.   ///sensors.begin();
  44.   Serial.begin(57600);
  45.   Serial.println("\n[getStaticIP]");

  46.   if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
  47.     Serial.println( "Failed to access Ethernet controller");

  48.   ether.staticSetup(myip, gwip, dnsip);

  49.   while (ether.clientWaitingGw())
  50.     ether.packetLoop(ether.packetReceive());
  51.   Serial.println("Gateway found");

  52.   timer = - REQUEST_RATE; // start timing out right away
  53. }

  54. void loop () {
  55.   ether.packetLoop(ether.packetReceive());

  56.   if (millis() > timer + REQUEST_RATE) {
  57.     timer = millis();
  58.     Serial.println("\n>>> REQ");
  59.     static char buf[20],buf1[20];
  60.     get_send_string(buf,buf1);
  61.     if (!ether.dnsLookup(website))
  62.       Serial.println("DNS failed");
  63.     ether.printIp("Server: ", ether.hisip);
  64.     switch(mode_id)
  65.     {
  66.     case 1:
  67.         Serial.println("\n>>> temperature");
  68.     ether.httpPost (urlBuf, website, apiKey, buf, my_result_cb);
  69.         mode_id--;
  70.         break;
  71.     case 0:
  72.         Serial.println("\n>>> humidity");
  73.       ether.httpPost (urlBuf1, website, apiKey, buf1, my_result_cb);
  74.       mode_id++;
  75.       break;
  76.       default:
  77.       mode_id=0;
  78.       break;

  79.     }
  80.    
  81.   }
  82. }

  83. void get_send_string(OUT char *p ,OUT char *p1){
  84.   
  85.   int chk = DHT11.read(DHT11PIN);
  86. // sensors.requestTemperatures();  
  87.   int Tc_100 =(float) DHT11.temperature*10;

  88.   int whole, fract;
  89.   whole = Tc_100/10 ;  // separate off the whole and fractional portions
  90.   fract = Tc_100 % 10;
  91.   //whole=35;
  92.   //fract=3;
  93.   sprintf(p,"{"value":%d.%d}",whole,fract);
  94.    int Tc_200 =(float) DHT11.humidity*10;
  95.     whole = Tc_200/10 ;  // separate off the whole and fractional portions
  96.   fract = Tc_200 % 10;
  97.   //whole=35;
  98.   //fract=3;
  99.   sprintf(p1,"{"value":%d.%d}",whole,fract);
  100.   
  101.   Serial.println(p);
  102.   Serial.println(p1);
  103. //  sprintf(p,"{"value":%d}",Tc_100 );
  104. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-6-22 23:08:19 | 显示全部楼层
// PIN Connections (Using Arduino UNO):
//   VCC -   3.3V
//   GND -    GND
//   SCK - Pin 13
//   SO  - Pin 12
//   SI  - Pin 11
//   CS  - Pin  8

温控用 pin 3
代码里看的到,目前用的是 tp-link做无线客户端模式

回复 支持 反对

使用道具 举报

发表于 2013-12-19 22:57:56 | 显示全部楼层
这贴很好,很有借鉴性,谢谢你的分享,非常有用
回复 支持 反对

使用道具 举报

发表于 2013-12-28 22:39:54 | 显示全部楼层
谢谢分享,数据传输正常。
回复 支持 反对

使用道具 举报

发表于 2014-3-28 11:30:28 | 显示全部楼层
湿度数据如何上传呢?
回复 支持 反对

使用道具 举报

发表于 2014-4-6 14:30:18 | 显示全部楼层
感谢,enc28j60的库太诡异,看了你的例子好多了
回复 支持 反对

使用道具 举报

发表于 2015-4-13 18:35:17 | 显示全部楼层
enc28j60 ping也是通的,但yeelink没有接收到数据,求解
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-19 05:42 , Processed in 0.040865 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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