Problem Communicating with ESP8266-01 Module......!

:confused: i ran into a problem my wifi is working good, it connects with my phone but when i connect it through remotexy App, it runs a log shows connection refused…! port “abc” from "port “xyz” i would have uploaded a screen shot but but i do not know how to upload it here.
i do not know. Can you help with his issue.

I use any app and code but all android apps give same response connection refused…!
please help…! :confused:

You need to disconnect from your wifi router.
Then try to connect to your ESP-01 module which should have been set up as an Access Point.

The other option is to have your ESP-01 get an IP address from your router and you connect to the ESP-01 using that dynamic IP address.

.

ieee488:
The other option is to have your ESP-01 get an IP address from your router and you connect to the ESP-01 using that dynamic IP address.

My esp IP is 192.168.4.1 why it always show whenever I check it with AT+CIFSR command

and when I put the same IP in my android App it still shows connection refused.

NOTE: Here I did not understand what is the role of a router. I need to control arduino pins or esp pins with android App so my local network is my phone connected with WiFi module not to a router or internet. Please make this point clear to me. I was following a tutorial to control led at GPIO2 of esp-01 module with App developed in app inventor tool. Thank you.

Not sure what you are doing in your hidden code. So not much else I can add.

.

#3

This is my code as copied from tutorial :

 #include <ESP8266WiFi.h>

const char* ssid = "SSID";//type your ssid
const char* password = "PASSWORD";//type your password

int relayPin = 2; // GPIO2 of ESP8266
WiFiServer ESPserver(80);//Service Port

void setup() 
{
Serial.begin(115200);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH);

Serial.println();
Serial.println();
Serial.print("Connecting to: ");
Serial.println(ssid);

WiFi.begin(ssid, password);
delay(5000);

/*
 The following four line of the 
 code will assign a Static IP Address to 
 the ESP Module. If you do not want this, 
 comment out the following four lines.  
 */

IPAddress ip(192,168,1,254);   
IPAddress gateway(192,168,1,1);   
IPAddress subnet(255,255,255,0);   
WiFi.config(ip, gateway, subnet);
delay(5000);

while (WiFi.status() != WL_CONNECTED) 
{
delay(100);
Serial.print("*");
}
Serial.println("");
Serial.println("WiFi connected");

// Start the server
ESPserver.begin();
Serial.println("Server started");

// Print the IP address
Serial.print("The URL to control ESP8266: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
}

void loop() 
{
// Check if a client has connected
WiFiClient client = ESPserver.available();
if (!client) 
{
return;
}

// Wait until the client sends some data
Serial.println("New Client");
while(!client.available())
{
delay(1);
}

// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();

// Match the request

int value = LOW;
if (request.indexOf("/RELAYON") != -1) 
{
Serial.println("LAMP is ON");
digitalWrite(relayPin, LOW);
value = LOW;
} 
if (request.indexOf("/RELAYOFF") != -1)
{
Serial.println("LAMP is OFF");
digitalWrite(relayPin, HIGH);
value = HIGH;
}

// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); //  IMPORTANT
client.println("<!DOCTYPE HTML>");
client.println("<html>");

client.print("Status of the Lamp: ");

if(value == LOW) 
{
client.print("ON");  
} 
else 
{
client.print("OFF");
}

delay(1);
//client.stop();
Serial.println("Client disconnected");
Serial.println("");
}

What does the output on the Serial Monitor show when you power up the ESP-01 ?

.

ieee488:
What does the output on the Serial Monitor show when you power up the ESP-01 ?

.

  1. When I program my esp with this code the ESP does not show up in my phone wifi devices list but when i flash it then shows.

  2. when esp is Programmed 100% complete and when i reset it and open serial monitor it shows connecting to ESP_YOUNUS........ which is my ssid. i have NOT set any password.

  3. Then it just goes on printing *************************** and does nothing else.!

In which MODE i have to set esp in like 2 or 3 / "AT+CWMODE=2 or 3?"

Are there any other parameters in this code that I need to change other than ssid and password my IP is 192.168.4.1 .
The blogger was talking of some static IP , gateway IP, and MASK where to get these i do not KNOW.
In case of BLE every program has resemblance with its different codes but here in ESP every code is far different from the other codes for the same function like controlling an LED why is it so..........? please help a bit more.....! :confused:

Hi all anyone for help..?......!

Look at the examples at GitHub - ekstrand/ESP8266wifi: ESP8266 Arduino library with built in reconnect functionality

ieee488:
Look at the examples at GitHub - ekstrand/ESP8266wifi: ESP8266 Arduino library with built in reconnect functionality

i WILL CHECK OUT THOSE EXAMPLES BUT BUT SEE THIS CODE I send commands from app and I prints the text that is defined in this code i.e all serial print commands in serial monitor but does not do anything to output pins.
and finally when i send command again it shows connection refused.

#include <SoftwareSerial.h>
 
#define DEBUG true
 
SoftwareSerial esp8266(2,3); // rx, tx
// This means that you need to connect the TX pin of the esp to the Arduino's pin 2
// and the RX pin of the esp to the Arduino's pin 3
int relay1 = 9;
int relay2 = 10;
int relay3 = 11;
int relay4 = 12;
int relay5 = 13;
 
 
int flag1 = 0;
int flag2 = 0;
int flag3 = 0;
int flag4 = 0;
int flag5 = 0;
 
void setup()
{
Serial.begin(9600);
esp8266.begin(9600); // your esp's baud rate might be different
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);
pinMode(relay3,OUTPUT);
pinMode(relay4,OUTPUT);
pinMode(relay5,OUTPUT);
 
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);
digitalWrite(relay5, LOW);
 
 
 
sendCommand("AT+RST\r\n",2000,DEBUG); // reset module
sendCommand("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
sendCommand("AT+CWJAP=\"YOUNUS\",\"87654321\"\r\n",3000,DEBUG);
delay(10000);
sendCommand("AT+CIFSR\r\n",1000,DEBUG); // get ip address
sendCommand("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
sendCommand("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
Serial.println("Server Ready");
}
 
void loop()
{
char pinNumber = '0';
char secondNumber = '0';
if(esp8266.available()) // check if the esp is sending a message
{
 
 
//delay(1000); // wait for the serial buffer to fill up (read all the serial data)
 
 
// get the connection id so that we can then disconnect
char connectionId = '0'; //esp8266.read(); // subtract 48 because the read() function returns
// the ASCII decimal value and 0 (the first decimal number) starts at 48
esp8266.find("pin="); // advance cursor to "pin="
char pinNumber = (esp8266.read()); // get first number i.e. if the pin 13 then the 1st number is 1
Serial.println("pin number:");
Serial.print(pinNumber);
 
char secondNumber = (esp8266.read());
Serial.println("second number:");
Serial.println(secondNumber);
String content ="" ;
 
// relay1 
if((pinNumber == '1')&&(secondNumber == '1') && ( flag1 == 0))
{
 digitalWrite(relay1, HIGH);
 content = "relay1 is ON ";
 flag1 = 1;
 
}
 
if((pinNumber == '1')&&(secondNumber == '8') && (flag1 == 1) )
{
 digitalWrite(relay1, LOW);
  content = "Relay1 is OFF ";
flag1 = 0;
 
 
} 
 
// relay2 
if((pinNumber == '2')&&(secondNumber == '3') && ( flag2 == 0))
{
 digitalWrite(relay2, HIGH);
 content = "relay2 is ON ";
 flag2 = 1;
 
}
 
if((pinNumber == '6')&&(secondNumber == '6') && (flag2 == 1) )
{
 digitalWrite(relay2, LOW);
  content = "Relay2 is OFF ";
flag2 = 0;
 
 
} 
// relay3
if((pinNumber == '4')&&(secondNumber == '4') && ( flag3 == 0))
{
 digitalWrite(relay3, HIGH);
 content = "relay3 is ON ";
 flag3 = 1;
 
}
 
if((pinNumber == '7')&&(secondNumber == '7') && (flag3 == 1) )
{
 digitalWrite(relay3, LOW);
  content = "Relay3 is OFF ";
flag3 = 0;
 
 
} 
 
// relay4
if((pinNumber == '9')&&(secondNumber == '9') && ( flag4 == 0))
{
 digitalWrite(relay4, HIGH);
 content = "relay4 is ON ";
 flag4 = 1;
 
}
 
if((pinNumber == '6')&&(secondNumber == '4') && (flag4 == 1) )
{
 digitalWrite(relay4, LOW);
  content = "Relay4 is OFF ";
flag4 = 0;
 
 
} 
 
// relay5
if((pinNumber == '3')&&(secondNumber == '3') && ( flag5 == 0))
{
 digitalWrite(relay5, HIGH);
 content = "relay5 is ON ";
 flag5 = 1;
 
}
 
if((pinNumber == '9')&&(secondNumber == '5') && (flag5 == 1) )
{
 digitalWrite(relay5, LOW);
  content = "Relay5 is OFF ";
flag5 = 0;
 
 
} 
 
 
 
 
// feedback 
if((pinNumber == '5')&&(secondNumber == '5') )
{
  if (digitalRead(relay1) )
  content += "Relay1 = ON"; 
  else 
  content += "Relay1 = OFF"; 
 
 
    if (digitalRead(relay2) )
  content += " Relay2 = ON"; 
  else 
  content += " Relay2 = OFF"; 
 
      if (digitalRead(relay3) )
  content += " Relay3 = ON"; 
  else 
  content += " Relay3 = OFF";
 
         if (digitalRead(relay4) )
  content += " Relay4 = ON"; 
  else 
  content += " Relay4 = OFF"; 
 
         if (digitalRead(relay5) )
  content += " Relay5 = ON"; 
  else 
  content += " Relay5 = OFF"; 
 
 
}
//******************************************
 
 
sendHTTPResponse(connectionId,content);
// make close command
String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId; // append connection id
closeCommand+="\r\n";
sendCommand(closeCommand,1000,DEBUG); // close connection
 
}
}
 
/*
* Name: sendData
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
int dataSize = command.length();
char data[dataSize];
command.toCharArray(data,dataSize);
esp8266.write(data,dataSize); // send the read character to the esp8266
if(debug)
{
Serial.println("\r\n====== HTTP Response From Arduino ======");
Serial.write(data,dataSize);
Serial.println("\r\n========================================");
}
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
 
/*
* Name: sendHTTPResponse
* Description: Function that sends HTTP 200, HTML UTF-8 response
*/
void sendHTTPResponse(char connectionId, String content)
{
// build HTTP response
String httpResponse;
String httpHeader;
// HTTP Header
httpHeader = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n";
httpHeader += "Content-Length: ";
httpHeader += content.length();
httpHeader += "\r\n";
httpHeader +="Connection: close\r\n\r\n";
httpResponse = httpHeader + content + " "; // There is a bug in this code: the last character of "content" is not sent, I cheated by adding this extra space
sendCIPData(connectionId,httpResponse);
}
 
/*
* Name: sendCIPDATA
* Description: sends a CIPSEND=<connectionId>,<data> command
*
*/
void sendCIPData(char connectionId, String data)
{
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=data.length();
cipSend +="\r\n";
sendCommand(cipSend,1000,DEBUG);
sendData(data,1000,DEBUG);
}
 
/*
* Name: sendCommand
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendCommand(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}

After this i will show you another code that works good turns on and off an LED at GPIO2 pin but after each on it disconnects and each off it disconnects why…

74younus:
After this i will show you another code that works good turns on and off an LED at GPIO2 pin but after each on it disconnects and each off it disconnects why......

I don't know.

I have no interest in this type of project, so hopefully someone can help you.

If I was to do a project that needs wifi, I would not be using a ESP-01.

Edit:
The ESP-01 also needs to have a reliable 3.3V power supply to function reliably.
I use a 3.3V 1A power supply.
ESP-01 cannot be powered from the Arduino Uno's 3.3V pin.

ieee488:
I don't know.

I have no interest in this type of project, so hopefully someone can help you.

If I was to do a project that needs wifi, I would not be using a ESP-01.

Edit:
The ESP-01 also needs to have a reliable 3.3V power supply to function reliably.
I use a 3.3V 1A power supply.
ESP-01 cannot be powered from the Arduino Uno's 3.3V pin.

OK thank You but somehow I got the problem solve.