I want to send sensor data to thingspeak using nodemcu esp8266 and arduino uno. So, the sensor only connect to arduino uno and i use serial communication between nodemcu esp8266 and arduino so that data from arduino can be read by nodemcu esp8266, and nodemcu esp8266 will send the data to thingspeak. I'm getting data on serial monitor but the data is not visible on thingspeak server. I have entered the credential code correctly. What should i do to make the sensor data visible on Thingspeak?
This is what I've done :
Code for Arduino Uno :
#include <DHT.h>
#define dhtPin 3
#define dhtType DHT22
DHT dht(dhtPin, dhtType);
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(t)|| isnan(h)) {
Serial.println("No Data");
delay(2000);
}
Serial.print("Temperature : ");
Serial.print(t);
Serial.print("Humidity : ");
Serial.print(h);
Serial.println();
delay(5000);
}
Code for NodeMCU ESP8266 :
#include<ESP8266WiFi.h>
#include<WiFiClient.h>
#include<ESP8266HTTPClient.h>
#include <ThingSpeak.h>
String URL = "http://api.thingspeak.com/update?api_key=APIKEY&field1=";
void setup() {
Serial.begin(115200);
WiFi.disconnect();
delay(2000);
Serial.print("Start connection");
WiFi.begin("SSID","PASSWORD");
while((!(WiFi.status()==WL_CONNECTED))){
delay(500);
Serial.print("..");
}
Serial.println("Connected");
}
void loop() {
if(Serial.available()>0){
String data=Serial.readStringUntil('\n');
Serial.println(data);
int comma = data.indexOf(',');
if(comma!=-1){
float t = data.substring(0,comma).toFloat();
float h = data.substring(comma+1).toFloat();
Serial.print(t);
Serial.println(h);
sendData(t,h);
}
}
}
void sendData(float t, float h){
WiFiClient client;
HTTPClient http;
String newUrl=URL+String(t)+"&field2="+String(h);
http.begin(client,newUrl);
int responsecode=http.GET();
String data = http.getString();
Serial.println(data);
http.end();
delay(5000);
}