mirror of
https://gitlab.com/wirelos/sprocket-plugin-rcswitch.git
synced 2025-12-14 22:02:22 +01:00
rf switch plugin
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "sprocket-plugin-template",
|
"name": "sprocket-plugin-rcswitch",
|
||||||
"keywords": "sprocket, plugin",
|
"keywords": "sprocket, plugin",
|
||||||
"description": "Template for a sprocket plugin",
|
"description": "Remote control RF switch plugin",
|
||||||
"authors":
|
"authors":
|
||||||
{
|
{
|
||||||
"name": "Patrick Balsiger",
|
"name": "Patrick Balsiger",
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
"repository":
|
"repository":
|
||||||
{
|
{
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://gitlab.com/wirelos/sprocket-plugin-template"
|
"url": "https://gitlab.com/wirelos/sprocket-plugin-rcswitch"
|
||||||
},
|
},
|
||||||
"frameworks": "arduino",
|
"frameworks": "arduino",
|
||||||
"platforms": "espressif8266, esp32",
|
"platforms": "espressif8266, esp32",
|
||||||
|
|||||||
@@ -12,11 +12,7 @@ lib_deps =
|
|||||||
TaskScheduler
|
TaskScheduler
|
||||||
SPIFFS
|
SPIFFS
|
||||||
ArduinoJson
|
ArduinoJson
|
||||||
ArduinoOTA
|
rc-switch
|
||||||
ESPAsyncTCP
|
|
||||||
ESP8266mDNS
|
|
||||||
ESP Async WebServer
|
|
||||||
painlessMesh
|
|
||||||
https://gitlab.com/wirelos/sprocket-lib.git#develop
|
https://gitlab.com/wirelos/sprocket-lib.git#develop
|
||||||
|
|
||||||
[env:basic]
|
[env:basic]
|
||||||
|
|||||||
48
src/RcSwitchConfig.h
Normal file
48
src/RcSwitchConfig.h
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
#ifndef __RC_SWITCH_CONFIG__
|
||||||
|
#define __RC_SWITCH_CONFIG__
|
||||||
|
|
||||||
|
#include <JsonStruct.h>
|
||||||
|
#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
|
||||||
34
src/RcSwitchPlugin.cpp
Normal file
34
src/RcSwitchPlugin.cpp
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
27
src/RcSwitchPlugin.h
Normal file
27
src/RcSwitchPlugin.h
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#ifndef __RC_SWITCH_PLUGIN__
|
||||||
|
#define __RC_SWITCH_PLUGIN__
|
||||||
|
|
||||||
|
#define _TASK_SLEEP_ON_IDLE_RUN
|
||||||
|
#define _TASK_STD_FUNCTION
|
||||||
|
|
||||||
|
#include <Plugin.h>
|
||||||
|
#include <RcSwitchConfig.h>
|
||||||
|
#include <RCSwitch.h>
|
||||||
|
#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
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
#include "TemplatePlugin.h"
|
|
||||||
|
|
||||||
TemplatePlugin::TemplatePlugin(TemplateConfig cfg)
|
|
||||||
{
|
|
||||||
templateConfig = cfg;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TemplatePlugin::activate(Scheduler *scheduler)
|
|
||||||
{
|
|
||||||
PRINT_MSG(Serial, "TEMPLATE", "plugin activated");
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
#ifndef __TEMPLATE_PLUGIN__
|
|
||||||
#define __TEMPLATE_PLUGIN__
|
|
||||||
|
|
||||||
#define _TASK_SLEEP_ON_IDLE_RUN
|
|
||||||
#define _TASK_STD_FUNCTION
|
|
||||||
|
|
||||||
#include <Plugin.h>
|
|
||||||
#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
|
|
||||||
@@ -1,26 +1,13 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "WiFiNet.h"
|
|
||||||
#include "Sprocket.h"
|
#include "Sprocket.h"
|
||||||
#include "TemplatePlugin.h"
|
#include "RcSwitchPlugin.h"
|
||||||
|
|
||||||
WiFiNet *network;
|
|
||||||
Sprocket *sprocket;
|
Sprocket *sprocket;
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE});
|
sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE});
|
||||||
sprocket->addPlugin(new TemplatePlugin({"sprocket"}));
|
sprocket->addPlugin(new RcSwitchPlugin({D4}));
|
||||||
|
|
||||||
network = new WiFiNet(
|
|
||||||
SPROCKET_MODE,
|
|
||||||
STATION_SSID,
|
|
||||||
STATION_PASSWORD,
|
|
||||||
AP_SSID,
|
|
||||||
AP_PASSWORD,
|
|
||||||
HOSTNAME,
|
|
||||||
CONNECT_TIMEOUT);
|
|
||||||
network->connect();
|
|
||||||
|
|
||||||
sprocket->activate();
|
sprocket->activate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user