Ruchir Sharma
Published © LGPL

Getting started with ESP-MESH and ESP-8266

ESP mesh n/w is created to control LED with push button and sending DHT data to other nodes. Turn on the LED when temp/hum is more.

BeginnerFull instructions provided1 hour3,756
Getting started with ESP-MESH and ESP-8266

Things used in this project

Story

Read more

Schematics

Mesh_nodes

Code

TempandLED_node1

Arduino
// Node 1
#include "painlessMesh.h"
#include <DHT.h>

// WiFi Credentials
#define   MESH_PREFIX     "AccordingToYou"
#define   MESH_PASSWORD   "somethingSecret"
#define   MESH_PORT       5555

//Pin Declaration
#define LED D5 //Relay1
#define DHTPIN D4

#define DHTTYPE DHT11     // DHT 11

int LState;


DHT dht(DHTPIN, DHTTYPE);

Scheduler userScheduler; 
painlessMesh  mesh;
void sendMessage() ; 
Task taskSendMessage( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );
void sendMessage()
{
  Serial.println();
  Serial.println("Start Sending....");
  
  // Serializing in JSON Format
  DynamicJsonDocument doc(1024);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  doc["TEMP"] = t;
  doc["HUM"] = h;
  String msg ;
  serializeJson(doc, msg);
  
  mesh.sendBroadcast( msg );
  Serial.println("Message from Node1");
  Serial.println(msg);
  taskSendMessage.setInterval((TASK_SECOND * 1, TASK_SECOND * 10));
}
// Needed for painless library
void receivedCallback( uint32_t from, String &msg ) {
  Serial.println();
  Serial.print("Message = ");Serial.println(msg);
  String json;
  DynamicJsonDocument doc(1024);
  json = msg.c_str();
  DeserializationError error = deserializeJson(doc, json);
  if (error)
  {
    Serial.print("deserializeJson() failed: ");
    Serial.println(error.c_str());
  }

  LState = doc["Button"]; 
  digitalWrite(LED, LState);
}
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(DHTPIN,INPUT);
  pinMode(LED, OUTPUT);
  
  mesh.setDebugMsgTypes( ERROR | STARTUP );  
  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() {
  mesh.update();
}

PushbuttonandLED_node2

Arduino
// Node 2
#include "painlessMesh.h"
// WiFi Credentials
#define   MESH_PREFIX     "AccordingToYou"
#define   MESH_PASSWORD   "somethingSecret"
#define   MESH_PORT       5555 // default number and should be an integer

#define button D4
#define LED D5

bool Bstate = 0; 

// Scheduler is used to synchronize the connection bw other nodes
Scheduler userScheduler; 
painlessMesh  mesh;

void sendMessage() ; // callback func
Task sendTask( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );

// everthying should be done in callback func
void sendMessage()
{
  digitalWrite(LED,HIGH); // when start sending
  Serial.println();
  Serial.println("Start Sending....");
  
  if(digitalRead(button) == LOW)
     Bstate = !Bstate; 
  
  
  
  Serial.println("Button State");Serial.println(Bstate);
  
  // Serialize the message
  DynamicJsonDocument doc(1024);
  doc["Button"] = Bstate; 
  String msg ;
  serializeJson(doc, msg); //
  mesh.sendBroadcast( msg );
  Serial.println("Message from Node2");
  Serial.println(msg);
  digitalWrite(LED,LOW); 
}

// Deserialize the message
void receivedCallback( uint32_t from, String &msg ) {

  Serial.println("Message ="); Serial.println(msg);
  String json = msg.c_str();;
  DynamicJsonDocument doc(1024);
  DeserializationError error = deserializeJson(doc, json);
  if (error)
  {
    Serial.print("deserializeJson() failed: ");
    Serial.println(error.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(button, INPUT);
  pinMode(LED, OUTPUT);
  
  mesh.setDebugMsgTypes( ERROR | STARTUP );  
  mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT );
  mesh.onReceive(&receivedCallback);
  mesh.onNewConnection(&newConnectionCallback);
  mesh.onChangedConnections(&changedConnectionCallback);
  mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
  userScheduler.addTask( sendTask );
  sendTask.enable();
}

void loop() {
  
  mesh.update();
}

LED_node3

Arduino
// Node 3

#include "painlessMesh.h"

// WiFi Credentials
#define   MESH_PREFIX     "AccordingToYou"
#define   MESH_PASSWORD   "somethingSecret"
#define   MESH_PORT       5555 // default number and should be an integer

#define LED D5

// As we want to read both the messages
int Bstate;
float h, t;


// Scheduler is used to synchronize the connection bw other nodes
Scheduler userScheduler; 
painlessMesh  mesh;


// Deserialize the message
void receivedCallback( uint32_t from, String &msg ) {

  Serial.println("Message from Node3");
  Serial.println("Message ="); Serial.println(msg);
  String json = msg.c_str();;
  DynamicJsonDocument doc(1024);
  DeserializationError error = deserializeJson(doc, json);
  if (error)
  {
    Serial.print("deserializeJson() failed: ");
    Serial.println(error.c_str());
  }
  Bstate = doc["Button"]; 
  t = doc["TEMP"];
  h = doc["HUM"];

  if (t > 27 || h > 50)  
  digitalWrite(LED,HIGH);
  else(digitalWrite(LED,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(LED, OUTPUT);
  
  mesh.setDebugMsgTypes( ERROR | STARTUP );  
  mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT );
  mesh.onReceive(&receivedCallback);
  mesh.onNewConnection(&newConnectionCallback);
  mesh.onChangedConnections(&changedConnectionCallback);
  mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
 
}

void loop() {
  
  mesh.update();
}

Credits

Ruchir Sharma

Ruchir Sharma

12 projects • 182 followers

Comments