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 (" \n Available 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 (" \n Invalid command" );
273+ printHelp ();
274+ return ;
275+ }
276+ }
277+
222278void 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
270327void 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 ()),
0 commit comments