The graph couldn't be seen on the chart although the code ha been run correctly

1 回表示 (過去 30 日間)
I have run the below code but unfortunatly I couldn't see the result as a chart in the channel, but it is seen on the treminal of the com port. Any hint? or suggestion?
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
//ThingSpeak IoT Device control Cloud
String apiKey = "xxxxxxxxxxxxxxxx"; //API key of Think speak
const char *ssid = "asamad";
const char *pass = "Gulf1234";
const char *server = "api.thinkspeak.com";
#define DHTPIN 2 // Digital pin D3 connected to the DHT sensor1
#define D4 4
DHT dht(DHTPIN, DHT22);
WiFiClient client;
void setup()
{
{
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(3000);
ESP.restart();
}
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_FS
type = "filesystem";
}
// NOTE: if updating FS this would be the place to unmount FS using FS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
dht.begin();
Serial.println("Connecting to");
Serial.println(ssid);
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
pinMode(D4, OUTPUT);
}//SetupEnd
void loop() {
ArduinoOTA.handle();
float t = dht.readTemperature();
float h = dht.readHumidity();
//Temperature Regulation-vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// if the temp. is high, turn ON the Fan:
if ( t > 26) {
digitalWrite(D4,LOW);
digitalWrite(D4, HIGH);
delay(5000);
} else {
// if the temp. is lOW, turn OFF the Fan:
digitalWrite(D4, LOW);
delay(2000);
}
if (client.connect(server,80)) // api.thingsspeaks.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr += "\r\n\r\n";
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("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius, Humidity: ");
Serial.print(h);
Serial.print("%, Send to Thingspeak.");
}
client.stop();
Serial.println("Waiting...");
delay(3000);
}

採用された回答

Christopher Stapels
Christopher Stapels 2022 年 7 月 21 日
We strongly reccomend using the ThingSpeak library for arduino, it takes care of a lot of code issues for you.
Troubleshooting steps:
Go to the API keys tab on your channel and copy the HTTP command for channel update. Use it in your broswer address window and make sure it updates your channel.
Then add Serial.println(postStr) statements to your code to make sure postStr matches the correct format shown on the API keys tab, or in your case on the Read data page, since you are using the POST method instead of GET.
You will find that you are missing a parameter name ("api_key=").
If you were still stuck here, then I reccomend using a hard coded value to post to ThingSpeak. You can even copy the GET command from your API keys tab and print it using client.println(), similar to how you are printing the POST HTTP commands.
Then you can work backwards to find the source of your trouble. In summary, I reccomend the library. Good luck with your IoT project! (ps. I removed your API keys to keep others from using up your messages)

その他の回答 (0 件)

コミュニティ

その他の回答  ThingSpeak コミュニティ

カテゴリ

Help Center および File ExchangeThingSpeak についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by