SHARE
TWEET

Untitled

a guest Nov 14th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include "painlessMesh.h"
  3. #define   LED             2
  4. #define   MESH_PREFIX     "Test"
  5. #define   MESH_PASSWORD   "somethingSneaky"
  6. #define   MESH_PORT       5555
  7.  
  8. Scheduler     userScheduler;
  9. painlessMesh  mesh;
  10.  
  11. String str;
  12. int Value = 0;
  13.  
  14. void receivedCallback( uint32_t from, String &msg );
  15.  
  16. size_t logServerId = 2135709220;
  17.  
  18. Task myLoggingTask(1500, TASK_FOREVER, []() {
  19.   if (mesh.isConnected(2135709220)) {
  20.  
  21.     DynamicJsonDocument jsonBuffer(1024);
  22.     JsonObject msg = jsonBuffer.to<JsonObject>();
  23.  
  24.     msg["topic"] = "sensor";
  25.     msg["value"] = Value;
  26.     str = "";
  27.     serializeJson(msg, str);
  28.  
  29.     mesh.sendSingle(logServerId, str);
  30.   }
  31. });
  32.  
  33. void setup() {
  34.   pinMode(LED, OUTPUT);
  35.   digitalWrite(LED, 0);
  36.   Serial.begin(115200);
  37.   Serial.println("");
  38.   Serial.println(" ON ********* ");
  39.  
  40.   mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT, WIFI_AP_STA, 6 );
  41.   mesh.onReceive(&receivedCallback);
  42.   userScheduler.addTask(myLoggingTask);
  43.   myLoggingTask.enable();
  44. }
  45.  
  46. void loop() {
  47.   userScheduler.execute(); // it will run mesh scheduler as well
  48.   mesh.update();
  49. }
  50.  
  51. void receivedCallback( uint32_t from, String &msg ) {
  52.   Serial.printf("logClient: Received from %u msg=%s\n", from, msg.c_str());
  53.  
  54.   DynamicJsonDocument jsonBuffer(1024 + msg.length());
  55.   DeserializationError error = deserializeJson(jsonBuffer, msg);
  56.   if (error) {
  57.     Serial.printf("DeserializationError\n");
  58.     return;
  59.   }
  60.   JsonObject root = jsonBuffer.as<JsonObject>();
  61.  
  62.   if (root.containsKey("value")) {
  63.     if (String("Received").equals(root["value"].as<String>())) {
  64.       Serial.printf("logServer responded!!!\n");
  65.       Serial.println("Job done, going to sleep");
  66.       ESP.deepSleep(0);
  67.     }
  68.   }
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top