Mesh network enables devices in your network to have faster speeds, greater coverage, and a more reliable connection.  The units (called “nodes”) will capture and rebroadcast the router’s signal. If nodes are removed from the network, it should self-heal, and route around the damage. The result is an efficient wireless network that provides a strong signal no matter where you are. 

I started with this project by configuring a serial communication with just two ESP32 micro-controllers using Arduino IDE and "painlessmesh" library. The two devices sent hello packets to each other every 5 seconds. The packets were analysed on the serial monitor. 

The next ting I was able to achieve was serial communication between five ESP32 thus forming a mesh network. I used Tera term to capture and analyse the packets. 

This mesh network is then connect to the home Wi-Fi in order to have the network connect to the Internet. During this milestone I faced a couple of challenges. For this to work you need a Wi-Fi router. In my case, I was using the xfinity hotspot which caused problems. Hence, I decided to create an external webpage server. 

The nodes (ESP32s) are configured with an IP address such as 192.168.4.1. When a mobile device is connected to the mesh network and when it enters the configured IP address, a webpage opens with a "message received" header. 

I wanted to figure out a way on how IoT devices would communicate with each other without the interference of a routing device. The devices should be able to form its own lookup table and route data between nodes. I tried a couple of protocols such as ESP-NOW and ESP- MQTT. I also looked at ways on how routers and switches work and I came across ARP (address resolution protocol) lookup table. 

I did code an ARP table in python. Whenever a router receives an ARP request, it makes an entry in its ARP table, assigning  local IP address of the client with its associated MAC address. I was not able to execute this in Arduino.

Looking at how google mesh network actually works, I realized that I need to configure multiple access points in order to have a robust Wi-Fi connection. With the bits and pieces of code I had done earlier, I was able to configure ESP32 as an AP as well as be connected to other nearby mesh nodes.

For testing purpose, I used my mobile device which would automatically connect to the nearest AP. I then tried to open the webpage from the device and it worked. 

A wireless mesh network (WMN) is a particular type of mobile ad hoc network (MANET). A pure MANET is dynamically formed by mobile devices without the requirement of any existing infrastructure or prior network configuration. Similar to WMN, a MANET also has the ability of self-organization, self-discovering, self-healing, and self-configuration. However, a WMN is typically a collection of stationary mesh routers (MRs) with each employing multiple radios. 

Future directions - Expanding this project by configuring ESP32 as a node in the mesh using Dynamic source routing protocol which is used in a MANET. 

Reference Links to get started with

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/mesh.html

https://www.espressif.com/en/products/software/esp-now/overview (using ESP-NOW protocol)

#include "painlessMesh.h"
#include <WiFi.h>

#define   MESH_PREFIX     "whateverYouLike"
#define   MESH_PASSWORD   "somethingSneaky"
#define   MESH_PORT       5555


// Replace with your network credentials
const char* ssid     = "ESP32-Access-Point";
const char* password = "123456789";


Scheduler userScheduler; // to control your personal task
painlessMesh  mesh;
// User stub
void sendMessage() ; // Prototype so PlatformIO doesn't complain

Task taskSendMessage( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );

void sendMessage() {
  String msg = "Hello from node ";
  msg += mesh.getNodeId();
  mesh.sendBroadcast( msg );
  taskSendMessage.setInterval( random( TASK_SECOND * 1, TASK_SECOND * 5 ));
}

// Needed for painless library
void receivedCallback( uint32_t from, String &msg ) {
  Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str());
}

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

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

void nodeTimeAdjustedCallback(int32_t offset) {
    Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset);
}

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;


void setup() {
  Serial.begin(115200);
    mesh.setDebugMsgTypes( ERROR | STARTUP );  // set before init() so that you can see startup messages

  mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT );
  mesh.onReceive(&receivedCallback);
  mesh.onNewConnection(&newConnectionCallback);
  mesh.onChangedConnections(&changedConnectionCallback);
  mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);

  userScheduler.addTask( taskSendMessage );
  taskSendMessage.enable();


  // Connect to Wi-Fi network with SSID and password
  Serial.print("Setting AP (Access Point)…");
  // Remove the password parameter, if you want the AP (Access Point) to be open
  WiFi.softAP(ssid, password);

  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(IP);
  
  server.begin();
}

void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
 
          if (currentLine.length() == 0) {
           
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
            

            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            
            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            //client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
            client.println(".button2 {background-color: #555555;}</style></head>");
            
            // Web Page Heading
            client.println("<body><h1>ESP32 Web Server</h1>");
            

            client.println("</body></html>");
            
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}

This code will create ESP32s as access points as well as form a mesh where the APs will communicate with each other periodically. It also shows how to create a web server and a HTML webpage.