import network
import time
from umqttsimple import MQTTClient
from machine import Pin
import dht
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASSWORD = ""
MQTT_BROKER = "broker.emqx.io"
MQTT_PORT = 1883
MQTT_CLIENT_ID = b"pico-w-mqtt-08"
STUDENT = "student08"
TOPIC_TEMP = (STUDENT + "/pico/temp").encode()
TOPIC_HUMID = (STUDENT + "/pico/humid").encode()import network
import time
from umqttsimple import MQTTClient
from machine import Pin
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASSWORD = ""
MQTT_BROKER = "broker.emqx.io"
MQTT_PORT = 1883
MQTT_CLIENT_ID = b"pico-w-mqtt-08"
MQTT_SUBSCRIBE_TOPIC = b"student08/pico/cmd"
led = Pin("LED",Pin.OUT)
# led.off()
led.value(0)
PING_INTERVAL = 30
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
print("Connecting to Wifi ....")
print("SSID :",WIFI_SSID)
wlan.connect(WIFI_SSID,WIFI_PASSWORD)
timeout = 10
while not wlan.isconnected() and timeout > 0:
print("Waiting to connection ...")
time.sleep(1)
timeout = timeout - 1
if wlan.isconnected():
print("Wifi Connected")
print("Network Config:")
print(wlan.ifconfig())
return wlan
else :
print("Wifi connection failed")
return None
def on_message(topic,msg):
text = msg.decode()
print("---------------------")
print("Message received")
print("Topic: ",topic.decode())
print("Message: ",text)
print("--------------------")
if led is not None:
if text == "LED ON":
led.value(1)
print("LED ON")
elif text == "LED OFF":
led.value(0)
print("LED OFF")
def connect_mqtt():
print("Connecting to MQTT Broker...")
print("Broker: ",MQTT_BROKER)
print("Port:",MQTT_PORT)
print("Client ID:",MQTT_CLIENT_ID.decode())
client = MQTTClient(
client_id = MQTT_CLIENT_ID,
server= MQTT_BROKER,
port=MQTT_PORT,
keepalive=60
)
client.set_callback(on_message)
client.connect()
print("MQTT Connected!")
client.subscribe(MQTT_SUBSCRIBE_TOPIC)
print("Subscribe to :",MQTT_SUBSCRIBE_TOPIC.decode())
return client
wifi = connect_wifi()
if wifi is not None:
mqtt_client = connect_mqtt()
else :
mqtt_client = None
last_ping = time.time()
while True:
mqtt_client.check_msg()
now = time.time()
if now - last_ping >= PING_INTERVAL:
mqtt_client.ping()
last_ping = now
time.sleep(0.1)
PUBLISH_INTERVAL = 3
sensor = dht.DHT22(Pin(20))
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
print("Connecting to Wifi ....")
print("SSID :",WIFI_SSID)
wlan.connect(WIFI_SSID,WIFI_PASSWORD)
timeout = 10
while not wlan.isconnected() and timeout > 0:
print("Waiting to connection ...")
time.sleep(1)
timeout = timeout - 1
if wlan.isconnected():
print("Wifi Connected")
print("Network Config:")
print(wlan.ifconfig())
return wlan
else :
print("Wifi connection failed")
return None
def on_message(topic,msg):
text = msg.decode()
print("-------------------------")
print("Message received")
print("Topic: ",topic.decode())
print("Message :",text)
print("--------------------------")
if led is not None:
if text == "LED ON":
led.value(1)
print("LED เปิด")
elif text == "LED OFF":
led.value(0)
print("LED ปิด")
def connect_mqtt():
print("Connecting to MQTT Broker...")
print("Broker: ",MQTT_BROKER)
print("Port:",MQTT_PORT)
print("Client ID:",MQTT_CLIENT_ID.decode())
client = MQTTClient(
client_id = MQTT_CLIENT_ID,
server= MQTT_BROKER,
port=MQTT_PORT,
keepalive=60
)
client.set_callback(on_message)
client.connect()
print("MQTT Connected!")
client.subscribe(MQTT_SUBSCRIBE_TOPIC)
print("Subscribed to :",MQTT_SUBSCRIBE_TOPIC.decode())
return client
wifi = connect_wifi()
if wifi is not None:
mqtt_client = connect_mqtt()
else :
mqtt_client = None
last_ping = time.time()
while True:
mqtt_client.check_msg()
now = time.time()
if now - last_ping >= PING_INTERVAL:
mqtt_client.ping()
last_ping = now
time.sleep(0.1)