Updated:
I connected my esp to external power, the blue light stays on, I start getting garbage data. I used voltage regulator which was giving 3.4v, power supply tested with 1A & 2A.
Old problem :
I have been trying to make ESP8266 since my last semester exams & now 2nd semester exams are going on. So i've been trying to make ESP8266 work for very long time. Earlier it was firmware problem, i updated it.
After a lot it worked. I tried many libraries - but none worked, either some are using specific port or some want specific firmware version which is really hard to find & then flash.
I am using telnet to check.
When i disable code to send data - i can send data madly, it works & never hangs up. But as i use AT+CPISEND
after 1-2 read/write - it stops responding.
I am powering using arduino with/out voltage divider. Its firmware - 0020000903
, AT Firmware v1.5
, 4mb flash size, I changed baudrate to 9600
.
Below is code i worte : ( I intentionally didn't remove comments)
#include <SoftwareSerial.
#define DEBUG true
SoftwareSerial esp8266(9,10); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
// This means that you need to connect the TX line from the esp to the Arduino's pin 2
// and the RX line from the esp to the Arduino's pin 3
void setup()
{
pinMode(11,OUTPUT);
digitalWrite(11,LOW);
pinMode(12,OUTPUT);
digitalWrite(12,LOW);
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
Serial.begin(9600);
esp8266.begin(9600); // your esp's baud rate might be different
Serial.print("Sending at..");
sendData("AT",2000,DEBUG);
//sendData("AT+RST",2000,DEBUG); // reset module
sendData("AT+CWMODE=1",1000,DEBUG);
// sendData("AT+CWLAP",1000,DEBUG);
//sendData("AT+CIOBAUD=9600",1000,DEBUG);
//sendData("AT+UART=9600,8,1,0,0",1000,DEBUG);
//sendData("AT+IPR=9600",1000,DEBUG);
// sendData("AT+CWJAP_DEF=\"mad\",\"agentis47?orBond?\"",15000,DEBUG);
//sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
sendData("AT+CIFSR",1000,DEBUG); // get ip address
sendData("AT+CIPMUX=1",1000,DEBUG); // configure for multiple connections
sendData("AT+CIPSERVER=1,80",1000,DEBUG); // turn on server on port 80
}
void loop()
{
if(esp8266.available()) // check if the esp is sending a message
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
Serial.write(c);
}
//Serial.println("IPD Search "+(len));
if(esp8266.find("+IPD,"))
{
delay(20); // 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
esp8266.read(); //skip the comma ','
//(CIPMUX=0): + IPD, <len>:
//(CIPMUX=1): + IPD, <id>, <len>: <data>
int len = (readUntil(":")).toInt();
if(len>500) len = 500; //prevent extra data we won't be sending more than 500bytes in one go.
char data[len];
esp8266.readBytes(data, len);
//data[len-2]='\0';
int pinNumber = (data[0]-48)*10; // get first number i.e. if the pin 13 then the 1st number is 1, then multiply to get 10
pinNumber += ((data[1])-48); // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first number
digitalWrite(pinNumber, !digitalRead(pinNumber));
Serial.println(String(len));
/*Serial.println("===");
Serial.println("--");
*/
//cleanBuffer();
//delay(10);
//Serial.println("Sendddiddding data");
//sendData(connectionId,String("OK"));
//Serial.println("Sent");
//closeConnection(connectionId);
//digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin
// make close command
//String closeCommand = "AT+CIPCLOSE=";
//closeCommand+=connectionId; // append connection id
//closeCommand+="\r\n";
//sendData(closeCommand,1000,DEBUG); // close connection
}
}
}
void cleanBuffer(){
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
Serial.write(c);
}
}
String sendData(int connectionId, String data){
//Serial.println("AT+CIPSEND="+String(connectionId)+ ","+String(data.length()));
//sendData("AT+CIPSEND="+String(connectionId)+ ","+String(data.length()),1000,DEBUG);
//sendData(data,100,DEBUG);
//closeConnection(connectionId);
String webpage = "<h1>Hello</h1><h2>World!</h2><button>LED1</button>";
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=webpage.length();
cipSend +="\r\n";
sendData(cipSend,1000,DEBUG);
sendData(webpage,1000,DEBUG);
webpage="<button>LED2</button>";
cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=webpage.length();
sendData(cipSend,1000,DEBUG);
sendData(webpage,1000,DEBUG);
String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId; // append connection id
sendData(closeCommand,3000,DEBUG);
/*
Serial.println("finding prompt");
if(esp8266.find(">")) //prompt offered by esp8266
{
Serial.println("Sending data");
// Serial.println("found > prompt - issuing GET request"); //a debug message
esp8266.println(data); //this is our http GET request
}
else
{
closeConnection(connectionId);
Serial.println("No '>' prompt received after AT+CPISEND");
}
*/
}
String closeConnection(int connectionId){
sendData("AT+CIPCLOSE="+String(connectionId),100,DEBUG);
}
String readUntil(String delimiter){
String response = "";
char c;
unsigned int dLen = delimiter.length(),n=0;
while(esp8266.available()&&(c= esp8266.read())){
n= (c==delimiter[n])?n+1:0;
if(n==dLen) break;
response+=c;
}
return response;
}
void sendRaw(String data){
esp8266.print(data);
}
String sendCommand(String command){
esp8266.print(command); // send the read character to the esp8266
esp8266.print("\r\n");
esp8266.flush();
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command); // send the read character to the esp8266
esp8266.print("\r\n");
esp8266.flush();
long int time = millis();
do
{
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;
}
}while( (time+timeout) > millis());
if(debug)
{
Serial.print(response);
}
return response;
}
Please help, I am stuck, Is there any other cheaper & better solution ? I am only looking for Wifi.