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.
조회 수: 3 (최근 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!