Go Down

Topic: Arduino and NodeMCU infinite loop when checking connection status esp8266.find (Read 1 time) previous topic - next topic

Qrczaak

Hello guys I am beginner with microcontrollers but I am trying to make a simple IoT with Arduino and NodeMCU as wifi. I was using this tutorial https://www.electronicshub.org/wifi-controlled-led-using-esp8266-and-arduino/ but I have encounter some problems with it. When I firstly uploaded it to my Arduino it was fine and Arduino could even  receive GET request from my server. Unfortunately second time and every other time when i tried to upload it, it gets into infinite loop in setup function line with while(!esp8266.find("OK")).

Code: [Select]
#include <SoftwareSerial.h>
SoftwareSerial esp8266(2,3); //Pin 2 & 3 of Arduino as RX and TX. Connect TX and RX of ESP8266 respectively.
#define DEBUG true
#define led_pin 11 //LED is connected to Pin 11 of Arduino


void setup()
  {
    pinMode(led_pin, OUTPUT);
    digitalWrite(led_pin, LOW);
    Serial.begin(9600);
    esp8266.begin(115200); //Baud rate for communicating with ESP8266. Your's might be different.
    esp8266Serial("AT+RST\r\n", 5000, DEBUG); // Reset the ESP8266
    esp8266Serial("AT+CWMODE=1\r\n", 5000, DEBUG); //Set station mode Operation
    esp8266Serial("AT+CWJAP=\"SSID\",\"Password\"\r\n", 5000, DEBUG);//Enter your WiFi network's SSID and Password.
                                   
    while(!esp8266.find("OK"))
    {
      }
    esp8266Serial("AT+CIFSR\r\n", 5000, DEBUG);//You will get the IP Address of the ESP8266 from this command.
    esp8266Serial("AT+CIPMUX=1\r\n", 5000, DEBUG);
    esp8266Serial("AT+CIPSERVER=1,80\r\n", 5000, DEBUG);
  }

void loop()
  {
    if (esp8266.available())
      {
        if (esp8266.find("+IPD,"))
          {
            String msg;
            esp8266.find("?");
            msg = esp8266.readStringUntil(' ');
            String command1 = msg.substring(0, 3);
            String command2 = msg.substring(4);
                       
            if (DEBUG)
              {
                Serial.println(command1);//Must print "led"
                Serial.println(command2);//Must print "ON" or "OFF"
              }
            delay(100);

              if (command2 == "ON")
                    {
                      digitalWrite(led_pin, HIGH);
                    }
                   else
                     {
                       digitalWrite(led_pin, LOW);
                     }
          }
      }
  }
   
String esp8266Serial(String command, const int timeout, boolean debug)
  {
    String response = "";
    esp8266.print(command);
    long int time = millis();
    while ( (time + timeout) > millis())
      {
        while (esp8266.available())
          {
            char c = esp8266.read();
            response += c;
          }
      }
    if (debug)
      {
        Serial.print(response);
      }
    return response;
  }
 


I tried to connect my devices in accordance to tutorial so I connected as follows:

Arduino NodeMCU
pin2 - TX
pin3 - RX
3.3V - 3V3

But as I said everything worked nice only at first upload then it gets stuck in a loop every other time. I was trying to reset Arduino and NodeMCU, even flash it but it didnt help.

I found one workaround, I changed:

Code: [Select]
    esp8266Serial("AT+CWJAP=\"MojeWifi\",\"abcdefgh\"\r\n", 5000, DEBUG);//Enter your WiFi network's SSID and Password.
                                   
    while(!esp8266.find("OK"))
    {}


with

Code: [Select]
String connection = esp8266Serial("AT+CWJAP=\"MojeWifi\",\"abcdefgh\"\r\n", 5000, DEBUG);//Enter your WiFi network's SSID and Password.
                                   
    if(connection.indexOf("OK") > 0){
        esp8266Serial("AT+CIFSR\r\n", 5000, DEBUG);//You will get the IP Address of the ESP8266 from this command.
            esp8266Serial("AT+CIPMUX=1\r\n", 5000, DEBUG);
            esp8266Serial("AT+CIPSERVER=1,80\r\n", 5000, DEBUG);
    }


and then it started to work again every time... BUT i think that using IF in this case is not the best idea.

I am not very familiar with serial connection but as far as I understand while loop is waiting for an answer from NodeMCU if it is connected to wifi and proceed code when it is connected (returns OK). When i changed it to IF statement it is not waiting for connection, it just check once if its connected and if its not then... nothing happens.

So my question is - do you have any ideas why it gets stuck in that loop? Do i connect something inproperly? Or maybe I received OK answer too fast and while loop could'nt manage to read it? Well, what do you think about IF statement in that situation?

PaulS

Quote
Unfortunately second time and every other time when i tried to upload it, it gets into infinite loop in setup function line with while(!esp8266.find("OK")).
Then, I suggest you stop blindly parsing data you can't see.

Read the data coming from the esp8266 instance, and store it in an array. Then, you can print what you receive, and, perhaps a clue by four will whack you.
The art of getting good answers lies in asking good questions.

Qrczaak

Read the data coming from the esp8266 instance
Well, if I understand you correctly I've alredy did what you aksed me to do (Serial.print(response))

Code: [Select]

String esp8266Serial(String command, const int timeout, boolean debug)
  {
    String response = "";
    esp8266.print(command);
    long int time = millis();
    while ( (time + timeout) > millis())
      {
        while (esp8266.available())
          {
            char c = esp8266.read();
            response += c;
          }
      }
    if (debug)
      {
        Serial.print(response);
      }
    return response;
  }{


Below function prints OK, nevertheless it still gets stuck in a loop.

Code: [Select]
esp8266Serial("AT+CWJAP=\"SSID\",\"Password\"\r\n", 5000, DEBUG);
    while(!esp8266.find("OK"))
    {}.



PaulS

Code: [Select]
  }{
There is not a single place in C++ coding where this crap is appropriate.

Quote
Below function prints OK, nevertheless it still gets stuck in a loop.
That does not print squat.
The art of getting good answers lies in asking good questions.

Qrczaak

Are you going to help or beeing rude? If you choose second option dont even read the rest of this post because like I said I am a beginner and looking for some actually helpful advice.

I know }{ is not appropriate and it was just misclick when i copied code.

And why do you tell me that this function does not print anything if it does? It prints response from esp -> Serial.print(response). There is a body of this function in previous post (and in first post).

PaulS

Quote
And why do you tell me that this function does not print anything if it does? It prints response from esp -> Serial.print(response). There is a body of this function in previous post (and in first post).
Your second snippet in the post I was referring to was:
Code: [Select]
esp8266Serial("AT+CWJAP=\"SSID\",\"Password\"\r\n", 5000, DEBUG);
    while(!esp8266.find("OK"))
    {}.

Which line, of those three, were you thinking of, when you said that that snippet printed something?

You don't need to bother replying, for my benefit, because I am done with you.
The art of getting good answers lies in asking good questions.

Qrczaak

I was thinking about the first line which is a function as you can see and which body is included just above this snippet.

Anyway, wherever code prints values from ESP it still prints "OK" and esp8266.find("OK") is not interpreting it correctly or I make it wrong way.

Go Up