Two ESP8266 talk each other
Posted: Mon Feb 26, 2018 6:42 pm
Hi,
I would like to communicate from an esp8266 to an other esp8266, without a Wifi existing hotspot. As it has to be autonomous, one have to be the AP and the server. The other esp have to connect to the AP and send data. The question is, how to send that data? I just need to send one value and process it with the server. I have tried with esp8266WebServer but without succeed. Could you please help me. Sorry for my perfectible english. Thank for your help.
Here is what I've tried at server's side: (if you prefer, you can find file in attachment)
And the client's side:
I would like to communicate from an esp8266 to an other esp8266, without a Wifi existing hotspot. As it has to be autonomous, one have to be the AP and the server. The other esp have to connect to the AP and send data. The question is, how to send that data? I just need to send one value and process it with the server. I have tried with esp8266WebServer but without succeed. Could you please help me. Sorry for my perfectible english. Thank for your help.
Here is what I've tried at server's side: (if you prefer, you can find file in attachment)
Code: Select all
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char* ssid = "ESP8266_TEST";
const char* password = "354BZGG_TEST";
ESP8266WebServer server(80);
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_AP);
setupAccessPoint();
delay(1000);
}
// Handling the / root web page from my server
void handle_index() {
server.send(200, "text/plain", "Get the f**k out from my server!");
}
// Handling the /feed page from my server
void handle_feed() {
//Récupération de la valeur de la variable temp
String i = server.arg("i");
Serial.print("i= ");
Serial.println(i);
}
void setupAccessPoint() {
Serial.println("** SETUP ACCESS POINT **");
Serial.println("- disconnect from any other modes");
WiFi.disconnect();
Serial.println("- start ap with SID: " + String(ssid));
WiFi.softAP(ssid, password, 9 , 0);
IPAddress myIP = WiFi.softAPIP();
Serial.print("- AP IP address is :");
Serial.println(myIP);
setupServer();
}
void setupServer() {
Serial.println("** SETUP SERVER **");
Serial.println("- starting server :");
server.on("/", handle_index);
server.on("/feed", handle_feed);
server.begin();
Serial.println("- server started");
};
void loop() {
server.handleClient();
}
And the client's side:
Code: Select all
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
// AP Wi-Fi credentials
const char* ssid = "ESP8266_TEST";
const char* password = "354BZGG_TEST";
// Local ESP web-server address
String serverHost = "http://192.168.4.1/feed";
String data;
int sleepInterval = 5;
// DEEP_SLEEP Timeout interval when connecting to AP fails
int failConnectRetryInterval = 2;
int counter = 0;
int i=0;
// Static network configuration
IPAddress ip(192, 168, 4, 4);
IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
WiFiClient client;
void setup() {
ESP.eraseConfig();
WiFi.persistent(false);
Serial.begin(115200);
Serial.println();
Serial.println("**************************");
Serial.println("**************************");
Serial.println("******** BEGIN ***********");
Serial.println("- set ESP STA mode");
WiFi.mode(WIFI_STA);
Serial.println("- connecting to wifi");
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
if(counter > 20){
Serial.println("- can't connect, going to sleep");
hibernate(failConnectRetryInterval);
}
delay(500);
Serial.print(".");
counter++;
}
Serial.println("- wifi connected");
}
void sendHttpRequest() {
HTTPClient http;
http.begin(serverHost);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.POST(data);
http.writeToStream(&Serial);
http.end();
}
void buildDataStream() {
data = "i=";
data += String(i);
Serial.println("- data stream: "+data);
}
void hibernate(int pInterval) {
WiFi.disconnect();
ESP.deepSleep(100 * 600000 * pInterval, WAKE_RFCAL);
delay(100);
}
void loop() {
Serial.println("- build DATA stream string");
buildDataStream();
i++;
Serial.println("- send GET request");
sendHttpRequest();
Serial.println();
}