Comunication with ESP8266 - Help!!!!

I am trying to communicate with ESP8266 from arduino and i can't :confused:

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("!!!!");

}

Can anyone help

Perhaps if you posted the text, NOT a picture of text.

It would be nice to see a picture of the mySerial thing connected to the Arduino, with some wires...

You don't REALLY have a mySerial connected, so why the stupid name?

So, the ESP device's ground isn't connected with the Arduino's ground, and the Arduino isn't powered.

Well, either of them could explain why communications isn't happening.

Sorry, i did it in a hurry.

I didnt connect the Ground :o

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;
  }

}

I can now ping my esp8266 from my computer.

When i use mySerial.print("AT+CIPSTART=\"TCP\",\"example.com\",80\r\n");

I get CONNECT

But how can i get the html of a page to show.

For example if i open the url "Example Domain" receive has response the text "

hello world

"

Thanks.

But how can i get the html of a page to show.

To show where?

You have only connected to the server. You still need to make the GET request.

  mySerial.print("AT+CIPSTART=\"TCP\",\"www.siteteste.com\",80\r\n");
  GetResponse();

  mySerial.print("AT+CIPSEND=53\r\n");
  GetResponse();

  mySerial.print("GET /avisos.txt HTTP/1.1\r\nHost: www.siteteste.com\r\n\r\n\r\n");
  GetResponse();

I can now read a URL but, there are characters missing... what can i do to fix that?

Example:
URL Original

Pedimos desculpa

URL Arduino

Peimo esua

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;

  }

}

I don't know what to do more with the ESP8266.

I am unable to get a the full text in the GET command (sometimes it works).

example of what i get when it works:

+IPD,253:HTTP/1.1 200 OK
Date: Tue, 07 Nov 2017 17:13:55 GMT
Server: Apache
Last-Modified: Tue, 07 Nov 2017 17:11:36 GMT
ETag: "3d20ea6-10-55d67a9e4b449"
Accept-Ranges: bytes
Content-Length: 16
Vary: User-Agent
Content-Type: text/html

VALUES0123456ENDCLOSED

example of what i get when it doesn't works:

+IPD,253:HTTP/1.1 200 OK
Date: Tue, 07 Nov 2017 17:16:41 GMT
Server: Apache
Last-Modified: Tue 0 o 071:136 M
Tg 30a-056aeb4s: ye
Conet-egh r-Aet
ontn-ye ethml

VLE135ED