├── AP-Server └── AP-Server.ino ├── Client └── Client.ino ├── README.md └── esp8266-http-p2p_bb.png /AP-Server/AP-Server.ino: -------------------------------------------------------------------------------- 1 | /* HTTP Web Server + Access Point example 2 | 3 | Ranjit Bhinge, 4 | Device Interactions 5 | 6 | www.device-interactions.com 7 | www.github.com/deviceinteractions 8 | 9 | http://blog.device-interactions.com 10 | http://shop.device-interactions.com 11 | 12 | This example sets up an ESP8266 as an HTTP Server and opens a WiFi hotspot 13 | The Client is expected to connect to this hotspot and send messages using HTTP 14 | 15 | The Messages will be visible in the Serial monitor 16 | 17 | */ 18 | 19 | 20 | #include 21 | #include 22 | 23 | IPAddress apIP(192,168,4,1); // Defining a static IP address for the Server 24 | 25 | // This is what the clients will need to connect to: 26 | const char *ssid = "ESP-SERVER"; // you can replace this with any other name 27 | const char *password = "password"; // you can replace this with any other password 28 | String msgBody = ""; 29 | 30 | ESP8266WebServer server(80); // Use the default port 80 for HTTP comms 31 | 32 | // handle requests at the root endpoint (http://xxx.xxx.xxx.xxx/) 33 | void handleRoot() { 34 | Serial.println("Root page accessed by a client!"); 35 | 36 | // Respond with a mini HTML page 37 | server.send ( 200, "text/html", "

Hi There!

"); 38 | } 39 | 40 | void handleMessage(){ 41 | 42 | // Check for valid request with a message (in the expected format) 43 | if(server.hasArg("body")){ 44 | digitalWrite(D5, HIGH); 45 | Serial.println("Message received from Client:"); 46 | msgBody = server.arg("body"); 47 | Serial.println(msgBody); 48 | 49 | // Generate and send back an acknowledgement response 50 | msgBody = "Hi, this is the Server. I got your message saying : " + msgBody; 51 | server.send ( 200, "text/plain", msgBody); 52 | delay(500) 53 | digitalWrite(D5, LOW); 54 | } 55 | // handle messages with invalid bodies 56 | else { 57 | Serial.println("Invalid message received"); 58 | server.send ( 200, "text/plain", "Recevied a message without a body!"); 59 | } 60 | 61 | Serial.println("-------------------------------------"); 62 | } 63 | 64 | // handle invalid requests 65 | void handleNotFound() { 66 | server.send ( 404, "text/plain", "404, No resource found"); 67 | } 68 | 69 | void setup() { 70 | 71 | pinMode(D4, OUTPUT); 72 | pinMode(D5, OUTPUT); 73 | Serial.begin(9600); 74 | Serial.println(); 75 | Serial.println("Configuring access point..."); 76 | 77 | //setup the custom IP address 78 | WiFi.mode(WIFI_AP_STA); 79 | 80 | // COnfigure the Access Point 81 | WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); // subnet FF FF FF 00 82 | 83 | // Start the Access Point 84 | WiFi.softAP(ssid, password); 85 | digitalWrite(D4, LOW); 86 | // Serial messages with Access Point details 87 | IPAddress myIP = WiFi.softAPIP(); 88 | Serial.print("AP IP address: "); 89 | Serial.println(myIP); 90 | 91 | server.on ( "/", handleRoot ); 92 | server.on ( "/message", handleMessage ); 93 | server.onNotFound ( handleNotFound ); 94 | 95 | server.begin(); 96 | Serial.println("HTTP server started"); 97 | } 98 | 99 | void loop() { 100 | // Constantly listen for Client requests 101 | server.handleClient(); 102 | } 103 | -------------------------------------------------------------------------------- /Client/Client.ino: -------------------------------------------------------------------------------- 1 | /* HTTP Client that sends messages over HTTP 2 | 3 | Ranjit Bhinge, 4 | Device Interactions 5 | 6 | www.device-interactions.com 7 | www.github.com/deviceinteractions 8 | 9 | http://blog.device-interactions.com 10 | http://shop.device-interactions.com 11 | 12 | This example sets up an ESP8266 as an HTTP Client and connects to the Server's WiFi hotspot 13 | This Client sends messages using HTTP GET requests and expects an acknowledgement response from the Server 14 | 15 | The Messages and responses will be visible in the Serial monitor 16 | 17 | To send a message, open the Serial Monitor and type it in the top bar and hit 'Send' 18 | 19 | To send long messages with spaces, please use '%20' instead of the space. 20 | eg: 'This%20is%20a%20space' -> 'This is a space' 21 | 22 | */ 23 | 24 | 25 | #include 26 | 27 | #include 28 | 29 | #include 30 | 31 | // This is the Server's WiFi network the client will try to connect to 32 | #define WIFI_SSID "ESP-SERVER" // same as the Server AP WiFi name 33 | #define WIFI_PASS "password" // same as the Server AP WiFi password 34 | 35 | String http_addr = "http://192.168.4.1"; // Server's IP address on the AP network 36 | 37 | // Global variables 38 | String url = ""; 39 | String body = ""; 40 | 41 | void setup() { 42 | pinMode(D4, OUTPUT); 43 | pinMode(D5, OUTPUT); 44 | Serial.begin(9600); // Init Serial comms 45 | 46 | // Clear the Serial buffer 47 | Serial.println(); 48 | Serial.println("Clearing Serial buffer..."); 49 | Serial.flush(); 50 | WiFi.begin(WIFI_SSID, WIFI_PASS); 51 | Serial.println("\nConnecting to the Server's Access Point"); 52 | 53 | // WiFi connection 54 | while (WiFi.status() != WL_CONNECTED) { 55 | digitalWrite(D4, LOW); 56 | delay(500); 57 | Serial.print("."); 58 | } 59 | Serial.println(""); 60 | Serial.print("Connected to "); 61 | Serial.println(WIFI_SSID); 62 | Serial.print("IP address: "); 63 | Serial.println(WiFi.localIP()); 64 | Serial.println(); 65 | Serial.println("Ready to send a message. Type into the Serial Monitor Bar and hit Send"); 66 | digitalWrite(D4, HIGH); 67 | } 68 | 69 | void loop() { 70 | // Auto reconnect on loss of WiFi connection 71 | if(WiFi.status() != WL_CONNECTED){ 72 | while (WiFi.status() != WL_CONNECTED) { 73 | digitalWrite(D4, LOW); 74 | delay(500); 75 | Serial.print("."); 76 | } 77 | Serial.println(""); 78 | Serial.print("Connected to "); 79 | Serial.println(WIFI_SSID); 80 | Serial.print("IP address: "); 81 | Serial.println(WiFi.localIP()); 82 | Serial.println(); 83 | Serial.println("Ready to send a message. Type into the Serial Monitor Bar and hit Send"); 84 | digitalWrite(D4, HIGH); 85 | } 86 | 87 | // wait for Serial input from the User 88 | if(Serial.available()>0){ 89 | body = Serial.readString(); 90 | // Check if the received message is valid 91 | if((body.length()>0)) { 92 | 93 | // Initiate the HTTP call based on the received User Input 94 | HTTPClient http; 95 | url = http_addr + "/message?body=" + body; 96 | Serial.print("[HTTP] starting...\n"); 97 | // configure traged server and url 98 | Serial.println(url); 99 | http.begin(url); 100 | 101 | Serial.print("Sending message - "); 102 | Serial.println(body); 103 | // start connection and send HTTP header 104 | int httpCode = http.GET(); 105 | 106 | // httpCode will be negative on error 107 | if(httpCode > 0) { 108 | // Receive the Server response 109 | if(httpCode == HTTP_CODE_OK) { 110 | digitalWrite(D5, HIGH); 111 | Serial.print("Received Response\n"); 112 | String payload = http.getString(); 113 | Serial.println(payload); 114 | Serial.flush(); 115 | } 116 | } else { 117 | Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); 118 | } 119 | 120 | http.end(); 121 | } 122 | } 123 | 124 | delay(500); 125 | digitalWrite(D5, LOW); 126 | delay(1500); 127 | } 128 | 129 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tutorial for Peer-to-Peer HTTP Communication using ESP8266 modules 2 | Refer to this tutorial : https://blog.device-interactions.com/2018/05/wireless-messaging-with-two-nodemcus.html 3 | 4 | ## Overview 5 | The Communication setup is a Master-Slave configuration where one module is a Server and the other is a Client. 6 | The Server Module generates a WiFi hotspot and allows clients to connect to it. 7 | The Client Module connects to this hotspot and sends messages, based on the User's Serial commands 8 | 9 | To get started, click the Clone/Download button in the top right corner and get a copy of this repository on your PC. 10 | Open the downloaded directory. 11 | 12 | ## The Server Module 13 | To set up the Server, follow these steps: 14 | 1. Open the AP-Server.ino file with Arduino IDE 15 | 2. Plug on your first ESP8266 Module 16 | 3. Select the right port and board (ESP8266 Module) 17 | 4. Open your Serial Monitor, and select 9600 baud rate 18 | 5. Click on 'Upload Sketch' (Ctrl+U) 19 | 6. Once the Sketch is uploaded, wait for the Serial message confirming that the Access Point and HTTP Server have started 20 | 7. Now leave this powered up 21 | 22 | ## The Client Module 23 | To set up the Client Module, follow these steps: 24 | 1. Open the Client.ino file with the Arduino IDE 25 | 2. Plug in the second ESP8266 Module 26 | 3. Select the right board and port (remember that the first ESP8266 module is connected too) 27 | 4. Open the Serial Monitor and select 9600 baud rate 28 | 5. Click on 'Upload Sketch' (Ctrl+U) 29 | 6. Once the sketch is uploaded, wait for the Serial message to confirm that the Client has connected to the Server's Access Point. 30 | 7. Once the connection takes place, it will prompt you for a Serial Command that can be provided from the top bar in the Serial Monitor 31 | 8. Now you can type in any message that you like in the Serial monitor bar, and that message will be sent to the Server over the Hotspot 32 | 9. You will also receive an acknowledgement message back from the Server regarding your message 33 | 34 | ### Breadboard connection 35 | The LED functionalities are implemented on the pins D4 and D5 of both NodeMCUs. 36 | LED at D4 is internally connected on the NodeMCU. 37 | LED at D5 needs to be connected via a 330 Ohm resistor. Refer to the connection diagram image in the Repo. 38 | 39 | ### Try connecting the two modules on two separate PCs, with the two Serial Monitors open. You will see all the magic happening! 40 | -------------------------------------------------------------------------------- /esp8266-http-p2p_bb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deviceinteractions/tutorial-esp8266-http-p2p/f7e72851e31338efc8c1fde7a41eb449176fb942/esp8266-http-p2p_bb.png --------------------------------------------------------------------------------