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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
//**********************************************************//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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
//**********************************************************//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(); } |