Last active
April 8, 2019 17:26
-
-
Save cowlicks/f530eeedb9bb96074f4e11b6dda4dc77 to your computer and use it in GitHub Desktop.
Get the ph and orp value from an aquacointelpro 1. It prints the data as json to stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
This scripts needs the `pyserial` python package installed to run (`pip install pyserial`). | |
Usage: | |
>>> Aqua().get_ph_orp() | |
{'ph': 6.82, 'orp': 116.0} | |
''' | |
import serial | |
import json | |
class Aqua: | |
def __init__(self): | |
self.conf = dict( | |
port='/dev/ttyUSB0', | |
baudrate=9600, | |
timeout=5, | |
parity=serial.PARITY_NONE, | |
stopbits=serial.STOPBITS_ONE, | |
bytesize=serial.EIGHTBITS) | |
self.commands = dict(list='l', status='c', datalog='d') | |
for name, command in self.commands.items(): | |
setattr(self, name, lambda command=command: self.commander(command)) | |
def commander(self, command): | |
if not isinstance(command, bytes): | |
command = command.encode('ascii') | |
if not command.endswith(b'\r'): | |
command += b'\r' | |
with serial.Serial(**self.conf) as ser: | |
ser.write(command) | |
out = ser.read_until(b'AquaController> ') | |
return out | |
def get_ph_orp(self): | |
data = self.status() | |
out = data.split(b'\r\n') | |
i = out.index(b' pH ORP ') | |
ph, orp = out[i + 1].split() | |
return dict(ph=float(ph), orp=float(orp)) | |
def main(): | |
print(json.dumps(Aqua().get_ph_orp())) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment