I am trying to communicate with ESP8266 from arduino and i can't
Can anyone help, please.
This is what i have done:
1 - Firmware update:
2 - Program to test boud rate and communication.
#include <SoftwareSerial.h>
#define sw_serial_rx_pin 2 // Connect this pin to TX on the esp8266
#define sw_serial_tx_pin 3 // Connect this pin to RX on the esp8266
SoftwareSerial mySerial(sw_serial_rx_pin, sw_serial_tx_pin); // RX, TX
// set the data rate for the SoftwareSerial port
int contbound=0;
int maxbound=9;
long mySensVals[10] = {0, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200};
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Test Start!!!");
mySerial.begin(mySensVals[9]);
}
void loop() { // run over and over
contbound = contbound + 1;
if (contbound>maxbound){contbound=1 ;}
if (mySerial.available()) {Serial.print((char)mySerial.read());}
Serial.read();
Serial.println(mySensVals[contbound]);
mySerial.begin(mySensVals[contbound]); delay(2000);
mySerial.print("AT+CIOBAUD="+String(mySensVals[contbound])+",8,1,0,0\r\n");
while (mySerial.available()) {
Serial.print((char)mySerial.read());
}
Serial.println("!!!!");
}
Now i am communicating with it. But i am unable work with it.
if i try to see available wifi or connect to one it will not work.
#include <SoftwareSerial.h>
#define sw_serial_rx_pin 2 // Connect this pin to TX on the esp8266
#define sw_serial_tx_pin 3 // Connect this pin to RX on the esp8266
SoftwareSerial mySerial(sw_serial_rx_pin, sw_serial_tx_pin); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
mySerial.begin(115200);
}
void loop() { // run over and over
Serial.println("Test Start!!!");
mySerial.print("AT\r\n");
while (mySerial.available()) {
Serial.print((char)mySerial.read());
}
delay(2000);
mySerial.print("AT+GMR\r\n");
while (mySerial.available()) {
Serial.print((char)mySerial.read());
}
delay(2000);
Serial.println("Test End!!!");
}
Test Start!!!
AT
j⸮H⸮AT+GMR
AT sersion;1.4.0.0(May 5 2017 16:00:59JC⸮*⸮)⸮⸮⸮ͥon:2.1.0(1)i1
Test End!!!
Test Start!!!
AT
OI
AT+GMR
@T sersion:0.4.0.0(May 5 2017 16:10:59JC⸮*⸮)⸮⸮⸮ͥ⸮⸮⸮r⸮r⸮B⸮⸮⸮⸮
i :Test End!!!
Test Start!!!
AT
⸮
OK
AT+GMR
@T sersion:1/4.0.0(May 4 2817 16:10;59JC⸮*⸮)⸮⸮⸮ͥ⸮⸮⸮r⸮r⸮B⸮⸮⸮⸮J
m0OTest End!!!
Test Start!!!
AT
C⸮j⸮H⸮AT+GMR
AT vY⸮⸮Z⸮K⸮r⸮r⸮r⸮Bj⸮⸮⸮⸮⸮⸮⸮⸮⸮Ҋ⸮Ҫ⸮Je
SDK veqsiog:2.1.0(016b⸮⸮⸮⸮⸮⸮遊⸮⸮
CTest End!!!
mySerial.print("AT\r\n");
while (mySerial.available()) {
Serial.print((char)mySerial.read());
}
How long are you allowing for the serial data to get to the ESP, to be processed, and for the response to get back? Is that really a reasonable length of time?
mySerial.begin(115200);
That is not a reliable speed for software serial.
mySerial.print("AT+GMR\r\n");
while (mySerial.available()) {
Serial.print((char)mySerial.read());
}
Again, how long are you allowing for the serial data to get to the ESP, to be processed, and for the response to get back? Is that really a reasonable length of time?
You are trying to force asynchronous serial communication to be synchronous, but you are sticking your head in the sand at the wrong times.
Create a function to read all incoming serial data, until a \r followed by a \n arrives, but for no longer than some defined amount of time.
Send data. Call that function to read any reply that arrives within the allowed window.
What do you think that AT by itself should do?
What do you think that AT+GMR should do?
I changed the baud rate to 9600 and it is working.
AT+UART_DEF=9600,8,1,0,0
After some search and some minor adaptions this is what i am now using to communicate.
#include <SoftwareSerial.h>
#define sw_serial_rx_pin 2 // Connect this pin to TX on the esp8266
#define sw_serial_tx_pin 3 // Connect this pin to RX on the esp8266
SoftwareSerial mySerial(sw_serial_rx_pin, sw_serial_tx_pin); // RX, TX
// set the data rate for the SoftwareSerial port
int contbound=0;
int maxbound=9;
#define network "SSID"
#define password "PASSWORD"
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
/*mySerial.begin(mySensVals[9]);*/
mySerial.begin(9600);
Serial.println("Test Start!!!");
mySerial.print("AT+RST\r\n");
GetResponse();
GetResponse();
delay (1000);
mySerial.print("AT+CWMODE=3\r\n");
GetResponse();
delay (1000);
mySerial.print("AT+CWLAP\r\n");
GetResponse();
delay (1000);
String comando="AT+CWJAP=\"";
comando+=network;
comando+="\",\"";
comando+=password;
comando+="\"";
comando+="\r\n";
mySerial.print(comando);
GetResponse();
delay (1000);
mySerial.println("AT+CIFSR\r\n");
GetResponse();
Serial.println("Test End!!!");
}
void loop() { // run over and over
}
void GetResponse() {
int limit=25000;
unsigned long timeenter=millis();
boolean keeprunning=true;
String ValStr1="";
unsigned long lastchar=0;
while (keeprunning) {
if (mySerial.available()) {
char chrread = mySerial.read();
lastchar=millis();
Serial.print(chrread);
ValStr1=ValStr1+chrread;
if (chrread==10) { // LF
byte lfloc=ValStr1.indexOf(13);
String ValStr2=ValStr1.substring(0,lfloc);
if (ValStr2=="OK") keeprunning=false;
if (ValStr2=="ERROR") keeprunning=false;
if (ValStr2=="FAIL") keeprunning=false;
if (ValStr2=="ready") keeprunning=false;
if (ValStr2=="no change") keeprunning=false;
ValStr1="";
}
}
if (millis()-timeenter > limit) keeprunning=false;
}
}
Changed baudrate to 4800 and it reads the complete file OK.
Here is my full code to communicate with ESP8266
#include <SoftwareSerial.h>
#define network "SSID"
#define password "PASSWORD"
#define sw_serial_rx_pin 2 // Connect this pin to TX on the esp8266
#define sw_serial_tx_pin 3 // Connect this pin to RX on the esp8266
SoftwareSerial mySerial(sw_serial_rx_pin, sw_serial_tx_pin); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Test Start!!!");
mySerial.begin(4800);
/* Code Change baudspeed */
// mySerial.print("AT+UART_DEF=4800,8,1,0,0\r\n");
// GetResponse();
// mySerial.begin(4800);
/**************************/
mySerial.print("AT+RST\r\n");
GetResponse();
GetResponse();
delay (1000);
mySerial.print("AT+CWMODE=3\r\n");
GetResponse();
mySerial.print("AT+CWLAP\r\n");
GetResponse();
String comando="AT+CWJAP=\"";
comando+=network;
comando+="\",\"";
comando+=password;
comando+="\"";
comando+="\r\n";
mySerial.print(comando);
GetResponse();
mySerial.print("AT+CIFSR\r\n");
GetResponse();
mySerial.print("AT+CIPSTART=\"TCP\",\"www.*******************.com\",80\r\n");
GetResponse();
delay (1000);
mySerial.print("AT+CIPSEND=63\r\n");
GetResponse();
mySerial.print("GET /avisos.txt HTTP/1.1\r\nHost: www.*******************.com\r\n\r\n\r\n");
GetResponse();
Serial.println("Test End!!!");
}
void loop() { // run over and over
}
void GetResponse() {
int limit=10000;
unsigned long timeenter=millis();
boolean keeprunning=true;
String ValStr1="";
unsigned long lastchar=0;
while (keeprunning) {
if (mySerial.available()) {
char chrread = mySerial.read();
lastchar=millis();
/*Serial.print(chrread);*/
ValStr1=ValStr1+chrread;
if (chrread==10) { // LF
Serial.print(ValStr1);
byte lfloc=ValStr1.indexOf(13);
String ValStr2=ValStr1.substring(0,lfloc);
if (ValStr2=="OK") keeprunning=false;
if (ValStr2=="ERROR") keeprunning=false;
if (ValStr2=="FAIL") keeprunning=false;
if (ValStr2=="ready") keeprunning=false;
if (ValStr2=="no change") keeprunning=false;
ValStr1="";
}
}
if (millis()-timeenter > limit) keeprunning=false;
}
}