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......