diff --git a/raspberry/python/mqtt2epaper.py b/raspberry/python/mqtt2epaper.py index ef5c2ad..12aad0b 100644 --- a/raspberry/python/mqtt2epaper.py +++ b/raspberry/python/mqtt2epaper.py @@ -45,10 +45,12 @@ 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)) - #Mis en memoire de l'image à afficher et affichage de l'image + # 90° rotation + 180° if parameter epd_rotate different of 0 + # The small translation corrects an offset after rotation of 270° + screen = image.rotate(90 + (epd_rotate * 180), + expand=True, + translate=(-6*epd_rotate, 0)) + # Put the picture to display in frame memory and display the new frame epd.set_frame_memory(screen, 0, 0) epd.display_frame() @@ -57,9 +59,7 @@ def update_epd(): ############# def on_connect(client, userdata, flags, rc): - """ - Callback pour la connection au MQTT : souscriptions aux topics - """ + """On connection callback : topics subscription""" print(mqtt.connack_string(rc)) if rc == 0: print("Subscribing to %s ..." % mqtt_topic) @@ -70,28 +70,28 @@ def on_connect(client, userdata, flags, rc): def on_message(client, userdata, msg): - """ - Callback de gestion des messages arrivant au MQTT : - affichage et enregistrement en base de données - """ + """Callback for managing data sent by MQTT broker""" + top=msg.topic[len(mqtt_topic)-1:].strip() + #List of the subtopics subtopics = top.split("/") + #Display atmospheric data from BME280 sensor if subtopics[0] == "BME": payload = msg.payload.decode() val = json.loads(payload) - #Test présence et cohérence de la valeur + #Is value consistent ? 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 + #Manage special character "degree" val['unit'] = val['unit'].replace('deg', '°') - #Affichage des données + #List of 'displayable' data types and their coordinate on screen coord_type = { 'AT' : (12, 50), 'RH' : (137, 50), 'AP' : (12, 72), @@ -99,46 +99,48 @@ def on_message(client, userdata, msg): } print(val) if val['type'] in coord_type: - #erase former text + #Erase old data draw.rectangle(coord_type[val['type']] + (coord_type[val['type']][0] + 112, coord_type[val['type']][1] + 20), fill=white) - #draw new values + #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é + #Check the date and time and offset to the right timezone 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) + # First erase old text draw.rectangle((0, 34, epd2in13.EPD_HEIGHT, 48), fill=white) + # and draw the new date draw.text((0, 34), dt_texte, font=font14, fill=black) + update_epd() def on_message_camera(client, userdata, msg): - """ - Update display with info from camera (camera shooting new photo or name of the - last photo) - """ + """Update display with info from camera (camera shooting new picture or name + of the last photo)""" + pl = msg.payload.decode() print("{} : {} ({})".format(msg.topic, pl, type(pl))) - # Photo en cours + # If a picture is been taken if pl == "1" and msg.topic == camera_mqtt_topic + "/shooting" : draw.rectangle((0, 106, epd2in13.EPD_HEIGHT, epd2in13.EPD_WIDTH), fill=white) draw.text((0, 106), "Shooting photo... ", font=font14, fill=black) update_epd() - #Dernière photo prise + #Last picture name (with date/time) if msg.topic == camera_mqtt_topic + "/last_photo": draw.rectangle((0, 106, epd2in13.EPD_HEIGHT, epd2in13.EPD_WIDTH), fill=white) draw.text((0, 106), "Last: " + pl, font=font14, fill=black) @@ -146,11 +148,9 @@ def on_message_camera(client, userdata, msg): def on_disconnect(client, userdata, msg): - """ - Callback de déconnexion au broker MQTT - """ + """Disconnection callback""" if msg != 0: - print("Déconnexion imprévu : %s" % msg) + print("Unexpected disconnection : {}".format(msg)) exit() ######## @@ -179,17 +179,19 @@ font20_bold = ImageFont.truetype(epd_bold_font_file, 20) image = Image.new('1', (epd2in13.EPD_HEIGHT, epd2in13.EPD_WIDTH), 255) draw = ImageDraw.Draw(image) -#Draw basic text and frame : +#Draw title in white on black draw.rectangle((0, 0, epd2in13.EPD_HEIGHT, 18), fill=black) draw.text((60, 1), "Projet Camétéo", font=font16_bold, fill=white) +#Write hostname and IP address draw.text((0, 19), "{} : IP= {}".format(socket.gethostname(), netifaces.ifaddresses('wlan0')[2][0]['addr']), font = font12, fill=black ) +# Draw the black lines for data table 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) @@ -199,11 +201,6 @@ draw.line((125, 49, 125, 93), fill=black) epd.clear_frame_memory(0xFF) update_epd() -# -#screen = image.rotate(90, expand=True) -#epd.set_frame_memory(screen, 0, 0) -#epd.display_frame() - #Toggle to partial refresh epd.init(epd.lut_partial_update) @@ -213,8 +210,7 @@ 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("feather0/NTP/date", on_message_date) +#Special callback for camera mqtt_client.message_callback_add(camera_mqtt_topic + "/#", on_message_camera) print(mqtt_host + ":" + str(mqtt_port))