潜水太久,我还是给大家分享个东西
功能:
1. 室内温度实时推送到yeelink
2. 从网络反馈信息可以了解网络时钟,现在几点了
3. I2C LCD 网络显示当前信息状态
4. 内置看门狗,以免死机或者路由器无响应
5. 以上纯属YY,当然还是有点实用价值
分享代码
- #include <SPI.h>
- #include <Ethernet.h>
- #include <math.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <avr/wdt.h>
- // for yeelink api
- #define APIKEY "706bfc67907daa044b5e95ec283caed3" // replace your yeelink api key here
- #define DEVICEID 1419 // replace your device ID
- #define SENSORID 1809 // replace your sensor ID
- #define ONE_WIRE_BUS 9
- char str[40];
- OneWire oneWire(ONE_WIRE_BUS);
- LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
- DallasTemperature sensors(&oneWire);
- // assign a MAC address for the ethernet controller.
- byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
- // initialize the library instance:
- EthernetClient client;
- //IPAddress ip(192,168,1,130);
- char server[] = "api.yeelink.net"; // name address for yeelink API
- unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
- boolean lastConnected = false; // state of the connection last time through the main loop
- const unsigned long postingInterval = 20*100; // delay between 2 datapoints, 30s
- void setup() {
- lcd.init(); // initialize the lcd
- sensors.begin();
- lcd.backlight();
- lcd.clear();
- // start serial port:
- Serial.begin(57600);
- lcd.setCursor(0, 0);
- lcd.print("*LINK THE NETWORK*");
- sensors.requestTemperatures();
- lcd.setCursor(0, 1);
- lcd.print("Please into RJ45");
- lcd.setCursor(0, 2);
- lcd.print("====================");
- lcd.setCursor(0, 3);
- lcd.print("Temperatures=");
- lcd.setCursor(14, 3);
- lcd.print(sensors.getTempCByIndex(0));
- delay(2000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("*Status of DHCP*");
- // start the Ethernet connection with DHCP:
-
- if (Ethernet.begin(mac) == 0) {
- Serial.println("Failed to configure Ethernet using DHCP");
- for(;;)
- lcd.setCursor(3, 1);
- lcd.print("Link Without HUB");
- }
- else {
- Serial.println("Ethernet configuration OK");
- lcd.setCursor(3, 1);
- lcd.print("Link With HUB");
- }
-
- delay(1000);
- lcd.clear();
- wdt_enable(WDTO_8S);
- }
- void loop() {
- // if there's incoming data from the net connection.
- // send it out the serial port. This is for debugging
- // purposes only:
- lcd.clear();
- int ROW=0;
- int num=0;
- delay(50);
- lcd.print("LOGIN INTELNET...");
-
-
- delay(50);
- lcd.setCursor(0, 0);
- while (client.available()) {
- char str = client.read();
- wdt_reset();
- if(client.available()){num++;}
- if((num%20==0&&num>15)){lcd.setCursor(0, ROW+=1);}
- if(ROW>3){delay(800);ROW=0;lcd.clear();}
- if(str=='\n'||str=='\r'){str=' ';}
- lcd.print(str);
- Serial.print(str);
- Serial.print("; = ");
- Serial.print(str,16);
- delay(15);
-
- }
- if (client.available()){lcd.setCursor(0, 0);lcd.clear();}
- // if there's no net connection, but there was one last time
- // through the loop, then stop the client:
-
- if (!client.connected() && lastConnected) {
- lcd.clear();
- Serial.println();
- lcd.setCursor(3,1);
- Serial.println("disconnecting.");
- lcd.print("disconnecting.");
- client.stop();
- delay(200);
- wdt_reset();
- }
- // if you're not connected, and ten seconds have passed since
- // your last connection, then connect again and send data:
- if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
- // read sensor data, replace with your code
- lcd.setCursor(0, 0);
- lcd.print("LOGIN INTELNET=");
- lcd.setCursor(0, 1);
- lcd.print("Connecting");
- lcd.setCursor(0, 2);
- lcd.print("Server=Sent:");
- lcd.setCursor(13, 2);
- lcd.print(sensors.getTempCByIndex(0));
- lcd.setCursor(18, 2);
- lcd.print("'C");
- float sensorReading = readLightSensor();
- //send data to server
- sendData(sensorReading);
- delay(50);
- }
- // store the state of the connection for next time through
- // the loop:
- lastConnected = client.connected();
- }
- // this method makes a HTTP connection to the server:
- void sendData(int thisData) {
- // if there's a successful connection:
- if (client.connect(server, 80)) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("connecting...");
- Serial.println("connecting...");
- // send the HTTP PUT request:
- client.print("POST /v1.0/device/");
- client.print(DEVICEID);
- client.print("/sensor/");
- client.print(SENSORID);
- client.print("/datapoints");
- client.println(" HTTP/1.1");
- client.println("Host: api.yeelink.net");
- client.print("Accept: *");
- client.print("/");
- client.println("*");
- client.print("U-ApiKey: ");
- client.println(APIKEY);
- client.print("Content-Length: ");
- lcd.setCursor(0, 1);
- lcd.print("Length: ");
- // calculate the length of the sensor reading in bytes:
- // 8 bytes for {"value":} + number of digits of the data:
- int thisLength = 10 + getLength(thisData);
- lcd.setCursor(9, 1);
- client.println(thisLength);
- lcd.print(thisLength);
- client.println("Content-Type: application/x-www-form-urlencoded");
-
- lcd.setCursor(2, 2);
- lcd.print("Connection: close");
- client.println("Connection: close");
- client.println();
- lcd.setCursor(1, 3);
- lcd.print("Vaule=");
- lcd.setCursor(7, 3);
- // here's the actual content of the PUT request:
- client.print("{"value":");
- client.print(thisData);
- lcd.print(sensors.getTempCByIndex(0));
- client.println("}");
- delay(400);
- }
- else {
- // if you couldn't make a connection:
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("connection failed");
- Serial.println("connection failed");
- Serial.println();
- lcd.setCursor(0, 1);
- lcd.print("disconnecting.");
- Serial.println("disconnecting.");
- lcd.setCursor(0, 2);
- lcd.print("client.stop()");
- client.stop();
- delay(500);
- }
- // note the time that the connection was made or attempted:
- lastConnectionTime = millis();
- }
- // This method calculates the number of digits in the
- // sensor reading. Since each digit of the ASCII decimal
- // representation is a byte, the number of digits equals
- // the number of bytes:
- int getLength(int someValue) {
- // there's at least one byte:
- int digits = 1;
- // continually divide the value by ten,
- // adding one to the digit count for each
- // time you divide, until you're at 0:
- int dividend = someValue /10;
- while (dividend > 0) {
- dividend = dividend /10;
- digits++;
- }
- // return the number of digits:
- return digits;
- }
- ///////////////////////////////////////////////////////////////////////////
- // get data from light sensor
- // you can replace this code for your sensor
- float readLightSensor()
- {
- float val=0;
- val=sensors.getTempCByIndex(0)*100;
- sensors.requestTemperatures();
- //lcd.print(sensors.getTempCByIndex(0));
- Serial.print("Sensor value is: ");
- Serial.println(sensors.getTempCByIndex(0));
-
- return val;
- }
复制代码 |