#include "WiFiEsp.h"
#include "SoftwareSerial.h"
#include "ThingSpeak.h"
#define ESP_BAUDRATE 115200
char ssid[] = "XXXXX";
char pass[] = "XXXXXX";
int status = WL_IDLE_STATUS;
void setup() {
Serial.begin(115200);
Serial1.begin(ESP_BAUDRATE);
WiFi.init(&Serial1);
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected");
}
WiFiEspClient client;
ThingSpeak.begin(client);
return;
}
void loop() {
int statusCode;
ThingSpeak.readFloatField(935349, 1);
statusCode = ThingSpeak.getLastReadStatus();
delay(10000);
return;
}
So I'm trying to connect my Arduino Mega 2560 to ThingSpeak, and read a data from a public channel. Before I put any code into void loop(), my arduino is successful in connecting to the wifi.
However, once I added the code into void loop, it just keeps on looping the code in void setup, and it wouldn't get to void loop even if I add return;. I've attached a photo of the message in serial monitor.
UKHeliBob:
What is the return; doing at the end of setup() ?
it wouldn't stop running void setup(), so I tried adding return; to see if it would stop and move on to void loop, but instead it just stayed in void setup
Have you commented out all the stuff in setup() and loop(). See if there is no rebooting, that's why setup in running all the time, then uncomment code, a few lines a a time, to see which thingy is causing your MCU to reboot?
Oh, is the ESP an 8266 or a ESP32?, it makes a difference.
Idahowalker:
Have you commented out all the stuff in setup() and loop(). See if there is no rebooting, that's why setup in running all the time, then uncomment code, a few lines a a time, to see which thingy is causing your MCU to reboot?
Oh, is the ESP an 8266 or a ESP32?, it makes a difference.
I don't know if software serial on an ESP8266 will run at 115200 baud but I have my doubts. I'd try 9600 first. I also doubt software serial even works on an ESP8266 because the interrupts needed for the WiFi to work mess the timing up. I couldn't get it to work.
I take it that despite the confusion you are using an ESP8266 not a Mega.
I suspect you have an Arduino mega and an ESP-01 connected to it, that you are trying to control with AT-commands, still only guess work though. It is not the most efficient way of using an ESP.
I also doubt software serial even works on an ESP8266
i also don't think it works, but to be fair, other than including the library, there is no object created, so in a way it isn't used. Maybe this code was migrated from an UNO. hwSerial (both 0 & 1) are being used.
WiFiEspClient client;
ThingSpeak.begin(client);
I don't know how this whole WiFiEspClient thing works, but doesn't the object go out of scope the moment that setup() finishes ? Wouldn't that result in the ThingSpeak object to no longer having anything to connect through ? That might cause the crash ?!
Deva_Rishi:
I don't know how this whole WiFiEspClient thing works, but doesn't the object go out of scope the moment that setup() finishes ? Wouldn't that result in the ThingSpeak object to no longer having anything to connect through ? That might cause the crash ?!
Deva_Rishi:
i also don't think it works, but to be fair, other than including the library, there is no object created, so in a way it isn't used. Maybe this code was migrated from an UNO. hwSerial (both 0 & 1) are being used.
At first I tried to use software serial and also couldn't get it to work, but I forgot to delete the library as well, sorry for the confusion.
Deva_Rishi:
WiFiEspClient client;
ThingSpeak.begin(client);
I don't know how this whole WiFiEspClient thing works, but doesn't the object go out of scope the moment that setup() finishes ? Wouldn't that result in the ThingSpeak object to no longer having anything to connect through ? That might cause the crash ?!
This seems to be the reason, it's working fine after I took it out of setup(). Thank you very much.