How to Use ThingSpeak with the Raspberry Pi Pico W
About the project
Learn how to create a real-time environmental monitoring system with the Raspberry Pi Pico W and ThingSpeak!
Project info
Difficulty: Easy
Estimated time: 1 hour
License: GNU Lesser General Public License version 3 or later (LGPL3+)
Items used in this project
Hardware components
Story
In this tutorial, we'll learn how to use ThingSpeak, a popular IoT platform, with the Raspberry Pi Pico W to collect and visualize sensor data. We'll be using a BME280 sensor to measure temperature, pressure, and humidity and send this data to ThingSpeak for real-time monitoring and analysis.
ThingSpeak is an open-source Internet of Things (IoT) platform that allows you to collect, store, analyze, visualize, and act on data from sensors or devices. It's particularly popular for its ease of use and integration with various hardware platforms like Raspberry Pi, Arduino, and ESP8266/ESP32.
In the end, we will have a dashboard that looks as follows, a simple weather station with gauges and graphs that updates in real time:
Getting StartedRequirements- Raspberry Pi Pico W
- BME280 sensor from ShillehTek
- Jumper wires
- Breadboard
- A computer with Thonny IDE installed
- Wi-Fi network credentials
- ThingSpeak account
If you are interested in using the BME280 specifically, there is another ShillehTek tutorial on YouTube here that quickly walks you through how to get setup.
-----
Before we get into the remainder, consider subscribing or supporting the channel. Also, be sure to checkout our various stores to shop products for Raspberry Pi, Arduino, ESP32, and DIY electronics at ShillehTek!
Subscribe:
Support:
https://www.buymeacoffee.com/mmshilleh
Hire me at UpWork to build your IoT projects:
https://www.upwork.com/freelancers/~017060e77e9d8a1157
Visit ShillehTek Store for Arduino and Raspberry Pi Sensors and Pre-SolderedComponents:
ShillehTek Website (Exclusive Discounts):
https://shillehtek.com/collections/all
ShillehTekAmazon Store:
ShillehTek Amazon Store - Canada
ShillehTek Amazon Store - Japan
Step 1-) Setting Up ThingSpeak- Create a ThingSpeak Account: If you don't have one, sign up for a ThingSpeak account at thingspeak.com.
- Create a New Channel:Go to the Channels tab and click "New Channel".Name your channel (e.g., "Pico W Weather Station").Add three fields: Temperature, Pressure, and Humidity.Save the channel and note down the
Write API Key
. You can get it in the API Keys tab.
Creating Visualizations -
Numeric Display for Temperature:
- Go to your channel view and click on the "Add Widget" button.
- Select "Numeric Display" from the widget options.
- Choose the field corresponding to Temperature.
- Save the widget to add it to your channel dashboard.
Graph for Pressure:
- Click on the "Add Visualizations" button.
- Choose the field corresponding to Pressure.
- Configure the time span and any other settings you prefer.
- Save the widget to add it to your channel dashboard.
Gauge for Humidity:
- Click on the "Add Widget" button.
- Select "Gauge" from the widget options.
- Choose the field corresponding to Humidity.
- Configure the gauge with appropriate min and max values (e.g., 0 to 100%).
- Save the widget to add it to your channel dashboard.
Now that you have all of this, you have the basis of what you need to start populating and visualizing the sensor data from the ShillehTek BME280 in ThinkSpeak. If you are using another sensor, you can play around with other widgets and visualizations as needed, the interface is fairly straightforward.
Step 2-) MicroPython CodeHere's the full code to read data from the BME280 sensor and send it to ThingSpeak:
from machine import Pin, I2C
import network
import urequests
import time
from time import sleep
import bme280
import constants
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
ssid = constants.INTERNET_NAME
password = constants.INTERNET_PASSWORD
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
api_key = 'your_API_KEY'
base_url = 'https://api.thingspeak.com/update'
def read_sensor():
bme = bme280.BME280(i2c=i2c)
temperature, pressure, humidity = [float(i) for i in bme.values]
return temperature, pressure, humidity
# Send data to ThingSpeak
while True:
temperature, pressure, humidity = read_sensor()
url = f"{base_url}?api_key={api_key}&field1={temperature}&field2={pressure}&field3={humidity}"
response = urequests.get(url)
print(response.text)
time.sleep(15)
Code Walkthrough- Import Libraries: We import necessary libraries including
network
for Wi-Fi connectivity,urequests
for HTTP requests, andbme280
for sensor interaction. - Initialize I2C: Set up I2C communication with the BME280 sensor.
- Connect to Wi-Fi: Use your Wi-Fi credentials to connect the Pico W to the internet.
- Read Sensor Data: Define a function
read_sensor
to get temperature, pressure, and humidity readings from the BME280 sensor. - Send Data to ThingSpeak: In an infinite loop, read data from the sensor and send it to ThingSpeak every 15 seconds.
If you are using the BME280 library, you need to modify the return values by removing all suffixes (e.g., 'C', 'hPa', '%') so that the data is interpretable by the IoT platform. Additionally, if you are using an earlier version of MicroPython, make sure you install urequests
, as it may not come pre-installed with some versions.
You also have the option to hardcode your Wi-Fi credentials directly into the code instead of using a constants file. Simply replace the placeholders with your actual Wi-Fi SSID and password.
Once your script is running, you should see your ThingSpeak dashboard updating in real-time! This setup can be further customized to suit your specific needs. Additionally, ThingSpeak offers various other data integration and visualization options to explore.
ConclusionCongratulations! You have successfully set up your Raspberry Pi Pico W to send sensor data to ThingSpeak. This setup allows you to monitor environmental conditions in real-time from anywhere. ThingSpeak provides powerful tools for data analysis and visualization, making it an excellent choice for IoT projects. Happy tinkering! Also, do not forget to subscribe if you have not!
Credits
mahmood-m-shilleh
Mechanical and Software Engineering Background. University at Buffalo 2019 Texas A&M 2021 I make data pipelines for my day job. Outside of work, I participate in online communities regarding Full Stack Engineering, Microelectronics, and more. You can find more details about me on my Youtube Channel. https://www.youtube.com/@mmshilleh Feel free to reach out!
Leave your feedback...