Add control from JSON payload
This commit is contained in:
parent
eacb06c0ae
commit
44b91c86f2
4 changed files with 48 additions and 25 deletions
|
@ -28,3 +28,6 @@ monitor_port = /dev/ttyUSB0
|
||||||
; upload_port = /dev/ttyUSB0
|
; upload_port = /dev/ttyUSB0
|
||||||
; monitor_speed = 115200
|
; monitor_speed = 115200
|
||||||
; monitor_port = /dev/ttyUSB0
|
; monitor_port = /dev/ttyUSB0
|
||||||
|
|
||||||
|
lib_deps =
|
||||||
|
bblanchon/ArduinoJson @ ^6.18.3
|
||||||
|
|
|
@ -1,36 +1,17 @@
|
||||||
#include "LED.cpp"
|
#include "JsonLightControl.cpp"
|
||||||
#include <BLEUtils.h>
|
#include <BLEUtils.h>
|
||||||
|
|
||||||
class BluetoothLEDCallback : public BLECharacteristicCallbacks {
|
class BluetoothLEDCallback : public BLECharacteristicCallbacks {
|
||||||
LED *_seatingLeds, *_kitchenLeds;
|
JsonLightControl *_lightControl;
|
||||||
|
|
||||||
void onWrite(BLECharacteristic *characteristic) {
|
void onWrite(BLECharacteristic *characteristic) {
|
||||||
std::string value = characteristic->getValue();
|
std::string value = characteristic->getValue();
|
||||||
if (value.length() > 0) {
|
_lightControl->action(value);
|
||||||
Serial.println(value.c_str());
|
|
||||||
if (value == "all on"){
|
|
||||||
_seatingLeds->on();
|
|
||||||
_kitchenLeds->on();
|
|
||||||
}
|
|
||||||
if (value == "all off"){
|
|
||||||
_seatingLeds->off();
|
|
||||||
_kitchenLeds->off();
|
|
||||||
}
|
|
||||||
if (value == "seating on")
|
|
||||||
_seatingLeds->on();
|
|
||||||
if (value == "kitchen on")
|
|
||||||
_kitchenLeds->on();
|
|
||||||
if (value == "seating off")
|
|
||||||
_seatingLeds->off();
|
|
||||||
if (value == "kitchen off")
|
|
||||||
_kitchenLeds->off();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
BluetoothLEDCallback(LED *seatingLeds, LED *kitchenLeds){
|
BluetoothLEDCallback(JsonLightControl *lightControl){
|
||||||
_seatingLeds = seatingLeds;
|
_lightControl = lightControl;
|
||||||
_kitchenLeds = kitchenLeds;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
35
src/JsonLightControl.cpp
Normal file
35
src/JsonLightControl.cpp
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
#include "LED.cpp"
|
||||||
|
#include <map>
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
|
class JsonLightControl{
|
||||||
|
std::map<const char*, LED*> _leds;
|
||||||
|
|
||||||
|
public:
|
||||||
|
void registerLEDs(const char *name, LED *leds){
|
||||||
|
_leds[name] = leds;
|
||||||
|
}
|
||||||
|
|
||||||
|
void action(std::string input){
|
||||||
|
if (input.length() <= 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Serial.println(input.c_str());
|
||||||
|
|
||||||
|
DynamicJsonDocument document(256);
|
||||||
|
deserializeJson(document, input.c_str());
|
||||||
|
|
||||||
|
auto lights = document["lights"];
|
||||||
|
Serial.println(lights.size());
|
||||||
|
auto on = document["on"].as<bool>();
|
||||||
|
for (int i = 0; i < lights.size(); i++)
|
||||||
|
{
|
||||||
|
auto name = lights[i].as<const char*>();
|
||||||
|
auto led = _leds.find(name)->second;
|
||||||
|
if (on)
|
||||||
|
led->on();
|
||||||
|
else
|
||||||
|
led->off();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
|
@ -11,7 +11,8 @@ LED _seatingLeds(&_seatingLedOutput, FADE_IN_DURATION, FADE_OUT_DURATION);
|
||||||
LEDOutput _kitchenLedOutput(1 , true);
|
LEDOutput _kitchenLedOutput(1 , true);
|
||||||
LED _kitchenLeds(&_kitchenLedOutput, FADE_IN_DURATION, FADE_OUT_DURATION);
|
LED _kitchenLeds(&_kitchenLedOutput, FADE_IN_DURATION, FADE_OUT_DURATION);
|
||||||
|
|
||||||
BluetoothLEDCallback _btCallback(&_seatingLeds, &_kitchenLeds);
|
JsonLightControl _jsonLightControl;
|
||||||
|
BluetoothLEDCallback _btCallback(&_jsonLightControl);
|
||||||
BluetoothService _btService("Van Lights", &_btCallback);
|
BluetoothService _btService("Van Lights", &_btCallback);
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
@ -23,6 +24,9 @@ void setup() {
|
||||||
_kitchenLedOutput.attach(18);
|
_kitchenLedOutput.attach(18);
|
||||||
_kitchenLedOutput.attach(23);
|
_kitchenLedOutput.attach(23);
|
||||||
|
|
||||||
|
_jsonLightControl.registerLEDs("seating", &_seatingLeds);
|
||||||
|
_jsonLightControl.registerLEDs("kitchen", &_kitchenLeds);
|
||||||
|
|
||||||
_btService.init();
|
_btService.init();
|
||||||
_btService.start();
|
_btService.start();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue