diff --git a/raspberry/python/mqtt2epaper.py b/raspberry/python/mqtt2epaper.py index f902134..ef5c2ad 100644 --- a/raspberry/python/mqtt2epaper.py +++ b/raspberry/python/mqtt2epaper.py @@ -1,9 +1,25 @@ -# -*- coding: utf-8 -*- +#!/usr/bin/python3 +# -*- coding: UTF8 -*- + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + """ Display data from MQTT broker on ePaper screen @author: arofarn """ +__version__ = 0.2 ########### # IMPORTS # @@ -25,11 +41,10 @@ mqtt_client_id = "epaper_display" white = 255 black = 0 - -""" -Update ePaper Display -""" def update_epd(): + """ + Update ePaper Display + """ #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° screen = image.rotate(90 + (epd_rotate * 180), expand=True, translate=(-6*epd_rotate, 0)) @@ -41,10 +56,10 @@ def update_epd(): # CALLBACKS # ############# -""" -Callback pour la connection au MQTT : souscriptions aux topics -""" def on_connect(client, userdata, flags, rc): + """ + Callback pour la connection au MQTT : souscriptions aux topics + """ print(mqtt.connack_string(rc)) if rc == 0: print("Subscribing to %s ..." % mqtt_topic) @@ -53,11 +68,12 @@ def on_connect(client, userdata, flags, rc): ) print("OK") -""" -Callback de gestion des messages arrivant au MQTT : -affichage et enregistrement en base de données -""" + def on_message(client, userdata, msg): + """ + Callback de gestion des messages arrivant au MQTT : + affichage et enregistrement en base de données + """ top=msg.topic[len(mqtt_topic)-1:].strip() subtopics = top.split("/") @@ -65,7 +81,6 @@ def on_message(client, userdata, msg): payload = msg.payload.decode() val = json.loads(payload) - #Test présence et cohérence de la valeur try: val['value'] = float(val['value'] ) @@ -85,38 +100,35 @@ def on_message(client, userdata, msg): print(val) if val['type'] in coord_type: #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, coord_type[val['type']][1] + 20), fill=white) #draw new values draw.text(coord_type[val['type']], "{:6.1f}{} ".format(val['value'], val['unit']), font=font20_bold, fill=black) + update_epd() + + # Print feather's date and time on epaper display + elif subtopics[0] == "SYS": + payload = msg.payload.decode() + val = json.loads(payload) + if val['type'] == "TIME": + try: + #Mise en forme de la date et heure reçu pour fuseau horaire configuré + dt_texte = datetime.strftime(datetime.strptime(val['value'], "%Y/%m/%d_%H:%M:%S") + TimeZone.utcoffset(None), + "%d/%m/%Y %H:%M:%S") + except: + dt_texte = payload + print(dt_texte) + draw.rectangle((0, 34, epd2in13.EPD_HEIGHT, 48), fill=white) + draw.text((0, 34), dt_texte, font=font14, fill=black) + update_epd() -""" -Callback managing date and time from MQTT broker -""" -def on_message_date(client, userdata, msg): - payload = msg.payload.decode() - print("Date : " + payload) - - try: - #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") - except: - dt_texte = payload - - #Affichage du résultat - draw.rectangle((0, 34, epd2in13.EPD_HEIGHT, 48), fill=white) - draw.text((0, 34), dt_texte, font=font14, fill=black) - update_epd() - -""" -Update display with info from camera (camera shooting new photo or name of the -last photo) -""" def on_message_camera(client, userdata, msg): + """ + Update display with info from camera (camera shooting new photo or name of the + last photo) + """ pl = msg.payload.decode() print("{} : {} ({})".format(msg.topic, pl, type(pl))) @@ -132,10 +144,11 @@ def on_message_camera(client, userdata, msg): draw.text((0, 106), "Last: " + pl, font=font14, fill=black) update_epd() -""" -Callback de déconnexion au broker MQTT -""" + def on_disconnect(client, userdata, msg): + """ + Callback de déconnexion au broker MQTT + """ if msg != 0: print("Déconnexion imprévu : %s" % msg) exit() @@ -168,8 +181,15 @@ draw = ImageDraw.Draw(image) #Draw basic text and frame : draw.rectangle((0, 0, epd2in13.EPD_HEIGHT, 18), fill=black) -draw.text((60, 1), "Projet Camétéo", font=font16_bold, fill=white) -draw.text((0, 19), "{} : IP= {}".format(socket.gethostname(), netifaces.ifaddresses('wlan0')[2][0]['addr']), font = font12, fill=black ) +draw.text((60, 1), + "Projet Camétéo", + font=font16_bold, + fill=white) +draw.text((0, 19), + "{} : IP= {}".format(socket.gethostname(), + netifaces.ifaddresses('wlan0')[2][0]['addr']), + font = font12, + fill=black ) draw.line((0,49,epd2in13.EPD_HEIGHT, 49), fill=black) draw.line((0,71,epd2in13.EPD_HEIGHT, 71), fill=black) draw.line((0,93,epd2in13.EPD_HEIGHT, 93), fill=black) @@ -194,7 +214,7 @@ 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("feather0/NTP/date", on_message_date) mqtt_client.message_callback_add(camera_mqtt_topic + "/#", on_message_camera) print(mqtt_host + ":" + str(mqtt_port)) diff --git a/raspberry/python/uart2mqtt.py b/raspberry/python/uart2mqtt.py index dd7911d..f7dac8a 100644 --- a/raspberry/python/uart2mqtt.py +++ b/raspberry/python/uart2mqtt.py @@ -17,7 +17,7 @@ """ Transmit data from UART (GPIO) to MQTT broker """ -__version__ = "0.1" +__version__ = "0.2" # Dictionnary of data types and units data_type = {'cput' : "AT",