esp8266 isn't reading or writing on the field

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

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

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

The connection isn't failing. the esp connects easily to the wifi. I tried 2 different modules both works fine with the internet but still keep getting the same problem
i'm using this lib
Are you able to try a different WiFi? There are a very few places in the world where ThingSpeak is blocked, but it is distantly possible. Are you able to connect via a browser window on the same network?
Can you show us the connection to wifi routine for the device? Is it straight from the example? Is it possible you are accidentally disconencting before the ThingSpeak connection is tried?
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
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 MATLAB 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!