# -*- coding: utf-8 -*- """ Display data from MQTT broker on ePaper screen @author: arofarn """ ########### # IMPORTS # ########### from cameteo_conf import * import paho.mqtt.client as mqtt from spidev import SpiDev import EPD_driver import json import netifaces mqtt_client_id = "epaper_display" ############# # CALLBACKS # ############# #Callback pour la connection au MQTT : souscriptions aux topics def on_connect(client, userdata, flags, rc): print(mqtt.connack_string(rc)) if rc == 0: print("Subscribing to %s ..." % mqtt_topic) client.subscribe([(mqtt_topic, 0), (camera_mqtt_topic + "/#", 0)] ) print("OK") #Callback de gestion des messages arrivant au MQTT : # affichage et enregistrement en base de données def on_message(client, userdata, msg): top=msg.topic[len(mqtt_topic)-1:].strip() subtopics = top.split("/") if subtopics[0] == "AdaBME280_1": payload = msg.payload.decode() print(payload) val = json.loads(payload) #Test présence et cohérence de la valeur try: val['value'] = float(val['value'] ) except: print("Value error: {}".format(val['value'])) val['value'] = float('nan') #Gestion du symbole des degrés parfois difficile pour certaines sources #val['unit'] = val['unit'].replace('deg', '°') #Affichage des données coord_type = { 'TA' : 33, 'HR' : 49, 'SLPA' : 65 } if val['type'] in coord_type: disp.Dis_String(125, coord_type[val['type']], "{} {} ".format(val['value'], val['unit']), 16) #Callback managing date and time from MQTT broker def on_message_date(client, userdata, msg): payload = msg.payload.decode() print("Date : " + payload) try: d = datetime.strptime(payload, "%H:%M:%S %d/%m/%Y") disp.Dis_String(0, 17, datetime.strftime(d + TimeZone.utcoffset(None), "%d/%m/%Y %H:%M:%S"), 16) except: disp.Dis_String(0, 17, payload, 16) #Update display with info from camera (camera shooting new photo or name of the # last photo) def on_message_camera(client, userdata, msg): pl = msg.payload.decode() print("{} : {} ({})".format(msg.topic, pl, type(pl))) if pl == "1" and msg.topic == camera_mqtt_topic + "/shooting" : disp.Dis_String(0, 105, "Shooting photo... ", 16) if msg.topic == camera_mqtt_topic + "/last_photo": disp.Dis_String(0, 105, "Last: " + pl, 16) #Callback de déconnexion au broker MQTT def on_disconnect(client, userdata, msg): if msg != 0: print("Déconnexion imprévu : %s" % msg) exit() ######## # Main # ######## #init and Clear full screen bus = 0 device = 0 disp = EPD_driver.EPD_driver(spi=SpiDev(bus, device)) print("Start display...") disp.Dis_Clear_full() disp.Dis_Clear_part() print("OK") #disp.Dis_String(0, 0, "{} | {}".format(socket.gethostname(), netifaces.ifaddresses('wlan0')[2][0]['addr']), 12) disp.Dis_String(0, 0, "IPv4 : {}".format(netifaces.ifaddresses('wlan0')[2][0]['addr']), 16) disp.Dis_String(0, 33, "Temperature : ", 16) disp.Dis_String(0, 49, "Humidity : ", 16) disp.Dis_String(0, 65, "Sea Pression : ", 16) #Connect to MQTT broker and loop... mqtt_client = mqtt.Client(mqtt_client_id, clean_session=True) mqtt_client.username_pw_set(mqtt_user, mqtt_pass) mqtt_client.on_connect = on_connect mqtt_client.on_message = on_message mqtt_client.on_disconnect = on_disconnect mqtt_client.message_callback_add("huzzah0/NTP/date", on_message_date) mqtt_client.message_callback_add(camera_mqtt_topic + "/#", on_message_camera) print(mqtt_host + ":" + str(mqtt_port)) mqtt_client.connect(mqtt_host, int(mqtt_port), 60) mqtt_client.loop_forever()