Advertisement
RuiViana

MeshNoV05.ino

Dec 12th, 2018
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.59 KB | None | 0 0
  1. //************************************************************
  2. // this is a simple example that uses the easyMesh library
  3. //
  4. // 1. blinks led once for every node on the mesh
  5. // 2. blink cycle repeats every BLINK_PERIOD
  6. // 3. sends a silly message to every node on the mesh at a random time between 1 and 5 seconds
  7. // 4. prints anything it receives to Serial.print
  8. //
  9. //
  10. //************************************************************
  11. #include <painlessMesh.h>
  12.  
  13. // some gpio pin that is connected to an LED...
  14. // on my rig, this is 5, change to the right number of your LED.
  15. #define   LED             2       // GPIO number of connected LED, ON ESP-12 IS GPIO2
  16. #define   bot             4       // GPIO botao, ON ESP-12 IS GPIO4
  17. #define   BLINK_PERIOD    3000 // milliseconds until cycle repeat
  18. #define   BLINK_DURATION  100  // milliseconds LED is on for
  19.  
  20. #define   MESH_SSID       "whateverYouLike"
  21. #define   MESH_PASSWORD   "somethingSneaky"
  22. #define   MESH_PORT       5555  // ESP8266 5555 e 5554
  23.  
  24. // Prototypes
  25. void sendMessage();
  26. void receivedCallback(uint32_t from, String & msg);
  27. void newConnectionCallback(uint32_t nodeId);
  28. void changedConnectionCallback();
  29. void nodeTimeAdjustedCallback(int32_t offset);
  30. void delayReceivedCallback(uint32_t from, int32_t delay);
  31.  
  32. Scheduler     userScheduler; // to control your personal task
  33. painlessMesh  mesh;
  34. bool stLED = 1;               // Status do LED  1 = Ligado
  35.  
  36. bool calc_delay = false;
  37. SimpleList<uint32_t> nodes;
  38. //------------------------------------------------------------
  39. //void sendMessage() ; // Prototype
  40. Task taskSendMessage( TASK_SECOND * 1, TASK_FOREVER, &sendMessage ); // start with a one second interval
  41.  
  42. // Task to blink the number of nodes
  43. Task blinkNoNodes;
  44. bool onFlag = false;
  45. //------------------------------------------------------------------------------
  46. void setup() {
  47.   Serial.begin(115200);
  48.   pinMode(LED, OUTPUT);
  49.   pinMode(bot, INPUT_PULLUP);
  50.   //mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
  51.   //mesh.setDebugMsgTypes(ERROR | DEBUG | CONNECTION | COMMUNICATION);  // set before init() so that you can see startup messages
  52.   //mesh.setDebugMsgTypes(ERROR | DEBUG | CONNECTION);  // set before init() so that you can see startup messages
  53.   //mesh.setDebugMsgTypes(CONNECTION);  // set before init() so that you can see startup messages
  54.  
  55.   mesh.init(MESH_SSID, MESH_PASSWORD, &userScheduler, MESH_PORT);
  56.   mesh.onReceive(&receivedCallback);
  57.   mesh.onNewConnection(&newConnectionCallback);
  58.   mesh.onChangedConnections(&changedConnectionCallback);
  59.   mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
  60.   mesh.onNodeDelayReceived(&delayReceivedCallback);
  61.  
  62.   userScheduler.addTask( taskSendMessage );
  63.   taskSendMessage.enable();
  64.  
  65.   blinkNoNodes.set(BLINK_PERIOD, (mesh.getNodeList().size() + 1) * 2, []() {
  66.     // If on, switch off, else switch on
  67.     if (onFlag)
  68.       onFlag = false;
  69.     else
  70.       onFlag = true;
  71.     blinkNoNodes.delay(BLINK_DURATION);
  72.  
  73.     if (blinkNoNodes.isLastIteration()) {
  74.       // Finished blinking. Reset task for next run
  75.       // blink number of nodes (including this node) times
  76.       blinkNoNodes.setIterations((mesh.getNodeList().size() + 1) * 2);
  77.       // Calculate delay based on current mesh time and BLINK_PERIOD
  78.       // This results in blinks between nodes being synced
  79.       blinkNoNodes.enableDelayed(BLINK_PERIOD -
  80.                                  (mesh.getNodeTime() % (BLINK_PERIOD * 1000)) / 1000);
  81.     }
  82.   });
  83.   userScheduler.addTask(blinkNoNodes);
  84.   blinkNoNodes.enable();
  85.  
  86.   randomSeed(analogRead(A0));
  87. }
  88. //------------------------------------------------------------------------------
  89. void loop() {
  90.   userScheduler.execute(); // it will run mesh scheduler as well
  91.   mesh.update();
  92.   //digitalWrite(LED, !onFlag);
  93.   if (digitalRead(bot) == LOW)                          // Se botao esta pressionado
  94.   {
  95.     delay(30);                                          // Debouncing
  96.     if (digitalRead(bot) == LOW)                        // Se botao continua pressionado
  97.     {
  98.       digitalWrite(LED, !digitalRead(LED));             // Inverte status LED
  99.     }
  100.   }
  101. }
  102. //------------------------------------------------------------------------------
  103. void sendMessage() {
  104. /*  String msg = "Eu sou o ESP2.0 ";
  105.   //String msg = "Eu sou o ESP2.54 ";
  106.   //  String msg = " Hello from node ";
  107.   msg += mesh.getNodeId();
  108.   //  msg += " myFreeMemory: " + String(ESP.getFreeHeap());
  109. */
  110.  
  111.   if (digitalRead(LED) == HIGH)
  112.     stLED = 0;
  113.   else
  114.     stLED = 1;
  115.   String msg = "|0|";       // ESP model ESP8266 = 0 ESP32 = 1
  116.   msg += "1|";              // status ESP
  117.   msg += "1|";              // Bot
  118.   msg += stLED;             // Status LED
  119.   msg += "|";               // Sepaarador
  120.   msg += analogRead(A0);    // Valor analogico
  121.   msg += "*";               // EOM
  122.  
  123.   uint32_t to = 887631948;              // ESP8266 pin2.54  887631948    ESP32 --> 3289255477  Envia single
  124.   mesh.sendBroadcast(msg);              // ESP8266 pin2.0   2143346377
  125.   //  mesh.sendSingle(to, msg);
  126.  
  127.   if (calc_delay) {
  128.     SimpleList<uint32_t>::iterator node = nodes.begin();
  129.     while (node != nodes.end()) {
  130.       mesh.startDelayMeas(*node);
  131.       node++;
  132.     }
  133.     calc_delay = false;
  134.   }
  135.  
  136.   // Serial.printf("S msg: %s\n", msg.c_str());
  137.  
  138.   taskSendMessage.setInterval( random(TASK_SECOND * 1, TASK_SECOND * 5));  // between 1 and 5 seconds
  139. }
  140. //------------------------------------------------------------------------------
  141. void receivedCallback(uint32_t from, String & msg) {
  142.   //  Serial.printf("startHere: from %u msg=%s\n", from, msg.c_str());
  143.   //  Serial.println(from,HEX);
  144.   if (from  ==  3289255477)
  145.   {
  146.     msg.remove(0, msg.length() - 1);                    // Separa o ultimo digito da msg
  147.     if (msg == "1")                                     // Se for 1
  148.       digitalWrite(LED, !digitalRead(LED));             // Inverte status do LED
  149.     else                                                // Se for != 1
  150.       digitalWrite(LED, HIGH);                          // Apaga o LED
  151.   }
  152. }
  153. //------------------------------------------------------------------------------
  154. void newConnectionCallback(uint32_t nodeId) {
  155.   // Reset blink task
  156.   onFlag = false;
  157.   blinkNoNodes.setIterations((mesh.getNodeList().size() + 1) * 2);
  158.   blinkNoNodes.enableDelayed(BLINK_PERIOD - (mesh.getNodeTime() % (BLINK_PERIOD * 1000)) / 1000);
  159.  
  160.   Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
  161. }
  162. //------------------------------------------------------------------------------
  163. void changedConnectionCallback() {
  164.   Serial.printf("Changed connections %s\n", mesh.subConnectionJson().c_str());
  165.   // Reset blink task
  166.   onFlag = false;
  167.   blinkNoNodes.setIterations((mesh.getNodeList().size() + 1) * 2);
  168.   blinkNoNodes.enableDelayed(BLINK_PERIOD - (mesh.getNodeTime() % (BLINK_PERIOD * 1000)) / 1000);
  169.  
  170.   nodes = mesh.getNodeList();
  171.  
  172.   Serial.printf("Num nodes: %d\n", nodes.size());
  173.   Serial.printf("Connection list:");
  174.  
  175.   SimpleList<uint32_t>::iterator node = nodes.begin();
  176.   while (node != nodes.end()) {
  177.     Serial.printf(" %u", *node);
  178.     node++;
  179.   }
  180.   Serial.println();
  181.   calc_delay = true;
  182. }
  183. //------------------------------------------------------------------------------
  184. void nodeTimeAdjustedCallback(int32_t offset) {
  185.   Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
  186. }
  187. //------------------------------------------------------------------------------
  188. void delayReceivedCallback(uint32_t from, int32_t delay) {
  189.   Serial.printf("Delay to node %u is %d us\n", from, delay);
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement