How to Use ThingSpeak with the Raspberry Pi Pico W
Tags: thingspeak, raspberrypi, programming, python
Published: 2024-05-31 23:41:15

{% embed https://www.youtube.com/watch?v=TPBf_Qaci8w %}

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:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vgq9qotuo12fa2jyk4yg.png)


**Requirements**

- Raspberry Pi Pico W
- [BME280 sensor from ShillehTek](https://www.amazon.com/dp/B0BQFV883T?maas=maas_adg_273838487DF6D497BDB90A5AA36C3658_afap_abs&ref_=aa_maas&tag=maas)
- [Jumper wires](https://www.amazon.com/dp/B0CN5RL615?maas=maas_adg_99FA512A620CB92E7455A93E620000C8_afap_abs&ref_=aa_maas&tag=maas)
- [Breadboard](https://shillehtek.com/blogs/news/how-to-use-thingspeak-with-the-raspberry-pi-pico-w)
- A computer with Thonny IDE installed
- Wi-Fi network credentials
- ThingSpeak account

**Wiring the BME280 Sensor**
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.

{% embed https://www.youtube.com/watch?v=R5dY2a0C9Y0 %}

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:**

[Youtube](https://www.youtube.com/@mmshilleh)

**Support:**

[https://buymeacoffee.com/mmshilleh](https://www.buymeacoffee.com/mmshilleh)

**Hire me at UpWork to build your IoT projects:**

[https://buymeacoffee.com/mmshilleh](https://www.upwork.com/freelancers/~017060e77e9d8a1157)

**Visit ShillehTek Store for Arduino and Raspberry Pi Sensors and Pre-Soldered Components:**

**ShillehTek Website (Exclusive Discounts):**

[https://buymeacoffee.com/mmshilleh](https://shillehtek.com/collections/all)

ShillehTekAmazon Store:

[ShillehTek Amazon Store - US](https://www.amazon.com/stores/page/F0566360-4583-41FF-8528-6C4A15190CD6?channel=yt)

[ShillehTek Amazon Store - Canada](https://www.amazon.ca/stores/page/036180BA-2EA0-4A49-A174-31E697A671C2?channel=canada)

[ShillehTek Amazon Store - Japan ](https://www.amazon.co.jp/stores/page/C388A744-C8DF-4693-B864-B216DEEEB9E3?channel=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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdz7yanzmqhsr8ch20q2.png)

**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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kx6hjhrm4bcd74567tbo.png)

**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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/efg7cz06zqod046fipot.png)



**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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amm4jeqs1xep92qezdt1.png)


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 ThingSpeak. 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 Code

Here'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, and bme280 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.

## Conclusion
Congratulations! 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!