Last active
August 3, 2019 17:29
-
-
Save amusarra/645c34b5dab4e3b18eb50529d2ba76a2 to your computer and use it in GitHub Desktop.
Raspberry PI Sense HAT: Come pubblicare i dati su CloudAMQP via MQTT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
try: | |
from sense_hat import SenseHat | |
except ImportError: | |
from sense_emu import SenseHat | |
import paho.mqtt.client as paho | |
import time | |
import datetime | |
import json | |
import socket | |
import signal | |
import sys | |
import uuid | |
__author__ = "Antonio Musarra <[email protected]>" | |
__copyright__ = "Copyright 2018 Antonio Musarra's Blog" | |
__version__ = "1.0.0" | |
__license__ = "MIT" | |
sleepTime = 5 | |
deviceId = socket.gethostname() + "-" + str(uuid.uuid4()) | |
sense = SenseHat() | |
sense.set_rotation(180) | |
red = (255, 0, 0) | |
# Return Sense temperature | |
def get_sense_temperature(): | |
return sense.temp | |
# Return Sens Pressure | |
def get_sense_pressure(): | |
return sense.pressure | |
# Return Sense Humidity | |
def get_sense_humidity(): | |
return sense.humidity | |
# Callback methods | |
def on_publish(client, userdata, mid): | |
print("Data published (Mid: %s)" % mid) | |
sense.show_message("Data published", text_colour=red) | |
def on_log(client, userdata, level, buf): | |
print(buf) | |
def on_connect(client, userdata, flags, rc): | |
if rc == 0: | |
print("Connected to MQTT broker (RC: %s)" % rc) | |
else: | |
print("Connection to MQTT broker failed (RC: %s)" % rc) | |
def on_disconnect(client, userdata, rc): | |
if rc != 0: | |
print("Unexpected disconnect") | |
else: | |
print("Disconnected from MQTT broker") | |
def disconnect(signum, frame): | |
""" | |
Signal handler to ensure we disconnect cleanly | |
in the event of a SIGTERM or SIGINT. | |
""" | |
client.loop_stop() | |
client.disconnect() | |
print("Exiting on signal %s" % signum) | |
sys.exit(signum) | |
client = paho.Client(deviceId) | |
client.username_pw_set( | |
"mygcwrqi:mygcwrqi", | |
password="qd2vevNTwprtOj_fwyb5LnL90nDxNmw3") | |
# Register callbacks | |
client.on_connect = on_connect | |
client.on_disconnect = on_disconnect | |
client.on_log = on_log | |
client.on_publish = on_publish | |
# Connect to MQTT broker | |
client.connect("whale.rmq.cloudamqp.com", 1883) | |
client.loop_start() | |
# use the signal module to handle signals | |
signal.signal(signal.SIGTERM, disconnect) | |
signal.signal(signal.SIGINT, disconnect) | |
# Collect telemetry information from Sense HAT and publish to | |
# MQTT broker in JSON format | |
while True: | |
temperature = get_sense_temperature() | |
pressure = get_sense_pressure() | |
humidity = get_sense_humidity() | |
telemetryData = dict(DeviceId=deviceId, | |
Timestamp=datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3], | |
Temperature=str(temperature), | |
Pressure=str(pressure), | |
Humidity=str(humidity) | |
) | |
telemetryDataJson = json.dumps(telemetryData) | |
(rc, mid) = client.publish( | |
"RPi.Data", | |
telemetryDataJson, qos=1) | |
time.sleep(sleepTime) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
try: | |
from sense_hat import SenseHat | |
except ImportError: | |
from sense_emu import SenseHat | |
sense = SenseHat() | |
sense.show_message("Benvenuto su https://www.dontesta.it") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
try: | |
from sense_hat import SenseHat | |
except ImportError: | |
from sense_emu import SenseHat | |
sense = SenseHat() | |
while True: | |
temperature = sense.get_temperature() | |
pressure = sense.get_pressure() | |
humidity = sense.get_humidity() | |
sense.show_message( | |
"The temperaturen is %d, the pressure is %d, and the humidity is %d." % (temperature, pressure, humidity)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment