arduino json

This commit is contained in:
Marcus 2023-08-02 01:04:39 +02:00
parent 30505b128c
commit 3931100b06
2 changed files with 53 additions and 15 deletions

32
main.py
View file

@ -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())

View file

@ -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);
}
}