Skip to content

Instantly share code, notes, and snippets.

@siddharthdeore
Last active October 19, 2019 15:18
Show Gist options
  • Select an option

  • Save siddharthdeore/aa3fa5080ba7fb081bcb0507f26b0d1a to your computer and use it in GitHub Desktop.

Select an option

Save siddharthdeore/aa3fa5080ba7fb081bcb0507f26b0d1a to your computer and use it in GitHub Desktop.
// sample web server node
#include "IPAddress.h"
#include "painlessMesh.h"
#ifdef ESP8266
#include "Hash.h"
#include <ESPAsyncTCP.h>
#else
#include <AsyncTCP.h>
#endif
#include <ESPAsyncWebServer.h>
#define MESH_PREFIX "wave-fi"
#define MESH_PASSWORD "wave-fi"
#define MESH_PORT 5555
#define STATION_SSID "no-way"
#define STATION_PASSWORD "no-way"
#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);
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);
// Bridge node, should (in most cases) be a root node.
mesh.setRoot(true);
// This node and all other nodes should ideally know the mesh contains a root, so call this on all nodes
mesh.setContainsRoot(true);
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");
mesh.sendBroadcast(msg);
}
});
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());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment