How to Connect RaspberryPi Pico to WiFi & Send Notifications

by sylwekbala in Circuits > Raspberry Pi

7589 Views, 16 Favorites, 0 Comments

How to Connect RaspberryPi Pico to WiFi & Send Notifications

Cover-2.jpg

In this project we show how to connect Raspberry Pi Pico to WiFi by using the Cricket WiFi module and send an alert to our phone. We also explain why using Cricket will make the WiFi connectivity so power efficient that our device can last on single batteries for months or even years.

This project can be realised by everyone who has some basic IT skills. We demonstrate step by step what needs to be done to put together a complete device.

To keep it easy we write a simple Python program which flashes LED light every 5 minutes on the Pico board. When the LED flashes it also instructs Cricket to send a notification to our phone.

We chose Blynk to send notifications as it is a very user friendly and reliable multi-platform mobile app. However, it can be any other internet service or local network system of your preference. Cricket allows us to integrate our device to almost any software via either MQTT or HTTP(S) protocols without any programming. Please checkout Cricket's documentation or other projects to learn more. The possibilities are endless!

We aim with this project to provide a reference for anyone wanting to build various IoT devices with all types of sensors including cameras and even more advanced peripherals. Instead of flashing LED we might want to do more advanced local compute processing and logic e.g. machine learning inference on images or audio. By doing this we can build sophisticated devices and systems such as a security alarm system for our home, as one of the examples.

Also moving forward you might want to consider other microcontrollers than Pico, if you are looking for more specialised and more power efficiency. However Pico is great for rapid prototyping and getting things done almost instantaneously.

Let's get started!

Supplies

Step 1) Program Raspberry Pi Pico

Pico+USB.jpg

Raspberry Pi Pico is a tiny, low-cost computer that you can use to build various devices by attaching peripherals. It is a versatile microcontroller with flexible digital interfaces and great support of software making it super easy to code and program those little boards. However, it doesn't come with a WiFi connectivity feature. The good news is it is fairly easy to extend it with WiFi without losing its power efficiency, which we demonstrate in this project. (For more information visit Raspberry Pi Pico official website)

We strongly recommend, if you are not familiar with Pico, to follow this awesome Getting started with Raspberry Pi Pico tutorial.

Now we are going to write a Python program to flash LED light on the Pico board and raise a signal for Cricket to connect over WiFi to the internet and send notification to our phone.

We write a "foo" function to do that and the function is called with a 5 minute interval by using Timer. The "foo" function does the following:

  • blinks the built-in LED (Pin25) for 0.8 second
  • sets Pin15 as an OUT for 0.8 second with the value = 1

The Pin15 is used to raise a signal for the Cricket WiFi module. When the pin is set to 1, it wakes up Cricket to send a request to Blynk. We will show later how to configure it. When the Pin15 is 0, Cricket is completely powered off. While Pico runs its program the WiFi connectivity is OFF and doesn't consume any energy. It is only powered up when it is needed.

Below is a complete program:

main.py

from machine import Pin, Timer
import time led = Pin(25, Pin.OUT) cricket = Pin(15, Pin.OUT) timer = Timer() def foo(timer): led.value(1) cricket.value(1) time.sleep(0.8) led.value(0) cricket.value(0) # The freq = 0.00333 represents 5 minutes interval timer.init(freq=0.00333, mode=Timer.PERIODIC, callback=foo)

Please note we save the above program to the main.py file on Pico. It must be main.py because we want Pico to run it when powered directly on batteries (instead of USB).

Now we can test our program. We disconnect USB and attach the battery to see if the LED blinks every 5 minutes. If it does let's move on to the next step.

Step 2) Install Blynk App

Screenshot_20210324-220138_Blynk.jpg
Screenshot_20210324-220319_Blynk.jpg

What is Blynk? Blynk is a multi-platform mobile app for iOS and Android phones to receive data and control microcontrollers over the Internet. It comes with a digital dashboard where we can build a graphic interface for any project by simply dragging and dropping widgets. (See Blynk official website for more information)

To get started we need to install the Blynk app on our phone first. Let's pick the right one for our phone:

Once installed we need to configure it. Open Dashboard settings and add the "Generic Board" device with the connection type: "Wi-Fi". Once it is created press the Email button to send an email with a unique authentication token. We will need this token to configure Cricket to send notifications to our phone.

Now let's add a Notification widget to our Dashboard and press the Play button in the upper right corner.

This is it! Our app is ready to receive notifications.

Before we move to the next step we can test if it works by executing the following "curl" command line:

curl -X POST "http://blynk-cloud.com/YOUR_KEY/notify" -H "Content-Type: application/json" -d '{"body":"Hello from Curl!"}'

NOTE: replace YOUR_KEY with the key which was sent to you via email.

Step 3) Configuring Cricket

Cricket-cfg.jpg
cricket_battery-sqr.jpg

IOT Cricket is an easy to use ultra-low power WiFi module, which can operate directly on batteries for a very long time (for years in many use cases). We can simply attach a sensor & battery and the device is ready to transmit data instantaneously via WiFi networks.

Let's first connect Cricket to our WiFi network:

  • Press the built-in Cricket's button for 5 seconds (it opens toe_device hotspot)
  • Connect to the "toe_device" hotspot from a phone or laptop
  • Pass your WiFi credentials: SSID and Password; and press CONNECT button

For more information please see Cricket's documentation here

In this project we use Cricket to make Pico sending notifications to our phone. We configure Cricket in such a way that on every wake up it connects to WiFi and sends a request to Blynk via HTTP POST method. Then Blynksends the notification to our phone.

Cricket wakes up when a voltage appears on the WAKE_UP pin. As our Pico's Pin15 is wired with Cricket's WAKE_UP pin, so when it is set to "1" it raises the voltage and wakes Cricket up.

Let's configure Cricket with the following settings:

  • CONNECTIVITY > type: HTTP_POST
  • CONNECTIVITY > url: http://blynk-cloud.com/YOUR_KEY/notify
  • CONNECTIVITY > payload: {"body":"Hello from Pico!"}
  • CONNECTIVITY > content-type: application/json
  • IO1 > force update: On

An example configuration may look like on the attached screenshot.

This is it. We can press the switch off button in the top right corner to exit from the configuration and our device is ready to go!


It's time to put all the components together!

Step 4) Assembly and Final Testing

Assembly-Complete.jpg
Assembly-Cricket.jpg
Assembly-Pico.jpg
Phone-notification.jpg

We are going to power both Pico and Cricket boards independently from the AAA batteries so they both have independent power sources.

NOTE: we might want to consider powering Pico from Cricket 3v3 in some use cases. This would further improve power efficiency. It is something to be considered for future projects.

Now let's put all the pieces together and test it out.

  • Connect Pico to the batteries and wire a yellow cable to Pin15
  • Connect the same yellow cable to Cricket's WAKE_UP pin
  • Connect Cricket to the batteries

A complete project assembly may look like on the attached photo. Of course we may compress it to a far smaller form factor by soldering components together. However, in this project we want to clearly show how all components are coming together.

After the assembly and connecting the battery, the project should be automatically up and running. Every 5 minutes the Pico board should be flashing LED and waking up Cricket to send notifications on our phone by using Blynk.

Summary

We have shown how to make IOT projects with Raspberry Pi Pico and integrate to a huge ecosystem of software and internet services by using the Cricket WiFi module. We demonstrated the integration with Blynk but the same principle applies to any other software and the internet service using either MQTT or HTTP (RESTfulApi).

This project is the base to build upon for more advanced devices. Instead of flashing LED, we can attach more advanced sensors to Pico (I2C, SPI, UART, etc.), do local processing and then use Cricket to send alerts or notifications when necessary. What is really cool is that our devices can connect directly via WiFi networks without proprietary IOT hubs and they can operate on single batteries for a really long time.

Now the sky is our limit of what other projects we can make!

Thanks for getting up to here. We hope you enjoyed this tutorial!