Raspberry Pi Pico W
Sensor Readings Over WiFi
For this project, I connected up a sensor to the Pico W. I went with a DS18B20 breakout and connected it to Pin 15, 3V3 and GND according to the markings on the breakout. I also have a 4.7K resistor connecting the signal line to 3V3. You could replace this with a different sensor if you needed. This is a photograph of the breadboard,
Here is the code I used to serve the web page over WiFi. I used the CDN for Bootstrap 5 libraries to make a simple responsive page that I could check on my phone.
from time import sleep from network import WLAN, STA_IF from secrets import secrets from socket import socket, getaddrinfo from machine import Pin from onewire import OneWire from ds18x20 import DS18X20 html = """<!DOCTYPE html> <!DOCTYPE html> <html lang="en"> <head> <title>Temperature Server</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="container-fluid p-5 bg-primary text-white text-center"> <h1>Pico W Temperature Server</h1> </div> <div class="container-fluid"> <p>The temperature is %.2f°C.</p> </div> <body> </html> """ wlan = WLAN(STA_IF) # set up temperature sensor with onewire ow = OneWire(Pin(15)) ds = DS18X20(ow) r = ds.scan()[0] def connect(): wlan.active(True) wlan.config(pm = 0xa11140) wlan.connect(secrets["ssid"], secrets["password"]) # try to connect or fail max_wait = 10 while max_wait >0: if wlan.status() <0 or wlan.status()>=3: break max_wait -= 1 print("Waiting for connection...") sleep(1) # connection error if wlan.status() != 3: return False else: status = wlan.ifconfig() print("Connected to", secrets["ssid"], "on", status[0]) print() return True def disconnect(): wlan.disconnect() print("Disconnected") print() connect() addr = getaddrinfo('0.0.0.0', 80)[0][-1] s = socket() s.bind(addr) s.listen(1) while True: try: cl, addr = s.accept() print("Client connected from", addr) request = cl.recv(1024) #print(request) ds.convert_temp() t = round(ds.read_temp(r), 2) print(t) response = html % t cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n') cl.send(response) cl.close() except OSError as e: cl.close() print("Connection closed.")
This is a screenshot of the page I loaded on my phone. I did this at school, so I could be quite a distance from the room where I had the sensor and still be able to get a reading.