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 vues (au cours des 30 derniers jours)
#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 commentaires
Christopher Stapels
Christopher Stapels le 21 Mar 2024
Modifié(e) : Christopher Stapels le 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
kavita le 21 Mar 2024
#include <WiFi.h>
#include <ThingSpeak.h>
#include<WiFiClient.h>
// WiFi credentials
const char* ssid = "Redmi";
const char* password = "kavita12";
// ThingSpeak parameters
const char* thingSpeakAddress = "api.thingspeak.com";
const char* apiKey = "EQO13SE8NXF1BNEY";
const unsigned long channelNumber = 2480606;
// Pin connected to the sensor
int triggerPin = 19;
int echoPin = 18;
WiFiServer server(80);
WiFiClient client;
void setup() {
Serial.begin(921600);
connectToWiFi();
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Read sensor data
digitalWrite(triggerPin, LOW);
delayMicroseconds(10);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
int pingTravelTime = pulseIn(echoPin, HIGH);
delay(500);
int pingTravelDistance = pingTravelTime * 0.0343 ;
Serial.print("pingTravelDistance :");
Serial.print(pingTravelDistance);
float distancetotarget = pingTravelDistance/2;
Serial.print("Distance to target: ");
Serial.print(distancetotarget);
Serial.println(" cm");
// Send data to ThingSpeak
sendToThingSpeak(pingTravelDistance);
// Update every 15 seconds
delay(15000);
}
void connectToWiFi() {
Serial.println("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to WiFi");
}
void sendToThingSpeak(int data) {
ThingSpeak.begin(client); // Initialize ThingSpeak
// Convert the integer data to a String
// Create a new entry on the ThingSpeak channel
ThingSpeak.writeField(channelNumber, 1, data, apiKey);
// Check for successful update
if (ThingSpeak.getLastReadStatus() == 200) {
Serial.println("Channel update successful.");
} else {
Serial.println("Problem updating channel. HTTP error code " + String(ThingSpeak.getLastReadStatus()));
}
}
now channel update successfull but still data is not showing on thingspeak pingTravelDistance :16Distance to target: 8.00 cm
Channel update successful.
pingTravelDistance :40Distance to target: 20.00 cm
Channel update successful.
pingTravelDistance :16Distance to target: 8.00 cm
Channel update successful.
pingTravelDistance :16Distance to target: 8.00 cm
Channel update successful.
pingTravelDistance :16Distance to target: 8.00 cm
Channel update successful.
pingTravelDistance :40Distance to target: 20.00 cm
Channel update successful.
pingTravelDistance :40Distance to target: 20.00 cm
Channel update successful.
pingTravelDistance :40Distance to target: 20.00 cm
result is like that. is there another type of setting are require on thingspeak because i wrote proper channelid and api key etc

Connectez-vous pour commenter.

Réponses (1)

Christopher Stapels
Christopher Stapels le 21 Mar 2024
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.

Communautés

Plus de réponses dans  ThingSpeak Community

Catégories

En savoir plus sur Read Data from Channel dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by