Add Python script for logging.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 21 Oct 2020 21:54:00 +0000 (23:54 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 21 Oct 2020 21:54:00 +0000 (23:54 +0200)
airingbutler.ino
log_mhz19.py [new file with mode: 0644]

index b1bbaf9507a822dbdb113a390a99f82d0a5d23eb..8ea9d41099e7a2f1686a015c835fb75311aee62c 100644 (file)
@@ -11,6 +11,7 @@ MHZ19 mhz19 = MHZ19(rx_pin,tx_pin);
 void setup()
 {
     Serial.begin(115200);
+    Serial.println("CO2,temp");
     mhz19.begin(rx_pin, tx_pin);
 }
 
@@ -19,9 +20,8 @@ void loop()
 {
     measurement_t m = mhz19.getMeasurement();
 
-    Serial.print("co2: ");
-    Serial.println(m.co2_ppm);
-    Serial.print("temp: ");
+    Serial.print(m.co2_ppm);
+    Serial.print(",");
     Serial.println(m.temperature);
     
     delay(500);
diff --git a/log_mhz19.py b/log_mhz19.py
new file mode 100644 (file)
index 0000000..57140d6
--- /dev/null
@@ -0,0 +1,49 @@
+import argparse
+import asyncio
+import logging
+
+import serial_asyncio  # pyserial-asyncio Debian: python3-serial-asyncio
+
+
+log = logging.getLogger(__name__)
+
+
+class LineBuffer:
+    def __init__(self):
+        self.buffer = ''
+
+    def write(self, data: bytes):
+        self.buffer += data.decode()
+
+
+class Receiver(asyncio.Protocol):
+    def __init__(self):
+        self.transport = None
+
+    def connection_made(self, transport):
+        self.transport = transport
+        log.debug('port opened')
+
+    def data_received(self, data):
+        data = data.decode()
+        print(data, end='')
+
+    def connection_lost(self, exc):
+        log.debug('port closed')
+        self.transport.loop.stop()
+
+
+def main():
+    parser = argparse.ArgumentParser('Logs CO2 and temperature from MH-Z19B sensor.')
+    parser.add_argument('--device', '-d', default='/dev/ttyACM0', help='Device where Arduino is connected to')
+    args = parser.parse_args()
+    logging.basicConfig(level=logging.INFO, format='%(asctime)-15s %(message)s')
+    loop = asyncio.get_event_loop()
+    coro = serial_asyncio.create_serial_connection(loop, Receiver, args.device, baudrate=115200)
+    loop.run_until_complete(coro)
+    loop.run_forever()
+    loop.close()
+
+
+if __name__ == '__main__':
+    main()