ajout des programmes

This commit is contained in:
Raphael Payet 2023-06-13 19:26:19 +04:00
parent ff881c10cc
commit 2157e40bfe
3 changed files with 101 additions and 0 deletions

26
code/capteur.py Executable file
View File

@ -0,0 +1,26 @@
import machine, onewire, ds18x20, time, dht
ds_pin = machine.Pin(5)
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
sensor = dht.DHT22(machine.Pin(4))
roms = ds_sensor.scan()
while True:
ds_sensor.convert_temp()
time.sleep_ms(750)
try:
time.sleep(2)
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
temp_f = temp * (9/5) + 32.0
print('Temperature: %3.1f C' %temp)
print('Humidity: %3.1f %%' %hum)
except OSError as e:
print('Failed to read sensor.')
for rom in roms:
print(ds_sensor.read_temp(rom), "° (avec sonde 1-wire)")
print('-')
time.sleep(5)

49
code/main.py Executable file
View File

@ -0,0 +1,49 @@
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.')

26
code/wifi.py Executable file
View File

@ -0,0 +1,26 @@
ssid = "PT4ObjConnect" # wifi router name
pw = "Admin2022" # wifi router password
import network
sta_if = network.WLAN(network.STA_IF)
sta_if.disconnect()
def do_connect():
import network
sta_if = network.WLAN(network.STA_IF)
sta_if.disconnect()
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect(ssid, pw)
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())#ip, masque , passerelle, dns
def configure_AP():
import network
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid='MP115', password='MP115')
do_connect()
#configure_AP()