Update to use new data from feather m0 express (UART->MQTT)

This commit is contained in:
Pierrick C 2018-08-04 19:29:49 +02:00
parent b7205f397f
commit be8d28a2f9
2 changed files with 29 additions and 24 deletions

View File

@ -15,7 +15,7 @@ mqtt_client_id = cameteo
mqtt_user = arofarn mqtt_user = arofarn
mqtt_pass = WaKaW9XMGUZ3rRJD mqtt_pass = WaKaW9XMGUZ3rRJD
mqtt_qos = 2 mqtt_qos = 2
mqtt_topic = huzzah0/# mqtt_topic = feather0/#
[FLASK] [FLASK]
http_host = 0.0.0.0 http_host = 0.0.0.0

View File

@ -33,7 +33,7 @@ def update_epd():
#rotation de l'image de 90° + 180° si le paramètre epd_rotate est différent de 0 #rotation de l'image de 90° + 180° si le paramètre epd_rotate est différent de 0
# la translation permet de rattraper un petit décalage en cas de rotation de 90+180° # la translation permet de rattraper un petit décalage en cas de rotation de 90+180°
screen = image.rotate(90 + (epd_rotate * 180), expand=True, translate=(-6*epd_rotate, 0)) screen = image.rotate(90 + (epd_rotate * 180), expand=True, translate=(-6*epd_rotate, 0))
#Mis en memoire de l'image à afficher et affichage de l'image #Mis en memoire de l'image à afficher et affichage de l'image
epd.set_frame_memory(screen, 0, 0) epd.set_frame_memory(screen, 0, 0)
epd.display_frame() epd.display_frame()
@ -52,7 +52,7 @@ def on_connect(client, userdata, flags, rc):
(camera_mqtt_topic + "/#", 0)] (camera_mqtt_topic + "/#", 0)]
) )
print("OK") print("OK")
""" """
Callback de gestion des messages arrivant au MQTT : Callback de gestion des messages arrivant au MQTT :
affichage et enregistrement en base de données affichage et enregistrement en base de données
@ -60,49 +60,55 @@ affichage et enregistrement en base de données
def on_message(client, userdata, msg): def on_message(client, userdata, msg):
top=msg.topic[len(mqtt_topic)-1:].strip() top=msg.topic[len(mqtt_topic)-1:].strip()
subtopics = top.split("/") subtopics = top.split("/")
if subtopics[0] == "AdaBME280_1": if subtopics[0] == "BME":
payload = msg.payload.decode() payload = msg.payload.decode()
print(payload)
val = json.loads(payload) val = json.loads(payload)
#Test présence et cohérence de la valeur
#Test présence et cohérence de la valeur
try: try:
val['value'] = float(val['value'] ) val['value'] = float(val['value'] )
except: except:
print("Value error: {}".format(val['value'])) print("Value error: {}".format(val['value']))
val['value'] = float('nan') val['value'] = float('nan')
#Gestion du symbole des degrés parfois difficile pour certaines sources #Gestion du symbole des degrés parfois difficile pour certaines sources
val['unit'] = val['unit'].replace('deg', '°') val['unit'] = val['unit'].replace('deg', '°')
#Affichage des données #Affichage des données
coord_type = { 'AT' : (12, 50), coord_type = { 'AT' : (12, 50),
'RH' : (137, 50), 'RH' : (137, 50),
'MSLP' : (12, 72), 'AP' : (12, 72),
'ALTI' : (137, 72), 'ALTI' : (137, 72),
} }
print(val)
if val['type'] in coord_type: if val['type'] in coord_type:
#erase former text #erase former text
draw.rectangle(coord_type[val['type']] + (coord_type[val['type']][0] + 112, coord_type[val['type']][1] + 20), fill=white) draw.rectangle(coord_type[val['type']] + (coord_type[val['type']][0] + 112,
#draw new values coord_type[val['type']][1] + 20),
draw.text(coord_type[val['type']], "{:6.1f}{} ".format(val['value'], val['unit']), font=font20_bold, fill=black) fill=white)
#draw new values
draw.text(coord_type[val['type']],
"{:6.1f}{} ".format(val['value'], val['unit']),
font=font20_bold,
fill=black)
""" """
Callback managing date and time from MQTT broker Callback managing date and time from MQTT broker
""" """
def on_message_date(client, userdata, msg): def on_message_date(client, userdata, msg):
payload = msg.payload.decode() payload = msg.payload.decode()
print("Date : " + payload) print("Date : " + payload)
try: try:
#Mise en forme de la date et heure reçu pour fuseau horaire configuré #Mise en forme de la date et heure reçu pour fuseau horaire configuré
dt_texte = datetime.strftime(datetime.strptime(payload, "%H:%M:%S %d/%m/%Y") + TimeZone.utcoffset(None), "%d/%m/%Y %H:%M:%S") dt_texte = datetime.strftime(datetime.strptime(payload, "%H:%M:%S %d/%m/%Y") + TimeZone.utcoffset(None), "%d/%m/%Y %H:%M:%S")
except: except:
dt_texte = payload dt_texte = payload
#Affichage du résultat #Affichage du résultat
draw.rectangle((0, 34, epd2in13.EPD_HEIGHT, 48), fill=white) draw.rectangle((0, 34, epd2in13.EPD_HEIGHT, 48), fill=white)
draw.text((0, 34), dt_texte, font=font14, fill=black) draw.text((0, 34), dt_texte, font=font14, fill=black)
update_epd() update_epd()
@ -112,14 +118,14 @@ last photo)
""" """
def on_message_camera(client, userdata, msg): def on_message_camera(client, userdata, msg):
pl = msg.payload.decode() pl = msg.payload.decode()
print("{} : {} ({})".format(msg.topic, pl, type(pl))) print("{} : {} ({})".format(msg.topic, pl, type(pl)))
# Photo en cours # Photo en cours
if pl == "1" and msg.topic == camera_mqtt_topic + "/shooting" : if pl == "1" and msg.topic == camera_mqtt_topic + "/shooting" :
draw.rectangle((0, 106, epd2in13.EPD_HEIGHT, epd2in13.EPD_WIDTH), fill=white) draw.rectangle((0, 106, epd2in13.EPD_HEIGHT, epd2in13.EPD_WIDTH), fill=white)
draw.text((0, 106), "Shooting photo... ", font=font14, fill=black) draw.text((0, 106), "Shooting photo... ", font=font14, fill=black)
update_epd() update_epd()
#Dernière photo prise #Dernière photo prise
if msg.topic == camera_mqtt_topic + "/last_photo": if msg.topic == camera_mqtt_topic + "/last_photo":
draw.rectangle((0, 106, epd2in13.EPD_HEIGHT, epd2in13.EPD_WIDTH), fill=white) draw.rectangle((0, 106, epd2in13.EPD_HEIGHT, epd2in13.EPD_WIDTH), fill=white)
@ -195,4 +201,3 @@ print(mqtt_host + ":" + str(mqtt_port))
mqtt_client.connect(mqtt_host, int(mqtt_port), 60) mqtt_client.connect(mqtt_host, int(mqtt_port), 60)
mqtt_client.loop_forever() mqtt_client.loop_forever()