json enc end decode
This commit is contained in:
parent
82944d7c77
commit
65465c37b7
2 changed files with 26 additions and 11 deletions
2
main.py
2
main.py
|
@ -13,7 +13,7 @@ ser = serial.Serial('/dev/ttyACM0', baudrate = 9600, timeout = 1)
|
|||
|
||||
|
||||
while 1:
|
||||
# { "command": "switch", "arg": "one", "state": "off"}
|
||||
# { "command": "switch", "arg": "one", "state": "off", "ver":"1690931931"}
|
||||
userInput = input('> ')
|
||||
ser.write(str(userInput).encode('ascii'))
|
||||
arduinoData = ser.readline().decode('ascii')
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
int analogPin = 3;
|
||||
int data = 0;
|
||||
char userInput;
|
||||
char* commandVer;
|
||||
|
||||
|
||||
void setup() {
|
||||
|
@ -24,10 +25,10 @@ if(Serial.available()>0) {
|
|||
//userInput = Serial.read();
|
||||
|
||||
// String input;
|
||||
//{ "command": "switch", "arg": "one", "state": "off", "ver":"1690931931"}
|
||||
StaticJsonDocument<96> jsonin;
|
||||
|
||||
StaticJsonDocument<96> doc;
|
||||
// { "command": "switch", "arg": "one", "state": "off"}
|
||||
DeserializationError error = deserializeJson(doc, Serial);
|
||||
DeserializationError error = deserializeJson(jsonin, Serial);
|
||||
|
||||
if (error) {
|
||||
Serial.print(F("deserializeJson() failed: "));
|
||||
|
@ -35,20 +36,33 @@ if(Serial.available()>0) {
|
|||
return;
|
||||
}
|
||||
|
||||
const char* command = doc["command"]; // "switch"
|
||||
const char* arg = doc["arg"]; // "one"
|
||||
const char* state = doc["state"]; // "off"
|
||||
const char* command = jsonin["command"]; // "switch"
|
||||
const char* arg = jsonin["arg"]; // "one"
|
||||
const char* state = jsonin["state"]; // "off"
|
||||
const char* ver = jsonin["ver"]; // "1690931931"
|
||||
|
||||
StaticJsonDocument<96> jsonout;
|
||||
|
||||
jsonout["command"] = "lol";
|
||||
jsonout["arg"] = "rofl";
|
||||
jsonout["state"] = "on";
|
||||
jsonout["ver"] = "1690931337";
|
||||
|
||||
serializeJson(jsonout, Serial);
|
||||
|
||||
/*
|
||||
Serial.print(command);
|
||||
Serial.print(" - ");
|
||||
Serial.print(arg);
|
||||
Serial.print(" - ");
|
||||
Serial.println(state);
|
||||
Serial.print(state);
|
||||
Serial.print(" - ");
|
||||
Serial.println(ver);
|
||||
|
||||
if(userInput == 'g') {
|
||||
|
||||
data = analogRead(analogPin);
|
||||
*/
|
||||
/*
|
||||
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
|
||||
delay(200); // wait for a second
|
||||
|
@ -63,10 +77,11 @@ if(Serial.available()>0) {
|
|||
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
|
||||
delay(200);
|
||||
|
||||
*/
|
||||
|
||||
//delay(2000);
|
||||
Serial.println(data);
|
||||
}
|
||||
*/
|
||||
//}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue