Programming ESP8266 as Client

Hello to everyone, i have an a question and I Didn´t Find a documentation about that

I'm programing a connected ESP8266 into my Arduino Uno. when i uses the GET method i can't read the response. here is my code

    #include <SoftwareSerial.h>
     
    #define DEBUG true
     
    SoftwareSerial esp8266(7,6); // make RX Arduino line is pin 7, make TX Arduino line is pin 6.
                                 // This means that you need to connect the TX line from the esp to the Arduino's pin 2
    int senderdata=0;    
    String currentLine="";// and the RX line from the esp to the Arduino's pin 3
    void setup()
    {
      Serial.begin(9600);
      esp8266.begin(9600); // your esp's baud rate might be different
      
      pinMode(11,OUTPUT);
      digitalWrite(11,LOW);
        pinMode(10,OUTPUT);
      digitalWrite(10,LOW);
    pinMode(12,OUTPUT);
      digitalWrite(12,LOW);
       
      sendData("AT+RST\r\n",2000,DEBUG); // reset module
  sendData("AT+CWMODE=3\r\n",1000,DEBUG);
  sendData("AT+CWJAP=\"D-link\",\"%123456789\"\r\n",1000,DEBUG);// configure as client
  delay(10000);
      sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
     
    }
     
    void loop()
    {
  
      //sendData("POST /index.aspx HTTP/1.1\r\nHost: 192.168.0.14\r\nContent-Type: text/html\r\nCon­tent-Length: 20\r\n\r\nToken=1234\r\n\r­\n",1000,DEBUG);
      
      if  (senderdata==0){
      
        sendData("AT+CIPSTART=\"TCP\",\"192.168.0.14\",80 \r\n",1000,DEBUG);
      delay(1000);
        sendData("AT+CIPSEND=63\r\n",1000,DEBUG);
      delay(500);
      sendData("GET /index.aspx?token=1234 HTTP/1.1\r\nHost: 192.168.0.14\r\n\r\n\r\n\r\n",1000,DEBUG);
      //sendData("\r\n",1000,DEBUG);
      delay(2000);
     senderdata=1;
     
      esp8266.find("pin=");
      int pinNumber= esp8266.read();
      Serial.println(pinNumber);
      digitalWrite(pinNumber,HIGH);
      }
    }  
    /*
    * 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 = "";
        
        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;
    }

Finally i cant turn on my led :frowning:

Response of get method is:

SEND OK

+IPD,638:HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sun, 09 Aug 2015 23:30:32 GMT
Content-Length: 417



<!DOCTYPE html>


<body>
    <form method="get" action="index.aspx?token=1234" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="agBjWUpqN0n3l9O+RShzWRr0gMzlaFT0femxE3d6vjDsyPy2n1qlzMzwYw/Cfb+1lx1tzeVOC2zqiXk+e5FkxbRfFGiZWBX/A2SJsKMRlMo=" />

<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="90059987" />
       pin=200</form>
</body>
</html>

OK

The sendData print a string response whenever you send data to the ESP8266.
Do you get a response when you send Commands (ESP8266 configurations) other than send to Wifi (POST)?

How do you know the problem isn't on the server to where you are sending the POST?

giova014:
The sendData print a string response whenever you send data to the ESP8266.
Do you get a response when you send Commands (ESP8266 configurations) other than send to Wifi (POST)?

How do you know the problem isn't on the server to where you are sending the POST?

tnx a lot in the server i only have this simple code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="peuwbA.index" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server" method="get">
       <% string diispositivo;
        diispositivo=Request.QueryString["Token"];
        if (diispositivo=="1234")
        {
            Response.Write("pin=200");
        } %>         

    </form>
</body>
</html>

I think the problem isn't in server

And the other question?

The sendData print a string response whenever you send data to the ESP8266.
Do you get a response when you send Commands (ESP8266 configurations) other than send to Wifi (POST)?

The response I'm saying is in the Serial Monitor of Arduino.

Edit: Now I see the GET method you posted in the first post.

What you need is to "filter" the pin=200 from GET?

giova014:
What you need is to "filter" the pin=200 from GET?

yes i need filter the pin from the GET method

What do you actually get in pinNumber in the line:

int pinNumber= esp8266.read();

?

What does the esp8266.read() returns? Char, int, array, etc?

giova014:
What do you actually get in pinNumber in the line:

int pinNumber= esp8266.read();

?

What does the esp8266.read() returns? Char, int, array, etc?

server normally responses 12, or 11, or 10 or 200 for a null
that is the pin number to turn on a led. its an int

in this moment esp8266.read() is returning a null value, esp8266 its a name of software serial

#7 below:

http://forum.arduino.cc/index.php/topic,148850.0.html

Tnx, But any idea to solve my trouble?

The lines below:

      esp8266.find("pin=");
      int pinNumber= esp8266.read();

Check if the find() returns true or false (the "pin=" is really found?)
The read() returns a byte and not a int, the numbers 12,11,10 are represented with 2 chars and 200 with 3. Can you make 200 be like 20 (2 chars)? So the length will be fixed.

So you have to read() two times after find() and convert from char to int.

giova014:
The lines below:

      esp8266.find("pin=");

int pinNumber= esp8266.read();




Check if the *find()* returns true or false (the "pin=" is really found?)
The *read()* returns a **byte** and not a **int**, the numbers 12,11,10 are represented with 2 chars and 200 with 3. Can you make 200 be like 20 (2 chars)? So the length will be fixed.

So you have to read() two times after find() and convert from char to int.

I've inserted the following line to try:

      Serial.println(esp8266.find("pin="));
      Serial.println(esp8266.read());

but ever the response is false (0) and the read is -1

I really need read Pin this is the response of the get command

SEND OK

+IPD,638:HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sun, 09 Aug 2015 23:30:32 GMT
Content-Length: 417



<!DOCTYPE html>


<body>
    <form method="get" action="index.aspx?token=1234" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="agBjWUpqN0n3l9O+RShzWRr0gMzlaFT0femxE3d6vjDsyPy2n1qlzMzwYw/Cfb+1lx1tzeVOC2zqiXk+e5FkxbRfFGiZWBX/A2SJsKMRlMo=" />

<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="90059987" />
       pin=200</form>
</body>
</html>

OK
0
-1

finally i try to use esp8266.available() to know if Esp8266 have data to read but the answer is 0

Take out the code below from sendData() :

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

This is reading the serial buffer, so when you try to read it outside the dataSend(), there will be nothing to read.

And more:
By default, the SoftwareSerial buffer have 64 bytes, which is 64 chars. The response from GET have much more than 64 chars.

The message below have 662 chars:

SEND OK

+IPD,638:HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sun, 09 Aug 2015 23:30:32 GMT
Content-Length: 417



<!DOCTYPE html>


<body>
    <form method="get" action="index.aspx?token=1234" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="agBjWUpqN0n3l9O+RShzWRr0gMzlaFT0femxE3d6vjDsyPy2n1qlzMzwYw/Cfb+1lx1tzeVOC2zqiXk+e5FkxbRfFGiZWBX/A2SJsKMRlMo=" />

<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="90059987" />
       pin=200</form>
</body>
</html>

OK

Which easily overflow the SoftwareSerial buffer!

So, it's better to check the "pin=" while receiving byte to byte just after the

esp8266.print(command);

Inside the dataSend()

Or you can store it in an array and check later.