初始化提交
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
//************************************************************
|
||||
// this is a simple example that uses the painlessMesh library
|
||||
//
|
||||
// 1. sends a silly message to every node on the mesh at a random time between 1 and 5 seconds
|
||||
// 2. prints anything it receives to Serial.print
|
||||
//
|
||||
//
|
||||
//************************************************************
|
||||
#include "painlessMesh.h"
|
||||
|
||||
#define MESH_PREFIX "whateverYouLike"
|
||||
#define MESH_PASSWORD "somethingSneaky"
|
||||
#define MESH_PORT 5555
|
||||
|
||||
Scheduler userScheduler; // to control your personal task
|
||||
painlessMesh mesh;
|
||||
|
||||
// User stub
|
||||
void sendMessage() ; // Prototype so PlatformIO doesn't complain
|
||||
|
||||
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 ));
|
||||
}
|
||||
|
||||
// Needed for painless library
|
||||
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);
|
||||
|
||||
//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
|
||||
mesh.setDebugMsgTypes( ERROR | STARTUP ); // 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);
|
||||
mesh.onChangedConnections(&changedConnectionCallback);
|
||||
mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
|
||||
|
||||
userScheduler.addTask( taskSendMessage );
|
||||
taskSendMessage.enable();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// it will run the user scheduler as well
|
||||
mesh.update();
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
//************************************************************
|
||||
// this is a simple example that uses the painlessMesh library to
|
||||
// connect to a node on another network. Please see the WIKI on gitlab
|
||||
// for more details
|
||||
// https://gitlab.com/painlessMesh/painlessMesh/wikis/bridge-between-mesh-and-another-network
|
||||
//************************************************************
|
||||
#include "painlessMesh.h"
|
||||
|
||||
#define MESH_PREFIX "whateverYouLike"
|
||||
#define MESH_PASSWORD "somethingSneaky"
|
||||
#define MESH_PORT 5555
|
||||
|
||||
#define STATION_SSID "mySSID"
|
||||
#define STATION_PASSWORD "myPASSWORD"
|
||||
#define STATION_PORT 5555
|
||||
uint8_t station_ip[4] = {192,168,1,128}; // IP of the server
|
||||
|
||||
// prototypes
|
||||
void receivedCallback( uint32_t from, String &msg );
|
||||
|
||||
painlessMesh mesh;
|
||||
|
||||
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 );
|
||||
// Setup over the air update support
|
||||
mesh.initOTA("bridge");
|
||||
|
||||
mesh.stationManual(STATION_SSID, STATION_PASSWORD, STATION_PORT, station_ip);
|
||||
// Bridge node, should (in most cases) be a root node. See [the wiki](https://gitlab.com/painlessMesh/painlessMesh/wikis/Possible-challenges-in-mesh-formation) for some background
|
||||
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);
|
||||
|
||||
|
||||
mesh.onReceive(&receivedCallback);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
mesh.update();
|
||||
}
|
||||
|
||||
void receivedCallback( uint32_t from, String &msg ) {
|
||||
Serial.printf("bridge: Received from %u msg=%s\n", from, msg.c_str());
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
[platformio]
|
||||
src_dir = .
|
||||
lib_extra_dirs = .piolibdeps/, ../../
|
||||
|
||||
[env:nodemcuv2]
|
||||
platform = espressif8266
|
||||
board = nodemcuv2
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
ArduinoJson
|
||||
ESPAsyncTCP
|
||||
|
||||
[env:esp32]
|
||||
platform = espressif32
|
||||
board = esp32dev
|
||||
framework = arduino
|
||||
lib_deps = ArduinoJson
|
||||
TaskScheduler
|
||||
AsyncTCP
|
||||
@@ -0,0 +1,33 @@
|
||||
//************************************************************
|
||||
// this is a simple example that uses the painlessMesh library and echos any
|
||||
// messages it receives
|
||||
//
|
||||
//************************************************************
|
||||
#include "painlessMesh.h"
|
||||
|
||||
#define MESH_PREFIX "whateverYouLike"
|
||||
#define MESH_PASSWORD "somethingSneaky"
|
||||
#define MESH_PORT 5555
|
||||
|
||||
// Prototypes
|
||||
void receivedCallback( uint32_t from, String &msg );
|
||||
|
||||
painlessMesh mesh;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
mesh.setDebugMsgTypes( ERROR | STARTUP | CONNECTION ); // set before init() so that you can see startup messages
|
||||
|
||||
mesh.init( MESH_PREFIX, MESH_PASSWORD, MESH_PORT );
|
||||
mesh.onReceive(&receivedCallback);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
mesh.update();
|
||||
}
|
||||
|
||||
void receivedCallback( uint32_t from, String &msg ) {
|
||||
Serial.printf("echoNode: Received from %u msg=%s\n", from, msg.c_str());
|
||||
mesh.sendSingle(from, msg);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
[platformio]
|
||||
src_dir = .
|
||||
lib_extra_dirs = .piolibdeps/, ../../
|
||||
|
||||
[env:nodemcuv2]
|
||||
platform = espressif8266
|
||||
board = nodemcuv2
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
ArduinoJson
|
||||
# painlessMesh
|
||||
@@ -0,0 +1,94 @@
|
||||
//************************************************************
|
||||
// this is a simple example that uses the painlessMesh library to
|
||||
// setup a node that logs to a central logging node
|
||||
// The logServer example shows how to configure the central logging nodes
|
||||
//************************************************************
|
||||
#include "painlessMesh.h"
|
||||
|
||||
#define MESH_PREFIX "whateverYouLike"
|
||||
#define MESH_PASSWORD "somethingSneaky"
|
||||
#define MESH_PORT 5555
|
||||
|
||||
Scheduler userScheduler; // to control your personal task
|
||||
painlessMesh mesh;
|
||||
|
||||
// Prototype
|
||||
void receivedCallback( uint32_t from, String &msg );
|
||||
|
||||
size_t logServerId = 0;
|
||||
|
||||
// Send message to the logServer every 10 seconds
|
||||
Task myLoggingTask(10000, TASK_FOREVER, []() {
|
||||
#if ARDUINOJSON_VERSION_MAJOR==6
|
||||
DynamicJsonDocument jsonBuffer(1024);
|
||||
JsonObject msg = jsonBuffer.to<JsonObject>();
|
||||
#else
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& msg = jsonBuffer.createObject();
|
||||
#endif
|
||||
msg["topic"] = "sensor";
|
||||
msg["value"] = random(0, 180);
|
||||
|
||||
String str;
|
||||
#if ARDUINOJSON_VERSION_MAJOR==6
|
||||
serializeJson(msg, str);
|
||||
#else
|
||||
msg.printTo(str);
|
||||
#endif
|
||||
if (logServerId == 0) // If we don't know the logServer yet
|
||||
mesh.sendBroadcast(str);
|
||||
else
|
||||
mesh.sendSingle(logServerId, str);
|
||||
|
||||
// log to serial
|
||||
#if ARDUINOJSON_VERSION_MAJOR==6
|
||||
serializeJson(msg, Serial);
|
||||
#else
|
||||
msg.printTo(Serial);
|
||||
#endif
|
||||
Serial.printf("\n");
|
||||
});
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
mesh.setDebugMsgTypes( ERROR | STARTUP | CONNECTION ); // set before init() so that you can see startup messages
|
||||
|
||||
mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT, WIFI_AP_STA, 6 );
|
||||
mesh.onReceive(&receivedCallback);
|
||||
|
||||
// Add the task to the your scheduler
|
||||
userScheduler.addTask(myLoggingTask);
|
||||
myLoggingTask.enable();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// it will run the user scheduler as well
|
||||
mesh.update();
|
||||
}
|
||||
|
||||
void receivedCallback( uint32_t from, String &msg ) {
|
||||
Serial.printf("logClient: Received from %u msg=%s\n", from, msg.c_str());
|
||||
|
||||
// Saving logServer
|
||||
#if ARDUINOJSON_VERSION_MAJOR==6
|
||||
DynamicJsonDocument jsonBuffer(1024 + msg.length());
|
||||
DeserializationError error = deserializeJson(jsonBuffer, msg);
|
||||
if (error) {
|
||||
Serial.printf("DeserializationError\n");
|
||||
return;
|
||||
}
|
||||
JsonObject root = jsonBuffer.as<JsonObject>();
|
||||
#else
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& root = jsonBuffer.parseObject(msg);
|
||||
#endif
|
||||
if (root.containsKey("topic")) {
|
||||
if (String("logServer").equals(root["topic"].as<String>())) {
|
||||
// check for on: true or false
|
||||
logServerId = root["nodeId"];
|
||||
Serial.printf("logServer detected!!!\n");
|
||||
}
|
||||
Serial.printf("Handled from %u msg=%s\n", from, msg.c_str());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
[platformio]
|
||||
src_dir = .
|
||||
lib_extra_dirs = .piolibdeps/, ../../
|
||||
|
||||
[env:nodemcuv2]
|
||||
platform = espressif8266
|
||||
board = nodemcuv2
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
ArduinoJson
|
||||
# painlessMesh
|
||||
@@ -0,0 +1,78 @@
|
||||
//************************************************************
|
||||
// this is a simple example that uses the painlessMesh library to
|
||||
// setup a single node (this node) as a logging node
|
||||
// The logClient example shows how to configure the other nodes
|
||||
// to log to this server
|
||||
//************************************************************
|
||||
#include "painlessMesh.h"
|
||||
|
||||
#define MESH_PREFIX "whateverYouLike"
|
||||
#define MESH_PASSWORD "somethingSneaky"
|
||||
#define MESH_PORT 5555
|
||||
|
||||
Scheduler userScheduler; // to control your personal task
|
||||
painlessMesh mesh;
|
||||
// Prototype
|
||||
void receivedCallback( uint32_t from, String &msg );
|
||||
|
||||
|
||||
// Send my ID every 10 seconds to inform others
|
||||
Task logServerTask(10000, TASK_FOREVER, []() {
|
||||
#if ARDUINOJSON_VERSION_MAJOR==6
|
||||
DynamicJsonDocument jsonBuffer(1024);
|
||||
JsonObject msg = jsonBuffer.to<JsonObject>();
|
||||
#else
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& msg = jsonBuffer.createObject();
|
||||
#endif
|
||||
msg["topic"] = "logServer";
|
||||
msg["nodeId"] = mesh.getNodeId();
|
||||
|
||||
String str;
|
||||
#if ARDUINOJSON_VERSION_MAJOR==6
|
||||
serializeJson(msg, str);
|
||||
#else
|
||||
msg.printTo(str);
|
||||
#endif
|
||||
mesh.sendBroadcast(str);
|
||||
|
||||
// log to serial
|
||||
#if ARDUINOJSON_VERSION_MAJOR==6
|
||||
serializeJson(msg, Serial);
|
||||
#else
|
||||
msg.printTo(Serial);
|
||||
#endif
|
||||
Serial.printf("\n");
|
||||
});
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE | DEBUG ); // all types on
|
||||
//mesh.setDebugMsgTypes( ERROR | CONNECTION | SYNC | S_TIME ); // set before init() so that you can see startup messages
|
||||
mesh.setDebugMsgTypes( ERROR | CONNECTION | S_TIME ); // set before init() so that you can see startup messages
|
||||
|
||||
mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT, WIFI_AP_STA, 6 );
|
||||
mesh.onReceive(&receivedCallback);
|
||||
|
||||
mesh.onNewConnection([](size_t nodeId) {
|
||||
Serial.printf("New Connection %u\n", nodeId);
|
||||
});
|
||||
|
||||
mesh.onDroppedConnection([](size_t nodeId) {
|
||||
Serial.printf("Dropped Connection %u\n", nodeId);
|
||||
});
|
||||
|
||||
// Add the task to the your scheduler
|
||||
userScheduler.addTask(logServerTask);
|
||||
logServerTask.enable();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// it will run the user scheduler as well
|
||||
mesh.update();
|
||||
}
|
||||
|
||||
void receivedCallback( uint32_t from, String &msg ) {
|
||||
Serial.printf("logServer: Received from %u msg=%s\n", from, msg.c_str());
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
[platformio]
|
||||
src_dir = .
|
||||
lib_extra_dirs = .piolibdeps/, ../../
|
||||
|
||||
[env:nodemcuv2]
|
||||
platform = espressif8266
|
||||
board = nodemcuv2
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
ArduinoJson
|
||||
# painlessMesh
|
||||
@@ -0,0 +1,118 @@
|
||||
//************************************************************
|
||||
// this is a simple example that uses the painlessMesh library to
|
||||
// connect to a another network and relay messages from a MQTT broker to the nodes of the mesh network.
|
||||
// To send a message to a mesh node, you can publish it to "painlessMesh/to/12345678" where 12345678 equals the nodeId.
|
||||
// To broadcast a message to all nodes in the mesh you can publish it to "painlessMesh/to/broadcast".
|
||||
// When you publish "getNodes" to "painlessMesh/to/gateway" you receive the mesh topology as JSON
|
||||
// Every message from the mesh which is send to the gateway node will be published to "painlessMesh/from/12345678" where 12345678
|
||||
// is the nodeId from which the packet was send.
|
||||
//************************************************************
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <painlessMesh.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <WiFiClient.h>
|
||||
|
||||
#define MESH_PREFIX "whateverYouLike"
|
||||
#define MESH_PASSWORD "somethingSneaky"
|
||||
#define MESH_PORT 5555
|
||||
|
||||
#define STATION_SSID "YourAP_SSID"
|
||||
#define STATION_PASSWORD "YourAP_PWD"
|
||||
|
||||
#define HOSTNAME "MQTT_Bridge"
|
||||
|
||||
// Prototypes
|
||||
void receivedCallback( const uint32_t &from, const String &msg );
|
||||
void mqttCallback(char* topic, byte* payload, unsigned int length);
|
||||
|
||||
IPAddress getlocalIP();
|
||||
|
||||
IPAddress myIP(0,0,0,0);
|
||||
IPAddress mqttBroker(192, 168, 1, 1);
|
||||
|
||||
painlessMesh mesh;
|
||||
WiFiClient wifiClient;
|
||||
PubSubClient mqttClient(mqttBroker, 1883, mqttCallback, wifiClient);
|
||||
|
||||
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. See [the wiki](https://gitlab.com/painlessMesh/painlessMesh/wikis/Possible-challenges-in-mesh-formation) for some background
|
||||
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);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
mesh.update();
|
||||
mqttClient.loop();
|
||||
|
||||
if(myIP != getlocalIP()){
|
||||
myIP = getlocalIP();
|
||||
Serial.println("My IP is " + myIP.toString());
|
||||
|
||||
if (mqttClient.connect("painlessMeshClient")) {
|
||||
mqttClient.publish("painlessMesh/from/gateway","Ready!");
|
||||
mqttClient.subscribe("painlessMesh/to/#");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void receivedCallback( const uint32_t &from, const String &msg ) {
|
||||
Serial.printf("bridge: Received from %u msg=%s\n", from, msg.c_str());
|
||||
String topic = "painlessMesh/from/" + String(from);
|
||||
mqttClient.publish(topic.c_str(), msg.c_str());
|
||||
}
|
||||
|
||||
void mqttCallback(char* topic, uint8_t* payload, unsigned int length) {
|
||||
char* cleanPayload = (char*)malloc(length+1);
|
||||
payload[length] = '\0';
|
||||
memcpy(cleanPayload, payload, length+1);
|
||||
String msg = String(cleanPayload);
|
||||
free(cleanPayload);
|
||||
|
||||
String targetStr = String(topic).substring(16);
|
||||
|
||||
if(targetStr == "gateway")
|
||||
{
|
||||
if(msg == "getNodes")
|
||||
{
|
||||
auto nodes = mesh.getNodeList(true);
|
||||
String str;
|
||||
for (auto &&id : nodes)
|
||||
str += String(id) + String(" ");
|
||||
mqttClient.publish("painlessMesh/from/gateway", str.c_str());
|
||||
}
|
||||
}
|
||||
else if(targetStr == "broadcast")
|
||||
{
|
||||
mesh.sendBroadcast(msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32_t target = strtoul(targetStr.c_str(), NULL, 10);
|
||||
if(mesh.isConnected(target))
|
||||
{
|
||||
mesh.sendSingle(target, msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
mqttClient.publish("painlessMesh/from/gateway", "Client not connected!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
IPAddress getlocalIP() {
|
||||
return IPAddress(mesh.getStationIP());
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
[platformio]
|
||||
src_dir = .
|
||||
lib_extra_dirs = .piolibdeps/, ../../
|
||||
|
||||
[env:nodemcuv2]
|
||||
platform = espressif8266
|
||||
board = nodemcuv2
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
PubSubClient
|
||||
# painlessMesh
|
||||
@@ -0,0 +1,119 @@
|
||||
#include<map>
|
||||
|
||||
#include "painlessMesh.h"
|
||||
using namespace painlessmesh;
|
||||
|
||||
typedef std::function<void(String &from, String &msg)> namedReceivedCallback_t;
|
||||
|
||||
class namedMesh : public painlessMesh {
|
||||
public:
|
||||
namedMesh() {
|
||||
auto cb = [this](uint32_t from, String &msg) {
|
||||
// Try to parse it.. Need to test it with non json function
|
||||
#if ARDUINOJSON_VERSION_MAJOR==6
|
||||
DynamicJsonDocument jsonBuffer(1024 + msg.length());
|
||||
deserializeJson(jsonBuffer, msg);
|
||||
JsonObject root = jsonBuffer.as<JsonObject>();
|
||||
#else
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject &root = jsonBuffer.parseObject(msg);
|
||||
#endif
|
||||
if (root.containsKey("topic") &&
|
||||
String("nameBroadCast").equals(root["topic"].as<String>())) {
|
||||
nameMap[from] = root["name"].as<String>();
|
||||
} else {
|
||||
if (userReceivedCallback)
|
||||
// If needed send it on to userReceivedCallback
|
||||
userReceivedCallback(from, msg);
|
||||
if (userNamedReceivedCallback) {
|
||||
String name;
|
||||
// If needed look up name and send it on to
|
||||
// userNamedReceivedCallback
|
||||
if (nameMap.count(from) > 0) {
|
||||
name = nameMap[from];
|
||||
} else {
|
||||
name = String(from);
|
||||
}
|
||||
userNamedReceivedCallback(name, msg);
|
||||
}
|
||||
}
|
||||
};
|
||||
painlessMesh::onReceive(cb);
|
||||
changedConnectionCallbacks.push_back([this](uint32_t id) {
|
||||
if (nameBroadCastTask.isEnabled())
|
||||
nameBroadCastTask.forceNextIteration();
|
||||
});
|
||||
}
|
||||
|
||||
String getName() {
|
||||
return nodeName;
|
||||
}
|
||||
|
||||
void setName(String &name) {
|
||||
nodeName = name;
|
||||
// Start broadcast task if not done yet
|
||||
if (!nameBroadCastInit) {
|
||||
// Initialize
|
||||
nameBroadCastTask.set(5*TASK_MINUTE, TASK_FOREVER,
|
||||
[this]() {
|
||||
String msg;
|
||||
// Create arduinoJson msg
|
||||
#if ARDUINOJSON_VERSION_MAJOR==6
|
||||
DynamicJsonDocument jsonBuffer(1024);
|
||||
JsonObject root = jsonBuffer.to<JsonObject>();
|
||||
root["topic"] = "nameBroadCast";
|
||||
root["name"] = this->getName();
|
||||
serializeJson(root, msg);
|
||||
#else
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& root = jsonBuffer.createObject();
|
||||
root["topic"] = "nameBroadCast";
|
||||
root["name"] = this->getName();
|
||||
root.printTo(msg);
|
||||
#endif
|
||||
this->sendBroadcast(msg);
|
||||
}
|
||||
);
|
||||
// Add it
|
||||
mScheduler->addTask(nameBroadCastTask);
|
||||
nameBroadCastTask.enableDelayed();
|
||||
|
||||
nameBroadCastInit = true;
|
||||
}
|
||||
nameBroadCastTask.forceNextIteration();
|
||||
}
|
||||
|
||||
using painlessMesh::sendSingle;
|
||||
bool sendSingle(String &name, String &msg) {
|
||||
// Look up name
|
||||
for (auto && pr : nameMap) {
|
||||
if (name.equals(pr.second)) {
|
||||
uint32_t to = pr.first;
|
||||
return painlessMesh::sendSingle(to, msg);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual void stop() {
|
||||
nameBroadCastTask.disable();
|
||||
mScheduler->deleteTask(nameBroadCastTask);
|
||||
painlessMesh::stop();
|
||||
}
|
||||
|
||||
virtual void onReceive(receivedCallback_t onReceive) {
|
||||
userReceivedCallback = onReceive;
|
||||
}
|
||||
void onReceive(namedReceivedCallback_t onReceive) {
|
||||
userNamedReceivedCallback = onReceive;
|
||||
}
|
||||
protected:
|
||||
String nodeName;
|
||||
std::map<uint32_t, String> nameMap;
|
||||
|
||||
receivedCallback_t userReceivedCallback;
|
||||
namedReceivedCallback_t userNamedReceivedCallback;
|
||||
|
||||
bool nameBroadCastInit = false;
|
||||
Task nameBroadCastTask;
|
||||
};
|
||||
@@ -0,0 +1,52 @@
|
||||
//************************************************************
|
||||
// this is a simple example that uses the painlessMesh library
|
||||
//
|
||||
// This example shows how to build a mesh with named nodes
|
||||
//
|
||||
//************************************************************
|
||||
#include "namedMesh.h"
|
||||
|
||||
#define MESH_SSID "whateverYouLike"
|
||||
#define MESH_PASSWORD "somethingSneaky"
|
||||
#define MESH_PORT 5555
|
||||
|
||||
Scheduler userScheduler; // to control your personal task
|
||||
namedMesh mesh;
|
||||
|
||||
String nodeName = "logNode"; // Name needs to be unique
|
||||
|
||||
Task taskSendMessage( TASK_SECOND*30, TASK_FOREVER, []() {
|
||||
String msg = String("This is a message from: ") + nodeName + String(" for logNode");
|
||||
String to = "logNode";
|
||||
mesh.sendSingle(to, msg);
|
||||
}); // start with a one second interval
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
mesh.setDebugMsgTypes(ERROR | DEBUG | CONNECTION); // set before init() so that you can see startup messages
|
||||
|
||||
mesh.init(MESH_SSID, MESH_PASSWORD, &userScheduler, MESH_PORT);
|
||||
|
||||
mesh.setName(nodeName); // This needs to be an unique name!
|
||||
|
||||
mesh.onReceive([](uint32_t from, String &msg) {
|
||||
Serial.printf("Received message by id from: %u, %s\n", from, msg.c_str());
|
||||
});
|
||||
|
||||
mesh.onReceive([](String &from, String &msg) {
|
||||
Serial.printf("Received message by name from: %s, %s\n", from.c_str(), msg.c_str());
|
||||
});
|
||||
|
||||
mesh.onChangedConnections([]() {
|
||||
Serial.printf("Changed connection\n");
|
||||
});
|
||||
|
||||
userScheduler.addTask(taskSendMessage);
|
||||
taskSendMessage.enable();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// it will run the user scheduler as well
|
||||
mesh.update();
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
[platformio]
|
||||
src_dir = .
|
||||
lib_extra_dirs = .piolibdeps/, ../../
|
||||
|
||||
[env:nodemcuv2]
|
||||
platform = espressif8266
|
||||
board = nodemcuv2
|
||||
framework = arduino
|
||||
lib_deps = ArduinoJson
|
||||
TaskScheduler
|
||||
ESPAsyncTCP
|
||||
|
||||
[env:esp32]
|
||||
platform = espressif32
|
||||
board = esp32dev
|
||||
framework = arduino
|
||||
lib_deps = ArduinoJson
|
||||
arduinoUnity
|
||||
TaskScheduler
|
||||
AsyncTCP
|
||||
@@ -0,0 +1,20 @@
|
||||
[platformio]
|
||||
src_dir = .
|
||||
lib_extra_dirs = .piolibdeps/, ../../
|
||||
|
||||
[env:nodemcuv2]
|
||||
platform = espressif8266
|
||||
board = nodemcuv2
|
||||
framework = arduino
|
||||
lib_deps = ArduinoJson
|
||||
TaskScheduler
|
||||
ESPAsyncTCP
|
||||
|
||||
[env:esp32]
|
||||
platform = espressif32
|
||||
board = esp32dev
|
||||
framework = arduino
|
||||
lib_deps = ArduinoJson
|
||||
arduinoUnity
|
||||
TaskScheduler
|
||||
AsyncTCP
|
||||
@@ -0,0 +1,153 @@
|
||||
//************************************************************
|
||||
// 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 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
|
||||
|
||||
// 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 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);
|
||||
|
||||
mesh.setDebugMsgTypes(ERROR | DEBUG); // set before init() so that you can see error 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() {
|
||||
mesh.update();
|
||||
digitalWrite(LED, !onFlag);
|
||||
}
|
||||
|
||||
void sendMessage() {
|
||||
String msg = "Hello from node ";
|
||||
msg += mesh.getNodeId();
|
||||
msg += " myFreeMemory: " + String(ESP.getFreeHeap());
|
||||
mesh.sendBroadcast(msg);
|
||||
|
||||
if (calc_delay) {
|
||||
SimpleList<uint32_t>::iterator node = nodes.begin();
|
||||
while (node != nodes.end()) {
|
||||
mesh.startDelayMeas(*node);
|
||||
node++;
|
||||
}
|
||||
calc_delay = false;
|
||||
}
|
||||
|
||||
Serial.printf("Sending message: %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: Received from %u msg=%s\n", from, msg.c_str());
|
||||
}
|
||||
|
||||
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);
|
||||
Serial.printf("--> startHere: New Connection, %s\n", mesh.subConnectionJson(true).c_str());
|
||||
}
|
||||
|
||||
void changedConnectionCallback() {
|
||||
Serial.printf("Changed connections\n");
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
[platformio]
|
||||
src_dir = .
|
||||
lib_extra_dirs = .piolibdeps/, ../../
|
||||
|
||||
[env:nodemcuv2]
|
||||
platform = espressif8266
|
||||
board = nodemcuv2
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
ESPAsyncTCP
|
||||
ESPAsyncWebServer
|
||||
ArduinoJson
|
||||
|
||||
[env:esp32]
|
||||
platform = espressif32
|
||||
board = esp32doit-devkit-v1
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
AsyncTCP
|
||||
AsyncWebServer
|
||||
ArduinoJson
|
||||
@@ -0,0 +1,89 @@
|
||||
//************************************************************
|
||||
// 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 <AsyncTCP.h>
|
||||
#endif
|
||||
#include <ESPAsyncWebServer.h>
|
||||
|
||||
#define MESH_PREFIX "whateverYouLike"
|
||||
#define MESH_PASSWORD "somethingSneaky"
|
||||
#define MESH_PORT 5555
|
||||
|
||||
#define STATION_SSID "mySSID"
|
||||
#define STATION_PASSWORD "myPASSWORD"
|
||||
|
||||
#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. See [the wiki](https://gitlab.com/painlessMesh/painlessMesh/wikis/Possible-challenges-in-mesh-formation) for some background
|
||||
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());
|
||||
}
|
||||
Reference in New Issue
Block a user