#include "CayenneDefines.h" #include "BlynkSimpleEsp8266.h" #include "CayenneWiFiClient.h" #include "ESP8266WiFi.h" #include "DHT.h" #include "Adafruit_Sensor.h" #include "DHT_U.h" #define DHTTYPE DHT11 // DHT 11 #define DHTPIN D5 // DHT 11 DHT_Unified dht(DHTPIN, DHTTYPE); WiFiClient client; // Cayenne authentication token. This should be obtained from the Cayenne Dashboard. char token[] = "CAYENNE_TOKEN"; char ssid[] = "YOUR_SSID"; char password[] = "YOUR_SSID_PASSWORD"; // ThingSpeak variables const int channelID = TS_CHANNEL_ID; // write ThingSpeak channel ID String writeAPIKey = "TS_API_KEY"; // write API key for your ThingSpeak Channel const char* server = "api.thingspeak.com"; const int delayLength = 1000; int count = 0; // for determining when to post to TS float t = 0; // Temperature float h = 0; // Humidity void setup(){ pinMode(D5, INPUT); dht.begin(); Serial.begin(9600); Cayenne.begin(token, ssid, password); } void loop() { count++; Cayenne.run(); sensors_event_t event; if(count % 5 == true) { dht.humidity().getEvent(&event); h = event.relative_humidity; dht.temperature().getEvent(&event); t = event.temperature; CayWrite(V0, t); CayWrite(V1, h); Serial.print("Cayenne write... "); } if(count == 30) { if (client.connect(server, 80)) { // Construct API request body String body = "field1="; body += String(t); body +="&field2="; body += String(h); client.print("POST /update HTTP/1.1\n"); client.print("Host: api.thingspeak.com\n"); client.print("Connection: close\n"); client.print("X-THINGSPEAKAPIKEY: " + writeAPIKey + "\n"); client.print("Content-Type: application/x-www-form-urlencoded\n"); client.print("Content-Length: "); client.print(body.length()); client.print("\n\n"); client.print(body); client.print("\n\n"); Serial.print("ThingSpeak write... "); client.stop(); } count = 0; } Serial.print(count); Serial.print(" T: "); Serial.print(t); Serial.print(" | H: "); Serial.println(h); delay(delayLength); } int CayWrite(char w, char z) { Cayenne.virtualWrite(w, z); }