Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //************************************************************
- // this is a simple example that uses the easyMesh library
- //
- // 1. blinks led once for every node on the mesh
- // 2. blink cycle repeats every BLINK_PERIOD
- // 3. sends a silly message to every node on the mesh at a random time between 1 and 5 seconds
- // 4. prints anything it receives to Serial.print
- //
- //
- //************************************************************
- #include <painlessMesh.h>
- // some gpio pin that is connected to an LED...
- // on my rig, this is 5, change to the right number of your LED.
- #define LED 2 // GPIO number of connected LED, ON ESP-12 IS GPIO2
- #define bot 4 // GPIO botao, ON ESP-12 IS GPIO4
- #define BLINK_PERIOD 3000 // milliseconds until cycle repeat
- #define BLINK_DURATION 100 // milliseconds LED is on for
- #define MESH_SSID "whateverYouLike"
- #define MESH_PASSWORD "somethingSneaky"
- #define MESH_PORT 5555 // ESP8266 5555 e 5554
- // Prototypes
- void sendMessage();
- void receivedCallback(uint32_t from, String & msg);
- void newConnectionCallback(uint32_t nodeId);
- void changedConnectionCallback();
- void nodeTimeAdjustedCallback(int32_t offset);
- void delayReceivedCallback(uint32_t from, int32_t delay);
- Scheduler userScheduler; // to control your personal task
- painlessMesh mesh;
- bool stLED = 1; // Status do LED 1 = Ligado
- bool calc_delay = false;
- SimpleList<uint32_t> nodes;
- //------------------------------------------------------------
- //void sendMessage() ; // Prototype
- Task taskSendMessage( TASK_SECOND * 1, TASK_FOREVER, &sendMessage ); // start with a one second interval
- // Task to blink the number of nodes
- Task blinkNoNodes;
- bool onFlag = false;
- //------------------------------------------------------------------------------
- void setup() {
- Serial.begin(115200);
- pinMode(LED, OUTPUT);
- pinMode(bot, INPUT_PULLUP);
- //mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
- //mesh.setDebugMsgTypes(ERROR | DEBUG | CONNECTION | COMMUNICATION); // set before init() so that you can see startup messages
- //mesh.setDebugMsgTypes(ERROR | DEBUG | CONNECTION); // set before init() so that you can see startup messages
- //mesh.setDebugMsgTypes(CONNECTION); // set before init() so that you can see startup messages
- mesh.init(MESH_SSID, MESH_PASSWORD, &userScheduler, MESH_PORT);
- mesh.onReceive(&receivedCallback);
- mesh.onNewConnection(&newConnectionCallback);
- mesh.onChangedConnections(&changedConnectionCallback);
- mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
- mesh.onNodeDelayReceived(&delayReceivedCallback);
- userScheduler.addTask( taskSendMessage );
- taskSendMessage.enable();
- blinkNoNodes.set(BLINK_PERIOD, (mesh.getNodeList().size() + 1) * 2, []() {
- // If on, switch off, else switch on
- if (onFlag)
- onFlag = false;
- else
- onFlag = true;
- blinkNoNodes.delay(BLINK_DURATION);
- if (blinkNoNodes.isLastIteration()) {
- // Finished blinking. Reset task for next run
- // blink number of nodes (including this node) times
- blinkNoNodes.setIterations((mesh.getNodeList().size() + 1) * 2);
- // Calculate delay based on current mesh time and BLINK_PERIOD
- // This results in blinks between nodes being synced
- blinkNoNodes.enableDelayed(BLINK_PERIOD -
- (mesh.getNodeTime() % (BLINK_PERIOD * 1000)) / 1000);
- }
- });
- userScheduler.addTask(blinkNoNodes);
- blinkNoNodes.enable();
- randomSeed(analogRead(A0));
- }
- //------------------------------------------------------------------------------
- void loop() {
- userScheduler.execute(); // it will run mesh scheduler as well
- mesh.update();
- //digitalWrite(LED, !onFlag);
- if (digitalRead(bot) == LOW) // Se botao esta pressionado
- {
- delay(30); // Debouncing
- if (digitalRead(bot) == LOW) // Se botao continua pressionado
- {
- digitalWrite(LED, !digitalRead(LED)); // Inverte status LED
- }
- }
- }
- //------------------------------------------------------------------------------
- void sendMessage() {
- /* String msg = "Eu sou o ESP2.0 ";
- //String msg = "Eu sou o ESP2.54 ";
- // String msg = " Hello from node ";
- msg += mesh.getNodeId();
- // msg += " myFreeMemory: " + String(ESP.getFreeHeap());
- */
- if (digitalRead(LED) == HIGH)
- stLED = 0;
- else
- stLED = 1;
- String msg = "|0|"; // ESP model ESP8266 = 0 ESP32 = 1
- msg += "1|"; // status ESP
- msg += "1|"; // Bot
- msg += stLED; // Status LED
- msg += "|"; // Sepaarador
- msg += analogRead(A0); // Valor analogico
- msg += "*"; // EOM
- uint32_t to = 887631948; // ESP8266 pin2.54 887631948 ESP32 --> 3289255477 Envia single
- mesh.sendBroadcast(msg); // ESP8266 pin2.0 2143346377
- // mesh.sendSingle(to, msg);
- if (calc_delay) {
- SimpleList<uint32_t>::iterator node = nodes.begin();
- while (node != nodes.end()) {
- mesh.startDelayMeas(*node);
- node++;
- }
- calc_delay = false;
- }
- // Serial.printf("S msg: %s\n", msg.c_str());
- taskSendMessage.setInterval( random(TASK_SECOND * 1, TASK_SECOND * 5)); // between 1 and 5 seconds
- }
- //------------------------------------------------------------------------------
- void receivedCallback(uint32_t from, String & msg) {
- // Serial.printf("startHere: from %u msg=%s\n", from, msg.c_str());
- // Serial.println(from,HEX);
- if (from == 3289255477)
- {
- msg.remove(0, msg.length() - 1); // Separa o ultimo digito da msg
- if (msg == "1") // Se for 1
- digitalWrite(LED, !digitalRead(LED)); // Inverte status do LED
- else // Se for != 1
- digitalWrite(LED, HIGH); // Apaga o LED
- }
- }
- //------------------------------------------------------------------------------
- void newConnectionCallback(uint32_t nodeId) {
- // Reset blink task
- onFlag = false;
- blinkNoNodes.setIterations((mesh.getNodeList().size() + 1) * 2);
- blinkNoNodes.enableDelayed(BLINK_PERIOD - (mesh.getNodeTime() % (BLINK_PERIOD * 1000)) / 1000);
- Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
- }
- //------------------------------------------------------------------------------
- void changedConnectionCallback() {
- Serial.printf("Changed connections %s\n", mesh.subConnectionJson().c_str());
- // Reset blink task
- onFlag = false;
- blinkNoNodes.setIterations((mesh.getNodeList().size() + 1) * 2);
- blinkNoNodes.enableDelayed(BLINK_PERIOD - (mesh.getNodeTime() % (BLINK_PERIOD * 1000)) / 1000);
- nodes = mesh.getNodeList();
- Serial.printf("Num nodes: %d\n", nodes.size());
- Serial.printf("Connection list:");
- SimpleList<uint32_t>::iterator node = nodes.begin();
- while (node != nodes.end()) {
- Serial.printf(" %u", *node);
- node++;
- }
- Serial.println();
- calc_delay = true;
- }
- //------------------------------------------------------------------------------
- void nodeTimeAdjustedCallback(int32_t offset) {
- Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
- }
- //------------------------------------------------------------------------------
- void delayReceivedCallback(uint32_t from, int32_t delay) {
- Serial.printf("Delay to node %u is %d us\n", from, delay);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement