BenTheRighteous
Well-known member
I'm trying to get a Raspberry Pi to monitor my Leaf's regen and here's the code I've come up with so far. It works but it's the result of trial-and-error so I'm sure it could get a lot cleaner, I just don't know how. Any feedback is welcome.
My two main issues are:
1. There's got to be a better way to read from the interface. Right now I just keep concatenating the result into a variable and once it ends with something logical, I assume I have the whole thing.
2. Not sure I'm sending the right series of AT commands. What I have works, but the AT MA command causes the scanner to spit out messages nonstop. I'd much rather have it provide a single message and stop until I ask again later, in other words I want to poll at my leisure instead of drink from a firehose.
For now the code just reads the value and prints it on the console, nothing more complicated than that.
Again, my code works, but I know there's a ton of room for improvement. Thanks for any feedback!
My two main issues are:
1. There's got to be a better way to read from the interface. Right now I just keep concatenating the result into a variable and once it ends with something logical, I assume I have the whole thing.
2. Not sure I'm sending the right series of AT commands. What I have works, but the AT MA command causes the scanner to spit out messages nonstop. I'd much rather have it provide a single message and stop until I ask again later, in other words I want to poll at my leisure instead of drink from a firehose.
For now the code just reads the value and prints it on the console, nothing more complicated than that.
Code:
import time
import os
dev = os.open('/dev/rfcomm0', os.O_RDWR | os.O_NONBLOCK | os.O_NOCTTY)
def printTwosComp(num, bits):
mask = 1 << bits
half = mask >> 1
if num < half:
return num
else:
return num - mask
def send(cmd):
print('Sending command:', cmd)
os.write(dev, cmd)
# time.sleep(0.2)
rsp = b''
while not (rsp.endswith(b'\r') or rsp.endswith(b'\n')):
time.sleep(0.2)
rsp += os.read(dev, 160)
print('partial:', rsp)
if rsp.endswith(b'>'):
print('early finish')
return
print('got echo')
rsp = b''
while not (rsp.endswith(b'\r') or rsp.endswith(b'>')):
time.sleep(0.2)
rsp += os.read(dev, 160)
print('partial:', rsp)
print(rsp)
print('got result')
time.sleep(0.2)
os.write(dev, b'at z\r')
time.sleep(1)
send(b'at z\r')
send(b'at l1\r')
send(b'at h1\r')
send(b'at cf 180\r')
os.write(dev, b'at ma\r')
rsp = b''
while True:
time.sleep(0.0001)
rsp += os.read(dev, 1)
str = rsp.decode('UTF-8')
if str.endswith('\r\n'):
# print(str)
print(rsp)
if len(rsp) > 10:
# 180 00 00 00 00 00 00 26 00
# 012345678901234567890123456
# 10 20
# math = (int(chr(rsp[19]), 16) << 12) + (int(chr(rsp[20]), 16) << 8) + (int(chr(rsp[22]), 16) << 4) + (int(chr(rsp[23]), 16)) # throttle position
math = (int(chr(rsp[10]), 16) << 12) + (int(chr(rsp[11]), 16) << 8) + (int(chr(rsp[13]), 16) << 4) + (int(chr(rsp[14]), 16)) # motor amps
print(math)
print(printTwosComp(math, 16))
rsp = b''
Again, my code works, but I know there's a ton of room for improvement. Thanks for any feedback!