Data is sent but not update

5 visualizzazioni (ultimi 30 giorni)
I write the code to sent the humidity and temperature from sensor to ThingSpeak. The data is sent but the field grap is not updating. I write code on nodeMCU.
#include <ESP8266WiFi.h>
#include <DHT.h>
//PIN
int FAN_PIN = 0;
int WATER_PIN = 2;
#define DHTTYPE DHT11
//variable
DHT dht;
float tempsetting = 28;
float humidsetting = 80;
//Wifi variables
String apiKey = "xxxxxxxxxxxxxxxx"; //Write API key
long channelID = 1872808;
const char* server = "api.thingspeak.com";
const char* ssid = "Chan family_2G";
const char*pass = "yyyyyyyyyyy";
WiFiClient client;
void setup() {
Serial.begin(9600);
delay(500);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
dht.setup(16);
}
void loop() {
delay(dht.getMinimumSamplingPeriod());
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
pinMode(FAN_PIN, OUTPUT);
pinMode(WATER_PIN, OUTPUT);
function();
cloud();
}
void function() {
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
Serial.print(dht.getStatusString());
Serial.print("\tHumidity :");
Serial.print(humidity, 1);
Serial.print("\t\tTemp C:");
Serial.println(temperature, 1);
if (humidity >= humidsetting) {
digitalWrite(WATER_PIN, LOW);
Serial.print("Water OFF");
}
else {
digitalWrite(WATER_PIN, HIGH);
Serial.print("Water ON");
}
if (temperature <= tempsetting) {
digitalWrite(FAN_PIN, LOW);
Serial.println("\tFan OFF");
}
else {
digitalWrite(FAN_PIN, HIGH);
Serial.println("\tFan ON");
}
delay(2000);
}
void cloud() {
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
String Humidity = String(humidity);
String Temperature = String(temperature);
if (isnan(temperature) || isnan(humidity)){
Serial.println("Failed to read value from sensor!");
return;
}
// Connect to ThingSpeak
WiFiClient client;
const int httpPort = 80;
if (!client.connect("api.thingspeak.com", httpPort)) {
Serial.println("Failed to connect to ThingSpeak");
return;
}
// Build the URL for the request
String url = "/update?api_key=";
url += apiKey;
url += "&field1=";
url += String(humidity);
url += "&field2=";
url += String(temperature);
// Make the HTTP GET request
if (client.connect(server,80)) {
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("\n\n");
}
// Wait for the response from ThingSpeak
int timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println("Timeout while reading response from ThingSpeak");
client.stop();
return;
}
}
// Close the connection to ThingSpeak
client.stop();
Serial.println();
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% and Temperature: ");
Serial.print(temperature);
Serial.println(" send to ThingSpeak");
Serial.println("Waiting...");
Serial.println();
Serial.println();
// Wait for a short period before sending the next request
delay(20000);
}

Risposta accettata

Christopher Stapels
Christopher Stapels il 10 Feb 2023
Is seems that you are building the url but never sending it to ThingSpeak. This would explain why neither method works. I would recomend switching to the ThingSpeak library, but you could also add lines line these to your post.
client.println( "Content-Length: " + String( url.length() ) );
client.println();
client.println( url );
You can compare to this soil moisture example.

Più risposte (1)

Christopher Stapels
Christopher Stapels il 6 Feb 2023
We suggest using the ThingSpeak library for Arduino, when you can.
The next thing I would suggest is to see if you can update your channel using a web browser in the address field. See the API keys tab on you channel view for the correct format. You can use the read data fromat on the same tab to see if any data is being written to ThingSpeak, even if your plots are not showing the data. This will allow you to see if the sensor is working or if there is some other spurious data.
Then use your code to write a fixed string or number (not the sensor value) to ThingSpeak. Like this:
// Build the URL for the request
String url = "/update?api_key=";
url += apiKey;
url += "&field1=25";
Then see if the value of 25 shows up in your channel.
  5 Commenti
Christopher Stapels
Christopher Stapels il 7 Feb 2023
Were you able to update the channel using the read API in the web browser?
Panithan Chantubtim
Panithan Chantubtim il 10 Feb 2023
I can send the data using update API in web

Accedi per commentare.

Community

Più risposte nel  ThingSpeak Community

Categorie

Scopri di più su Read Data from Channel in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by