-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix arduino not sending data to the COM-port
- Loading branch information
Showing
3 changed files
with
57 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,57 @@ | ||
#include <ArduinoJson.h> | ||
|
||
static const char* TYPE = "type"; | ||
static const char* PAYLOAD = "payload"; | ||
static const char* CHANGE_STATE = "change_state"; | ||
static const char* QUERY_STATE = "query_state"; | ||
static const char* QUERY_STATE_RESULT = "query_state_result"; | ||
|
||
const int capacity = 100; | ||
StaticJsonDocument<capacity> doc; | ||
bool state = false; | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
pinMode(13, OUTPUT); | ||
digitalWrite(13, LOW); | ||
} | ||
void change_state(bool new_state, bool persist_change = true) { | ||
if (persist_change) { | ||
state = new_state; | ||
} | ||
|
||
void flash() { | ||
digitalWrite(13, HIGH); | ||
delay(200); | ||
digitalWrite(13, LOW); | ||
delay(200); | ||
digitalWrite(13, HIGH); | ||
delay(200); | ||
digitalWrite(13, LOW); | ||
delay(200); | ||
if (state) { | ||
if (new_state) { | ||
digitalWrite(13, HIGH); | ||
} else { | ||
digitalWrite(13, LOW); | ||
} | ||
} | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
Serial.setTimeout(50); | ||
pinMode(13, OUTPUT); | ||
change_state(false); | ||
} | ||
|
||
void loop() { | ||
if (Serial.available() > 0) { | ||
String msg = Serial.readStringUntil('%'); | ||
// skip the message end character | ||
Serial.read(); | ||
String msg = Serial.readString(); | ||
|
||
deserializeJson(doc, msg); | ||
if (doc["type"] == "change_state" && doc["payload"] == true) { | ||
digitalWrite(13, HIGH); | ||
state = true; | ||
} else if (doc["type"] == "change_state" && doc["payload"] == false) { | ||
digitalWrite(13, LOW); | ||
state = false; | ||
} else if (doc["type"] == "query_state") { | ||
|
||
if (doc[TYPE] == CHANGE_STATE) { | ||
if (doc[PAYLOAD] == true) { | ||
change_state(true); | ||
} else if (doc[PAYLOAD] == false) { | ||
change_state(false); | ||
} | ||
} else if (doc[TYPE] == QUERY_STATE) { | ||
DynamicJsonDocument dyn_doc(100); | ||
dyn_doc["type"] = "query_state_result"; | ||
dyn_doc["payload"] = state; | ||
dyn_doc[TYPE] = QUERY_STATE_RESULT; | ||
dyn_doc[PAYLOAD] = state; | ||
|
||
String msg; | ||
serializeJson(dyn_doc, msg); | ||
msg += '%'; | ||
|
||
flash(); | ||
|
||
Serial.write(msg.c_str()); | ||
} else { | ||
Serial.write("{\"type\": \"error\", \"payload\": \"unknown_command\"}%"); | ||
Serial.write("{\"type\": \"error\", \"payload\": \"unknown_command\"}"); | ||
} | ||
} | ||
} |