import network
import time
import machine
import dht
import json
import urequests
# ==============================
# WIFI
# ==============================
SSID = "Wokwi-GUEST"
PASSWORD = ""
# ==============================
# TAGOIO
# ==============================
TAGOIO_TOKEN = "1ffdc31d-d5a8-4ed7-8b80-0727a4cd8f29"
TAGOIO_URL = "https://api.tago.io/data"
# ==============================
# CAPTEURS
# ==============================
ldr = machine.ADC(26)
dht_sensor = dht.DHT22(machine.Pin(5))
led = machine.Pin("LED", machine.Pin.OUT)
# ==============================
# CONNEXION WIFI
# ==============================
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(SSID, PASSWORD)
print("Connexion WiFi...")
while not wifi.isconnected():
time.sleep(1)
print("WiFi connecté")
print(wifi.ifconfig())
# ==============================
# ENVOI VERS TAGOIO
# ==============================
def send_to_tagoio(temperature, humidity, light):
payload = [
{"variable": "temperature", "value": temperature, "unit": "C"},
{"variable": "humidity", "value": humidity, "unit": "%"},
{"variable": "light", "value": light, "unit": "%"},
]
headers = {
"Content-Type": "application/json",
"Device-Token": TAGOIO_TOKEN,
}
try:
response = urequests.post(TAGOIO_URL, data=json.dumps(payload), headers=headers)
print("TagoIO status:", response.status_code)
response.close()
except Exception as e:
print("Erreur envoi TagoIO :", e)
# ==============================
# BOUCLE PRINCIPALE
# ==============================
while True:
try:
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
value = ldr.read_u16()
light = int(value * 100 / 65535)
print("T:", temperature, "H:", humidity, "L:", light)
send_to_tagoio(temperature, humidity, light)
except Exception as e:
print("Erreur capteur :", e)
time.sleep(10)
# import network
# import time
# import machine
# import dht
# import json
# from simple import MQTTClient
# # ==============================
# # WIFI
# # ==============================
# SSID = "Wokwi-GUEST"
# PASSWORD = ""
# # ==============================
# # MQTT
# # ==============================
# BROKER = "broker.hivemq.com"
# BROKER_PORT = 1883
# CLIENT_ID = "amal"
# PUB_TOPIC = b"pico/data"
# SUB_TOPIC = b"pico/control"
# # ==============================
# # CAPTEURS
# # ==============================
# # LDR sur GP26 ADC0
# ldr = machine.ADC(26)
# # DHT22 sur GP5
# dht_sensor = dht.DHT22(machine.Pin(5))
# # LED interne Pico W
# led = machine.Pin("LED", machine.Pin.OUT)
# # ==============================
# # CONNEXION WIFI
# # ==============================
# wifi = network.WLAN(network.STA_IF)
# wifi.active(True)
# wifi.connect(SSID, PASSWORD)
# print("Connexion WiFi...")
# while not wifi.isconnected():
# time.sleep(1)
# print("WiFi connecté")
# print(wifi.ifconfig())
# # ==============================
# # CALLBACK MQTT
# # ==============================
# def callback(topic, msg):
# message = msg.decode()
# print("Commande reçue :", message)
# if message == "ON":
# led.value(1)
# print("LED ON")
# elif message == "OFF":
# led.value(0)
# print("LED OFF")
# # ==============================
# # CONNEXION MQTT
# # ==============================
# client = None
# def connect_mqtt():
# global client
# try:
# client = MQTTClient(
# CLIENT_ID,
# BROKER,
# BROKER_PORT,
# keepalive=60
# )
# client.set_callback(callback)
# client.connect()
# client.subscribe(SUB_TOPIC)
# print("MQTT connecté")
# except Exception as e:
# print("Erreur connexion MQTT :", e)
# time.sleep(5)
# connect_mqtt()
# connect_mqtt()
# # ==============================
# # BOUCLE PRINCIPALE
# # ==============================
# while True:
# try:
# # Lecture DHT22
# dht_sensor.measure()
# temperature = dht_sensor.temperature()
# humidity = dht_sensor.humidity()
# # Lecture LDR
# value = ldr.read_u16()
# light = int(value * 100 / 65535)
# # Données JSON
# data = {
# "temperature": temperature,
# "humidity": humidity,
# "light": light
# }
# print("Envoi MQTT :", data)
# # Publication MQTT
# client.publish(PUB_TOPIC, json.dumps(data))
# # Vérifier commandes reçues
# client.check_msg()
# except Exception as e:
# print("Erreur MQTT :", e)
# print("Reconnexion...")
# try:
# client.disconnect()
# except:
# pass
# time.sleep(3)
# connect_mqtt()
# time.sleep(5)
# projet fle7aa capteur NTC ,soil measure
# import network
# import time
# import dht
# from simple import MQTTClient
# from machine import Pin, ADC
# import ujson
# rain_sensor = ADC(26)
# soil_hum_sens = ADC(27)
# conversion_factor = 100 / 65535
# soil_temp_sensor = ADC(28)
# relay = Pin(0, Pin.OUT)
# wlan = network.WLAN(network.STA_IF)
# wlan.active(True)
# wlan.connect("BEE_2B45", "E01CFC9B2B45")
# mqtt_server = "broker.emqx.io"
# client_id = "rain"
# pub_topic = b"pico/data"
# topic_sub = b"pico/control"
# def mqtt_callback(topic, msg):
# message = msg.decode("utf-8")
# print("Received from Node-RED:", message)
# if message == "auto_mode":
# if soil_hum < 50:
# relay.on()
# else:
# relay.off()
# if message == "ir_on":
# relay.on()
# if message == "ir_off":
# relay.off()
# def mqtt_connect():
# client = MQTTClient(client_id, mqtt_server)
# client.set_callback(mqtt_callback)
# client.connect()
# client.subscribe(topic_sub)
# return client
# client = mqtt_connect()
# while True:
# rain = rain_sensor.read_u16() * conversion_factor
# soil_hum = soil_hum_sens.read_u16() * conversion_factor
# temperature = soil_temp_sensor.read_u16() * conversion_factor
# data = {
# "rain": rain,
# "soil_humidity": soil_hum,
# "temperature": temperature
# }
# client.publish(pub_topic, ujson.dumps(data))
# client.check_msg()
# time.sleep(2)