极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8857|回复: 3

潜水浮出,分享个小东西[室内温度推送+实时显示+网络时钟]

[复制链接]
发表于 2013-1-28 21:10:46 | 显示全部楼层 |阅读模式
潜水太久,我还是给大家分享个东西

功能:
1. 室内温度实时推送到yeelink
2. 从网络反馈信息可以了解网络时钟,现在几点了
3. I2C LCD 网络显示当前信息状态
4. 内置看门狗,以免死机或者路由器无响应
5. 以上纯属YY,当然还是有点实用价值










分享代码

  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <math.h>
  4. #include <Wire.h>
  5. #include <LiquidCrystal_I2C.h>
  6. #include <OneWire.h>
  7. #include <DallasTemperature.h>
  8. #include <avr/wdt.h>

  9. // for yeelink api
  10. #define APIKEY         "706bfc67907daa044b5e95ec283caed3" // replace your yeelink api key here
  11. #define DEVICEID       1419 // replace your device ID
  12. #define SENSORID       1809 // replace your sensor ID
  13. #define ONE_WIRE_BUS 9
  14. char str[40];

  15. OneWire oneWire(ONE_WIRE_BUS);
  16. LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display
  17. DallasTemperature sensors(&oneWire);


  18. // assign a MAC address for the ethernet controller.
  19. byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
  20. // initialize the library instance:
  21. EthernetClient client;

  22. //IPAddress ip(192,168,1,130);
  23. char server[] = "api.yeelink.net";   // name address for yeelink API

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

  27. void setup() {

  28. lcd.init();                      // initialize the lcd
  29. sensors.begin();
  30. lcd.backlight();
  31. lcd.clear();

  32.   // start serial port:
  33.   Serial.begin(57600);
  34.   lcd.setCursor(0, 0);
  35.   lcd.print("*LINK THE NETWORK*");
  36.   sensors.requestTemperatures();
  37.   lcd.setCursor(0, 1);
  38.   lcd.print("Please into RJ45");
  39.   lcd.setCursor(0, 2);
  40.   lcd.print("====================");  
  41.   lcd.setCursor(0, 3);
  42.   lcd.print("Temperatures=");
  43.   lcd.setCursor(14, 3);
  44.   lcd.print(sensors.getTempCByIndex(0));
  45.   delay(2000);
  46.   lcd.clear();
  47.   lcd.setCursor(0, 0);
  48.   lcd.print("*Status of DHCP*");
  49.   // start the Ethernet connection with DHCP:
  50.    
  51.   if (Ethernet.begin(mac) == 0) {
  52.     Serial.println("Failed to configure Ethernet using DHCP");
  53.     for(;;)
  54.    lcd.setCursor(3, 1);
  55.    lcd.print("Link Without HUB");
  56.   }
  57.   else {
  58.     Serial.println("Ethernet configuration OK");
  59.    lcd.setCursor(3, 1);
  60.    lcd.print("Link With HUB");
  61.   }
  62.   
  63.   delay(1000);
  64.   lcd.clear();
  65. wdt_enable(WDTO_8S);
  66. }






  67. void loop() {
  68.   // if there's incoming data from the net connection.
  69.   // send it out the serial port.  This is for debugging
  70.   // purposes only:
  71.   lcd.clear();

  72.   int ROW=0;
  73.   int num=0;
  74.   delay(50);
  75.    lcd.print("LOGIN INTELNET...");
  76.    
  77.    
  78.    delay(50);
  79.    lcd.setCursor(0, 0);
  80.   while (client.available()) {
  81.     char str = client.read();
  82.     wdt_reset();  
  83.     if(client.available()){num++;}
  84.     if((num%20==0&&num>15)){lcd.setCursor(0, ROW+=1);}
  85.     if(ROW>3){delay(800);ROW=0;lcd.clear();}
  86.     if(str=='\n'||str=='\r'){str=' ';}
  87.     lcd.print(str);
  88.     Serial.print(str);
  89.     Serial.print("; = ");
  90.     Serial.print(str,16);
  91.     delay(15);
  92.      
  93.   }
  94. if (client.available()){lcd.setCursor(0, 0);lcd.clear();}
  95.   // if there's no net connection, but there was one last time
  96.   // through the loop, then stop the client:

  97.   if (!client.connected() && lastConnected) {
  98.     lcd.clear();
  99.     Serial.println();
  100.     lcd.setCursor(3,1);
  101.     Serial.println("disconnecting.");
  102.     lcd.print("disconnecting.");
  103.     client.stop();
  104.     delay(200);
  105.      wdt_reset();  
  106.   }

  107.   // if you're not connected, and ten seconds have passed since
  108.   // your last connection, then connect again and send data:
  109.   if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
  110.     // read sensor data, replace with your code
  111.      lcd.setCursor(0, 0);
  112.      lcd.print("LOGIN INTELNET=");
  113.      lcd.setCursor(0, 1);
  114.      lcd.print("Connecting");
  115.      lcd.setCursor(0, 2);
  116.      lcd.print("Server=Sent:");
  117.      lcd.setCursor(13, 2);
  118.      lcd.print(sensors.getTempCByIndex(0));
  119.      lcd.setCursor(18, 2);
  120.      lcd.print("'C");
  121.     float sensorReading = readLightSensor();
  122.     //send data to server
  123.     sendData(sensorReading);
  124.     delay(50);
  125.   }
  126.   // store the state of the connection for next time through
  127.   // the loop:
  128.   lastConnected = client.connected();
  129. }













  130. // this method makes a HTTP connection to the server:
  131. void sendData(int thisData) {
  132.   // if there's a successful connection:
  133.   if (client.connect(server, 80)) {
  134.     lcd.clear();
  135.     lcd.setCursor(0, 0);
  136.     lcd.print("connecting...");
  137.     Serial.println("connecting...");
  138.     // send the HTTP PUT request:
  139.     client.print("POST /v1.0/device/");
  140.     client.print(DEVICEID);
  141.     client.print("/sensor/");
  142.     client.print(SENSORID);
  143.     client.print("/datapoints");
  144.     client.println(" HTTP/1.1");
  145.     client.println("Host: api.yeelink.net");
  146.     client.print("Accept: *");
  147.     client.print("/");
  148.     client.println("*");
  149.     client.print("U-ApiKey: ");
  150.     client.println(APIKEY);
  151.     client.print("Content-Length: ");
  152.     lcd.setCursor(0, 1);
  153.     lcd.print("Length: ");
  154.     // calculate the length of the sensor reading in bytes:
  155.     // 8 bytes for {"value":} + number of digits of the data:
  156.     int thisLength = 10 + getLength(thisData);
  157.     lcd.setCursor(9, 1);
  158.     client.println(thisLength);
  159.     lcd.print(thisLength);
  160.     client.println("Content-Type: application/x-www-form-urlencoded");
  161.    
  162.     lcd.setCursor(2, 2);
  163.     lcd.print("Connection: close");
  164.     client.println("Connection: close");
  165.     client.println();
  166.     lcd.setCursor(1, 3);
  167.     lcd.print("Vaule=");
  168.     lcd.setCursor(7, 3);
  169.     // here's the actual content of the PUT request:
  170.     client.print("{"value":");
  171.     client.print(thisData);
  172.     lcd.print(sensors.getTempCByIndex(0));
  173.     client.println("}");
  174.     delay(400);

  175.   }
  176.   else {
  177.     // if you couldn't make a connection:
  178.     lcd.clear();
  179.     lcd.setCursor(0, 0);
  180.     lcd.print("connection failed");
  181.     Serial.println("connection failed");
  182.     Serial.println();
  183.     lcd.setCursor(0, 1);
  184.     lcd.print("disconnecting.");
  185.     Serial.println("disconnecting.");
  186.     lcd.setCursor(0, 2);
  187.     lcd.print("client.stop()");   
  188.     client.stop();
  189.     delay(500);
  190.   }
  191.    // note the time that the connection was made or attempted:
  192.   lastConnectionTime = millis();
  193. }

  194. // This method calculates the number of digits in the
  195. // sensor reading.  Since each digit of the ASCII decimal
  196. // representation is a byte, the number of digits equals
  197. // the number of bytes:
  198. int getLength(int someValue) {
  199.   // there's at least one byte:
  200.   int digits = 1;
  201.   // continually divide the value by ten,
  202.   // adding one to the digit count for each
  203.   // time you divide, until you're at 0:
  204.   int dividend = someValue /10;
  205.   while (dividend > 0) {
  206.     dividend = dividend /10;
  207.     digits++;
  208.   }
  209.   // return the number of digits:
  210.   return digits;
  211. }

  212. ///////////////////////////////////////////////////////////////////////////
  213. // get data from light sensor
  214. // you can replace this code for your sensor
  215. float readLightSensor()
  216. {
  217.   float val=0;
  218.   val=sensors.getTempCByIndex(0)*100;
  219.   sensors.requestTemperatures();
  220.   //lcd.print(sensors.getTempCByIndex(0));
  221.   Serial.print("Sensor value is: ");
  222.   Serial.println(sensors.getTempCByIndex(0));
  223.    
  224.   return val;
  225. }
复制代码

本帖子中包含更多资源

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

x
回复

使用道具 举报

发表于 2013-1-29 12:09:17 | 显示全部楼层
你连加密串都放出来了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-1-29 13:00:24 | 显示全部楼层
大公无私啊
回复 支持 反对

使用道具 举报

发表于 2013-7-26 11:05:09 | 显示全部楼层
支持一下!
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-8 15:44 , Processed in 0.040798 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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