Good evening,
Trying to create a link between an Arduino and pi using appdaemon. I’ve cut the bulk of the code out to simplify (sorry if some of the tabbing is out due to copy-paste)
Python:
import appdaemon.plugins.hass.hassapi as hass
import datetime
import time
import serial
import json
class ArduinoPiComms(hass.Hass):
def initialize(self):
start_time = self.datetime() + datetime.timedelta(seconds=1)
self.handle = self.run_every(self.run_every_Serial,start_time,1)
def run_every_Serial(self, kwargs):
self.ser = serial.Serial(
port = '/dev/ttyACM0',
baudrate = 38400,
timeout=2.5,
parity=serial.PARITY_NONE,
bytesize=serial.EIGHTBITS,
stopbits=serial.STOPBITS_ONE
)
data = {"mes":1000, etc } #message is ~60 bytes of data
data = json.dumps(data).encode('ascii')
self.log("1. outgoing msg: %s\n", data)
if ser.isOpen():
#send data
ser.write(data)
ser.flush()
try:
#read
doc = ser.readline()
self.log(2)
except Exception as e:
self.log("Error : try to parse an incomplete message")
pass
ser.close()
doc = json.loads(doc)
self.log("2. Recieved: %s\n",doc)
now the code its self works, send and receives data but!!! it does this ass about. i.e. it opens up and reads the message first then sends the data (confirmed in logs)
i.e it will show:
10:12:45.34: outgoing : {"mes":1000, etc }
10:12:45.30: Recieved: {incoming...}
now the weird thing is if I reduce the size of the outgoing message it works, okay, but if I leave it long with ~data points it works the wrong way.
Any idea as to why/how I can code around this?