You are on page 1of 7

ESP to ESP Communication | Circuits4you.com https://circuits4you.

com/2018/01/01/esp-to-esp-communication/

1 /*
2 Wireless Serial using UDP ESP8266
3 Hardware: NodeMCU

1 of 7 3/12/2019, 9:07 PM
ESP to ESP Communication | Circuits4you.com https://circuits4you.com/2018/01/01/esp-to-esp-communication/

4 Circuits4you.com
5 2018
6 Master Board creates Access Point
7 */
8 #include <ESP8266WiFi.h>
9 #include <WiFiUdp.h>
10
11 const char *ssid = "circuits4you";
12 const char *pass = "password";
13
14 unsigned int localPort = 2000; // local port to listen for UDP packets
15
16 IPAddress ServerIP(192,168,4,1);
17 IPAddress ClientIP(192,168,4,2);
18
19 // A UDP instance to let us send and receive packets over UDP
20 WiFiUDP udp;
21
22 char packetBuffer[9]; //Where we get the UDP data
23 //=======================================================================
24 // Setup
25 //=======================================================================
26 void setup()
27 {
28 Serial.begin(9600);
29 Serial.println();
30 WiFi.softAP(ssid, pass); //Create Access point
31
32 //Start UDP
33 Serial.println("Starting UDP");
34 udp.begin(localPort);
35 Serial.print("Local port: ");
36 Serial.println(udp.localPort());
37 }
38 //======================================================================
39 // MAIN LOOP
40 //======================================================================
41 void loop()
42 {
43 int cb = udp.parsePacket();
44 if (!cb)
45 {
46 //If serial data is recived send it to UDP
47 if(Serial.available()>0)
48 {
49 udp.beginPacket(ClientIP, 2000);
50 //Send UDP requests are to port 2000

1 /*
2 Wireless Serial using UDP ESP8266
3 Hardware: NodeMCU
4 Circuits4you.com
5 2018
6 Slave Board connects to Access Point
7 */
8 #include <ESP8266WiFi.h>
9 #include <WiFiUdp.h>
10

2 of 7 3/12/2019, 9:07 PM
ESP to ESP Communication | Circuits4you.com https://circuits4you.com/2018/01/01/esp-to-esp-communication/

11 const char *ssid = "circuits4you";


12 const char *pass = "password";
13
14 unsigned int localPort = 2000; // local port to listen for UDP packets
15
16 IPAddress ServerIP(192,168,4,1);
17 IPAddress ClientIP(192,168,4,2);
18
19 // A UDP instance to let us send and receive packets over UDP
20 WiFiUDP udp;
21
22 char packetBuffer[9]; //Where we get the UDP data
23 //======================================================================
24 // Setup
25 //======================================================================
26 void setup()
27 {
28 Serial.begin(9600);
29 Serial.println();
30
31 WiFi.begin(ssid, pass); //Connect to access point
32
33 Serial.println("");
34
35 // Wait for connection
36 while (WiFi.status() != WL_CONNECTED) {
37 delay(500);
38 Serial.print(".");
39 }
40 Serial.println("");
41 Serial.print("Connected to ");
42 Serial.println(ssid);
43 Serial.print("IP address: ");
44 Serial.println(WiFi.localIP());
45
46 //Start UDP
47 Serial.println("Starting UDP");
48 udp.begin(localPort);
49 Serial.print("Local port: ");
50 Serial.println(udp.localPort());
51 }
52 //======================================================================
53 // MAIN LOOP
54 //======================================================================
55 void loop()
56 {
57 int cb = udp.parsePacket();
58 if (!cb)
59 {
60 //If serial data is recived send it to UDP
61 if(Serial.available()>0)
62 {
63 udp.beginPacket(ServerIP, 2000); //Send Data to Master unit
64 //Send UDP requests are to port 2000
65
66 char a[1];
67 a[0]=char(Serial.read()); //Serial Byte Read
68 udp.write(a,1); //Send one byte to ESP8266
69 udp.endPacket();
70 }
71 }
72 else {
73 // We've received a UDP packet, send it to serial
74 udp.read(packetBuffer, 1); // read the packet into the buffer, we are reading only one

3 of 7 3/12/2019, 9:07 PM
ESP to ESP Communication | Circuits4you.com https://circuits4you.com/2018/01/01/esp-to-esp-communication/

75 Serial.print(packetBuffer);
76 delay(20);
77 }
78 }
79 //=======================================================================

4 of 7 3/12/2019, 9:07 PM
ESP to ESP Communication | Circuits4you.com https://circuits4you.com/2018/01/01/esp-to-esp-communication/

1 /*
2 Wireless Serial using UDP ESP8266
3 Hardware: NodeMCU
4 Circuits4you.com
5 2018
6 UDP Broadcast multi esp to esp communication
7 */
8 #include <ESP8266WiFi.h>
9 #include <WiFiUdp.h>
10
11 const char *ssid = "circuits4you.com";
12 const char *pass = "password";
13
14 unsigned int localPort = 2000; // local port to listen for UDP packets
15
16 IPAddress SendIP(192,168,43,255); //UDP Broadcast IP data sent to all devicess on same networ
17
18 // A UDP instance to let us send and receive packets over UDP

5 of 7 3/12/2019, 9:07 PM
ESP to ESP Communication | Circuits4you.com https://circuits4you.com/2018/01/01/esp-to-esp-communication/

19 WiFiUDP udp;
20
21 char packetBuffer[9]; //Where we get the UDP data
22 //======================================================================
23 // Setup
24 //=======================================================================
25 void setup()
26 {
27 Serial.begin(9600);
28 Serial.println();
29
30 WiFi.begin(ssid, pass); //Connect to access point
31
32 Serial.println("");
33
34 // Wait for connection
35 while (WiFi.status() != WL_CONNECTED) {
36 delay(500);
37 Serial.print(".");
38 }
39 Serial.println("");
40 Serial.print("Connected to ");
41 Serial.println(ssid);
42 Serial.print("IP address: ");
43 Serial.println(WiFi.localIP());
44
45 //Start UDP
46 Serial.println("Starting UDP");
47 udp.begin(localPort);
48 Serial.print("Local port: ");
49 Serial.println(udp.localPort());
50 }
51 //======================================================================
52 // MAIN LOOP
53 //======================================================================
54 void loop()
55 {
56 int cb = udp.parsePacket();
57 if (!cb)
58 {
59 //If serial data is recived send it to UDP
60 if(Serial.available()>0)
61 {
62 udp.beginPacket(SendIP, 2000); //Send Data to Master unit
63 //Send UDP requests are to port 2000
64
65 char a[1];

6 of 7 3/12/2019, 9:07 PM
ESP to ESP Communication | Circuits4you.com https://circuits4you.com/2018/01/01/esp-to-esp-communication/

7 of 7 3/12/2019, 9:07 PM

You might also like