0% found this document useful (0 votes)
10 views2 pages

ESP Code

The document is an Arduino sketch for an ESP8266 that connects to a WiFi network and sets up a WebSocket server. It reads messages from an Arduino Mega, extracts slot status information, and sends HTTP POST requests to a backend server to update the status of the slots. The code includes functions for handling WebSocket events and sending HTTP requests based on the received messages.

Uploaded by

Blue Lions
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

ESP Code

The document is an Arduino sketch for an ESP8266 that connects to a WiFi network and sets up a WebSocket server. It reads messages from an Arduino Mega, extracts slot status information, and sends HTTP POST requests to a backend server to update the status of the slots. The code includes functions for handling WebSocket events and sending HTTP requests based on the received messages.

Uploaded by

Blue Lions
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <ESP8266WiFi.

h>
#include <WebSocketsServer.h>
#include <ESP8266HTTPClient.h> // Include the HTTP client for sending requests

const char* ssid = "GlobeAtHome_67589";


const char* password = "32G4AFN7D2A";

WebSocketsServer webSocket(81); // WebSocket on port 81

void setup() {
Serial.begin(9600); // This is for communication with Arduino Mega (TX/RX)

WiFi.begin(ssid, password);

Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("\nWiFi connected.");
Serial.print("IP: ");
Serial.println(WiFi.localIP());

webSocket.begin();
webSocket.onEvent(webSocketEvent);
}

void loop() {
webSocket.loop();

if (Serial.available()) { // Reading from Serial (NodeMCU's TX/RX pins connected


to Mega)
String message = Serial.readString(); // Read the serial message from Arduino
Mega
message.trim(); // Clean up the message

// Log the received message for debugging


Serial.print("Received from Arduino Mega: ");
Serial.println(message);

// Check if the message is in the correct format (SLOT <slot_number> STATUS


<1/0>)
if (message.startsWith("SLOT") && message.indexOf("STATUS") != -1) {
// Extract the slot number and status
int slot = message.substring(5, message.indexOf("STATUS") - 1).toInt();
String status = message.substring(message.indexOf("STATUS") + 7);

// Log the extracted slot and status


Serial.print("Extracted Slot: ");
Serial.println(slot);
Serial.print("Extracted Status: ");
Serial.println(status);

if (status == "1" || status == "0") {


sendPostRequest(slot, status); // Send the status to the backend
} else {
Serial.println("Invalid status received.");
}
}
}
}

void webSocketEvent(uint8_t client_num, WStype_t type, uint8_t * payload, size_t


length) {
switch(type) {
case WStype_TEXT:
String message = (char*)payload; // Convert the payload to a string
message += "\n"; // Add a newline at the end to indicate end of message
Serial.println(message); // Send the whole message with a newline
break;
}
}

void sendPostRequest(int slot, String message) {


WiFiClient client;
const char* host = "192.168.254.101"; // Replace with your server's IP or domain
const int port = 80; // Default HTTP port

if (client.connect(host, port)) {
// Update status based on message from NodeMCU
String status = (message == "1") ? "occupied" : "available"; // 1 -> occupied,
0 -> available
String postData = "slot_number=" + String(slot) + "&status=" + message;
// Send slot and status

// Send HTTP POST request to the new PHP file


client.print("POST /system/update-slot-nodemcu.php HTTP/1.1\r\n");
client.print("Host: 192.168.254.100\r\n"); // Replace with your server's IP or
domain
client.print("Content-Type: application/x-www-form-urlencoded\r\n");
client.print("Content-Length: " + String(postData.length()) + "\r\n");
client.print("\r\n");
client.print(postData); // Send the POST data
Serial.println("Status: " + status);
Serial.println("POST Data: " + postData);

delay(500); // Wait for the server response


client.stop(); // Close the connection
Serial.println("Status sent to backend");
} else {
Serial.println("Connection failed to backend");
}
}

You might also like