2017年10月15日 星期日

NodeMCU Temperature, Humidity data upload on Thingspeak on Arduino IDE

NodeMCU Temperature, Humidity data upload on Thingspeak on Arduino IDE









// Simple code upload the tempeature and humidity data using thingspeak.com
// Hardware: NodeMCU,DHT11

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <ESP8266WiFi.h>
//String apiKey = "Your API of thingsspeak";     //  Enter your Write API key from ThingSpeak
String apiKey = "3V8PO4MIDZ5DGKMT";     //  Enter your Write API key from ThingSpeak

//const char *ssid =  "Your wifi Network name";     // replace with your wifi ssid and wpa2 key
//const char *pass =  "Network password";
const char* ssid = "74170287";
const char* password = "24063173";
const char* server = "api.thingspeak.com";

#define DHTPIN 0          //pin where the dht11 is connected
DHT dht(DHTPIN, DHT11);

WiFiClient client;
void setup() 
{
       Serial.begin(115200);
       delay(10);
       dht.begin();
       Serial.println("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() 
{
  
      float h = dht.readHumidity();
      float t = dht.readTemperature();
      
              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, i've set it to 30 seconds
  delay(10000);
}

2 則留言:

  1. 您好,請問最近ThingSpeak的上傳方式有改嗎?
    我照著您的做法做一遍,但是資料傳不上去~
    已知:
    1.開發版確定有聯網
    2.在瀏覽器上直接輸入https://api.thingspeak.com/update?api_key=###########&field1=0是有用的
    3.想請問您為甚麼要使用POST方法而不是API KEY網頁右下角給的GET方法範例,有什麼不一樣的地方嗎?
    謝謝

    回覆刪除
  2. 我找到答案了:
    https://community.thingspeak.com/forum/announcements/sending-data-to-thingspeak-api-from-device-but-channel-is-not-updated-what-changed/
    Vinod於2019.1.4寫到
    「需使用delay等待伺服器回應,再傳送下一個client.print()指令」
    他也建議改用:
    https://github.com/mathworks/thingspeak-arduino的library讓上傳更穩定

    回覆刪除

Node-Red & ModeBus FC=1

Node-Red & ModeBus FC=1 write address=0..7   8bits read address=16..23 8bits  [{"id":"cf7c1253d4d997dd","type&q...