50 lines
1.3 KiB
Python
Executable File
50 lines
1.3 KiB
Python
Executable File
from machine import Pin, SoftI2C
|
|
from umqtt.robust import MQTTClient
|
|
import sys
|
|
from time import sleep
|
|
import ssd1306,dht
|
|
import machine
|
|
#pin
|
|
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
|
|
sensor = dht.DHT22(machine.Pin(14))
|
|
|
|
# Définition de la tailler de l'écran oled
|
|
oled_width = 128
|
|
oled_height = 64
|
|
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
|
|
#Mqtt
|
|
client = MQTTClient(client_id=b'micropython',
|
|
server=b'192.168.1.75',
|
|
port=1883,
|
|
user=b'brocolis',
|
|
password=b'brocolis',
|
|
ssl=False)
|
|
|
|
try:
|
|
client.connect()
|
|
print('Connection ok');
|
|
except Exception as e:
|
|
print('Could not connect to MQTT server {}{}'.format(type(e).__name__, e))
|
|
sys.exit()
|
|
|
|
while True:
|
|
try:
|
|
sensor.measure()
|
|
temp = round(sensor.temperature(),2)
|
|
hum = round(sensor.humidity(),2)
|
|
#payload={"temp" : str(temp),"hum" : str(hum)}
|
|
payload='{"temp" :'+str(temp)+', "hum" :'+str(hum)+'}'
|
|
client.publish('/capteur/dht22', payload)
|
|
oled.text(str(hum)+"%", 0, 0)
|
|
oled.text(str(temp)+"C", 0, 10) # Affichage de la temperature
|
|
oled.show() # Affiche les valeurs sur l'écran
|
|
sleep(10)
|
|
oled.fill(0) # Renitialise l'affichage
|
|
|
|
except OSError as e:
|
|
print('Failed to read sensor.')
|
|
|
|
|
|
|
|
|