The Raspberry Pi Pico W packs a ton of performance into a really inexpensive bundle. For simply $6 we get the identical twin core Arm Cortex M0+ CPU working at 133 MHz, 264KB of RAM and 2MB of flash reminiscence as the unique Raspberry Pi Pico. However we additionally get a 2.4 GHz succesful Wi-Fi chip, all in the identical DIP bundle.
The inclusion of Wi-Fi is the most important draw. We’ve a easy and low cost microcontroller, with sufficient horsepower to perform many initiatives sometimes reserved for extra highly effective and costly Raspberry Pis, such because the Raspberry Pi 4 and the Raspberry Pi Zero 2 W.
Wi-Fi permits Web of Issues (IoT) initiatives and for that we want a safe and easy-to-use means to put in writing initiatives. Step ahead Anvil, which, gives Pico W firmware that trivialises IoT initiatives and takes benefit of its uplink service. With Anvil and the uplink service, we will simply write code on the net that may work together with the $6 Wi-Fi enabled micro-controller.
On this how-to we will write a venture that permits two-way communication between the Raspberry Pi Pico W and Anvil’s servers within the cloud. We are going to ship reside sensor knowledge from the Raspberry Pi Pico W to an internet app on Anvil’s servers. We will additionally ship messages from the app to the Raspberry Pi Pico W, to be displayed on a small LCD show.
For this venture you’ll need
- Raspberry Pi Pico W
- An I2C LCD show
- A DHT11 Temperature Sensor
- 4x Feminine to male jumper wires
- 3x Male to Male jumper wires
- 1x Half dimension breadboard
Constructing the Circuit for Anvil on a Pico W
The circuit for this venture consists of 1 enter system, a DHT11 temperature sensor, and one output, an LCD display screen related over I2C. The DHT11 will measure the temperature and humidity, which is then despatched to Anvil to be displayed in an internet app. The LCD display screen will show quick messages, typed into the identical Anvil internet app.
The DHT11 has 4 pins. Trying from the entrance (the blue “cage”) we hook up with the Raspberry Pi Pico W as follows
Pin 1: (Crimson Wire) Join VCC to 3V3(OUT) of the Pico W.
Pin 2: (Yellow Wire) Join Knowledge out to GP4 of the Pico W.
Pin 3: No connection.
Pin 4: (Black Wire) Join GND to GND of the Pico W.
The LCD has 4 connections.
GND: (Black Wire) Hook up with any GND on the Pico W.
VCC: (Crimson Wire) Hook up with VBUS on the Pico W.
SDA: (Yellow Wire) Hook up with I2C0 SDA on the Pico W.
SCL: (Orange Wire) Hook up with I2C0 SCL on the Pico W.
Test your wiring earlier than shifting onwards.
Organising the Raspberry Pi Pico W
Anvil makes use of a customized firmware picture for the Raspberry Pi Pico W, a picture which simplifies connecting the Pico W to Anvil’s companies. The firmware is predicated on MicroPython, with the Pico W showing as a USB drive with two recordsdata (boot.py and predominant.py). Earlier than we will write any code for our venture we first must flash the customized firmware to the Pico W, and hook up with our Wi-Fi.
1. Obtain the customized Raspberry Pi Pico W firmware from Anvil.
2. Push and maintain the BOOTSEL button on the Pico, then hook up with your laptop utilizing a micro USB cable. Launch BOOTSEL as soon as the drive RPI-RP2 seems in your laptop.
3. Drag and drop the Anvil UF2 file onto the RPI-RP2 drive. The Raspberry Pi Pico will reboot and can now run MicroPython.
4. Open File Explorer and navigate to the brand new USB drive. Anvil’s firmware makes use of MicroPython, and presents itself as a USB drive, just like CircuitPython.
5. Open boot.py in a textual content editor and alter the values for WIFI_SSID and WIFI_PASSWORD to match your setup. Save and exit when performed. This file comprises all the Python code which is run at boot. On this case, it’ll hook up with our Wi-Fi AP and flash the Pico W’s onboard LED if there is no such thing as a connection.
Your Raspberry Pi Pico W ought to now hook up with your Wi-Fi and the onboard LED ought to cease blinking. A static LED means we’re related to the Wi-Fi and prepared for our venture.
Code for the Raspberry Pi Pico
1. Set up Thonny in your laptop in case you haven’t performed so already. Comply with the steps in our article on easy methods to arrange a Raspberry Pi Pico W. Thonny is a straightforward to make use of Python editor which has been configured to work with the Raspberry Pi Pico.
2. Create a brand new clean file in Thonny.
3. Open this hyperlink and duplicate the textual content from the web page.
4. Save the file to the Raspberry Pi Pico as lcd_api.py
5. Create one other clean file.
6. Open this hyperlink and duplicate the textual content from the web page.
7. Save the file to the Raspberry Pi Pico as pico_i2c_lcd.py These two recordsdata allow the usage of the I2C LCD display screen with the Raspberry Pi Pico W.
8. Open predominant.py, discovered on the Raspberry Pi Pico W drive.
9. Delete the contents of predominant.py.
10. Import three modules of pre-written code. The primary is anvil.pico and this permits our Pico W to hook up with Anvil’s servers. Subsequent, uasyncio creates an asynchronous scheduler on the Pico W, we want this for working concurrent capabilities in our code. The third module is machine, and from that we want the Pin (GPIO pins) and I2C courses to speak with the GPIO and LCD display screen.
import anvil.pico
import uasyncio as a
from machine import Pin, I2C
11. Import three extra modules. The time module permits management over the tempo of our code. The dht module is particular for the DHT11 (and DHT22) temperature sensor and acts as a better means to get knowledge from the sensor. The pico_i2c_lcd permits our code to make use of the LCD show.
from time import sleep
import dht
from pico_i2c_lcd import I2cLcd
12. Create an object, i2c, to create a connection from our code to the I2C bus. I2C0 has SDA at pin 0 and SCL at pin 1.
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
13. Create an object I2C_ADDR to retailer the I2C tackle of the LCD show. This line of code will scan the I2C bus for the tackle and retailer it within the object.
I2C_ADDR = i2c.scan()[0]
14. Create an object, liquid crystal display, to make a connection between our code and the LCD show. This makes use of the i2c object, the I2C tackle, and units the show to have 2 strains and 16 characters. There are screens with differing sizes, so regulate this to match your display screen.
liquid crystal display = I2cLcd(i2c, I2C_ADDR, 2, 16)
15. Flip the LCD backlight on, and set the cursor to blink. The backlight is crucial because the display screen will be exhausting to learn. The flashing cursor, that’s extra of a retro vibe.
liquid crystal display.backlight_on()
liquid crystal display.blink_cursor_on()
16. Create an object UPLINK_KEY, and for now retailer clean knowledge. That is the place our Anvil Pico Uplink key can be saved. We are going to get that data later.
UPLINK_KEY = " “
17. Create an object, sensor, to retailer a connection from our code to the DHT11 temperature sensor. Utilizing this object we will work together with the DHT11 and get knowledge.
sensor = dht.DHT11(Pin(2))
18. Use a Python decorator to instruct Anvil that the subsequent operate is callable from the Anvil internet app. Decorators are a method so as to add extra performance to a operate, with out impacting the operate.
@anvil.pico.callable(is_async=True)
19. Create a operate, dht11data which is able to work with the scheduler.
async def dht11data():
20. Take a studying with the DHT11. We have to do that to be able to get the uncooked knowledge. Observe that we at the moment are contained in the operate and our code is now indented to indicate that it belongs to the operate.
sensor.measure()
21. Create two objects, temp and hum to retailer the temperature and humidity readings from the DHT11. The spherical() operate rounds the values to 1 decimal place.
temp = spherical(sensor.temperature(),1)
hum = spherical(sensor.humidity(),1)
22. Create an object, knowledge, which is able to retailer a string / sentence containing the temperature and humidity knowledge utilizing string formatting to insert the info into the sentence.
knowledge = ("Temperature: {}°C Humidity: {:.0f}% ".format(temp, hum))
23. Print the sentence to the Python shell, after which use return to output the sentence for Anvil to learn. These strains are the final for this operate.
print(knowledge)
return knowledge
24. Create one other decorator, this time for a operate that may present messages on the LCD display screen. The message is handed as an argument to the operate (message).
@anvil.pico.callable(is_async=True)
async def show_message(message):
25. Use a for loop to flash the LCD backlight 3 times. This may get our consideration for an incoming message. The backlight is solely turned on and off, with a brief pause between every.
for i in vary(3):
liquid crystal display.backlight_on()
sleep(0.2)
liquid crystal display.backlight_off()
sleep(0.2)
26. Flip the backlight on. We’d like the backlight to see the textual content.
liquid crystal display.backlight_on()
27. Write the message to the LCD show, after which pause for ten seconds. Lengthy sufficient to learn the message. Then clear the LCD show. That is the tip of this operate.
liquid crystal display.putstr(message)
sleep(10)
liquid crystal display.clear()
28. Join the Pico W to Anvil’s servers utilizing your uplink key. We are going to get the important thing later on this venture.
anvil.pico.join(UPLINK_KEY)
29. Save the code as predominant.p
Anvil Code for Raspberry Pi Pico W
1. Go to the Anvil web site and click on on Begin Constructing.
2. Register or join a free account.
3. Create a brand new clean app.
4. Choose Materials Design Theme.
The Anvil editor is cut up into two sections. Design and Code. Within the Design part we create the consumer interface for our app. In Code, we give the applying the performance that we require. We’ll begin by including components to the app, after which we will add performance to the weather.
1. From the Toolbox, drag a Label instrument and place it on “Drop title right here.”
2. Change the label textual content by way of the textual content area within the properties menu.
3. Drag a picture instrument into the shape. With this we will add a brand or picture to our venture.
4. From Properties, choose Supply and choose the picture in your software.
5. From the Toolbox, drag one other label and place it underneath the picture. This may change into an instruction to the consumer.
6. Write an instruction to the consumer, on this case to put in writing a message.
7. From the Toolbox, drag a textual content field and place it to the best of the “Write a message” label. Anvil will regulate the structure to accommodate the textual content field.
8. From the Toolbox, drag an area and place under the picture, and above the label and textual content field. This spacer is used so as to add just a little distance between the picture and the app.
9. From the Toolbox, drag a button and place it subsequent to the textual content field. This button can be used to “ship” a message to the Raspberry Pi Pico W.
10. Change the button textual content by way of the Properties part. A easy “Present Message” is all we want as this button will set off a operate to ship a message to the Pico W’s LCD display screen.
11. Drag one other label and place it underneath the earlier components. This label will present the DHT11 sensor knowledge from the Pico W.
12. Change the title of the label to Temp. Anvil will auto insert “self.”. This may allow us to determine the right label in Code, which is able to show our sensor knowledge.
13. In Properties, scroll right down to Textual content and click on MORE. Then set the textual content to daring, and font dimension to 32pt.
14. Within the Toolbox, click on on Extra Parts and drag the Timer into the shape.
15. Utilizing the Properties, set the timer to an interval of three seconds. This time is used to replace the display screen for our sensor knowledge.
With the design now full, our focus turns to the Code part which is able to convey the performance to the venture.
1. Double click on on the SHOW MESSAGE button to open a cut up view code editor. The code which pertains to the button is routinely highlighted.
2. Add a brand new line to the operate which is able to name the “present message” operate that we created on the Raspberry Pi Pico W. The operate on the Pico W requires a message to be despatched, and we get this by utilizing the textual content from the textual content field. The anvil.server_call_s name will run the command, however we are going to see no output within the internet app.
anvil.server.call_s("show_message",self.text_box_1.textual content)
3. Double click on on the Timer ingredient to edit its code.
4. Add two strains to the timer operate. The primary to create a variable, knowledge, which is able to retailer the info despatched utilizing the “dht11data” operate on the Pico W. The second units the Temp label textual content to indicate the message from the Pico W.
knowledge = anvil.server.call_s("dht11data")
self.Temp.textual content = knowledge
Linking Raspberry Pi Pico W to Anvil
Anvil has an “uplink” instrument, which permits the Raspberry Pi Pico W (and the Raspberry Pi) to securely hook up with Anvil’s servers and talk between the Pico W and the net app.
1. On the left of the display screen, click on on the blue + and from the menu choose Uplink.
2. Click on on Allow server Uplink.
3. Copy the uplink key. Don’t share this key with anybody else.
4. Open predominant.py in Thonny and paste within the Uplink key to the UPLINK_KEY variable. Click on Save.
Working the Internet Software
With either side of the venture full, now we use Anvil’s Uplink to speak between the Raspberry Pi Pico W and Anvil’s servers.
1. In Thonny, click on on Run to start out the code. It should take the Raspberry Pi Pico W a number of moments to hook up with the Wi-Fi, affirmation of which will be seen within the Python shell.
2. Return to Anvil and click on on Run to start out the net app.
3. Sort in a message, and click on SHOW MESSAGE to ship it to the Pico W. The LCD show will flash 3 times after which show the message. The present temperature and humidity will seem within the app.
Publishing the Software
At the moment the applying is simply accessible to us. We are able to simply share the applying by publishing it. However first we have to give our app a reputation, title, description and brand.
1. Click on on My Apps / Materials Design to open the Settings tab.
2. Give the applying a reputation, title, description, and add an icon.
3. Click on Publish.
4. Click on Add Personal URL and duplicate the URL. This URL is helpful for sharing with associates and colleagues when testing the app.
5. Click on the hyperlink to make sure that every little thing is working. Observe that you’ll want to start out the code on the Raspberry Pi Pico W.
6. Click on Add Public hyperlink to create a public app.his hyperlink is less complicated to learn . A public hyperlink can be utilized when you find yourself able to share the venture on social media.
7. Click on the hyperlink to check that it really works. The code on the Raspberry Pi Pico W should even be working.
We now have a working internet software working by way of a Raspberry Pi Pico W and Anvil’s servers.