ESP32 + ThingSpeak just send zero value.
Show older comments
Hi everyone.
I'm trying to send the ADC value from a potentiometer to the ThingSpeak channel and I'm dealing with the problem that I send data to ThingSpeak and I just receive zero value in the channel even if I turn the potentiometer knob. If I run the program without the ThingSpeak configuration everything work well.
Could you help me, please?
I leave the source code below.
Thank you.
/*LIBRARIES*/
#include <Arduino.h>
#include "ThingSpeak.h"
#include "WiFi.h"
/*DEFINITIONS*/
#define ANALOGPIN 15
/*THINGSPEAK CONFIGURATION*/
const char* ssid = "xxxxxx";
const char* password = "xxxxxx";
unsigned long channelID = xxxxxxx;
const char* WriteAPIKey = "xxxxxx";
WiFiClient Client;
/*PROTOTYPES*/
void readPot (void);
/*--START FILE--*/
void setup()
{
delay(2000);
Serial.begin(115200);
Serial.println("ESP32 ADC");
/*WIFI CONNECTION*/
WiFi.begin(ssid,password);
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("\nWifi connected");
ThingSpeak.begin(Client);
}
void loop()
{
delay(2000);
readPot();
ThingSpeak.writeFields(channelID,WriteAPIKey);
Serial.println("Send data.");
Serial.println("---------------\n");
delay(18000);
}
/*FUNCTION*/
void readPot (void)
{
int potVal = analogRead(ANALOGPIN);
while(isnan(potVal))
{
Serial.println("Read failed");
delay(2000);
potVal = analogRead(ANALOGPIN);
}
Serial.println("\nADC: ");
Serial.println(potVal);
ThingSpeak.setField(1,potVal);
}
/*-- END OF FILE --*/
2 Comments
Armando Villegas
on 10 Feb 2023
Christopher Stapels
on 10 Feb 2023
I would consider returning the pot value from your function readPot and them calling setField in the same scope (in the main loop) as writefields. I'm not sure the variable information set with setField() in the function is transferred back to the main function.
I have multiple devices running for multiple years that read the ADC while wifi is connected with no problem.
Answers (0)
Communities
More Answers in the ThingSpeak Community
Categories
Find more on Read Data from Channel in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!