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) ser = serial.Serial('/dev/ttyACM0', baudrate = 9600, timeout = 1)
time.sleep(3) #time.sleep(3)
def getValues():
#ser.write(b'g')
lol = "g"
ser.write(str(lol).encode('ascii'))
arduinoData = ser.readline().decode('ascii')
return arduinoData
while 1: while 1:
userInput = input('Get data point?') # { "command": "switch", "arg": "one", "state": "off"}
userInput = input('> ')
if userInput == 'y': ser.write(str(userInput).encode('ascii'))
print(getValues()) 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 analogPin = 3;
int data = 0; int data = 0;
char userInput; char userInput;
void setup() { void setup() {
// put your setup code here, to run once: // put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600); Serial.begin(9600);
} }
void loop() { void loop() {
// put your main code here, to run repeatedly: // 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') { if(userInput == 'g') {
@ -34,7 +64,7 @@ void loop() {
delay(200); delay(200);
*/ */
delay(2000); //delay(2000);
Serial.println(data); Serial.println(data);
} }
} }