Esp 8266 moduleis not connecting to wifi

Hi all ,
can you please tell me where the mistake was happened and I don't understand also
why my Wi-Fi module isn't connecting to Wi-Fi here is code .

#include<SoftwareSerial.h>
int sensor;
int data;
int pump_status;
SoftwareSerial esp(3, 4);
#define SSID "ram"
#define PASS "ramgopal"
// send at commands to ESP8266
void sendAT(String cmd) {
Serial.print("SEND: ");
esp.println(cmd);
Serial.println(cmd);
}
boolean connectwifi() {
Serial.println("AT+CWMODE=1");
delay(2000);
Serial.println("AT+CWLAP");
String cmd = "AT+CWJAP=""SSID"",""PASS""";
sendAT(cmd);
delay(5000);

if (Serial.find("Error")) {
Serial.println("Received:Error couldn't connect to network");
return false;
}
else {
Serial.print("wifi connected \n");
}
cmd = "AT+CIPMUX=0";
sendAT(cmd);
if (Serial.find("Error")) {
esp.print("Received: Error");
return false;
}
}
void setup() {
Serial.begin(9600);
esp.begin(9600);
sendAT("AT");
delay(1000);
Serial.println("Received : OK \n ESP8266 ready to send data outside if block\n");
Serial.println(Serial.find("OK"));
if (Serial.find("OK")) {
Serial.println("Received : OK \n ESP8266 ready to send data \n");
connectwifi();
}
}

void loop() {
sensor = analogRead(A0);
data = map(sensor, 0, 1023, 100, 0);
String sensor_value = String(data);
Serial.print("Soil Moisture: ");
Serial.println(data);

if (data < 76) {
digitalWrite(8, HIGH);
pump_status = 100;
}
else {
digitalWrite(8, LOW);
pump_status = 0;
}
String pump = String(pump_status);
updateTS(sensor_value, pump);
delay(3000);
}

void updateTS(String T, String P) {
String cmd = "AT+CIPSTART="TCP","api.thingspeak.com",81";
sendAT(cmd);

Serial.print("TemparatureData And Pump Status ");
Serial.print(T);
Serial.print(P);
delay(2000);
if (Serial.find("Error")) {
esp.print("Received Error \n EXIT1:");
return;
}
// cmd = "GET /update?key=https://api.thingspeak.com/update?api_key=KZQ30BI64ZOLF9CY&field1=" + T + "&field2=" + P + "\r\n";
cmd="GET /update?key=KZQ30BI64ZOLF9CY&field1="+T+"&field2="+P+"\r\n"; // update the temprature and humidity values on thingspeak url,replace xxxxxxx with your write api key
Serial.print("AT+CIPSEND=");
Serial.println(cmd.length());
if (Serial.find(">")) {
esp.print(">");
esp.print(cmd);
Serial.print(cmd);

}
else {
sendAT("AT+CIPCLOSE");
}
if (Serial.find("Error")) {
esp.println("Received : Error \n EXIT2");
}
Serial.println();
}

Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

  String cmd = "AT+CWJAP=""SSID"",""PASS""";

Have you tried printing cmd before using it ?
Is it what you expect ?

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

What is the exact type of ESP8266-modul you are using?
There is a chance to completely avoid this AT-command-stuff

best regards Stefan

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.