In this recipe, we are
going to demonstrate Basic local machine to machine interactions. To do
that, we will build a simple project that demonstrates machine to machine
interaction between two ESP8266 boards. This will enable you to create simple
networks using your ESP8266 boards, with no Internet connection.
Getting ready
You will need the following hardware components for this project:
- Two ESP8266 boards
- Two USB cables
- 220 Ω resistor
- LED
- Momentary Push button
- 10 kΩ resistor
Connect a Push button and a 10 kΩ pull up resistor to the GPIO2 pin. The Push button will be used as
an input. This ESP8266 board will be configured as the client.
For the second ESP8266, connect an LED to
the GPIO2 pin via a 220 Ω current limiting resistor. This ESP8266 is going to
be configured as the server. The server setup will look like this:
You can make Bussiness queries with by using business queries section. You can find that at the bottom of this page
- You can make queries related to place ads, product placement, and advertising
- make project queries related to DIY project section (college and school students for their projects)
How to do it…
To successfully transfer data between the two ESP8266 boards, set
up one of the boards as a client and the other one as the server. You will
configure the client to send a request to the server. The request will
correspond to the state of the input pin GPIO2 of the ESP8266.
Configure the other ESP8266 as a webserver and use it to create an access point so that the client connects directly to it. This will eliminate the need for an external Wi-Fi hotspot. The ESP8266 that is configured as a server will change the status of the output pin GPIO2 to correspond to the request received from the client.A reply will be sent to the clientstating the current status of the output pin GPIO2 of the ESP8266.
- check here my previous tutorials
- ESP32-CAM Face detection|Face Recognition
- Machine to Machine Interactions
- mesh networking- explained
Here is the sketch for
the ESP8266 server:
1.
Include libraries:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include
<ESP8266WebServer.h>
1.
Set the ssid and password for you access point:
const char* ssid =
"hotspot_ssid";
const char* password =
"hotspot_password";
// Create an instance of the
server
// specify the port to listen
on as an argument
WiFiServer
server(80);
1.
Initialize the serial
communication port:
void setup() {
delay(1000);
Serial.begin(115200);
delay(10);
Set GPIO2 as output:
// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, 0);
Start the access point using the SSID and password that you provided earlier:
Serial.print("Configuring
access point...");
/* You can remove the password
parameter if you want the AP to be open. */
WiFi.softAP(ssid, password);
IPAddress myIP =
WiFi.softAPIP();
Serial.print("AP IP
address: ");
Serial.println(myIP);
server.begin();
Serial.println("HTTP
server started");
}
Check the client has connected to the server and read any incoming data from the client:
void loop() {
// Check if a client has
connected
WiFiClient client =
server.available();
if (!client) {
return;
}
// Wait until the client sends
some data
Serial.println("new
client");
while(!client.available()){
Serial.print('.');
delay(1);
}
// Read the first line of the
request
String req =
client.readStringUntil('\r');
Serial.println(req);
client.flush();
Evaluate the request from the client to
determine what state the GPIO2 pin should be set at:
// Match the request
int val;
if
(req.indexOf("/gpio/0") != -1)
val = 0;
else if
(req.indexOf("/gpio/1") != -1)
val = 1;
else {
Serial.println("invalid
request");
client.stop();
return;
}
Set the GPIO2 pin state to
correspond to the request from the client:
// Set GPIO2 according to the
request
digitalWrite(2, val);
client.flush();
Generate the response that the server will send to the client:
// Prepare the response
String s =
(val)?"high":"low";
Send the response to
the client and end the session:
// Send the response to the
client
client.print(s);
delay(1);
Serial.println("Client
disonnected");
// The client will actually be
disconnected
// when the function returns
and 'client' object is detroyed
}
Upload this sketch to the ESP8266 board
that will be used as a server. Remember to change hotspot_ssid and hotspot_password to your preferred
access point SSID and password respectively, before uploading the code. Open
the serial monitor and note down the IP address of your ESP8266 server. You
will include it in the ESP8266 client sketch.
The sketch for your
ESP8266 client is as follows:
ffInclude library:
#include
<ESP8266WiFi.h>
ffSet SSID and password for your server's access point:
const char* ssid =
"your-ssid";
const char* password =
"your-password";
ffSet the IP address of
your ESP8266 server:
const char* host =
"192.XXX.XXX.XXX";
ffInitialize the serial
communication port, set GPIO2 as an input, and
connect to the Wi-Fi access point:
void setup() {
Serial.begin(115200);
delay(10);
// prepare GPIO2
pinMode(2, INPUT);
// We start by connecting to a
WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting
to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() !=
WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi
connected");
Serial.println("IP
address: ");
Serial.println(WiFi.localIP());
}
- you can follow my video version of tutorials from YouTube too.
ffDelay for 5 seconds
before starting the loop and then read the
status of the GPIO2 pin:
void loop() {
delay(5000);
int val = digitalRead(2);
ffConnect to the server:
Serial.print("connecting
to ");
Serial.println(host);
// Use WiFiClient class to
create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host,
httpPort)) {
Serial.println("connection
failed");
return;
}
ffGenerate the URL for our GET request and print it on the serial monitor:
// We now create a URI for the
request
String url =
"/gpio/";
url +=
(val)?"1":"0";
Serial.print("Requesting
URL: ");
Serial.println(url);
ffSend the request to the server and check
whether the connection has been timed out:
// This will send the request
to the server
client.print(String("GET
") + url + " HTTP/1.1\r\n" +
"Host: " + host +
"\r\n" +
"Connection:
close\r\n\r\n");
unsigned long timeout =
millis();
while (client.available() == 0)
{
if (millis() - timeout >
5000) {
Serial.println(">>>
Client Timeout !");
client.stop();
return;
}
}
ffRead the reply from
the server and display it on the serial monitor, then close the connection:
// Read all the lines of the
reply from server and print them to Serial
while(client.available()){
String line =
client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing
connection"); }
Upload this sketch to
the ESP8266 board that will be used as a client. Remember to change your_ssid and your_password to the name for the
access point SSID and password respectively, before uploading the code. Also
replace 192.XXX.XXX.XXX with the IP address of
the ESP8266 board that is configured as a server.
Once you have uploaded the code onto both
ESP8266 boards, turn on the setups. You can do that by connecting both ESP8266
boards to your computer via USB cable, or power them via external means
(battery/power adapter). However, it is best to leave the ESP8266 client setup
connected to the computer, so that you can read feedback from the server on the
Arduino serial monitor. You will be able to turn on or off the LED connected to
the ESP8266 server by pressing or releasing the Push button respectively.
How it works…
The ESP8266 server
sketch starts by including the required ESP8266 libraries, after which the
access point SSID and password are defined and the Wi-Fi server object created. In the setup section of the sketch, the program
initializes the serial port and creates a Wi-Fi access point using the provided
SSID and password. The server is initialized after that.
The ESP8266 server
listens to see whether there are any clients that are connecting to it. If they
are there, it reads the request that the client has sent and checks to see if
it is valid. If the request contains the string /gpio/0 and /gpio/1, it is considered valid.
A /gpio/0 request causes the ESP8266 to turn off the LED and send back a
reply to the client that says LOW to signify the state
of GPIO2 pin. A /gpio/1 request causes the
ESP8266 to turn on the LED and send back a reply to the client that says HIGH to signify the state of the GPIO2 pin. Once the reply has been
sent, the server ends the session and waits for another client to connect to
it.
The ESP8266 client
sketch also starts by including the ESP8266 Wi-Fi library and definition of the SSID, password, and host IP address
of the ESP8266 server. The sketch then initializes the serial port and connects
to the ESP8266 access point.
In the loop section of the sketch,
the state of GPIO2 is read and the ESP8266 client connects to the server. If
the state of the GPIO2 pin is high (Push button pressed), the
client sends a /gpio/1 request to the server.
If the state of GPIO2 is low (Push button released), the
client sends a /gpio/0 request to the server.
The ESP8266 then reads the reply from the server. If the data was sent to the
server successfully, the ESP8266 client receives the state of the GPIO2 pin of
the ESP8266 server.
No comments:
Post a Comment