add neopixel example

This commit is contained in:
2018-08-29 10:54:22 +02:00
parent 74e9b8df4f
commit 945bf78459
11 changed files with 676 additions and 1 deletions

View File

@@ -0,0 +1,67 @@
#ifndef __MESH_APP__
#define __MESH_APP__
#include <painlessMesh.h>
#include <base/MeshSprocket.h>
#include <MeshNet.h>
#include "config.h"
#include "NeoPattern.cpp"
#include "NeoPatternState.cpp"
#include "NeoPattern_api_json.h"
#include "NeoPattern_api_modes.cpp"
#include "utils_print.h"
using namespace std;
using namespace std::placeholders;
class MeshPixel : public MeshSprocket {
public:
NeoPixelConfig pixelConfig;
NeoPattern* pixels;
NeoPatternState state;
Task animation;
MeshPixel(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg, NeoPixelConfig pixelCfg) : MeshSprocket(cfg, otaCfg, webCfg) {
pixelConfig = pixelCfg;
pixels = new NeoPattern(pixelCfg.length, pixelCfg.pin, NEO_GRB + NEO_KHZ800);
pixels->begin();
pixels->setBrightness(pixelCfg.brightness);
pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelCfg.updateInterval);
}
Sprocket* activate(Scheduler* scheduler, Network* network) {
// call parent method that enables dispatching and plugins
MeshSprocket::activate(scheduler, network);
net->mesh.onNewConnection(bind(&MeshPixel::newConnection,this, _1));
net->mesh.onChangedConnections(bind(&MeshPixel::connectionChanged,this));
// pixel task
animation.set(TASK_MILLISECOND * pixelConfig.updateInterval, TASK_FOREVER, bind(&MeshPixel::animate, this, pixels));
addTask(animation);
return this;
} using MeshSprocket::activate;
void animate(NeoPattern* pixels){
pixels->Update();
}
void onMessage( uint32_t from, String &msg ) {
PRINT_MSG(Serial, SPROCKET_TYPE, "msg from %u = %s\n", from, msg.c_str());
state.fromJsonString(msg);
PIXEL_FNCS[state.mode](pixels, state.valueStr);
}
void newConnection(uint32_t nodeId){
PRINT_MSG(Serial, SPROCKET_TYPE, "connected to %u", nodeId);
pixels->ActivePattern = NONE;
pixels->ColorSet(pixels->Wheel(COLOR_CONNECTED));
}
void connectionChanged(){
if(!net->mesh.getNodeList().size()){
pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelConfig.updateInterval);
}
}
};
#endif

View File

@@ -0,0 +1,7 @@
# Mesh Sprocket Example
## OTA
mosquitto_sub -h citadel.lan -p 1883 -v -t '#'
Enable OTA:
mosquitto_pub -h citadel.lan -p 1883 -t '/down/wirelos/gateway' -m '{"target":"broadcast", "domain": "wirelos", "msg": {"target":"broadcast", "type": 3, msg: "OTA"} }'

View File

@@ -0,0 +1,43 @@
#ifndef __MESH_CONFIG__
#define __MESH_CONFIG__
// Scheduler config
#define _TASK_SLEEP_ON_IDLE_RUN
#define _TASK_STD_FUNCTION
// Chip config
#define SPROCKET_TYPE "MeshPixel"
#define SERIAL_BAUD_RATE 115200
#define STARTUP_DELAY 3000
// Mesh config
#define SPROCKET_MODE 0
#define WIFI_CHANNEL 11
#define MESH_PORT 5555
#define MESH_PREFIX "whateverYouLike"
#define MESH_PASSWORD "somethingSneaky"
#define STATION_SSID "Th1ngs4p"
#define STATION_PASSWORD "th3r31sn0sp00n"
#define HOSTNAME "mesh-node"
#define MESH_DEBUG_TYPES ERROR | STARTUP | CONNECTION
//ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE
// OTA config
#define OTA_PORT 8266
#define OTA_PASSWORD ""
// WebServer
#define WEB_CONTEXT_PATH "/"
#define WEB_DOC_ROOT "/www"
#define WEB_DEFAULT_FILE "index.html"
// NeoPixel
#define LED_STRIP_PIN D8
#define LED_STRIP_LENGTH 24
#define LED_STRIP_BRIGHTNESS 12
#define LED_STRIP_UPDATE_INTERVAL 100
#define LED_STRIP_DEFAULT_COLOR 100
#define COLOR_CONNECTED LED_STRIP_DEFAULT_COLOR
#define COLOR_NOT_CONNECTED 254
#endif

View File

@@ -0,0 +1,25 @@
#include "config.h"
#include "MeshNet.h"
#include "MeshPixel.h"
MeshNet net({
SPROCKET_MODE, WIFI_CHANNEL,
MESH_PORT, MESH_PREFIX, MESH_PASSWORD,
STATION_SSID, STATION_PASSWORD, HOSTNAME,
MESH_DEBUG_TYPES
});
MeshPixel sprocket(
{ STARTUP_DELAY, SERIAL_BAUD_RATE },
{ OTA_PORT, OTA_PASSWORD },
{ WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE },
{ LED_STRIP_PIN, LED_STRIP_LENGTH, LED_STRIP_BRIGHTNESS, LED_STRIP_UPDATE_INTERVAL, LED_STRIP_DEFAULT_COLOR }
);
void setup() {
sprocket.join(net);
}
void loop() {
sprocket.loop();
yield();
}