Void Setup looping itself

#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.

What is the return; doing at the end of setup() ?

@xhahahx

Your topic was Moved to it’s current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

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.

It's ESP8266

Arduino Mega 2560 has 4 hardware serial ports: Serial (USB to PC), Serial1, Serial2, Serial3.
You don't need software serial.

How do you know it doesn't make it to loop? Put a Serial.print in loop to confirm it doesn't make it. Take out the return in loop() as well.

xhahahx:
It’s ESP8266

Then why put

Arduino Mega 2560

?

An ESP8266 looks like this:

A Mega looks like this:

Already you are starting out by confusing those who would lend a hand.

With the next post, start over.

Post the well formatted code in code tags, the device being used, the problem, what the code is supposed to do and what it actually does.

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 ?!

Ding. Ding. Ding. Winner, winner! +1

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.

xhahahx
++Karma; // For posting code correctly and for letting us know that the advice helped solve your problem.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.