-->
Page 1 of 1

How do i get data from ESP8266?

PostPosted: Thu Jul 14, 2016 9:20 pm
by hugues
Hi there,

I try to send date from esp8266 to another esp8266..

but i couldn't get date, Please help me thx

in the sent side

void Esp8266ToEsp8266()
{
String IPSAT="IP ADDRESS";
String on="13";
String cmd_on = "AT+CIPSTART=\"TCP\",\"";
cmd_on += IPSAT; //host
cmd_on += "\",80";
esp8266.println(cmd_on);

if(esp8266.find("Error")){
Serial.println("AT+CIPSTART error");
return;
}



// prepare GET string
// String getStr_no = "GET /"+on;
String getStr_no = "GET /"+file1+"?pin=" + on ;
//getStr += String(t);
Serial.println(getStr_no);

getStr_no +=" HTTP/1.1\r\nHost:"+IPSAT;
getStr_no += "\r\n\r\n";

// send data length
cmd_on = "AT+CIPSEND=";
cmd_on += String(getStr_no.length());
esp8266.println(cmd_on);

if(esp8266.find(">")){
esp8266.print(getStr_no);
}
else{
esp8266.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");
}
delay(15000);
}


int the receive side , i use example from the internet

#include <SoftwareSerial.h>

#define DEBUG true

SoftwareSerial esp8266(8,9); // 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()
{
Serial.begin(9600);
esp8266.begin(115200); // your esp's baud rate might be different


pinMode(13,OUTPUT);
digitalWrite(13,LOW);

sendData("AT+RST\r\n",2000,DEBUG); // reset module
sendData("AT+CWMODE=3\r\n",1000,DEBUG); // configure as access point
sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
}

void loop()
{
if(esp8266.available()) // check if the esp is sending a message
{


if(esp8266.find("GET,"))
{
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

esp8266.find("pin="); // advance cursor to "pin="

int pinNumber = (esp8266.read()-48)*10; // get first number i.e. if the pin 13 then the 1st number is 1, then multiply to get 10
pinNumber += (esp8266.read()-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)); // toggle pin

// make close command
String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId; // append connection id
closeCommand+="\r\n";

sendData(closeCommand,1000,DEBUG); // close connection
}
}
}

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