I want to send my ultrasonic sensor data on thing speak through arduino programming using esp32 microcontroller . but it is not sending the data on thingspeak.
5 ビュー (過去 30 日間)
古いコメントを表示
#include <WiFi.h>
#include "ThingSpeak.h"
const char* ssid = "Redmi"; // your network SSID (name)
const char* password = "kavita12"; // your network password
WiFiServer server(80);
WiFiClient client;
//char* myChannelID = "2475720";
unsigned long myChannelnumber = 2;
char *myWriteAPIKey = "xxxxxxxxxxx";
//unsigned long lastTime = 0;
//unsigned long timerDelay = 30000; // Interval between each data send attempt
int triggerPin = 19;
int echoPin = 18;
void setup() {
Serial.begin(921600); // Initialize serial communication
WiFi.mode(WIFI_STA); // Set WiFi mode to station mode
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.println("Attempting to connect to WiFi...");
WiFi.begin(ssid, password);
//int attempts = 0;
while (WiFi.status() != WL_CONNECTED )
{
delay(5000); // Wait 5 seconds before retrying
Serial.println("Retrying WiFi connection...");
}
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("Connected to WiFi.");
}
else
{
Serial.println("Failed to connect to WiFi.");
}
server.begin();
ThingSpeak.begin(client);
}
void loop()
{
digitalWrite(triggerPin, LOW);
delayMicroseconds(10);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
int pingTravelTime = pulseIn(echoPin, HIGH);
delay(500);
float pingTravelDistance = pingTravelTime * 0.0343 / 2;
Serial.print("Distance to target: ");
Serial.print(pingTravelDistance);
Serial.println(" cm");
ThingSpeak.setField(1,pingTravelDistance );
//int x = ThingSpeak.writeField(myChannelID, 1, pingTravelDistance, myWriteAPIKey);
int x = ThingSpeak.writeFields(myChannelnumber, myWriteAPIKey);
if (x == 200)
{
Serial.println("Channel update successful.");
}
else
{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
//lastTime = millis();
delay(10000);
}
4 件のコメント
Christopher Stapels
2024 年 3 月 21 日
編集済み: Christopher Stapels
2024 年 3 月 21 日
are you seing wither of these messages?
"Channel update successful."
"Problem updating channel. HTTP error code " + String(x)
?
Are you able to update the channel via web browser address bar?
回答 (1 件)
Christopher Stapels
2024 年 3 月 21 日
Please try writing using the browser once so there is at least some data in your channel. See the api keys tab of your channel for the syntax to write a single value to your channel.
I would not use .getLastReadStatus() to check the update.
Try to read the value for .writefield for error checking as in the library examples. Then we might be able to see what is actually happening.
0 件のコメント
コミュニティ
その他の回答 ThingSpeak コミュニティ
参考
カテゴリ
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!