diff --git a/library.json b/library.json index 38d576d..ca75e54 100644 --- a/library.json +++ b/library.json @@ -1,7 +1,7 @@ { - "name": "sprocket-plugin-template", + "name": "sprocket-plugin-rcswitch", "keywords": "sprocket, plugin", - "description": "Template for a sprocket plugin", + "description": "Remote control RF switch plugin", "authors": { "name": "Patrick Balsiger", @@ -11,7 +11,7 @@ "repository": { "type": "git", - "url": "https://gitlab.com/wirelos/sprocket-plugin-template" + "url": "https://gitlab.com/wirelos/sprocket-plugin-rcswitch" }, "frameworks": "arduino", "platforms": "espressif8266, esp32", diff --git a/platformio.ini b/platformio.ini index 8fe8167..eda3064 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,11 +12,7 @@ lib_deps = TaskScheduler SPIFFS ArduinoJson - ArduinoOTA - ESPAsyncTCP - ESP8266mDNS - ESP Async WebServer - painlessMesh + rc-switch https://gitlab.com/wirelos/sprocket-lib.git#develop [env:basic] diff --git a/src/RcSwitchConfig.h b/src/RcSwitchConfig.h new file mode 100644 index 0000000..fe4f265 --- /dev/null +++ b/src/RcSwitchConfig.h @@ -0,0 +1,48 @@ +#ifndef __RC_SWITCH_CONFIG__ +#define __RC_SWITCH_CONFIG__ + +#include +#include "utils/print.h" + +using namespace std; +using namespace std::placeholders; + +struct RcSwitchConfig +{ + int txPin; +}; + +struct RcSwitchPayload +{ + const char *group; + const char *device; +}; + +struct RcSwitchPayloadJson : public RcSwitchPayload, public JsonStruct +{ + int verifyJsonObject(JsonObject &json) + { + return json.containsKey("group") + && json.containsKey("device") + && json.success(); + }; + void mapJsonObject(JsonObject &root) + { + root["group"] = group; + root["device"] = device; + } + void fromJsonObject(JsonObject &json) + { + if (!verifyJsonObject(json)) + { + Serial.println("ERROR: cannot parse JSON object"); + valid = 0; + return; + } + group = getAttr(json, "group", group); + device = getAttr(json, "device", device); + valid = 1; + }; +}; + +#endif \ No newline at end of file diff --git a/src/RcSwitchPlugin.cpp b/src/RcSwitchPlugin.cpp new file mode 100644 index 0000000..b9774ae --- /dev/null +++ b/src/RcSwitchPlugin.cpp @@ -0,0 +1,34 @@ +#include "RcSwitchPlugin.h" + +RcSwitchPlugin::RcSwitchPlugin(RcSwitchConfig cfg) +{ + config = cfg; + rcSwitch = new RCSwitch(); +} + +void RcSwitchPlugin::activate(Scheduler *scheduler) +{ + rcSwitch->enableTransmit(config.txPin); + subscribe("rf/switch/on", bind(&RcSwitchPlugin::switchOn, this, _1)); + subscribe("rf/switch/off", bind(&RcSwitchPlugin::switchOff, this, _1)); + PRINT_MSG(Serial, "RC", "plugin activated"); +} + +void RcSwitchPlugin::switchOn(String msg) +{ + Serial.println("Switch ON"); + RcSwitchPayloadJson payload; + payload.fromJsonString(msg); + if(payload.valid){ + rcSwitch->switchOn(payload.group, payload.device); + } +} +void RcSwitchPlugin::switchOff(String msg) +{ + Serial.println("Switch OFF"); + RcSwitchPayloadJson payload; + payload.fromJsonString(msg); + if(payload.valid){ + rcSwitch->switchOff(payload.group, payload.device); + } +} \ No newline at end of file diff --git a/src/RcSwitchPlugin.h b/src/RcSwitchPlugin.h new file mode 100644 index 0000000..9fa73d6 --- /dev/null +++ b/src/RcSwitchPlugin.h @@ -0,0 +1,27 @@ +#ifndef __RC_SWITCH_PLUGIN__ +#define __RC_SWITCH_PLUGIN__ + +#define _TASK_SLEEP_ON_IDLE_RUN +#define _TASK_STD_FUNCTION + +#include +#include +#include +#include "utils/print.h" + +using namespace std; +using namespace std::placeholders; + + +class RcSwitchPlugin : public Plugin +{ + public: + RCSwitch *rcSwitch; + RcSwitchConfig config; + RcSwitchPlugin(RcSwitchConfig cfg); + void activate(Scheduler *scheduler); + void switchOn(String msg); + void switchOff(String msg); +}; + +#endif \ No newline at end of file diff --git a/src/TemplatePlugin.cpp b/src/TemplatePlugin.cpp deleted file mode 100644 index d98fb20..0000000 --- a/src/TemplatePlugin.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "TemplatePlugin.h" - -TemplatePlugin::TemplatePlugin(TemplateConfig cfg) -{ - templateConfig = cfg; -} - -void TemplatePlugin::activate(Scheduler *scheduler) -{ - PRINT_MSG(Serial, "TEMPLATE", "plugin activated"); -} diff --git a/src/TemplatePlugin.h b/src/TemplatePlugin.h deleted file mode 100644 index a32c461..0000000 --- a/src/TemplatePlugin.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef __TEMPLATE_PLUGIN__ -#define __TEMPLATE_PLUGIN__ - -#define _TASK_SLEEP_ON_IDLE_RUN -#define _TASK_STD_FUNCTION - -#include -#include "utils/utils_print.h" - -using namespace std; -using namespace std::placeholders; - -struct TemplateConfig -{ - const char *var; -}; - -class TemplatePlugin : public Plugin -{ - public: - TemplateConfig templateConfig; - TemplatePlugin(TemplateConfig cfg); - void activate(Scheduler *scheduler); -}; - -#endif \ No newline at end of file diff --git a/src/examples/basic/main.cpp b/src/examples/basic/main.cpp index a545da9..242c5e3 100644 --- a/src/examples/basic/main.cpp +++ b/src/examples/basic/main.cpp @@ -1,26 +1,13 @@ #include "config.h" -#include "WiFiNet.h" #include "Sprocket.h" -#include "TemplatePlugin.h" +#include "RcSwitchPlugin.h" -WiFiNet *network; Sprocket *sprocket; void setup() { sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE}); - sprocket->addPlugin(new TemplatePlugin({"sprocket"})); - - network = new WiFiNet( - SPROCKET_MODE, - STATION_SSID, - STATION_PASSWORD, - AP_SSID, - AP_PASSWORD, - HOSTNAME, - CONNECT_TIMEOUT); - network->connect(); - + sprocket->addPlugin(new RcSwitchPlugin({D4})); sprocket->activate(); }