From 3931100b06e1532b6d8f7d126fd0b7071e420866 Mon Sep 17 00:00:00 2001 From: Marcus Date: Wed, 2 Aug 2023 01:04:39 +0200 Subject: [PATCH] arduino json --- main.py | 32 ++++++++++++++++++++------------ main.ino => main/main.ino | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 15 deletions(-) rename main.ino => main/main.ino (61%) diff --git a/main.py b/main.py index 63b5e88..5dc86a2 100755 --- a/main.py +++ b/main.py @@ -5,18 +5,26 @@ import time ser = serial.Serial('/dev/ttyACM0', baudrate = 9600, timeout = 1) -time.sleep(3) - -def getValues(): - #ser.write(b'g') - lol = "g" - ser.write(str(lol).encode('ascii')) - arduinoData = ser.readline().decode('ascii') - return arduinoData +#time.sleep(3) while 1: - userInput = input('Get data point?') - - if userInput == 'y': - print(getValues()) + # { "command": "switch", "arg": "one", "state": "off"} + userInput = input('> ') + ser.write(str(userInput).encode('ascii')) + arduinoData = ser.readline().decode('ascii') + print(arduinoData) + + +##def getValues(): +## #ser.write(b'g') +## lol = "g" +## ser.write(str(lol).encode('ascii')) +## arduinoData = ser.readline().decode('ascii') +## return arduinoData +## +##while 1: +## userInput = input('Get data point?') +## +## if userInput == 'y': +## print(getValues()) diff --git a/main.ino b/main/main.ino similarity index 61% rename from main.ino rename to main/main.ino index 38cf0c3..14802c9 100644 --- a/main.ino +++ b/main/main.ino @@ -1,20 +1,50 @@ +#include "ArduinoJson.h" + int analogPin = 3; int data = 0; char userInput; + void setup() { // put your setup code here, to run once: pinMode(LED_BUILTIN, OUTPUT); + + + Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: - if(Serial.available()>0) { - userInput = Serial.read(); +if(Serial.available()>0) { + + //userInput = Serial.read(); + + // String input; + + StaticJsonDocument<96> doc; + // { "command": "switch", "arg": "one", "state": "off"} + DeserializationError error = deserializeJson(doc, Serial); + + if (error) { + Serial.print(F("deserializeJson() failed: ")); + Serial.println(error.f_str()); + return; + } + + const char* command = doc["command"]; // "switch" + const char* arg = doc["arg"]; // "one" + const char* state = doc["state"]; // "off" + + + Serial.print(command); + Serial.print(" - "); + Serial.print(arg); + Serial.print(" - "); + Serial.println(state); if(userInput == 'g') { @@ -34,7 +64,7 @@ void loop() { delay(200); */ - delay(2000); + //delay(2000); Serial.println(data); } }