Looping in setup?

My code (Modified ITEAD WeeESP library example) is now looping after the “setup end” it seems to be getting the “AT+CIFSR” command and then looping. How can I fix this?

#include "ESP8266.h"

#define SSID        "-----------"
#define PASSWORD    "-----------"
#define HOST_NAME   "www.google.co.uk"
#define HOST_PORT   (80)

#include "ESP8266.h"
#include <SoftwareSerial.h>
#include <Arduino.h>
#include <U8x8lib.h>

U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);

SoftwareSerial mySerial(3, 2); /* RX:D3, TX:D2 */
ESP8266 wifi(mySerial);

void setup(){
    Serial.begin(9600);
    u8x8.begin();
    u8x8.setPowerSave(0);
    u8x8.setFont(u8x8_font_chroma48medium8_r);  
    Serial.print("setup begin\r\n");
    u8x8.drawString(0,1,"Setup...");
    Serial.print("FW Version:");
    Serial.println(wifi.getVersion().c_str());

    if (wifi.setOprToStationSoftAP()) {
        Serial.print("to station + softap ok\r\n");
        u8x8.drawString(0,1,"Connected! 1/2");   
    } else {
        Serial.print("to station + softap err\r\n");
    }

    if (wifi.joinAP(SSID, PASSWORD)) {
        Serial.print("Join AP success\r\n");
        u8x8.drawString(0,1,"Connected! 2/2");      
    }
    else {
        Serial.print("Join AP failure\r\n");
    }    
        Serial.print("setup end\r\n");
        u8x8.drawString(0,1,"Setup Complete!"); 

}
void loop(){
uint8_t buffer[1024] = {0};

    if (wifi.createTCP(HOST_NAME, HOST_PORT)) {
        Serial.print("create tcp ok\r\n");
    } else {
        Serial.print("create tcp err\r\n");
    }

    char *test = "GET / HTTP/1.1\r\nHost: www.google.co.uk\r\nConnection: close\r\n\r\n";
    wifi.send((const uint8_t*)test, strlen(test));

    uint32_t len = wifi.recv(buffer, sizeof(buffer), 10000);
    if (len > 0) {
        Serial.print("Received:[");
        for(uint32_t i = 0; i < len; i++) {
            Serial.print((char)buffer[i]);
        }
        Serial.print("]\r\n");
    }

    if (wifi.releaseTCP()) {
        Serial.print("release tcp ok\r\n");
    } else {
        Serial.print("release tcp err\r\n");
    }
    
    while(1);   
}

Not too much of a surprise that it's looping after setup, because that's what the loop() function does.

As in the whole thing. It doesn't go into the loop.

GridseedsFTW:
As in the whole thing. It doesn't go into the loop.

Instead of describing what you think it is doing, describe what happens. Are you seeing the "setup end" output over and over or what? Your current description doesn't make much sense especially in light of what you wrote in the first post.

Here is what the serial monitor sees. This happens over and over

snip.PNG

What type of Arduino?

uint8_t buffer[1024] = {0};

With all those libraries installed, do you have enough RAM for an array that big? Maybe you are having a stack overflow and it is resetting the board.

1 Like

Seeing

  while(1);

inside the loop() function is very strange.

aarg:
Seeing

  while(1);

inside the loop() function is very strange.

With ESP8266, that will cause WDT resets.

gfvalvo:
With ESP8266, that will cause WDT resets.

From the output it wouldn't appear that the code is even getting that far.

Arduino Pro Mini 3.3v 8MHz version is the board, SSD1306 for the OLED (which works fine) and an ESP-01 board SoftSerial to pins 2&3.

The pro mini only has 2K of RAM. That one array is taking up half of it. The display library probably uses a huge chunk too. Either try using freemem at the end of setup to see how much is there or reduce the size of that array to 30 or 40 bytes and see if that lets it run.

Delta_G:
freemem at the end of setup to see how much is there or reduce the size of that array to 30 or 40 bytes

I have reduced the buffer size by half and it has seemed to have fixed the looping, thank you for the help.

by the way, noobie question, what is this "freemem" and how would it be used? 5 mins of google didn't help that much.

Sorry, I meant freeRam. Check the bottom of this page. Arduino Playground - AvailableMemory

There are tons of options to get that information, but what I was after was to find out how much RAM was free at the end of setup before you create that huge array.