import network
import time
import urequests as requests
import random
# ===== WiFi 設定 =====
WIFI_SSID1 = '1234' # "Your network name";
WIFI_PASS1 = '5678' # "Your network password";
WIFI_SSID2 = "2345" # Your WiFi SSID
WIFI_PASS2 = "6789" # Your WiFi Password
WIFI_SSID3 = "3456" # Your WiFi SSID
WIFI_PASS3 = "7890" # Your WiFi Password
# ===== Webhook URL =====
# 以 Make.com 為例,可以建立一個 Webhook,會得到一個 URL
WEBHOOK_URL = "https://hook.eu2.make.com/bfwz66fne4x1dug4pphofpk2qc9d7hdc" # 改成你的 Webhook URL
# === 連線 WiFi ===
def connect_wifi():
print("Scanning for WiFi networks, please wait...")
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
num = 0
for (ssid, bssid, channel, RSSI, authmode, hidden) in wifi.scan():
if WIFI_SSID1 in ssid:
WIFI_SSID = WIFI_SSID1 # Your WiFi SSID
WIFI_PASS = WIFI_PASS1 # Your WiFi Password
num=1
break
elif WIFI_SSID2 in ssid:
WIFI_SSID = WIFI_SSID2 # Your WiFi SSID
WIFI_PASS = WIFI_PASS2 # Your WiFi Password
num=2
break
elif WIFI_SSID3 in ssid:
WIFI_SSID = WIFI_SSID3 # Your WiFi SSID
WIFI_PASS = WIFI_PASS3 # Your WiFi Password
num=3
break
elif "Wokwi-GUEST" in ssid:
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASS = ""
num=4
break
if (num == 0):
print("Couldn't get a wifi connection")
while True:
pass
# authmodes = ['Open', 'WEP', 'WPA-PSK' 'WPA2-PSK4', 'WPA/WPA2-PSK']
print("* {:s}".format(ssid))
# print(" - Auth: {} {}".format(authmodes[authmode], '(hidden)' if hidden else ''))
print(" - Channel: {}".format(channel))
print(" - RSSI: {}".format(RSSI))
print(" - BSSID: {:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}".format(*bssid))
if not wifi.isconnected():
print('Connecting to network...')
wifi.connect(WIFI_SSID, WIFI_PASS)
while not wifi.isconnected():
time.sleep(1)
print('Network config:', wifi.ifconfig())
return
# ===== 傳送 Webhook =====
def send_webhook(value):
try:
data = {"temperature": value}
res = requests.post(WEBHOOK_URL, json=data)
print("Upload:", data, "->", res.status_code)
res.close()
except Exception as e:
print("Error:", e)
# Start Function
if __name__ == '__main__':
connect_wifi() # Connecting to WiFi Router
while True:
print("----------------------")
temperature = random.randint(20, 30) # 假裝感測器數據
send_webhook(temperature)
time.sleep(20) # 每20秒上傳一次