Data is sent but not update
Afficher commentaires plus anciens
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);
}
Réponse acceptée
Plus de réponses (1)
Christopher Stapels
le 6 Fév 2023
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 commentaires
Panithan Chantubtim
le 6 Fév 2023
Christopher Stapels
le 6 Fév 2023
Can you describe in more detail what you tried? There were several suggestions in my post.
If the number of entries went up but you dont see data in the field plots, that usually means you are writing blank or string data to ThingSpeak. You can see the data that you wrote by reading back the data, as I described above.
The read API has this format
https://api.thingspeak.com/channels/949687/feeds.json?api_key=xxxxxxxxxxxxxxxx&results=2
Panithan Chantubtim
le 7 Fév 2023
Christopher Stapels
le 7 Fév 2023
Were you able to update the channel using the read API in the web browser?
Panithan Chantubtim
le 10 Fév 2023
Communautés
Plus de réponses dans ThingSpeak Community
Catégories
En savoir plus sur ThingSpeak dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!