Raspberry Pi announced an incredible sale. They’re offering their Wi-Fi board for just $7, making it perfect for quickly transforming into a portable Wi-Fi thermometer.
What’s even better is that development is now easier than ever. You no longer need to wrestle with complex C or C++ programming—MicroPython makes coding for it accessible to everyone.
Traditionally, you can watch this project in a video:
Today, we’ll not only read sensor data but also send it to a Wi-Fi network, enabling us to view it on a mobile phone. Yes, anyone on the local network will be able to see the temperature in real-time. Let’s get started!
Temperature sensorThe DS18B20 is a popular and affordable sensor, costing around two dollars. To use it, we’ll also need a resistor, which typically comes included in the sensor kit.
I can start setting up the project in Visual Studio Code. First, I’ll open an empty folder. Then, I’ll navigate to the Settings menu, open Extensions, search for "Python, " and install it. After that, I’ll add the Raspberry Pi Pico and MicroPico plugins.
Next, I’ll create a simple Python script to test the setup. I’ll name the file main.py
and add the line:
print("Hello!")
To connect the Pico board, I’ll go to View > Command Palette, search for the “Connect” action from MicroPico, and establish the connection.
Once connected, I can upload the project and press “Run” to execute the file. If everything works, the “Hello” output confirms the Pico is functioning correctly!
Now, let’s talk about connecting the thermosensor. You can either use a sensor module with an integrated resistor (no soldering required) or solder a resistor directly between the red and yellow wires of the temperature sensor. Both options work, so it’s up to your preference.
I chose the soldering method because it’s more compact. I soldered the sensor directly to the Raspberry Pi Pico to keep the setup small and neat.
Though a breadboard could have been used, I wanted this sensor dedicated permanently to the thermometer.
Additionally, I designed a custom case in Fusion 360, which can be downloaded and 3D-printed.
Since I don’t know who will receive the message, I’ll use UDP broadcast messages to notify all devices connected to the same Wi-Fi network.
Under the hood, the Raspberry PI Pico sends a message to a Wi-Fi router:
The router forwards the message to all devices in the local network:
All we need to know is just the current configuration of the Pico Wi-Fi connection and port.
SoftwareWith the hardware ready, it’s time for the software. I’ll use a framework with pre-built classes to simplify the process.First, I’ll clone the code repository using the command:
git init
git submodule add https://github.com/Nerdy-Things/itk_pico.git
After cloning, the itk_pico
folder will appear in the explorer.
Let’s check out the temperature.py
file. This class initializes the sensor and includes a method to read its temperature.
import machine
import onewire
import ds18x20
import time
from .logger import Logger
class TemperatureSensor:
_pin: int = 0
_one_wire: onewire.OneWire
_sensor: ds18x20.DS18X20
_devices = []
def __init__(self, pin: int) -> None:
self._pin = pin
self._one_wire = onewire.OneWire(machine.Pin(pin))
self._sensor = ds18x20.DS18X20(self._one_wire)
self._devices = self._sensor.scan()
Logger.print("Found devices:", self._devices)
if not self._devices:
raise RuntimeError("No DS18B20 found!")
def get_temperature(self):
for device in self._devices:
self._sensor.convert_temp()
time.sleep(1)
temp = self._sensor.read_temp(device)
Logger.print("Temperature:", temp)
return temp
To implement it:
from itk_pico.temperature import TemperatureSensor
temperature_sensor = TemperatureSensor(15)
while True:
temperature = temperature_sensor.get_temperature()
sleep(1)
The next step is transmitting temperature data to the local network. To achieve this, I’ll set up a Wi-Fi connection:
from itk_pico.wifi import WiFi
......
wifi = WiFi()
wifi.connect("SSID", "PASSWORD") # Enter credentials
while True:
wifi.try_reconnect_if_lost()
....
To broadcast the temperature data:
from itk_pico.udp import Udp
from itk_pico.ip_utils import get_broadcast_address
import json
UDP_PORT = 5468
......
udp = Udp(UDP_PORT)
......
while True:
......
broadcast_ip_address = get_broadcast_address(wifi.get_ip_address(), wifi.get_subnet_mask())
message_dict = {"name": "Kitchen", "temperature": temperature}
message = json.dumps(message_dict)
udp.send(message, broadcast_ip_address, UDP_PORT)
......
The full code:
from itk_pico.temperature import TemperatureSensor
from itk_pico.wifi import WiFi
from itk_pico.udp import Udp
from itk_pico.ip_utils import get_broadcast_address
from time import sleep
import json
UDP_PORT = 5468
temperature_sensor = TemperatureSensor(15)
wifi = WiFi()
udp = Udp(UDP_PORT)
wifi.connect("itkacher", "itkacher")
while True:
wifi.try_reconnect_if_lost()
temperature = temperature_sensor.get_temperature()
broadcast_ip_address = get_broadcast_address(wifi.get_ip_address(), wifi.get_subnet_mask())
message_dict = {"name": "Kitchen", "temperature": temperature}
message = json.dumps(message_dict)
udp.send(message, broadcast_ip_address, UDP_PORT)
sleep(10)
Once the code is running, I can see in the terminal that messages are being sent. To verify, I’ll use Wireshark on my laptop to filter UDP messages on the same port and confirm that the JSON data matches the terminal output.
Finally, let’s display the data using a cross-platform app. I've created a simple app that can be run on iOS, Android, and Desktop.
It's available here. So, let's clone it!
git clone https://github.com/Nerdy-Things/raspberry-pi-pico-thermo-sensor-cross-platform.git
To run the desktop app, simply execute:
cd raspberry-pi-pico-thermo-sensor-cross-platform
cd kotlin-multiplatform
./gradlew run
It will launch the desktop version:
To run the app on an Android phone, connect it to your computer and press the Run button in Android Studio.
For iPhones, open the iosApp.xcodeproj
file in Xcode and click Run to build and install the app.
With this setup, you can connect as many sensors as you like!
In less than ten minutes and for under ten dollars, we’ve built a fully functional Wi-Fi thermometer. Isn’t that amazing?
Cheers!
Comments