From 526181d7ac6e59ca4544e64fc84316662a102349 Mon Sep 17 00:00:00 2001 From: Amal Abeygunawardana Date: Wed, 9 Dec 2020 16:15:56 +1100 Subject: [PATCH] Add command support using built-in LED --- include/Commands.h | 36 +++++++++++++++++++++ platformio.ini | 4 ++- src/Commands.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 31 +++++++++++++++++- 4 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 include/Commands.h create mode 100644 src/Commands.cpp diff --git a/include/Commands.h b/include/Commands.h new file mode 100644 index 0000000..b8cb218 --- /dev/null +++ b/include/Commands.h @@ -0,0 +1,36 @@ +#ifndef ARDUINO_H +#define ARDUINO_H +#include +#endif + +class Command +{ + public: + virtual String getCode(); + virtual int execute(const char* payload); +}; + +typedef Command* CommandPtr; + +class CommandHandler +{ + private: + CommandPtr *_commands; + int _size; + int _maxCommands; + public: + CommandHandler(int maxCommands); + ~CommandHandler(); + void registerCommand(Command *command); + int handleCommand(const char* templateCode, const char* payload); +}; + +class BuiltinLedCommand : public Command +{ + private: + int _state; + public: + BuiltinLedCommand(); + String getCode(); + int execute(const char* payload); +}; \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 12118a8..22656da 100644 --- a/platformio.ini +++ b/platformio.ini @@ -20,4 +20,6 @@ lib_deps = knolleary/PubSubClient@^2.8 adafruit/DHT sensor library@^1.4.1 adafruit/Adafruit Unified Sensor@^1.1.4 -build_flags = -D ESP32 -D CORE_DEBUG_LEVEL=5 + janelia-arduino/Vector@^1.2.0 +; build_flags = -D ESP32 -D CORE_DEBUG_LEVEL=5 +build_flags = -D ESP32 diff --git a/src/Commands.cpp b/src/Commands.cpp new file mode 100644 index 0000000..4064a14 --- /dev/null +++ b/src/Commands.cpp @@ -0,0 +1,79 @@ +#ifndef COMMANDS_H +#define COMMANDS_H +#include "Commands.h" +#endif + +#include + +#define LED_BUILTIN 10 +const int MAX_COMMANDS = 10; + +CommandHandler::CommandHandler(int maxCommands) +{ + this->_maxCommands = maxCommands; + this->_commands = new CommandPtr[maxCommands]; + this->_size = 0; +}; + +CommandHandler::~CommandHandler() +{ + if (this->_commands != NULL) + free(this->_commands); +}; + +void CommandHandler::registerCommand(Command *command) +{ + if (this->_size >= this->_maxCommands) + { + throw "Max commands reached"; + } + + this->_commands[this->_size++] = command; +}; + +int CommandHandler::handleCommand(const char *commandCode, const char *payload) +{ + Serial.printf("command: %s, payload: %s \n", commandCode, payload); + if (this->_commands == NULL) + return -1; + + for (int i = 0; i < this->_size; i++) + { + CommandPtr command = this->_commands[i]; + if (command->getCode() == commandCode) + { + command->execute(payload); + return 0; + } + } + + return -1; +}; + +BuiltinLedCommand::BuiltinLedCommand() +{ + pinMode(LED_BUILTIN, OUTPUT); + this->_state = HIGH; + digitalWrite(LED_BUILTIN, this->_state); +}; + +String BuiltinLedCommand::getCode() +{ + return "LED"; +}; + +int BuiltinLedCommand::execute(const char *payload) +{ + Serial.printf("BuiltinLedCommand::payload: %s \n", payload); + if (strcmp("ON", payload) == 0) + { + this->_state = LOW; + } + else + { + this->_state = HIGH; + } + + digitalWrite(LED_BUILTIN, this->_state); + return 0; +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 9c2169a..3eb762d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,12 @@ #include "Configuration.h" #endif +#ifndef COMMANDS_H +#define COMMANDS_H +#include "Commands.h" +#endif + +#include #include "DHT.h" #define DHTPIN 26 #define DHTTYPE DHT21 // AM2301 @@ -26,6 +32,8 @@ char* _tenantId; char* _userName; char* _password; char* _clientId; +CommandHandler _commandHandler = CommandHandler(3); +BuiltinLedCommand _ledCommand = BuiltinLedCommand(); void printConfig() { @@ -64,6 +72,19 @@ void connectCumulocityServer(bool requestDeviceCreds) _client.registerDevice(_clientId, (char *)"c8y_esp32"); } +int handleCumulocityOperation(char* templateCode, char* payload) +{ + Serial.printf("handleCumulocityOperation:: command: %s, payload: %s \n", templateCode, payload); + if(strcmp(templateCode, "511") == 0) // If c8y_Command + { + char* commandCode = strtok(payload, " "); + char* commandPayload = strtok(NULL, " "); + return _commandHandler.handleCommand(commandCode, commandPayload); + } + + return -1; +} + void setup() { Serial.begin(115200); @@ -112,12 +133,20 @@ void setup() if(_tenantId != NULL) free(_tenantId); _tenantId = strdup(received.tenant); - settings.userName = _tenantId; + settings.userName = _userName; settings.password = _password; settings.tenantId = _tenantId; _config.persistSettings(SPIFFS, settings); } + // Setup commands and the handler + _commandHandler.registerCommand(&_ledCommand); + String supportedOperationsStr = "c8y_Command,c8y_Restart"; + char* supportedOperations = strdup(supportedOperationsStr.c_str()); + _client.setCallback(handleCumulocityOperation); + _client.setSupportedOperations(supportedOperations); + _client.getPendingOperations(); + // Init temp/humidity sensor library dht.begin(); }