Data is sent but not update
7 次查看(过去 30 天)
显示 更早的评论
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);
}
0 个评论
采纳的回答
Christopher Stapels
2023-2-10
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.
更多回答(1 个)
Christopher Stapels
2023-2-6
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 个评论
Christopher Stapels
2023-2-7
Were you able to update the channel using the read API in the web browser?
社区
更多回答在 ThingSpeak Community
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Read Data from Channel 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!