程式碼:
https://gist.github.com/clive520/d8c47a06dca2195b2b832bc1fc687c0c
利用ESP8266及一個DHT11製作一個機房溫度通知器
每一個小時會將資料傳送到LINE,Google,ThingSpeak
溫度超過40度,每5分鐘以LINE通知
#include <ESP8266WiFi.h> //WIFI #include <WiFiUdp.h> #include "esp_NTPClient.h" //NTP #include <WiFiClientSecure.h> #include "esp_DHTesp.h" //DHT 11 float Temp = 0; //溫度 float HUM = 0; //濕度 String T_Time; //NTP取出時間 String T_Time_STR; //NTP取出時間,換成int的中繼站 int H01 = 0; int H10 = 0; int HH = 0; int M01 = 0; int M10 = 0; int MM = 0; //start_num,控制開始傳送,避免同一分鐘內送多次 int start_num = 0; int LINE_num = 10; //每小時的XX幾分鐘,上傳資料到LINE int Google_num = 20; //每小時的XX幾分鐘,上傳資料到Google int ThingSpeak_num = 30; //每小時的XX幾分鐘,上傳資料到ThingSpeak //盡量不要新同時間上傳,每小時傳送一次,一天12次 int LINE_warning_NUM = 0; //NTP WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP); DHTesp dht; //DHT 11 //WIFI WiFiClient client; WiFiClientSecure Secure_client; void setup() { pinMode(2, OUTPUT); pinMode(4, INPUT); Serial.begin(9600); WIFI_connection(); //副程式 WIFI_connection //NTP timeClient.begin(); timeClient.setTimeOffset(28800); //台灣時區+8,所以放入28800 dht.setup(4);//DHT 11 接在腳位4 } void loop() { delay(30000); get_time(); //副程式 取得時間並轉成int get_data(); //副程式 取得DHT11資料 if (start_num == 0) { //start_num == 0 啟動,再判斷時間是否正確 if (MM == LINE_num ) { LINE(); start_num = 1; Serial.println(start_num); } //上傳資料到LINE if (MM == ThingSpeak_num ) { ThingSpeak(); start_num = 1; Serial.println(start_num); } //上傳資料到ThingSpeak if (MM == Google_num ) { Google(); start_num = 1; Serial.println(start_num); } //上傳資料到Google表單 } else if (MM != LINE_num && MM != Google_num && MM != ThingSpeak_num ) { start_num = 0; Serial.println(start_num); } //若溫度高於40度,每五分鐘以LINE警告一次 if(Temp >= 40 && LINE_warning_NUM == 0){ LINE_warning(); LINE_warning_NUM = LINE_warning_NUM + 1 ; } else if (LINE_warning_NUM > 0 && LINE_warning_NUM < 10 ){LINE_warning_NUM = LINE_warning_NUM + 1 ;} else {LINE_warning_NUM = 0;} } //WIFI_connection void WIFI_connection() { WiFi.begin("book","12345678"); while(WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(WiFi.localIP()); for (int count = 0; count < 5; count++) { digitalWrite(2,HIGH); delay(500); digitalWrite(2,LOW); delay(500); } } //取得時間並轉成int void get_time() { timeClient.update(); T_Time = timeClient.getFormattedTime(); Serial.println(T_Time); T_Time_STR = T_Time.charAt(0); H10 = T_Time_STR.toInt(); T_Time_STR = T_Time.charAt(1); H01 = T_Time_STR.toInt(); HH = H10 * 10 + H01; T_Time_STR = T_Time.charAt(3); M10 = T_Time_STR.toInt(); T_Time_STR = T_Time.charAt(4); M01 = T_Time_STR.toInt(); MM = M10 * 10 + M01; //Serial.println((String("1=") + String(H10) + String("2=") + String(H01) + String("HH=") + String(HH))); Serial.println((String("4=") + String(M10) + String("5=") + String(M01) + String("MM=") + String(MM))); } //取得DHT11資料 void get_data() { Temp = dht.getTemperature(); HUM = dht.getHumidity(); if (isnan(Temp) || isnan(HUM) ) { //判斷是否有讀到數值 Serial.println("Failed to read from DHT sensor!"); return; } //Serial.println(Temp); //Serial.println(HUM); } //上傳資料到ThingSpeak void ThingSpeak() { if(client.connect("api.thingspeak.com", 80)) { String things_request = "GET /update?api_key=SPL5BSYPN18QOMQ8&field1="+ String(Temp)+"&field2="+ String(HUM)+"\r\n\r\n"; client.print(things_request); client.stop(); } Serial.println("ThingSpeak"); } //上傳資料到Google表單 void Google() { if (Secure_client.connect("docs.google.com", 443)) { Secure_client.println("GET /forms/d/e/1FAIpQLSeKbv3Yo2r8WTlpk4y6RGOGYbQ3dQNvTScaESSaWREITVXw6g/formResponse?entry.1273328924="+ String(Temp)+"&entry.1741593155="+ String(HUM)+"&submit=Submit HTTP/1.1"); Secure_client.println("Host: docs.google.com"); Secure_client.println("Connection: close"); Secure_client.println(); Secure_client.stop(); } Serial.println("Google"); } //上傳資料到LINE void LINE() { if (Secure_client.connect("notify-api.line.me",443)) { String data = "message="+String((String("\n四樓機房溫度:") + String(Temp) + String("\n四樓機房濕度:") + String(HUM))); Secure_client.println("POST /api/notify HTTP/1.1"); Secure_client.println("Host: notify-api.line.me"); Secure_client.println("Authorization: Bearer DhryVqqdnWsStwHzraLj1NV90NMK96IgQWniF3omKub"); Secure_client.println("Content-Type: application/x-www-form-urlencoded"); Secure_client.print("Content-Length: "); Secure_client.println(data.length()); Secure_client.println(); Secure_client.println(data); Secure_client.stop(); } Serial.println("LINE"); } //溫度過高時,及時上傳資料到LINE void LINE_warning() { if (Secure_client.connect("notify-api.line.me",443)) { String data = "message="+String((String("\n四樓機房溫度過高!!\n請特別注意!!\n四樓機房溫度:") + String(Temp) + String("\n四樓機房濕度:") + String(HUM))); Secure_client.println("POST /api/notify HTTP/1.1"); Secure_client.println("Host: notify-api.line.me"); Secure_client.println("Authorization: Bearer DhryVqqdnWsStwHzraLj1NV90NMK96IgQWniF3omKub"); Secure_client.println("Content-Type: application/x-www-form-urlencoded"); Secure_client.print("Content-Length: "); Secure_client.println(data.length()); Secure_client.println(); Secure_client.println(data); Secure_client.stop(); } Serial.println("LINE_warning"); }
沒有留言:
張貼留言