2017-09-18 22:37:04 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Display data from MQTT broker on ePaper screen
|
2017-09-16 01:36:47 +02:00
|
|
|
|
2017-09-18 22:37:04 +02:00
|
|
|
@author: arofarn
|
|
|
|
"""
|
2017-09-16 01:36:47 +02:00
|
|
|
|
|
|
|
###########
|
|
|
|
# IMPORTS #
|
|
|
|
###########
|
|
|
|
|
2017-09-18 22:37:04 +02:00
|
|
|
from cameteo_conf import *
|
2017-09-16 01:36:47 +02:00
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
from spidev import SpiDev
|
|
|
|
import EPD_driver
|
|
|
|
import json
|
|
|
|
import netifaces
|
|
|
|
|
2017-09-16 22:40:14 +02:00
|
|
|
mqtt_client_id = "epaper_display"
|
|
|
|
|
2017-09-16 01:36:47 +02:00
|
|
|
#############
|
|
|
|
# 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)
|
2017-09-18 22:37:04 +02:00
|
|
|
client.subscribe([(mqtt_topic, 0),
|
|
|
|
(camera_mqtt_topic + "/#", 0)]
|
|
|
|
)
|
2017-09-16 01:36:47 +02:00
|
|
|
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("/")
|
|
|
|
|
2017-09-16 22:40:14 +02:00
|
|
|
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)
|
2017-09-16 01:36:47 +02:00
|
|
|
|
2017-09-18 23:10:36 +02:00
|
|
|
#Callback managing date and time from MQTT broker
|
2017-09-16 01:36:47 +02:00
|
|
|
def on_message_date(client, userdata, msg):
|
|
|
|
payload = msg.payload.decode()
|
2017-09-16 22:40:14 +02:00
|
|
|
print("Date : " + payload)
|
|
|
|
try:
|
|
|
|
d = datetime.strptime(payload, "%H:%M:%S %d/%m/%Y")
|
2017-09-18 22:37:04 +02:00
|
|
|
disp.Dis_String(0, 17, datetime.strftime(d + TimeZone.utcoffset(None), "%d/%m/%Y %H:%M:%S"), 16)
|
2017-09-16 22:40:14 +02:00
|
|
|
except:
|
|
|
|
disp.Dis_String(0, 17, payload, 16)
|
|
|
|
|
2017-09-18 22:37:04 +02:00
|
|
|
#Update display with info from camera (camera shooting new photo or name of the
|
|
|
|
# last photo)
|
2017-09-16 22:40:14 +02:00
|
|
|
def on_message_camera(client, userdata, msg):
|
2017-09-18 22:37:04 +02:00
|
|
|
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)
|
|
|
|
|
2017-09-16 01:36:47 +02:00
|
|
|
#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)
|
2017-09-16 22:40:14 +02:00
|
|
|
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)
|
2017-09-16 01:36:47 +02:00
|
|
|
|
|
|
|
#Connect to MQTT broker and loop...
|
2017-09-16 22:40:14 +02:00
|
|
|
mqtt_client = mqtt.Client(mqtt_client_id, clean_session=True)
|
2017-09-16 01:36:47 +02:00
|
|
|
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)
|
2017-09-16 22:40:14 +02:00
|
|
|
mqtt_client.message_callback_add(camera_mqtt_topic + "/#", on_message_camera)
|
2017-09-16 01:36:47 +02:00
|
|
|
|
|
|
|
print(mqtt_host + ":" + str(mqtt_port))
|
|
|
|
mqtt_client.connect(mqtt_host, int(mqtt_port), 60)
|
|
|
|
|
|
|
|
mqtt_client.loop_forever()
|
|
|
|
|