3
3
* experience
4
4
*/
5
5
6
- #define SANDBOX_VERSION " 1.0 .0"
6
+ #define SANDBOX_VERSION " 1.1 .0"
7
7
8
8
#include < Arduino.h>
9
9
@@ -219,6 +219,62 @@ void decodeMessage(const char *message) {
219
219
}
220
220
}
221
221
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
+
222
278
void setup () {
223
279
Log.begin (115200 );
224
280
@@ -253,6 +309,7 @@ void setup() {
253
309
if (err != ECC608.ERR_OK ) {
254
310
Log.error (" Could not retrieve thing name from the ECC" );
255
311
Log.error (" Unable to initialize the MQTT topics. Stopping..." );
312
+ LedCtrl.on (Led::ERROR);
256
313
return ;
257
314
}
258
315
@@ -269,6 +326,12 @@ unsigned long timeLastCellToggle = millis() + 500;
269
326
270
327
void loop () {
271
328
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
+
272
335
// ----------------------------------------------------------
273
336
if (state == NOT_CONNECTED) {
274
337
if ((millis () - timeLastCellToggle) > 500 ) {
@@ -444,7 +507,7 @@ void loop() {
444
507
" {\" type\" : \" data\" ,\
445
508
\" data\" : { \
446
509
\" Temperature\" : %d, \
447
- \" Red Light\" : %d \
510
+ \" Light Intensity \" : %d \
448
511
} \
449
512
}" ,
450
513
int (Mcp9808.readTempC ()),
0 commit comments