MKK
MKK

Reputation: 49

How to initialise wifi module to Arduino. ESP8266

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?

Upvotes: 0

Views: 421

Answers (1)

Lavanya U
Lavanya U

Reputation: 23

#include <ESP8266WiFi.h>
const char* ssid = "";                //replace with our wifi ssid
const char* password = "";         //replace with your wifi password
Serial.print("Connecting to wifi: ");
  Serial.println(ssid);
  
   while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print(".");
   }
   Serial.println("");
   Serial.println("WiFi connected");
   Serial.println("IP address: ");
   Serial.println(WiFi.localIP());```

Upvotes: 1

Related Questions