### Author: Enzo Tiberghien - et223ea
# Building a Weather Station with DHT11 Sensor and LED using Raspberry Pi Pico W
## Description
This project aims to build a weather station using the DHT11 sensor to measure temperature and humidity. The Raspberry Pi Pico W microcontroller, along with MicroPython, will be utilized to collect sensor data. Additionally, an LED will be incorporated into the weather station to indicate when the humidity exceeds a certain threshold. The collected data will be transmitted to Ubidots, an IoT platform, for storage and visualization.
## Objective
The objective of this project is to create a weather station that provides real-time temperature and humidity data. The chosen components and technologies will allow for accurate measurement and data transmission. The LED indicator will serve as an immediate visual cue when the humidity exceeds a predefined threshold. By utilizing Ubidots as the IoT platform, the collected data can be stored and visualized, enabling users to monitor and analyze the weather conditions over time. The project aims to offer insights into the local climate, facilitating data-driven decision-making and enhancing overall environmental awareness.
Overall, the estimated time to complete this project, including setup, assembly, coding, and testing, is approximately 3 to 4 hours
## Materials
- Raspberry Pi Pico W: The Raspberry Pi Pico W microcontroller board will serve as the main control unit for the weather station.
- USB-A cable: A USB-A cable is needed to connect the Raspberry Pi Pico W to a computer for programming and power supply
- DHT11 sensor: The DHT11 sensor will be used to measure temperature and humidity.
- Breadboard: A breadboard with sufficient points (such as an 830-point breadboard) will be used for circuit connections
- LED: A LED will be integrated into the weather station to indicate when the humidity exceeds a specific threshold.
- Jumper wires: Male-to-male jumper wires will be used to establish connections between the Raspberry Pi Pico W, DHT11 sensor, LED, and breadboard.
All the material was found here

## Computer Setup
1. Install VS Code on your computer.
2. Download the MicroPython firmware for Raspberry Pi Pico W from the official MicroPython website.
3. Connect the Raspberry Pi Pico W to your computer using a USB cable.
## Putting Everything Together
1. Connect the DHT11 sensor to the Raspberry Pi Pico W as follows:
- DHT11 VCC to Raspberry Pi Pico W 3V3 (3.3V) pin
- DHT11 GND to Raspberry Pi Pico W GND (Ground) pin
- DHT11 DATA to Raspberry Pi Pico W GP4 (GPIO4) pin
2. Connect the LED to the Raspberry Pi Pico W as follows:
- LED anode (longer leg) to Raspberry Pi Pico W GP5 (GPIO5) pin
- LED cathode (shorter leg) to a resistor (220 ohms)
- Connect the other end of the resistor to Raspberry Pi Pico W GND (Ground) pin
3. Make sure all connections are secure and the components are properly inserted into the breadboard.
## Platform
For this project, we will use Ubidots as the IoT platform to store and visualize the collected data. Please create an account on Ubidots and obtain an API token.
## The Code
#### How to upload the code
1. Download and install the Pymakr extension from Visual Studio Code.
2. Open Visual Studio Code and navigate to the project folder.
3. Connect the Raspberry Pi Pico W to your computer via USB.
4. In Visual Studio Code, click on the Pymakr icon in the sidebar to open the Pymakr console.
5. Select the appropriate serial port for the Raspberry Pi Pico W in the Pymakr console.
6. Click on the "Connect" button in the Pymakr console to establish a connection.
7. Once connected, you can click on the "Upload" button in the Pymakr console to upload the code to the Raspberry Pi Pico W.
8. Make sure to follow the specific instructions provided by Pymakr for your operating system and hardware setup.
```python
# boot.py -- run on boot-up
import network
from time import sleep
import machine
WIFI_SSID = "Your router name" # Assign your the SSID of your network
WIFI_PASS = "Your password" # Assign your the password of your network
# Function for WiFi connection
def connect():
wlan = network.WLAN(network.STA_IF) # Put modem on Station mode
if not wlan.isconnected(): # Check if already connected
print('connecting to network...')
wlan.active(True) # Activate network interface
# set power mode to get WiFi power-saving off (if needed)
wlan.config(pm = 0xa11140)
wlan.connect(WIFI_SSID, WIFI_PASS) # Your WiFi Credential
print('Waiting for connection...', end='')
# Check if it is connected otherwise wait
while not wlan.isconnected() and wlan.status() >= 0:
print('.', end='')
sleep(1)
# Print the IP assigned by router
ip = wlan.ifconfig()[0]
print('\nConnected on {}'.format(ip))
return ip
connect()
# Main.py
# main.py -- put your code here!
import machine
import dht
import utime
import urequests as requests
import network
from time import sleep
import random
TOKEN = "" #Put here your TOKEN
DEVICE_LABEL = "" # Assign the device label desire to be send
VARIABLE_LABEL = "" # Assign the variable label desire to be send
DELAY = 5 # Delay in seconds
# Initialize the DHT11 sensor
sensor = dht.DHT11(machine.Pin(27))
led_pin = machine.Pin(13, machine.Pin.OUT)
# Builds the json to send the request
def build_json(variable, value):
try:
data = {variable: {"value": value}}
return data
except:
return None
# Sending data to Ubidots Restful Webserice
def sendData(device, variable, value):
try:
url = "https://industrial.api.ubidots.com/"
url = url + "api/v1.6/devices/" + device
headers = {"X-Auth-Token": TOKEN, "Content-Type": "application/json"}
data = build_json(variable, value)
if data is not None:
print(data)
req = requests.post(url=url, headers=headers, json=data)
return req.json()
else:
pass
except:
pass
def read_sensor():
sensor.measure()
temperature = sensor.temperature()
humidity = sensor.humidity()
print("Temperature: {}°C".format(temperature))
print("Humidity: {}%".format(humidity))
if humidity > 50:
# Turn on the LED
led_pin.on()
print("LED turned on")
else:
# Turn off the LED
led_pin.off()
print("LED turned off")
# Main loop to continuously read sensor data
while True:
sensor.measure()
temperature = sensor.temperature()
humidity = sensor.humidity()
print("Temperature: {}°C".format(temperature))
print("Humidity: {}%".format(humidity))
sendData(DEVICE_LABEL, "temperature", temperature)
sendData(DEVICE_LABEL, "humidity", humidity)
sleep(DELAY)
```
## Transmitting the Data / Connectivity
In this project, the temperature and humidity data will be transmitted to Ubidots using the Ubidots API. In the code provided, the data is transmitted every 5 seconds. You can adjust the delay between data transmissions by modifying the DELAY variable in the code. Simply change the value to the desired delay in seconds. For example, to transmit data every 10 seconds, modify the line DELAY = 5 to DELAY = 10. The code includes a function `sendData()` that sends the data to Ubidots using the HTTP POST method.
## Presenting the Data
To view and analyze the data on Ubidots:
1. Log in to your Ubidots account.
2. Create a new device and variable with labels that match the ones used in the code.
3. Ubidots provides various visualization options, including line charts, gauges, and maps. Customize your dashboard according to your preferences.

## Finalizing the Design
Congratulations! You have successfully built a weather station using the DHT11 sensor, Raspberry Pi Pico W, breadboard, and LED. The data is being transmitted to Ubidots for storage and visualization. Experiment with different threshold values and explore additional features provided by Ubidots to enhance your weather station.
