#!/usr/bin/env python3 from RFXtrx import PySerialTransport import sys, datetime, os if __name__ == "__main__": transport = PySerialTransport('/dev/ttyUSB0') transport.reset() while True: if os.path.isfile('/home/pi/weather/stop'): break recv = transport.receive_blocking() if not recv: continue timestamp = datetime.datetime.timestamp(datetime.datetime.now()) sys.stderr.write("Raw: %s\n"%str(recv)) try: device_id = recv.device.id_string device_type = recv.device.type_string except Exception as e: sys.stderr.write("Cannot find device id, exception %s." % str(e)) continue data = {} for k,v in recv.values.items(): data[k.lower().replace(" ", "_")] = v data_type = None if "rain_rate" in data: data_type = "rain" elif "wind_gust" in data: data_type = "wind" elif "temperature" in data: data_type = "temp" else: sys.stderr.write("Unknown device '%s' with id '%s', ignoring.\n"%(device_type, device_id)) continue out = [ "type::%s" % data_type, "timestamp::%f" % timestamp, "device_type::%s" % device_type, "device_id::%s" % device_id ] for key in sorted(data.keys()): out.append("%s::%s" % (key, data[key])) print("\t".join(out)) sys.stderr.flush() sys.stdout.flush() sys.stderr.write("stop\n") sys.stdout.flush()