# Reference: https://github.com/lesp/LXF314---ntfy.sh-with-Raspberry-Pi-Pico-W
# https://github.com/robert-hh/Onewire_DS18X20
from machine import Pin
import machine, onewire, ds18x20, time
import network
import urequests as requests
# AP
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASS = ""
# ntfy.sh url
URL = "https://ntfy.sh/MicropythonTest"
def connect_wifi():
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
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
# Start Function
if __name__ == '__main__':
# Define GPIO pins for LED and push button
led1_pin = Pin(26, Pin.OUT)
led2_pin = Pin(27, Pin.OUT)
led1_pin.value(False)
led2_pin.value(False)
button_pin = Pin(22, Pin.IN, Pin.PULL_UP) # Internal pull-up resistor
ds_pin = machine.Pin(0)
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
roms = ds_sensor.scan()
print(roms)
connect_wifi() # Connecting to WiFi Router
requests.post(URL,
headers={"Content-Type": "text/plain"},
data="temperature")
#requests.close()
print("Send success!")
while True:
led1_pin.value(not led1_pin.value())
ds_sensor.convert_temp()
time.sleep(1)
for rom in roms:
led2_pin.value(True)
temp_c = ds_sensor.read_temp(rom)
temperature = str(temp_c)+" C" # Degree Celsius => \u2103
print(temperature)
#connect_wifi() # Connecting to WiFi Router
requests.post(URL,
headers={"Content-Type": "text/plain"},
data=temperature)
#requests.close()
print("Send success!")
led2_pin.value(False)
time.sleep(10)