Raspberry Pi Pico Send BME280 Readings to ThingSpeak

In this user guide, we will learn how to publish BME280 sensor readings to ThingSpeak using Micropython, Raspberry Pi Pico, and ESP-01 WiFi module. We will use Thonny IDE to program our Raspberry Pi Pico board which will be connected to a temperature, humidity, and pressure sensor. Our main aim is to transmit these sensor readings to ThingSpeak easily and interactively demonstrate them. Any appropriate sensor can be used such as DS18B20, BME680, LM35, and MPU6050 but for this article, we will use a BME280 sensor which is used to measure temperature, pressure, and humidity.

Raspberry Pi Pico Send BME280 Readings to ThingSpeak

Raspberry Pi Pico does not support Wi-Fi capabilities hence we have to use a separate Wi-Fi module to enable Wi-Fi connectivity. Therefore, we will interface and program ESP-01 Wi-Fi module with Raspberry Pi Pico to enable Wi-Fi features. We will use Thonny IDE to program Raspberry Pi Pico with ESP-01 and BME280 in MircoPython. We will use AT commands through the serial port that is UART to configure the ESP-01 Wi-Fi module.

The following content will be covered in this article:

  1. Introduction to BME280 sensor
  2. Connecting Raspberry Pi Pico with BME280 and ESP-01 Wi-Fi module
  3. Getting ThingSpeak API Ready
  4. Publish to multiple fields of sensor readings to ThingSpeak (Temperature, humidity and pressure)

We have similar guides with DHT22 and DS18B20:

Prerequisites

Before we start this lesson make sure you are familiar with and have the latest version Python 3 in your system, have set up MicoPython in Raspberry Pi Pico, and have a running Integrated Development Environment(IDE) in which we will be doing the programming. We will be using the same Thonny IDE as we have done previously when we learned how to blink and chase LEDs in micro-python. If you have not followed our previous tutorial, you check here:

If you are using uPyCraft IDE, you can check this getting started guide:

Recommended Readings:

BME280 sensor Introduction

The BME280 sensor is used to measure readings regarding ambient temperature, barometric pressure, and relative humidity. It is mostly used in web and mobile applications where low power consumption is key. This sensor uses I2C or SPI to communicate data with the micro-controllers. Although there are several different versions of BME280 available in the market, the one we will be studying uses the I2C communication protocol.

BME280 Pinout Diagram
BME280 Module Pinout

Interfacing Raspberry Pi Pico with BME280 and ESP-01

Raspberry Pi Pico with BME280 and ESP-01

This section shows how to connect Raspberry Pi Pico with BME280 sensor and ESP-01.

We will require the following components:

  • Raspberry Pi Pico
  • BME280 Sensor
  • ESP-01 Module
  • Connecting Wires
  • Breadboard

Raspberry Pi Pico with BME280

The connection of BME280 with the Raspberry Pi Pico is very simple. We have to connect the VCC terminal with 3.3V, ground with the ground (common ground), SCL of the sensor with SCL of the board, and SDA of the sensor with the SDA pin of the board.

Raspberry Pi Pico I2C Pins

Raspberry Pi Pico has two I2C controllers. Both I2C controllers are accessible through GPIO pins of Raspberry Pi Pico. The following table shows the connection of GPIO pins with both I2C controllers. Each connection of the controller can be configured through multiple GPIO pins as shown in the figure. But before using an I2C controller, you should configure in software which GPIO pins you want to use with a specific I2C controller. 

I2C ControllerGPIO Pins
I2C0 – SDAGP0/GP4/GP8/GP12/GP16/GP20
I2C0 – SCLGP1/GP5/GP9/GP13/GP17/GP21
I2C1 – SDAGP2/GP6/GP10/GP14/GP18/GP26
I2C1 – SCLGP3/GP7/GP11/GP15/GP19/GP27

The connections between the two devices which we are using can be seen below.

BME280Raspberry Pi Pico
VCC3.3V
SDAGP2 (I2C1 SDA)
SCLGP3 (I2C1 SCL)
GNDGND

We have used the same connections as specified in the table above. However, you can use other combinations of SDA/SCL pins as well but remember to change them in the MicroPython script.

Raspberry Pi Pico with ESP-01

The ESP-01 module consists of 8 pins. However, we will use 5 pins to connect with the Pi Pico board. These include the VCC, EN, GND, RX, and TX pins. RX and TX pins of the module will be connected with the UART pins of the Pi Pico board. Let us first have a look at the Raspberry Pi Pi UART Pins.

Raspberry Pi Pico UART Pins

Raspberry Pi Pico contains two identical UART peripherals with separate 32×8 Tx and 32×12 Rx FIFOs.

Raspberry Pi Pico pinout diagram

The following table lists the GPIO pins for both UART peripherals which are exposed on Raspberry Pi Pico development board pinouts. 

UART PinsGPIO Pins
UART0-TXGP0/GP12/GP16
UART0-RXGP1/GP13/GP17
UART1-TXGP4/GP8
UART1-RXGP5/GP9

For this guide, we will use UART0-TX and RX pins.

Follow the connection diagram below to connect the two devices.

Raspberry Pi PicoESP-01
3.3VVCC
3.3VEN
GNDGND
GP1 (UART0 RX)TX
GP0 (UART0 TX)RX

Connection Diagram Raspberry Pi Pico with BME280 and ESP-01

We have used the same connections as given in the two tables above. All three devices will be commonly grounded and will be powered with the same 3.3V pin of Raspberry Pi Pico.

The diagram below shows the connection diagram of Raspberry Pi Pico with BME280 and ESP-01.

Raspberry Pi Pico with BME280 and ESP-01 connection diagram
Raspberry Pi Pico with BME280 and ESP-01 connection diagram

BME280 MicroPython Library

We will have to install the BME280 library for MicroPython to continue with our project.

To successfully do that, open your Thonny IDE with your Raspberry Pi Pico plugged in your system. Go to Tools > Manage Packages. This will open up the Thonny Package Manager.

Raspberry Pi Pico Installing ssd1306 OLED library MicoPython 1

Search for “bme280” in the search bar by typing its name and clicking the button ‘Search on PyPI.’ From the following search results click on the one highlighted below: micropython-bme280. Install this library.

Installing BME280 Micropython library Thonny

After few moments, this library will get successfully installed. Now we are ready to program our Raspberry Pi Pico with a BME280 sensor in MicroPython.

Getting ThingSpeak API Ready

ThingSpeak is an open-source API that is used to store or retrieve data using HTTP or MQTT protocol. This takes place over the Internet or through the LAN. We will use this API to publish sensor readings from BME280 integrated with our Raspberry Pi Pico board. In ThingSpeak you can access your data from anywhere in the world. You can display your data in plots and graphs.

For more information about ThingSpeak API, you can have a look at our previous ESP32/ESP8266 tutorials given below:

Creating Account

ThingSpeak API is free to use but we will have to create a MathWorks Account.
First go to the following website: https://thingspeak.com/
The following window will appear. Click on the ‘Get Started for Free’ button.

ESP32 HTTP ThingSpeak get started

Now you will be redirected to the account window. If you already have an existing MathWorks account you can use that to log in. Otherwise, you will have to create a new one. Click ‘Create One!’ to make a new MathWorks account.

ESP32 HTTP ThingSpeak account

When you have successfully signed in you will receive the following notification:
Click ‘OK’.

ESP32 HTTP ThingSpeak account successful

Publish to multiple fields of sensor readings to ThingSpeak (Temperature, Humidity and Pressure)

We will start by creating a new channel for our project. Go to Channels > My Channels. Then click ‘New Channel’.

ESP32 HTTP ThingSpeak new channel

You will be prompted to give a name to your channel. We will give a name, some description and mark the first field. In this section, we will show you how to publish multiple data. You can use any name, description, and field according to your preference. There are a total of eight fields that we can add to our channel at the same time. We will tick the first three fields and add the names. Click ‘Save Channel’ to proceed.

thingspeak create new channel multiple fields1

Your channel will now be created.

thingspeak create new channel multiple fields2

Go to private view and click the pencil icon on top of each Field Chart. This will let us customize the graphs according to our preference. You can add details accordingly. After you press the Save button, the chart will get updated to the settings you just set.

thingspeak create new channel multiple fields3

After that go to the API key tab and click it. You will now be able to access your unique API key. Save it and keep it secure as you will need it later in the program code.

thingspeak create new channel multiple fields4

Raspberry Pi Pico & ESP-01 ThingSpeak MicroPython Sketch

Open your Thonny IDE and go to File > New to open a new file. Copy the code given below in that file. You just have to replace the network credentials and your ThingSpeak API key.

import uos
import machine
import utime
from machine import Pin, I2C        #importing relevant modules & classes
import bme280       #importing BME280 library

myHOST = 'api.thingspeak.com'
myPORT = '80'
myAPI = '****************'

print()
print("Machine: \t" + uos.uname()[4])
print("MicroPython: \t" + uos.uname()[3])

i2c=I2C(1,sda=Pin(2), scl=Pin(3), freq=400000)   #initializing the I2C method 

uart0 = machine.UART(0, baudrate=115200)
print(uart0)

def Rx_ESP_Data():
    recv=bytes()
    while uart0.any()>0:
        recv+=uart0.read(1)
    res=recv.decode('utf-8')
    return res
def Connect_WiFi(cmd, uart=uart0, timeout=3000):
    print("CMD: " + cmd)
    uart.write(cmd)
    utime.sleep(7.0)
    Wait_ESP_Rsp(uart, timeout)
    print()

def Send_AT_Cmd(cmd, uart=uart0, timeout=3000):
    print("CMD: " + cmd)
    uart.write(cmd)
    Wait_ESP_Rsp(uart, timeout)
    print()
    
def Wait_ESP_Rsp(uart=uart0, timeout=3000):
    prvMills = utime.ticks_ms()
    resp = b""
    while (utime.ticks_ms()-prvMills)<timeout:
        if uart.any():
            resp = b"".join([resp, uart.read(1)])
    print("resp:")
    try:
        print(resp.decode())
    except UnicodeError:
        print(resp)
    
Send_AT_Cmd('AT\r\n')          #Test AT startup
Send_AT_Cmd('AT+GMR\r\n')      #Check version information
Send_AT_Cmd('AT+CIPSERVER=0\r\n')      #Check version information
Send_AT_Cmd('AT+RST\r\n')      #Check version information
Send_AT_Cmd('AT+RESTORE\r\n')  #Restore Factory Default Settings
Send_AT_Cmd('AT+CWMODE?\r\n')  #Query the Wi-Fi mode
Send_AT_Cmd('AT+CWMODE=1\r\n') #Set the Wi-Fi mode = Station mode
Send_AT_Cmd('AT+CWMODE?\r\n')  #Query the Wi-Fi mode again
Connect_WiFi('AT+CWJAP="YOUR_SSID","YOUR_PASSWORD"\r\n', timeout=5000) #Connect to AP
Send_AT_Cmd('AT+CIFSR\r\n',timeout=5000)    #Obtain the Local IP Address
Send_AT_Cmd('AT+CIPMUX=1\r\n')    #Obtain the Local IP Address
utime.sleep(1.0)
print ('Starting connection to ESP8266...')
while True:
    bme = bme280.BME280(i2c=i2c)        #BME280 object created
    temperature = bme.values[0]         #reading the value of temperature
    pressure = bme.values[1]            #reading the value of pressure
    humidity = bme.values[2]            #reading the value of humidity
    print('Temperature: ', temperature[:-1])    #printing BME280 values
    print('Humidity: ', humidity[:-1])
    print('Pressure: ', pressure[:-3])
    print ('!About to send data to thingspeak')
    sendData = 'GET /update?api_key='+ myAPI +'&field1='+str(temperature[:-1]) +'&field2='+str(pressure[:-3]) +'&field3='+str(humidity[:-1])
    Send_AT_Cmd('AT+CIPSTART=0,\"TCP\",\"'+ myHOST +'\",'+ myPORT+'\r\n')
    utime.sleep(1.0)
    Send_AT_Cmd('AT+CIPSEND=0,' +str(len(sendData)+4) +'\r\n')
    utime.sleep(1.0)
    Send_AT_Cmd(sendData +'\r\n')
    utime.sleep(4.0)
    Send_AT_Cmd('AT+CIPCLOSE=0'+'\r\n') # once file sent, close connection
    utime.sleep(4.0)
    print ('Data send to thing speak')

How the Code Works?

We will start by importing the machine module and the uos module. We will also import I2C and Pin class from the machine module. This is because we have to specify the pin for I2C communication. We also import the utime module so that we will be able to add a delay in between our readings. Also, import the bme280 library which we previously installed.

import uos
import machine
import utime
from machine import Pin, I2C        #importing relevant modules & classes
import bme280       #importing BME280 library

Then, we will print the information about our current operating system in the Thonny shell terminal. We will uos.uname() and print the operating system version and release.

print()
print("Machine: \t" + uos.uname()[4])
print("MicroPython: \t" + uos.uname()[3])

Initialize I2C Communication

Next, we will initialize the I2C GPIO pins for SCL and SDA respectively. We have used the I2C1 SCL and I2C0 SDA pins.

We have created an I2C() method which takes in four parameters. The first parameter is the I2C channel that we are using. The second parameter specifies the I2C GPIO pin of the board which is connected to the SDA line. The third parameter specifies the I2C GPIO pin of the board which is connected to the SCL line. The last parameter is the frequency connection.

We are setting the SCL on pin 3 and the SDA on pin 2.

i2c=I2C(1,sda=Pin(2), scl=Pin(3), freq=400000)

Initialize UART Communication

Then we will create an uart object by using UART() and specify the UART channel as the first parameter and the baud rate as the second parameter. We are using UART0 in this case with baud rate 115200 for the uart communication. ESP8266 has a default baud rate of 115200 hence we will use the same baud rate here for Raspberry Pi Pico UART communication in order to create synchronization. Moreover we will also print the UART details in the shell terminal.

uart0 = machine.UART(0, baudrate=115200)
print(uart0)

Also, include the ThingSpeak host, port and write API key to successfully publish readings to the API. Make sure to replace your API key in the place specified by ‘****************’

myHOST = 'api.thingspeak.com'
myPORT = '80'
myAPI = '****************'

This Connect_WiFi() function is used to connect ESP8266 with WiFi.

def Connect_WiFi(cmd, uart=uart0, timeout=3000):
    print("CMD: " + cmd)
    uart.write(cmd)
    utime.sleep(7.0)
    Wait_ESP_Rsp(uart, timeout)
    print()

Next, we will define three functions. The first one is Rx_ESP_Data(). This reads the serial data being received. This data is decoded from UTF-8 format and returned.

def Rx_ESP_Data():
    recv=bytes()
    while uart0.any()>0:
        recv+=uart0.read(1)
    res=recv.decode('utf-8')
    return res

The second function is Send_AT_Cmd(cmd, uart=uart0, timeout=3000). It takes in three parameters, the AT command, the UART channel and the response time. This function will be used to it send an AT command to ESP8266 via uart0. The response time is set to 3 seconds.


def Send_AT_Cmd(cmd, uart=uart0, timeout=3000):
    print("CMD: " + cmd)
    uart.write(cmd)
    Wait_ESP_Rsp(uart, timeout)
    print()
    

The Wait_ESP_Rsp(uart=uart0, timeout=3000) function waits for 3 seconds to get the response from ESP8266. After receiving the data from ESP8266 it concatenates the received bytes and prints them on the shell terminal.

def Wait_ESP_Rsp(uart=uart0, timeout=3000):
    prvMills = utime.ticks_ms()
    resp = b""
    while (utime.ticks_ms()-prvMills)<timeout:
        if uart.any():
            resp = b"".join([resp, uart.read(1)])
    print("resp:")
    try:
        print(resp.decode())
    except UnicodeError:
        print(resp)

AT Commands

Now let us look at the series of AT commands that we will send through UART0 to ESP8266.

Send_AT_Cmd('AT\r\n')          #Test AT startup
Send_AT_Cmd('AT+GMR\r\n')      #Check version information
Send_AT_Cmd('AT+CIPSERVER=0\r\n')      #Check version information
Send_AT_Cmd('AT+RST\r\n')      #Check version information
Send_AT_Cmd('AT+RESTORE\r\n')  #Restore Factory Default Settings
Send_AT_Cmd('AT+CWMODE?\r\n')  #Query the Wi-Fi mode
Send_AT_Cmd('AT+CWMODE=1\r\n') #Set the Wi-Fi mode = Station mode
Send_AT_Cmd('AT+CWMODE?\r\n')  #Query the Wi-Fi mode again
Connect_WiFi('AT+CWJAP="YOUR_SSID","YOUR_PASSWORD"\r\n', timeout=5000) #Connect to AP
Send_AT_Cmd('AT+CIFSR\r\n',timeout=5000)    #Obtain the Local IP Address
Send_AT_Cmd('AT+CIPMUX=1\r\n')    #Obtain the Local IP Address
utime.sleep(1.0)
Send_AT_Cmd('AT+CIPSERVER=1,80\r\n')    #Obtain the Local IP Address
utime.sleep(1.0)

AT: This type of command is used to test the startup function of WiFi module. The response would be ok, against this command if everything is ok.

Send_AT_Cmd('AT\r\n')          #Test AT startup

AT+GMR : This type of AT command is used to check the version of AT command and we used SDK version of AT command in this type of WIFI module.

Send_AT_Cmd('AT+GMR\r\n')      #Check version information

AT+CIPSERVER=0: This configures the ESP8266 as server and sets the mode as 0 which means delete server (need to follow by restart)

Send_AT_Cmd('AT+CIPSERVER=0\r\n')     

AT+RST: This type of command is used for reset the WiFi module when it is in working condition. The response would be ok, when reset the module.

Send_AT_Cmd('AT+RST\r\n')   

AT+RESTORE: This type of command is used to restore factory settings means, when this command is entered then all the parameters are reset automatically to default one’s.

Send_AT_Cmd('AT+RESTORE\r\n')  #Restore Factory Default Settings

AT+CWMODE? : This type of command is used to query the WiFi mode of ESP8266.

Send_AT_Cmd('AT+CWMODE?\r\n')  #Query the WiFi mode

AT+CWMODE=1 : This sets the WiFi mode of ESP8266 in this case in station mode.

Send_AT_Cmd('AT+CWMODE=1\r\n') #Set the WiFi mode = Station mode

AT+CWJAP=”SSID”,”PASSWORD”\r\n’, timeout=TIME_ms : This connects the ESP8266 with an AP whose SSID and password are given, The timeout here is the reconnection time.

Connect_WiFi('AT+CWJAP="YOUR_SSID","YOUR_PASSWORD"\r\n', timeout=5000) #Connect to AP

AT+CIFSR: This command obtains the local IP address.

Send_AT_Cmd('AT+CIFSR\r\n')

AT+CIPMUX=1:This command is used to enable multiple connections (maximum 4)

Send_AT_Cmd('AT+CIPMUX=1\r\n')

AT+CIPSERVER=1: This command configures ESP8266 as server.

Send_AT_Cmd('AT+CIPSERVER=1,80\r\n')

Get Sensor Readings and Publish to ThingSpeak

Inside the while loop we will first create an object of bme280 named bme and access the temperature, humidity and pressure through it. These sensor readings will be saved in their respective variables: temperature, humidity, and pressure.

while True:
    bme = bme280.BME280(i2c=i2c)        #BME280 object created
    temperature = bme.values[0]         #reading the value of temperature
    pressure = bme.values[1]            #reading the value of pressure
    humidity = bme.values[2]            #reading the value of humidity

We will print these sensor readings without the unit in the Thonny shell terminal.

    print('Temperature: ', temperature[:-1])    #printing BME280 values
    print('Humidity: ', humidity[:-1])
    print('Pressure: ', pressure[:-3])

Finally, we will send these sensor readings to ThingSpeak dashboard.

print ('!About to send data to thingspeak')
    sendData = 'GET /update?api_key='+ myAPI +'&field1='+str(temperature[:-1]) +'&field2='+str(pressure[:-3]) +'&field3='+str(humidity[:-1])
    Send_AT_Cmd('AT+CIPSTART=0,\"TCP\",\"'+ myHOST +'\",'+ myPORT+'\r\n')
    utime.sleep(1.0)
    Send_AT_Cmd('AT+CIPSEND=0,' +str(len(sendData)+4) +'\r\n')
    utime.sleep(1.0)
    Send_AT_Cmd(sendData +'\r\n')
    utime.sleep(4.0)
    Send_AT_Cmd('AT+CIPCLOSE=0'+'\r\n') # once file sent, close connection
    utime.sleep(4.0)
    print ('Data send to thing speak')

We will create a variable ‘sendData’ that contains a GET request that we will make to ThingSpeak to update the respective fields with the values held in the variables.

sendData = 'GET /update?api_key='+ myAPI +'&field1='+str(temperature[:-1]) +'&field2='+str(pressure[:-3]) +'&field3='+str(humidity[:-1])

First, we are writing the AT command: AT+CIPSTART=0,\”TCP\”,\”‘+ myHOST +’\”,’+ myPORT+’\r\n’ This command is used to set the port connection and address of port.

Next after a delay of 1 second, we send the AT command: AT+CIPSEND=’ID’, ‘LENGTH’ This will set the length of the data that will be sent. It tells the WI-FI module how many bytes we want to send to the server.

Then we send the data to ThingSpeak via to be pusblished to their respective fields.

After that, we will close the multiple connections as we are sending the AT command: AT+CIPCLOSE=’ID’.

    Send_AT_Cmd('AT+CIPSTART=0,\"TCP\",\"'+ myHOST +'\",'+ myPORT+'\r\n')
    utime.sleep(1.0)
    Send_AT_Cmd('AT+CIPSEND=0,' +str(len(sendData)+4) +'\r\n')
    utime.sleep(1.0)
    Send_AT_Cmd(sendData +'\r\n')
    utime.sleep(4.0)
    Send_AT_Cmd('AT+CIPCLOSE=0'+'\r\n') # once file sent, close connection
    utime.sleep(4.0)
    print ('Data send to thing speak')

Demonstration

After you have uploaded your code to Raspberry Pi Pico board head over to the shell terminal. In the Thonny shell terminal, you will be able to view the board gets connected. After every few seconds, new BME280 sensor readings will keep appearing as they get published to their respective fields.

Raspberry Pi Pico send data to ThingSpeak shell terminal

Next, open the ThingSpeak API and you will be able to see temperature, pressure and humidity readings updating in your charts.

Raspberry Pi Pico send data to ThingSpeak
ThingSpeak Dashboard

Watch the demonstration below to have a better insight:

Conclusion

In conclusion, we were able to learn how to publish sensor readings to ThingSpeak using MicroPython and Raspberry Pi Pico using ESP-01 Wi-Fi module in a fairly easy way. Likewise, you can publish single or multiple data to ThingSpeak API which you will be able to access from anywhere around the world.

You can check these related BME280 tutorials:

Leave a Comment