I'm working on a Arduino project found online but I'm having trouble with the configuration of the wifi module. Im using a Az Delivery ESP8266 wifi module and the code I have written is the following
#define SSID "SSID" //replace XXXXX by your router SSID
#define PASS "PSW!" //replace YYYYY by your router password
//communication with wifi module
monitor.flush();
monitor.println("AT");
delay(2000);
if(monitor.find("OK")){
Serial.println("Communication with ESP8266 module: OK");
}
else {
Serial.println("ESP8266 module ERROR");
}
//connect wifi router
connectWiFi();
Serial.print("Sampling (");
Serial.print(sampletime_ms/1000);
Serial.println("s)...");
//initialize timer
starttime = millis();
}
boolean connectWiFi(){
Serial.println("Connecting wi-fi...");
String cmd ="AT+CWMODE=1";
monitor.println(cmd);
delay(2000);
monitor.flush(); //clear buffer
cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
monitor.println(cmd);
delay(5000);
if(monitor.find("OK")){
Serial.println("Connection succeeded!");
return true;
}else{
Serial.println("Connection failed!");
return false;
}
Serial.println();
}
I get no error on running and the code compiles but when I open the serial monitor I see that the wifi module fails the first cycle and prints module ERROR.
What am I missing?