Easily Create an IoT App with Blynk and Raspberry Pi Pico W
Tags: raspberrypi, iot, webdev, beginners
Published: 2023-06-16 18:35:11

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

Blynk is an Internet of Things (IoT) platform that allows you to easily build and control connected projects. It provides a simple way to connect hardware devices, such as Arduino, Raspberry Pi, ESP8266, and others, to the internet and control them remotely using a mobile app.

In today’s tutorial, we will be showing how to create a Desktop App using Blynk with the Raspberry Pi Pico W and the BME280 temperature, humidity, and pressure sensor. It is incredibly simple to set up and free to get started, really the only thing you need is an active internet connection, and the same steps can be utilized to set up an IOS or Android app.

Another thing to note is that you do not have to use the BME280 sensor, you can stream any data you like. If you are interested in buying a BME280, you can purchase one from [Amazon](https://www.amazon.com/dp/B0BXWHTXWT?maas=maas_adg_BBDCA6DDAD1DFDD3ED8775988114F6FB_afap_abs&ref_=aa_maas&tag=maas).

Either way, let’s get into it, but be sure to like, comment, and subscribe to continue to support ShillehTek content!

**Step 1-) Setup Blynk Dashboard**

- Create a Blynk account at blynk.io
- Create a new device on your dashboard as follows:

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

- You can select Template > Quickstart Template
- Save the information they give you, you will need the Auth Key in your MicroPython Code

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

- Edit the dashboard from the dropdown
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xkk55zpc6qa9d4dlvvo6.png)
- Delete the dashboard components and drag and drop three gauges from the Widget Box.

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

- Edit the three Widget Boxes respectively with titles Temperature, Humidity, and Pressure. You can create a new Datastream for each one and select a unit and Virtual Pin. I selected virtual pins 7, 8, and 9 respectively for my Gauges. You can alter the max and min values and other properties.
- That is all you need to get set up for Blynk Dashboard.

**Step 2-) Code Setup**

Now that you have your dashboard set up, you can use the following code on your Pico W to send the information you would like.

```
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)
```

<u>Some notes about the code.</u>

- You need the Blynk library file in your libs folder on your Pico W. You can find it here -> https://github.com/shillehbean/youtube-channel/blob/main/blynklib.py
- You can download the micropython bme280 library from the package manager in Thonny if you are using the BME280
- You will need to replace the virtual pin numbers with the numbers you assigned on Blynk
- You will also have to replace the values of the constants for the internet name, password, and Blynk auth token
- We utilize the virtual_write method to send information to the gauges we defined. You can customize these as you like.
- That is the code at a high level, if you have everything set up correctly you should be able to run it with no issues!

**Conclusion**

This is a fantastic way to create simple IoT apps that actually have powerful use cases. The biggest benefit of using this method is that it is so quick to launch an app that many users can begin to use it.

The drawback is that you will hit a paywall if you actually want to use this for production since there are many useful features that you will want/have to use down the line if you want to scale. However, their pricing is pretty solid. Wish I had a discount to give you!

Other than that please do not forget to subscribe to ShillehTek on [Youtube](https://www.youtube.com/@mmshilleh). Let me know if there are any questions will try my best to help.