Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //************************************************************
- // this is a simple example that uses the painlessMesh library to
- // connect to a another network and broadcast message from a webpage to the edges of the mesh network.
- // This sketch can be extended further using all the abilities of the AsyncWebserver library (WS, events, ...)
- // for more details
- // https://gitlab.com/painlessMesh/painlessMesh/wikis/bridge-between-mesh-and-another-network
- // for more details about my version
- // https://gitlab.com/Assassynv__V/painlessMesh
- // and for more details about the AsyncWebserver library
- // https://github.com/me-no-dev/ESPAsyncWebServer
- //************************************************************
- #include "IPAddress.h"
- #include "painlessMesh.h"
- #ifdef ESP8266
- #include "Hash.h"
- #include <ESPAsyncTCP.h>
- #else
- #include "SPIFFS.h"
- #include <AsyncTCP.h>
- #endif
- #include <ESPAsyncWebServer.h>
- #define MESH_PREFIX "whateverYouLike"
- #define MESH_PASSWORD "somethingSneaky"
- #define MESH_PORT 5555
- #define STATION_SSID "xxxxxxx"
- #define STATION_PASSWORD "yyyyyyyy"
- #define HOSTNAME "HTTP_BRIDGE"
- // Prototype
- void receivedCallback( const uint32_t &from, const String &msg );
- IPAddress getlocalIP();
- painlessMesh mesh;
- AsyncWebServer server(80);
- IPAddress myIP(0,0,0,0);
- IPAddress myAPIP(0,0,0,0);
- //------------------------------------------------------------------
- void setup() {
- Serial.begin(115200);
- if (!SPIFFS.begin()) {
- Serial.println("An Error has occurred while mounting SPIFFS");
- return;
- }
- //mesh.setDebugMsgTypes( ERROR | STARTUP | CONNECTION ); // set before init() so that you can see startup messages
- // Channel set to 6. Make sure to use the same channel for your mesh and for you other
- // network (STATION_SSID)
- mesh.init( MESH_PREFIX, MESH_PASSWORD, MESH_PORT, WIFI_AP_STA, 6 );
- mesh.onReceive(&receivedCallback);
- mesh.stationManual(STATION_SSID, STATION_PASSWORD);
- mesh.setHostname(HOSTNAME);
- myAPIP = IPAddress(mesh.getAPIP());
- Serial.println("My AP IP is " + myAPIP.toString());
- /*
- //Async webserver
- server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send(200, "text/html", "<form>Text to Broadcast<br><input type='text' name='BROADCAST'><br><br><input type='submit' value='Submit'></form>");
- if (request->hasArg("BROADCAST")){
- String msg = request->arg("BROADCAST");
- msg += mesh.getNodeId();
- // mesh.sendBroadcast(msg);
- uint32_t myId = 887631948;
- mesh.sendSingle(myId, msg);
- }
- });
- */
- server.on("/html", HTTP_GET, [](AsyncWebServerRequest * request) {
- request->send(SPIFFS, "/test.html", "text/html");
- if (request->hasArg("BROADCAST")){
- String msg = request->arg("BROADCAST");
- msg += mesh.getNodeId();
- uint32_t myId = 887631948;
- mesh.sendSingle(myId, msg);
- }
- });
- /* server.on("/estilo.css", HTTP_GET, [](AsyncWebServerRequest * request) {
- request->send(SPIFFS, "/estilo.css", "text/css");
- });
- server.on("/javascript1.js", HTTP_GET, [](AsyncWebServerRequest * request) {
- request->send(SPIFFS, "/javascript1.js", "text/javascript");
- });
- */
- server.begin();
- }
- void loop() {
- mesh.update();
- if(myIP != getlocalIP()){
- myIP = getlocalIP();
- Serial.println("My IP is " + myIP.toString());
- }
- }
- //------------------------------------------------------------------
- void receivedCallback( const uint32_t &from, const String &msg ) {
- Serial.printf("bridge: Received from %u msg=%s\n", from, msg.c_str());
- }
- //------------------------------------------------------------------
- IPAddress getlocalIP() {
- return IPAddress(mesh.getStationIP());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement