json enc end decode

This commit is contained in:
Marcus 2023-08-02 01:35:46 +02:00
parent 82944d7c77
commit 65465c37b7
2 changed files with 26 additions and 11 deletions

View file

@ -13,7 +13,7 @@ ser = serial.Serial('/dev/ttyACM0', baudrate = 9600, timeout = 1)
while 1: while 1:
# { "command": "switch", "arg": "one", "state": "off"} # { "command": "switch", "arg": "one", "state": "off", "ver":"1690931931"}
userInput = input('> ') userInput = input('> ')
ser.write(str(userInput).encode('ascii')) ser.write(str(userInput).encode('ascii'))
arduinoData = ser.readline().decode('ascii') arduinoData = ser.readline().decode('ascii')

View file

@ -4,6 +4,7 @@
int analogPin = 3; int analogPin = 3;
int data = 0; int data = 0;
char userInput; char userInput;
char* commandVer;
void setup() { void setup() {
@ -24,10 +25,10 @@ if(Serial.available()>0) {
//userInput = Serial.read(); //userInput = Serial.read();
// String input; // String input;
//{ "command": "switch", "arg": "one", "state": "off", "ver":"1690931931"}
StaticJsonDocument<96> jsonin;
StaticJsonDocument<96> doc; DeserializationError error = deserializeJson(jsonin, Serial);
// { "command": "switch", "arg": "one", "state": "off"}
DeserializationError error = deserializeJson(doc, Serial);
if (error) { if (error) {
Serial.print(F("deserializeJson() failed: ")); Serial.print(F("deserializeJson() failed: "));
@ -35,20 +36,33 @@ if(Serial.available()>0) {
return; return;
} }
const char* command = doc["command"]; // "switch" const char* command = jsonin["command"]; // "switch"
const char* arg = doc["arg"]; // "one" const char* arg = jsonin["arg"]; // "one"
const char* state = doc["state"]; // "off" 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(command);
Serial.print(" - "); Serial.print(" - ");
Serial.print(arg); Serial.print(arg);
Serial.print(" - "); Serial.print(" - ");
Serial.println(state); Serial.print(state);
Serial.print(" - ");
Serial.println(ver);
if(userInput == 'g') { if(userInput == 'g') {
data = analogRead(analogPin); data = analogRead(analogPin);
*/
/* /*
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second 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 digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(200); delay(200);
*/
//delay(2000); //delay(2000);
Serial.println(data); Serial.println(data);
} */
//}
} }
} }