2016年12月29日 星期四

ESP-12測試版 + 溫度溼度感測器 + ThingSpeak

課程-ThingSpeak + 溫度溼度感測器 

目的:

使用ESP-12測試版GPIO16 來讀取DHT11溫度溼度感測器的值,並每隔2秒中上傳至ThingSpeak

電子元件:

1)麵包板 x 1
2) ESP-12測試版主板 x 1
3) 5V , 3.3V 轉換板 x1
4) DHT11 溫度溼度感測器 x1

軟體: 
1)Arduino IDE 1.6.5版本
2) DHT.zip
3) 留意  ESP8266  ESP-12 Tools Board --uploading 接線










程式 :
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //參考 http://www.arduinesp.com/thingspeak #include <ESP8266WiFi.h> #include <dht.h> // replace with your channel's thingspeak API key, String apiKey = "TW60RSJ9YFYLQRGK"; const char* ssid = "74170287"; const char* password = "24063173"; const char* server = "api.thingspeak.com"; //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #define dht_dpin 16 //定義訊號要從進來 dht DHT; WiFiClient client; void setup() { Serial.begin(115200); delay(10); pinMode(dht_dpin,INPUT); WiFi.begin(ssid, password); Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); } void loop() { //++++++++++++++++++++++++++++++++++++++++++++++ DHT.read11(dht_dpin); //去library裡面找DHT.read11 float h = DHT.humidity; float t = DHT.temperature; if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; } if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com String postStr = apiKey; postStr +="&field1="; postStr += String(t); postStr +="&field2="; postStr += String(h); postStr += "\r\n\r\n"; client.print("POST /update HTTP/1.1\n"); client.print("Host: api.thingspeak.com\n"); client.print("Connection: close\n"); client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n"); client.print("Content-Type: application/x-www-form-urlencoded\n"); client.print("Content-Length: "); client.print(postStr.length()); client.print("\n\n"); client.print(postStr); Serial.print("Temperature: "); Serial.print(t); Serial.print(" degrees Celcius Humidity: "); Serial.print(h); Serial.println("% send to Thingspeak"); } client.stop(); Serial.println("Waiting..."); // thingspeak needs minimum 15 sec delay between updates delay(20000); }

沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...