What is Mesh Networking | Mesh Networking based Car | ESP8266 and ESP32

What is Mesh Networking | Mesh Networking based Car | ESP8266 and ESP32

ESP-WIFI-MESH is a wireless communication network with nodes organized in a mesh topology using the simultaneous AP-STA feature on Espressif SoCs. It provides a self-forming and self-healing network, with ease of deployment.

So today we are using mesh networking in order to make a wireless controlled car using esp8266 and esp32.


We are using painless mesh library. painlessMesh is a library that takes care of the particulars of creating a simple mesh network using esp8266 and esp32 hardware. The goal is to allow the programmer to work with a mesh network without having to worry about how the network is structured or managed.

Link for Painless mesh library – https://github.com/gmag11/painlessMesh

Code for Remote – ESP32

//**********************************************************//This code is designed by robocircuits
//https://youtube.com/robocircuits
//robocircuits.com
//************************************************************
#include "painlessMesh.h"

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

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() {
  int x = analogRead(32);
  int y = analogRead(33);
  Serial.print(x);
  Serial.print(" - ");
  Serial.println(y);
  String json = "";
  DynamicJsonDocument doc(1024);
  
  if(x > 3900)
  {
    doc["steer"] = "left";
  }
  else if(x > 500 && x < 3500)
  {
    doc["steer"] = "middle";
  }
  else if(x < 500)
  {
    doc["steer"] = "right";
  }

  if(y > 3900)
  {
    doc["mov"] = "foreward";
  }
  else if(y > 500 && y < 3500)
  {
    doc["mov"] = "stop";
  }
  else if(y < 500)
  {
    doc["mov"] = "backward";
  }
  
  serializeJson(doc,json);
  Serial.println(json);

  mesh.sendBroadcast( json );
  
  taskSendMessage.setInterval( random( TASK_SECOND * 1, TASK_SECOND * 1 ));
}

// 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);
}

void setup() {
  Serial.begin(115200);
  pinMode(32,INPUT);
  pinMode(33,INPUT);
//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
  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();
}

void loop() {
  // it will run the user scheduler as well
  mesh.update();
}

Code for Car – ESP8266

//**********************************************************//This code is designed by robocircuits
//https://youtube.com/robocircuits
//robocircuits.com
//************************************************************
#include "painlessMesh.h"

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

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.println(msg);
  String json = ""; 
  DynamicJsonDocument doc(1024);
  json = msg.c_str();
  DeserializationError error = deserializeJson(doc,json);
  if(error)
  {
    Serial.println(" deserialization failed");
  }
 // String steer = "";
 // steer = doc["steer"];
  
  if(doc["mov"] == "foreward")
  {
    Serial.println("foreward");
    digitalWrite(D1,LOW);
    digitalWrite(D2,HIGH);
    digitalWrite(D5,LOW);
    digitalWrite(D6,HIGH);
  }
  else if(doc["mov"] == "backward")
  {
    Serial.println("backward");
    digitalWrite(D1,HIGH);
    digitalWrite(D2,LOW);
    digitalWrite(D5,HIGH);
    digitalWrite(D6,LOW);
  }
  else if(doc["steer"] == "left")
  {
    Serial.println("left");
    digitalWrite(D1,LOW);
    digitalWrite(D2,HIGH);
    digitalWrite(D5,HIGH);
    digitalWrite(D6,LOW);
  }
  else if(doc["steer"] == "right")
  {
    Serial.println("right");
    digitalWrite(D1,HIGH);
    digitalWrite(D2,LOW);
    digitalWrite(D5,LOW);
    digitalWrite(D6,HIGH);
  }
  else
  {
    Serial.println("stop");
    digitalWrite(D1,LOW);
    digitalWrite(D2,LOW);
    digitalWrite(D5,LOW);
    digitalWrite(D6,LOW);
  }
  
}

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);
}

void setup() {
  Serial.begin(115200);
  pinMode(D0,OUTPUT);
  pinMode(D1,OUTPUT);
  pinMode(D2,OUTPUT);
  pinMode(D5,OUTPUT);
  pinMode(D6,OUTPUT);
  pinMode(D7,OUTPUT);

 // digitalWrite(D0,HIGH);
 // digitalWrite(D7,HIGH);

  analogWrite(D0,512);
  analogWrite(D7,512);
  
//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
  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();
}

void loop() {
  // it will run the user scheduler as well
  mesh.update();
}

Leave a Reply