Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15374

Wireless esp32 mesh networking

$
0
0

Hi I have worked on this code and took assistance by using painless mesh library and webserial i uploaded a code its working perfectly when i open the serial monitor but when i use web browser the ip address is not accessible although the IP address is correct i used to power esp exernally and wanna see the serial output on any one of the esp it doesnot receive message from other esp which are powered externally.My endpoint is to establish a wireless mesh communicaton between esps for drone swarm

#include <WiFi.h>
#include <painlessMesh.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <WebSerial.h>

// WiFi credentials
const char* ssid = "FLASH FIBER 202";         // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "ECHO321654"; // The password of the Wi-Fi network

// Mesh network settings
#define MESH_PREFIX "mesh"
#define MESH_PASSWORD "password"
#define MESH_PORT 5555

// Set a unique name for each ESP32 node
const char* nodeName = "ESP32_Node_6"; // Change this for each node

painlessMesh mesh;
AsyncWebServer server(80); // Initialize AsyncWebServer on port 80

String receivedData; // Store serial data received
String messages; // Store messages sent by ESP32 nodes

// Define mesh event callback function
void receivedCallback(uint32_t from, String &msg) {
    Serial.printf("Received from node %u: %s\n", from, msg.c_str());
    receivedData = "Received from node " + String(from) + ": " + msg;
}

void newConnectionCallback(uint32_t nodeId) {
    Serial.printf("New Connection, nodeId = %u\n", nodeId);
}

void changedConnectionCallback() {
    Serial.printf("Changed connections\n");
}

// Function to send a message to all nodes in the mesh network
void sendMessage(String message) {
    mesh.sendBroadcast(String(nodeName) + ": " + message);
    messages += String(nodeName) + ": " + message + "<br>";
}

void handleRootRequest(AsyncWebServerRequest *request) {
    String response = "<html><head><title>ESP32 Mesh</title></head><body>";
    response += "<h1>Received Data</h1>";
    response += "<p>" + receivedData + "</p>";
    response += "<h1>Messages</h1>";
    response += "<p>" + messages + "</p>";
    response += "</body></html>";
    request->send(200, "text/html", response);
}

void recvMsg(uint8_t *data, size_t len){
  WebSerial.println("Received Data...");
  String d = "";
  for(int i=0; i < len; i++){
    d += char(data[i]);
  }
  WebSerial.println(d);
}

void setup() {
    Serial.begin(115200);
  
    // Connect to WiFi network
    WiFi.begin(ssid, password);
    Serial.print("Connecting to ");
    Serial.print(ssid);
    Serial.println(" ...");

    int i = 0;
    while (WiFi.status() != WL_CONNECTED && i < 30) { // Wait for the Wi-Fi to connect or timeout after 30 seconds
        delay(1000);
        Serial.print(++i);
        Serial.print(' ');
    }

    if (WiFi.status() == WL_CONNECTED) {
        Serial.println("\nConnection established!");
        Serial.print("IP address: ");
        Serial.println(WiFi.localIP()); // Send the IP address of the ESP32 to the computer
    } else {
        Serial.println("\nConnection failed. Check your SSID and password.");
        return;
    }

    // Initialize mesh network
    mesh.setDebugMsgTypes(ERROR | STARTUP);
    mesh.init(MESH_PREFIX, MESH_PASSWORD, MESH_PORT);
    mesh.onReceive(&receivedCallback);
    mesh.onNewConnection(&newConnectionCallback);
    mesh.onChangedConnections(&changedConnectionCallback);

    // Set up HTTP route to handle serial data and messages
    server.on("/", HTTP_GET, handleRootRequest);

    // Start web server
    server.begin();

    // WebSerial is accessible at "<IP Address>/webserial" in browser
    WebSerial.begin(&server);
    WebSerial.msgCallback(recvMsg);

    Serial.println("Mesh network initialized!");
}

void loop() {
    mesh.update();

    // Example: Send a message every 5 seconds
    static unsigned long lastMessageTime = 0;
    unsigned long currentTime = millis();
    if (currentTime - lastMessageTime > 5000) {
        sendMessage("Hello from SIXTH!");
        lastMessageTime = currentTime;
    }
}

3 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 15374

Trending Articles