RuiViana

NoV05.ino

Feb 17th, 2019
237
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 += "|";               // Separador
  120.   msg += analogRead(A0);    // Valor analogico
  121.   msg += "*";               // EOM
  122.  
  123.   mesh.sendBroadcast(msg);              
  124.  
  125.   if (calc_delay) {
  126.     SimpleList<uint32_t>::iterator node = nodes.begin();
  127.     while (node != nodes.end()) {
  128.       mesh.startDelayMeas(*node);
  129.       node++;
  130.     }
  131.     calc_delay = false;
  132.   }
  133. //   Serial.printf("S msg: %s\n", msg.c_str());
  134.   taskSendMessage.setInterval( random(TASK_SECOND * 1, TASK_SECOND * 5));  // between 1 and 5 seconds
  135. }
  136. //------------------------------------------------------------------------------
  137. void receivedCallback(uint32_t from, String & msg) {
  138.     Serial.printf("startHere: from %u msg=%s\n", from, msg.c_str());
  139.   //  Serial.println(from,HEX);
  140.   if (from  ==  3289255477)
  141.   {
  142.     msg.remove(0, msg.length() - 1);                    // Separa o ultimo digito da msg
  143.     if (msg == "1")                                     // Se for 1
  144.       digitalWrite(LED, !digitalRead(LED));             // Inverte status do LED
  145.     else                                                // Se for != 1
  146.       digitalWrite(LED, HIGH);                          // Apaga o LED
  147.   }
  148. }
  149. //------------------------------------------------------------------------------
  150. void newConnectionCallback(uint32_t nodeId) {
  151.   // Reset blink task
  152.   onFlag = false;
  153.   blinkNoNodes.setIterations((mesh.getNodeList().size() + 1) * 2);
  154.   blinkNoNodes.enableDelayed(BLINK_PERIOD - (mesh.getNodeTime() % (BLINK_PERIOD * 1000)) / 1000);
  155.  
  156.   Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
  157. }
  158. //------------------------------------------------------------------------------
  159. void changedConnectionCallback() {
  160.   Serial.printf("Changed connections %s\n", mesh.subConnectionJson().c_str());
  161.   // Reset blink task
  162.   onFlag = false;
  163.   blinkNoNodes.setIterations((mesh.getNodeList().size() + 1) * 2);
  164.   blinkNoNodes.enableDelayed(BLINK_PERIOD - (mesh.getNodeTime() % (BLINK_PERIOD * 1000)) / 1000);
  165.  
  166.   nodes = mesh.getNodeList();
  167.  
  168.   Serial.printf("Num nodes: %d\n", nodes.size());
  169.   Serial.printf("Connection list:");
  170.  
  171.   SimpleList<uint32_t>::iterator node = nodes.begin();
  172.   while (node != nodes.end()) {
  173.     Serial.printf(" %u", *node);
  174.     node++;
  175.   }
  176.   Serial.println();
  177.   calc_delay = true;
  178. }
  179. //------------------------------------------------------------------------------
  180. void nodeTimeAdjustedCallback(int32_t offset) {
  181.   Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
  182. }
  183. //------------------------------------------------------------------------------
  184. void delayReceivedCallback(uint32_t from, int32_t delay) {
  185.   Serial.printf("Delay to node %u is %d us\n", from, delay);
  186. }
RAW Paste Data