ESP32 + ThingSpeak just send zero value.

2 Ansichten (letzte 30 Tage)
Armando Villegas
Armando Villegas am 9 Feb. 2023
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 Kommentare
Armando Villegas
Armando Villegas am 10 Feb. 2023
I could find the problem.
In that link explain why after connect to WiFi you lost the values form ADC.
Christopher Stapels
Christopher Stapels am 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.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Communitys

Weitere Antworten in  ThingSpeak Community

Kategorien

Mehr zu Read Data from Channel finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by