Skip to content

Commit

Permalink
Add command support using built-in LED
Browse files Browse the repository at this point in the history
  • Loading branch information
amalabey committed Dec 9, 2020
1 parent bec6c02 commit 526181d
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 2 deletions.
36 changes: 36 additions & 0 deletions include/Commands.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef ARDUINO_H
#define ARDUINO_H
#include <Arduino.h>
#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);
};
4 changes: 3 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
79 changes: 79 additions & 0 deletions src/Commands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#ifndef COMMANDS_H
#define COMMANDS_H
#include "Commands.h"
#endif

#include <string.h>

#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;
};
31 changes: 30 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
#include "Configuration.h"
#endif

#ifndef COMMANDS_H
#define COMMANDS_H
#include "Commands.h"
#endif

#include <string.h>
#include "DHT.h"
#define DHTPIN 26
#define DHTTYPE DHT21 // AM2301
Expand All @@ -26,6 +32,8 @@ char* _tenantId;
char* _userName;
char* _password;
char* _clientId;
CommandHandler _commandHandler = CommandHandler(3);
BuiltinLedCommand _ledCommand = BuiltinLedCommand();

void printConfig()
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 526181d

Please sign in to comment.