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.
Show older comments
#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 Comments
Christopher Stapels
on 20 Mar 2024
Are you getting any messages from the code on your serial monitor?
Are you able to update the channel via web browser address bar?
If you are able to update your channel via the browser, I reccomend sending a constant value from your code instead of the ping sonar value. Then you can make sure you communicatoin with ThingSpeak is correct, and then work on the sonar part.
kavita
on 21 Mar 2024
Christopher Stapels
on 21 Mar 2024
Edited: Christopher Stapels
on 21 Mar 2024
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?
kavita
on 21 Mar 2024
Answers (1)
Christopher Stapels
on 21 Mar 2024
0 votes
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.
Communities
More Answers in the ThingSpeak Community
Categories
Find more on Downloads in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!