RuiViana

ServerMesh.ino

Dec 8th, 2018
175
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //************************************************************
  2. // this is a simple example that uses the painlessMesh library to
  3. // connect to a another network and broadcast message from a webpage to the edges of the mesh network.
  4. // This sketch can be extended further using all the abilities of the AsyncWebserver library (WS, events, ...)
  5. // for more details
  6. // https://gitlab.com/painlessMesh/painlessMesh/wikis/bridge-between-mesh-and-another-network
  7. // for more details about my version
  8. // https://gitlab.com/Assassynv__V/painlessMesh
  9. // and for more details about the AsyncWebserver library
  10. // https://github.com/me-no-dev/ESPAsyncWebServer
  11. //************************************************************
  12.  
  13. #include "IPAddress.h"
  14. #include "painlessMesh.h"
  15.  
  16. #ifdef ESP8266
  17. #include "Hash.h"
  18. #include <ESPAsyncTCP.h>
  19. #else
  20. #include "SPIFFS.h"
  21. #include <AsyncTCP.h>
  22. #endif
  23. #include <ESPAsyncWebServer.h>
  24.  
  25. #define   MESH_PREFIX     "whateverYouLike"
  26. #define   MESH_PASSWORD   "somethingSneaky"
  27. #define   MESH_PORT       5555
  28.  
  29. #define   STATION_SSID     "xxxxxxx"
  30. #define   STATION_PASSWORD "yyyyyyyy"
  31.  
  32. #define HOSTNAME "HTTP_BRIDGE"
  33.  
  34. // Prototype
  35. void receivedCallback( const uint32_t &from, const String &msg );
  36. IPAddress getlocalIP();
  37.  
  38. painlessMesh  mesh;
  39. AsyncWebServer server(80);
  40. IPAddress myIP(0,0,0,0);
  41. IPAddress myAPIP(0,0,0,0);
  42. //------------------------------------------------------------------
  43. void setup() {
  44.   Serial.begin(115200);
  45.  
  46.   if (!SPIFFS.begin()) {
  47.     Serial.println("An Error has occurred while mounting SPIFFS");
  48.     return;
  49.   }
  50.  
  51.  
  52.   //mesh.setDebugMsgTypes( ERROR | STARTUP | CONNECTION );  // set before init() so that you can see startup messages
  53.  
  54.   // Channel set to 6. Make sure to use the same channel for your mesh and for you other
  55.   // network (STATION_SSID)
  56.   mesh.init( MESH_PREFIX, MESH_PASSWORD, MESH_PORT, WIFI_AP_STA, 6 );
  57.   mesh.onReceive(&receivedCallback);
  58.  
  59.   mesh.stationManual(STATION_SSID, STATION_PASSWORD);
  60.   mesh.setHostname(HOSTNAME);
  61.   myAPIP = IPAddress(mesh.getAPIP());
  62.   Serial.println("My AP IP is " + myAPIP.toString());
  63. /*
  64.   //Async webserver
  65.   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  66.     request->send(200, "text/html", "<form>Text to Broadcast<br><input type='text' name='BROADCAST'><br><br><input type='submit' value='Submit'></form>");
  67.     if (request->hasArg("BROADCAST")){
  68.       String msg = request->arg("BROADCAST");
  69.       msg += mesh.getNodeId();
  70.  //     mesh.sendBroadcast(msg);
  71.      uint32_t myId = 887631948;
  72.      mesh.sendSingle(myId, msg);
  73.     }
  74.   });
  75.  
  76. */
  77.  
  78.  server.on("/html", HTTP_GET, [](AsyncWebServerRequest * request) {
  79.     request->send(SPIFFS, "/test.html", "text/html");
  80.  
  81.   if (request->hasArg("BROADCAST")){
  82.       String msg = request->arg("BROADCAST");
  83.       msg += mesh.getNodeId();
  84.      uint32_t myId = 887631948;
  85.      mesh.sendSingle(myId, msg);
  86.     }
  87.  
  88.   });
  89.  
  90. /*  server.on("/estilo.css", HTTP_GET, [](AsyncWebServerRequest * request) {
  91.     request->send(SPIFFS, "/estilo.css", "text/css");
  92.   });
  93.  
  94.   server.on("/javascript1.js", HTTP_GET, [](AsyncWebServerRequest * request) {
  95.     request->send(SPIFFS, "/javascript1.js", "text/javascript");
  96.   });
  97.  
  98. */
  99.  
  100.   server.begin();
  101.  
  102. }
  103.  
  104. void loop() {
  105.   mesh.update();
  106.   if(myIP != getlocalIP()){
  107.     myIP = getlocalIP();
  108.     Serial.println("My IP is " + myIP.toString());
  109.   }
  110. }
  111. //------------------------------------------------------------------
  112. void receivedCallback( const uint32_t &from, const String &msg ) {
  113.   Serial.printf("bridge: Received from %u msg=%s\n", from, msg.c_str());
  114. }
  115. //------------------------------------------------------------------
  116. IPAddress getlocalIP() {
  117.   return IPAddress(mesh.getStationIP());
  118. }
RAW Paste Data