hi i have developed year age an Adruino application that allows me send and receive data from a mobile application using esp8266
esp01
back then it worked really well but now i tried to rerun the same code using a different esp8266 esp01
the app won't work so i was wondering if the firmware flash has to do something with it because flashed the first esp firmware and the second is not but a friend of mine worked with the second one fine so here is my code
float Temperatur;
String Temperatur1;
#include <SoftwareSerial.h>
#define DEBUG true
SoftwareSerial esp8266(8,9);
void setup()
{
Serial.begin(9600);
esp8266.begin(9600); // your esp's baud rate might be different
pinMode(11,OUTPUT);
digitalWrite(11,LOW);
pinMode(12,OUTPUT);
digitalWrite(12,LOW);
Serial.println("Server START");
sendCommand("AT+RST\r\n",2000,DEBUG); // reset module
sendCommand("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
//sendCommand("AT+CWJAP=\"AF23_WI-FI_476C\",\"A776RAEQ\"\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()
{
/*
Temperatur=(5.0*analogRead(A0)*100.0)/1024;
Serial.println(Temperatur);
Temperatur1=(5.0*analogRead(A0)*100.0)/1024.0;
Serial.println(Temperatur1);
if(Temperatur > 50)
{digitalWrite(11, HIGH);
digitalWrite(12,LOW);}
*/
if(esp8266.available()) // check if the esp is sending a message
{
Serial.println("esp avai");
if(esp8266.find((char*)"+IPD,"))
{Serial.println("found ipd");
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
int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns
// the ASCII decimal value and 0 (the first decimal number) starts at 48
// sendHTTPResponse(connectionId,Temperatur);
esp8266.find((char*)"pin="); // advance cursor to "pin="
int pinNumber = (esp8266.read()-48); // get first number i.e. if the pin 13 then the 1st number is 1
int secondNumber = (esp8266.read()-48);
if(secondNumber>=0 && secondNumber<=9)
{
pinNumber*=10;
pinNumber +=secondNumber; // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first number
}
if((pinNumber==12)&&(pinNumber!=11))
{digitalWrite(12, HIGH);
digitalWrite(11,LOW);}
if((pinNumber==11)&&(pinNumber!=12))
{digitalWrite(12, LOW);
digitalWrite(11,HIGH);}
if((pinNumber==13)&&(pinNumber!=12)&&(pinNumber!=11))
{digitalWrite(12, LOW);
digitalWrite(11,LOW);}
// build string that is send back to device that is requesting pin toggle
String content;
if(pinNumber==11)
{
content = "L'exutoire est en cours d'ouverture";
}
else if(pinNumber==12)
{
content = "L'exutoire est en cours de fermeture";
}
else if (pinNumber==13)
{ content = " Il est arrêter ";}
else if (pinNumber==10)
{ content = Temperatur1 ;}
sendHTTPResponse(connectionId,content);
}
}else {
Serial.println("not available");
}
}
/*
* 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(int 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(int 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;
}
at first i got no response from the esp then i tried to change esp8266.begin(9600);
to 115200 i got this
Server START
AZ+RST
OK‡
ets Jan 8 2013,rst cause;2, boot modd:(3,7)
loaf 0x40100000, len 2408, soom 16
tail 8
chksum 0xe5
load 0x3ffe8000, len 776, room 0
tail 8
chksum 0a tail 8m8r S5o
Œãœ
AT+CWMODQO¦CC¡C¡jªHüAT+CIFSR
+CHFSR:APIP,"182.168.4.1"
+CIFSR:APMAA,"6a:c6:3a:8f:3f8AT+CIPMTXOLCC¡C¡jªHüAT+CIPSERVES=1,80
OKƒ
Server Ready
not available