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")).
#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:
esp8266Serial("AT+CWJAP=\"MojeWifi\",\"abcdefgh\"\r\n", 5000, DEBUG);//Enter your WiFi network's SSID and Password.
while(!esp8266.find("OK"))
{}
with
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?