Skip to content

Commit 8c30781

Browse files
author
m19936
committed
Added command handler for sandbox
1 parent 3c66649 commit 8c30781

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

examples/sandbox/sandbox.ino

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* experience
44
*/
55

6-
#define SANDBOX_VERSION "1.0.0"
6+
#define SANDBOX_VERSION "1.1.0"
77

88
#include <Arduino.h>
99

@@ -219,6 +219,62 @@ void decodeMessage(const char *message) {
219219
}
220220
}
221221

222+
void remove_spaces(char *s) {
223+
char *d = s;
224+
do {
225+
while (*d == ' ') { ++d; }
226+
} while (*s++ = *d++);
227+
}
228+
229+
void printHelp() {
230+
Log.rawf("\nAvailable Commands\n"
231+
"-----------------------\n"
232+
"help\t\t Print this message\n"
233+
"loglevel=level\t Set the log level. Available levels are debug, "
234+
"info, warn, error\n"
235+
"-----------------------\r\n");
236+
}
237+
238+
void handleSerialCommand(const char *instruction, uint16_t instructionLen) {
239+
// Find the first occurrence of '='
240+
char *equalIndex = strchr(instruction, '=');
241+
242+
// If we did not find it, treat is at a non-value command
243+
if (equalIndex == NULL) {
244+
equalIndex = (char *)(&instruction[instructionLen - 1]);
245+
Log.debug("Given command is non-value");
246+
}
247+
248+
// Extract the command
249+
uint16_t cmdLen = equalIndex - instruction;
250+
char cmd[cmdLen + 1];
251+
memcpy(cmd, instruction, cmdLen);
252+
cmd[cmdLen] = '\0';
253+
254+
// Extract the value
255+
uint16_t valueLen = instructionLen - cmdLen - 1;
256+
char value[valueLen + 1];
257+
memcpy(value, instruction + cmdLen + 1, valueLen);
258+
value[valueLen] = '\0';
259+
260+
// Depending on the cmd content, execute different commands
261+
if (strcmp(cmd, "help") == 0) {
262+
printHelp();
263+
} else if (strcmp(cmd, "loglevel") == 0) {
264+
if (!Log.setLogLevelStr(value)) {
265+
Log.errorf("Could not set log level %s\r\n", value);
266+
} else {
267+
Log.rawf("Log level is now %s\r\n", value);
268+
}
269+
} else if (strcmp(cmd, "heartbeat") == 0) {
270+
event_flags |= SEND_HEARTBEAT_FLAG;
271+
} else {
272+
Log.info("\nInvalid command");
273+
printHelp();
274+
return;
275+
}
276+
}
277+
222278
void setup() {
223279
Log.begin(115200);
224280

@@ -253,6 +309,7 @@ void setup() {
253309
if (err != ECC608.ERR_OK) {
254310
Log.error("Could not retrieve thing name from the ECC");
255311
Log.error("Unable to initialize the MQTT topics. Stopping...");
312+
LedCtrl.on(Led::ERROR);
256313
return;
257314
}
258315

@@ -269,6 +326,12 @@ unsigned long timeLastCellToggle = millis() + 500;
269326

270327
void loop() {
271328

329+
// See if there are any messages for the command handler
330+
if (Serial3.available()) {
331+
String extractedString = Serial3.readStringUntil('\n');
332+
handleSerialCommand(extractedString.c_str(), extractedString.length());
333+
}
334+
272335
// ----------------------------------------------------------
273336
if (state == NOT_CONNECTED) {
274337
if ((millis() - timeLastCellToggle) > 500) {
@@ -444,7 +507,7 @@ void loop() {
444507
"{\"type\": \"data\",\
445508
\"data\": { \
446509
\"Temperature\": %d, \
447-
\"Red Light\": %d \
510+
\"Light Intensity\": %d \
448511
} \
449512
}",
450513
int(Mcp9808.readTempC()),

src/log.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ void LogClass::setLogLevel(const LogLevel log_level) {
4141
this->log_level = log_level;
4242
}
4343

44+
bool LogClass::setLogLevelStr(const char *log_level) {
45+
LogLevel ll = LogLevel::NONE;
46+
if (strstr(log_level, "debug") != NULL) {
47+
ll = LogLevel::DEBUG;
48+
} else if (strstr(log_level, "info") != NULL) {
49+
ll = LogLevel::INFO;
50+
} else if (strstr(log_level, "warn") != NULL) {
51+
ll = LogLevel::WARN;
52+
} else if (strstr(log_level, "error") != NULL) {
53+
ll = LogLevel::ERROR;
54+
}
55+
56+
if (ll == LogLevel::NONE) {
57+
return false;
58+
}
59+
60+
this->setLogLevel(ll);
61+
return true;
62+
}
63+
4464
void LogClass::begin(const unsigned long baud_rate) {
4565
this->uart->begin(baud_rate);
4666
}

src/log.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class LogClass {
1818
void setOutputUart(UartClass *uart);
1919

2020
void setLogLevel(const LogLevel log_level);
21+
bool setLogLevelStr(const char *log_level);
2122

2223
void begin(const unsigned long baud_rate);
2324
void end();

0 commit comments

Comments
 (0)