esp8266 isn't reading or writing on the field

4 views (last 30 days)
esp8266 keeps displaying 0 even though the field is 1, and it's not writing to it. it was working fine for months. and today out of the blue it's not working. checked the esp and it's working fine. tried writing and reading from the channel using an app and it's working fine. what could be the problem?
void loop() {
int A = ThingSpeak.readLongField(ChannelNumber, FieldNumber1, ReadAPIKey);
Serial.println(A);
if (A == 1 && !relayTriggered) { // only trigger the relay if it hasn't been triggered before
digitalWrite(relay, LOW);
delay(200);
digitalWrite(relay, HIGH);
ThingSpeak.writeField(ChannelNumber, FieldNumber1, 1, WriteAPIKey); // Write 0 back to FieldNumber1 using ThingSpeak API
relayTriggered = true; // set the flag to true after the relay has been triggered
} else if (A == 0) {
relayTriggered = false; // reset the flag when the field value changes to 0
}
ThingSpeak.writeField(ChannelNumber, FieldNumber1, 1, WriteAPIKey);
}
  1 Comment
Muhammad
Muhammad on 5 Jun 2023
i tried this " ThingSpeak.begin(client);
if (client.connected()) {
Serial.println("ThingSpeak connection successful");
} else {
Serial.println("ThingSpeak connection failed");
}" to see if it can connect to thingspeak but it

Sign in to comment.

Accepted Answer

Christopher Stapels
Christopher Stapels on 5 Jun 2023
Edited: Christopher Stapels on 5 Jun 2023
If your connection is failing, that would explain why you aren't getting the correct values. One thing I have found with the ESP is that you sometimes have to wait longer for a connection. If you have a set delay in the connect routine, try adding more delay. Also try moving to a place where there may be a stronger connection or try a different Wi-Fi altogether.
You should consider using the ThingSpeak communication library for Arduino etc. It takes care of a lot of that trouble for you and has plenty of great associated code examples.
  4 Comments
Muhammad
Muhammad on 5 Jun 2023
Edited: Muhammad on 5 Jun 2023
I don't think that there is any problem with the connection because it has been working for 2 months now without any problem, and yeah I tried a different network. and it's connecting to Thingspeak. I made an app to read and write to the channel and it's working fine. but when it comes to esp its not working
Christopher Stapels
Christopher Stapels on 8 Jun 2023
Did you use a different device and had sucess connecting to ThingSpeak but the ESP still did not, even on the new wifi network? Im sorry I wasnt sure what you meant. Is the situation still the same?

Sign in to comment.

More Answers (0)

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Read Data from Channel 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!