Skip to content

Raspberry Pi Pico W BME280: Send data to Blynk | ShillehTek

October 23, 2023

Video Tutorial (Optional)

Watch first if you want to follow along while setting up the Blynk dashboard and sending BME280 sensor data from a Raspberry Pi Pico W.

Project Overview

In this project, you use a Raspberry Pi Pico W with a BME280 temperature, humidity, and pressure sensor to stream live readings to a Blynk dashboard so you can monitor data remotely in an app.

Blynk is an Internet of Things (IoT) platform that lets you connect hardware (Arduino, Raspberry Pi, ESP8266, and more) to the internet and control or monitor it using a mobile app. The same basic steps can be used for iOS, Android, or desktop dashboards, as long as you have an active internet connection.

You do not have to use the BME280 specifically. You can stream any data you like, as long as you update the code and Blynk datastreams accordingly.

  • Time: 20 to 40 minutes
  • Skill level: Beginner
  • What you will build: A Blynk dashboard that displays temperature, humidity, and pressure sent from a Pico W over Wi-Fi

Parts List

From ShillehTek

External

Note: The code uses I2C(0) on the Pico W with SDA on GP0 and SCL on GP1. The example Blynk datastreams use Virtual Pins V7, V8, and V9, but you must match whatever you configure in your own Blynk dashboard.

Step-by-Step Guide

Step 1 - Set up your Blynk dashboard

Goal: Create a Blynk device and dashboard widgets that will display your sensor readings.

What to do: Create a Blynk account at blynk.io, then create a new device from your dashboard.

Blynk dashboard device creation screen used for a Raspberry Pi Pico W sensor project
Creating a new device in the Blynk dashboard.
  • Create a new device on your dashboard.
  • You can select Template > Quickstart Template.
  • Save the information Blynk provides, especially the Auth Token, because you will need it in your MicroPython code.
  • Edit the dashboard from the dropdown.
Blynk Quickstart Template selection screen for creating an IoT dashboard
Blynk template selection and device setup.
  • Delete the dashboard components and drag and drop three Gauge widgets from the Widget Box.
  • Edit each Gauge and set the titles to Temperature, Humidity, and Pressure.
  • Create a new Datastream for each Gauge and select a unit and Virtual Pin. In this example, the virtual pins are V7, V8, and V9.
  • Optionally adjust min, max, and other widget properties to your needs.
Blynk dashboard showing three gauge widgets configured for temperature, humidity, and pressure virtual pins
Example dashboard layout using three gauges.

Expected result: You have a Blynk device created, you saved your Auth Token, and your dashboard has three Gauge widgets bound to virtual pins (for example V7, V8, V9).

Step 2 - Prepare the Pico W libraries and constants

Goal: Make sure your Pico W has the required MicroPython libraries and configuration values.

What to do: On your Pico W filesystem:

  • Add the Blynk library file blynklib.py to your libs folder (or whatever path your environment uses): https://github.com/shillehbean/youtube-channel/blob/main/blynklib.py
  • Install the MicroPython bme280 library (Thonny package manager is one option).
  • Create or edit your constants module so it contains your Wi-Fi name, Wi-Fi password, and Blynk Auth Token.
  • Confirm your Blynk Virtual Pin numbers match the ones you assigned to the dashboard gauges.

Expected result: Your Pico W environment has blynklib.py available, the bme280 library installed, and your constants file contains the internet credentials and Blynk auth token.

Step 3 - Upload the MicroPython code and run it

Goal: Read BME280 data over I2C and publish it to Blynk virtual pins.

What to do: Copy the following code to your Pico W and run it. Update your constants (internet name, password, and auth token) and update the virtual pins if your dashboard uses different ones.

Code:

from machine import Pin, I2C
import network
import time

from blynklib import Blynk
import bme280

import constants


i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
BLYNK = Blynk(constants.BLYNK_AUTH_TOKEN)


def connect_to_internet(ssid, password):
    # Pass in string arguments for ssid and password

    # Just making our internet connection
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(ssid, password)

    # Wait for 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...')
        time.sleep(1)

    # Handle connection error
    if wlan.status() != 3:
        print(wlan.status())
        raise RuntimeError('network connection failed')
    else:
        print('connected')
        print(wlan.status())
        status = wlan.ifconfig()


connect_to_internet(constants.INTERNET_NAME, constants.INTERNET_PASSWORD)


while True:
    bme = bme280.BME280(i2c=i2c)
    temperature, pressure, humidity = bme.read_compensated_data()

    # Print sensor data to console
    print('Temperature: {:.1f} C'.format(temperature/100))
    print('Humidity: {:.1f} %'.format(humidity/1024))
    print('Pressure: {:.1f} hPa'.format(pressure/25600))

    BLYNK.virtual_write(7, temperature/100)
    BLYNK.virtual_write(8, humidity/1024)
    BLYNK.virtual_write(9, pressure/25600)

    BLYNK.run()
    time.sleep(1)

Expected result: The Pico W connects to Wi-Fi, prints temperature, humidity, and pressure to the console, and your three Blynk Gauge widgets update using the virtual pins you configured.

Conclusion

You built a simple IoT dashboard by sending BME280 temperature, humidity, and pressure data from a Raspberry Pi Pico W to Blynk using virtual pins. This approach is quick to launch and easy to extend for other sensors and data streams.

Want the exact parts used in this build? Grab what you need from ShillehTek.com. If you want help customizing this project or building something similar for your product, check out our IoT consulting services.