ESP32 + ThingSpeak just send zero value.

7 visualizaciones (últimos 30 días)
Armando Villegas
Armando Villegas el 9 de Feb. de 2023
Comentada: Christopher Stapels el 10 de Feb. de 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 comentarios
Armando Villegas
Armando Villegas el 10 de Feb. de 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 el 10 de Feb. de 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.

Iniciar sesión para comentar.

Respuestas (0)

Comunidades de usuarios

Más respuestas en  ThingSpeak Community

Categorías

Más información sobre Read Data from Channel en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by