Professional Documents
Culture Documents
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/
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