fork download
  1. #include "painlessMesh.h"
  2. #include <WiFiManager.h> // https://g...content-available-to-author-only...b.com/tzapu/WiFiManager
  3. #define serial serial = ()
  4.  
  5. void setup()
  6. // WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
  7. // it is a good practice to make sure your code sets wifi mode how you want it.
  8.  
  9. // put your setup code here, to run once:
  10. Serial.begin(115200);
  11.  
  12. //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
  13. WiFiManager wm;
  14.  
  15. // reset settings - wipe stored credentials for testing
  16. // these are stored by the esp library
  17. wm.resetSettings();
  18.  
  19. // Automatically connect using saved credentials,
  20. // if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
  21. // if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
  22. // then goes into a blocking loop awaiting configuration and will return success result
  23.  
  24. bool res;
  25. // res = wm.autoConnect(); // auto generated AP name from chipid
  26. // res = wm.autoConnect("AutoConnectAP"); // anonymous ap
  27. res = wm.autoConnect("AutoConnectAP","password"); // password protected ap
  28.  
  29. if(!res) {
  30. Serial.println("Failed to connect");
  31. // ESP.restart();
  32. }
  33. else {
  34. //if you get here you have connected to the WiFi
  35. Serial.println("connected...yeey :)");
  36. }
  37.  
  38. }
  39.  
  40. void loop() {
  41. // put your main code here, to run repeatedly:
  42. }
  43.  
  44. #define MESH_PREFIX "AutoConnectAP" // Troque para o nome da sua rede
  45. #define MESH_PASSWORD "password" // coloque a senha da sua rede
  46. #define MESH_PORT 5555
  47.  
  48. Scheduler userScheduler;
  49.  
  50. painlessMesh mesh;
  51.  
  52. void sendMessage(); /
  53. Task taskSendMessage(TASK_SECOND * 1, TASK_FOREVER, &sendMessage);
  54.  
  55. void sendMessage() {
  56. String msg = "Hello from node ";
  57. msg += mesh.getNodeId();
  58. mesh.sendBroadcast(msg);
  59. taskSendMessage.setInterval(random(TASK_SECOND * 1, TASK_SECOND * 5));
  60. }
  61.  
  62. void receivedCallback(uint32_t from, String &msg) {
  63. Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str());
  64. }
  65. void newConnectionCallback(uint32_t nodeId) {
  66. Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
  67. }
  68. void changedConnectionCallback() { Serial.printf("Changed connections\n"); }
  69. void nodeTimeAdjustedCallback(int32_t offset) {
  70. Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
  71. }
  72. void setup() {
  73. Serial.begin(115200);
  74. // COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
  75. mesh.setDebugMsgTypes(
  76. ERROR | STARTUP |
  77. DEBUG); // set before init() so that you can see startup messages
  78. mesh.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT);
  79. mesh.onReceive(&receivedCallback);
  80. mesh.onNewConnection(&newConnectionCallback);W
  81. mesh.onChangedConnections(&changedConnectionCallback);
  82. mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCall);
  83. mesh.initOTAReceive("otareceiver");
  84. userScheduler.addTask(taskSendMessage);
  85. taskSendMessage.enable();
  86.  
  87. void loop()
  88. mesh.update();
  89.  
Success #stdin #stdout 0.02s 25828KB
stdin
Standard input is empty
stdout
#include "painlessMesh.h"
#include <WiFiManager.h> // https://g...content-available-to-author-only...b.com/tzapu/WiFiManager
#define serial serial = ()

   void setup() 
   // WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
   // it is a good practice to make sure your code sets wifi mode how you want it.

   // put your setup code here, to run once:
   Serial.begin(115200);
   
   //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
   WiFiManager wm;

   // reset settings - wipe stored credentials for testing
   // these are stored by the esp library
     wm.resetSettings();

   // Automatically connect using saved credentials,
   // if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
   // if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
   // then goes into a blocking loop awaiting configuration and will return success result

   bool res;
   // res = wm.autoConnect(); // auto generated AP name from chipid
   // res = wm.autoConnect("AutoConnectAP"); // anonymous ap
   res = wm.autoConnect("AutoConnectAP","password"); // password protected ap

   if(!res) {
        Serial.println("Failed to connect");
        // ESP.restart();
   } 
   else {
        //if you get here you have connected to the WiFi    
        Serial.println("connected...yeey :)");
   }

}

void loop() {
   // put your main code here, to run repeatedly:   
}

#define MESH_PREFIX "AutoConnectAP"   // Troque para o nome da sua rede
#define MESH_PASSWORD "password"   // coloque a senha da sua rede
#define MESH_PORT 5555

Scheduler userScheduler;  

painlessMesh mesh;

void sendMessage();  /
Task taskSendMessage(TASK_SECOND * 1, TASK_FOREVER, &sendMessage);

void sendMessage() {
  String msg = "Hello from node ";
  msg += mesh.getNodeId();
  mesh.sendBroadcast(msg);
  taskSendMessage.setInterval(random(TASK_SECOND * 1, TASK_SECOND * 5));
}

void receivedCallback(uint32_t from, String &msg) {
  Serial.printf("startHere: Received from %u msg=%s\n", from, msg.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);
  // COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
  mesh.setDebugMsgTypes(
      ERROR | STARTUP |
      DEBUG);  // set before init() so that you can see startup messages
  mesh.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT);
  mesh.onReceive(&receivedCallback);
  mesh.onNewConnection(&newConnectionCallback);W
  mesh.onChangedConnections(&changedConnectionCallback);
  mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCall);
  mesh.initOTAReceive("otareceiver");
  userScheduler.addTask(taskSendMessage);
  taskSendMessage.enable();

void loop() 
  mesh.update();