Use the Anvil IoT service to collect data from your Raspberry Pi Pico W

Anvil is a web app builder website that has just launched a new “IoT Toolkit” for the Raspberry Pi Pico W called the Pico Uplink. By downloading their UF2 image (which is MicroPython-based), you will be able to send data to their site and then create your own dashboard to view the data that has been collected.

What do I do?

First of all, take a look at their announcement blog post. This will give you a fair indication of whether their offering is suitable for what you’re trying to do. After that, take a look at their Pico page here.

You will need to sign up to Anvil – it is free to start with, but there are Pricing plans to take a look at if you’re going to use it in anger.

The following steps are part of their Get Started guide. I’ll write my observations as we go.

Get the firmware

Once you’ve confirmed your email address, you will need to download their UF2 firmware for the Pico W.

Make sure that you download the “complete” UF2 initially, then the Firmware only version after you’ve started writing your custom code.

Put your Pico W into bootloader mode (pull the power cable, press BOOTSEL and then plug it back in) and then drag the UF2 file to your Pico “drive”.

Interestingly, this gives you a PICO_W drive showing the code. This is how CircuitPython works, so it’s interesting to see this variant of MicroPython do that, too. Normally, you can only access it using Thonny, but this looks like you can get to the files through your File Explorer. For now, we’ll just use Thonny and see how we go!

Now, we’re going to use the Anvil Editor to build our “app”. This is using the beta version of their new version, so it will be interesting to see if it works! šŸ™‚

As instructed, I created a Blank App using the Material Design theme.

I clicked the + icon, chose Uplink and then clicked Enable server Uplink. This gave me a long Uplink key. I copied this and pasted it into main.py on the Pico W for safe keeping. Later on, I suspect I will want to put it into a config file and use a .gitignore file so that it’s hidden from GitHub.

For now, I’m ignoring the Python examples, but it could be potentially confusing if you’re not using full Python to see that!

Connecting to wi-fi and to Anvil

As instructed, I opened up boot.py and put in my wi-fi SSID and password. (This gave me something of a dilemma as my wi-fi is a bit spotty in the office!)

Having already put my Uplink key in boot.py, I knew I was ready to go.

I pulled the power and then re-inserted it. The LED went solid then flashed rapidly. This, according to the instructions, is what I was looking for as it means the wi-fi connection and the Anvil Uplink both work.

If you want to re-run the code in Thonny, make sure you’re on main.py when you hit F5 as doing that with boot.py means it doesn’t “move on”.

Create an action button

In the Anvil editor, I dragged a button into the main window and gave it the code as instructed:

def button_1_click(self, **event_args):
  """This method is called when the button is clicked"""
  anvil.server.call('pico_fn', 18) # Choose any number you like!

I then ran the Anvil app, clicked the buttonĀ on the right hand side and saw the Pico’s onboard LED flash rapidly! That’s exciting – clearly the Pico W is polling Anvil somehow to see if something’s been clicked to activate the code.

Publishing the app

I want this on the web as a public app. I clicked Publish and then chose to get a Public URL. It gave me the URL, bizarrely and randomly: https://big-tinted-jump.anvil.app. Apparently, you can change that to something else if you want, which is neat.

I then accessed that public URL and, lo and behold, by button flashes my Pico W LED.

What next? Internet of Things data display!

Well, that’s interesting and simple, but it’s not really “Internet of Things” as I understand it. For that, I need sensors to be sending data to Anvil, store it there and display it on a dashboard.

Fortunately, the Pico has an on-board temperature sensor. Now, it’s not great for taking the temperature of the surrounding air, but it will give you a reading of the temperature of the board itself. Here’s the code:

from machine import ADC

sensor = ADC(4)
conversion_factor = 3.3 / 65535
temp_reading = sensor.read_u16() * conversion_factor
real_temperature = 27 - (temp_reading - 0.706) / 0.001721
print(real_temperature)

So, I added that to my main.py and it printed out 29.85… which is a reading, ignoring how accurate for a moment.

How do I get that to Anvil?

First of all, in the Default Database, I created a new table called temperature_readings with three fields: source, temperature_reading and reading_date_time.

Then, I added a new Server Module and wrote a new function based on the Advanced Tips page.

@anvil.server.callable
def record_temperature(source, temperature_reading):
  app_tables.temperature_readings.add_row(source=source, temperature_reading=temperature_reading, reading_date_time=datetime.now())

This should, if I’m correct, be a function which records into the new table anything I ask it to. I included the “source” column so that I could have multiple temperature sensors stored in the same table. I’m hoping that’s the write approach!

I then created a button which would call the Pico and tell it to get the temperature sensor data from the Pico and put it in a table:

def button_1_click(self, **event_args):
  anvil.server.call('record_visitor')

This is great, of course, but what I really want is for the Pico toĀ push the data to Anvil, not do it “on request”.

So, IĀ think what I need is some kind of scheduler on the Anvil side toĀ poll the Pico. Let’s see, what can I find with a quick Google:

Ah. Scheduled Tasks. From reading the page, it’s clear that that is what I want – to poll a sensor using the Uplink functionality. BUT!

Scheduled Tasks are available on theĀ Personal PlanĀ and above. Ā£10 a month. Oh. Guess I won’t be able to then! Oh well. It’s useful, then, if you just want to poll-on-request, but not for gathering data… At least, not unless you pay.

Create an access point with the Raspberry Pi Pico W, serve a web-page and flash the on-board LED

Following various examples, I’ve created a MicroPython script that does the following:

  • Create an access point on the Raspberry Pi Pico W.
  • Create a web server.
  • Serve a simple page on that web server.
  • Toggle/flash the on-board LED when that page is loaded/refreshed.

TL;DR – see the code here

Download MicroPython and install it

First of all, you’ll need to download theĀ latest MicroPython UF2. The one I’m using is v1.19.1-108-g474c47d59 (2022-07-08) .uf2 from this page. I found out via the Raspberry Pi forums that there was something broken on versions earlier than the 07-05 one – check out the thread here.

Next, you will need to put the Pico into bootloader mode and then drag the UF2 file to the external device that comes up.

Initial access point

Use Thonny to program the initial code:

import socket
import network

ssid = "PicoW"
password = "123456789"

ap = network.WLAN(network.AP_IF)
ap.config(essid=ssid, password=password) 
ap.active(True)

while ap.active == False:
  pass

print("Access point active")
print(ap.ifconfig())

Despite setting the SSID, to start with I got a generic wi-fi name starting with “PICO”. In order to get PicoW working, I had to power cycle the Pico (rather than just CTRL-D and run again). This is behaviour described here on the RP forums.

I tried connecting to it using my mobile andĀ success – I could connect to the access point, but had no Internet (as expected, obviously).

Serving a web page

Now, I want to serve a web page of some kind from the Pico W, otherwise it’s pretty useless. What’s the point in having an access point that does nothing?

I found this MicroPython library called tinyweb which is like Flask, but for MicroPython instead of full-blown Python. This I can use to establish “routes” which will deliver specific content via the web browser.

tinyweb requires, as it says in the README, the logging library. I had to scratch around to find that, so here it is.

I uploaded the tinyweb folder and the logging.py file to the PicoW and added

import tinyweb

at the top of the code. This ran correctly, so I then looked at the tinyweb documentation for how to create a web server.

I added some more code (which you can see on my own GitHub repo – WordPress is doing VERY weird things if I paste it in here!)

I connected to the AP again using my phone, turned off mobile data to make sure it didn’t try and serve content from the Internet, and then accessed http://192.168.4.1/ (which had been printed to the REPL which I could see because I was using Thonny).

To my surprise, I gotĀ Hello world! on the browser. Yay! It works!

Flash the onboard LED

I added the definition of the onboard LED:

led = Pin("LED", Pin.OUT)

and set the / path of the tinyweb web server to toggle it on and off.

Now, when I refresh the Hello World page, the LED goes on and off. Wonderful!

See the code on GitHub.

Raspberry Pi Pico W launched – build your Internet of Things projects much easier!

Image from Raspberry Pi

Raspberry Pi has today launched the Raspberry Pi Pico W (see the official announcement here).

This uses the same RP2040 chip as the regular Pico but has wi-fi built into it! This is going to get eaten up by the Internet of Things crowd and, at $6 (Ā£6 ish), it is incredibly good value for money!

You can buy a Pico W from Pimoroni and of course from The Pi Hut and there are other resellers out there too.

Pimoroni have got ahead of the game (as usual) by developing several Pico-with-wireless boards and add-ons. My favourite is the Inventor 2040 W which has motor drivers, audio and breakouts (as well as wi-fi) onboard. Good job, folks! There are also other sensor/IoT based boards in the works and the e-ink boards are already available. Best place to go is the New Products page.

You can read a review of the Pico W by Les Pounder here and there’s a new issue of The MagPi out covering the launch. If you’re on Twitter, other coverage will come out over the day, I’m sure.

Opinion

Ā£6? That’s ridiculous for an IoT platform with the kind of support and quality that Raspberry Pi puts out. The RP2040 is a great chip and the 2MB of onboard memory isĀ just enough for most uses, although 4MB wouldn’t have gone amiss (I appreciate it wouldn’t have been Ā£6 then, but I can wish!) Go and buy one if you need the wi-fi connectivity or a cheap Bluetooth device (when the software catches up). Well done, Raspberry Pi – this one is going to go like hotcakes!

Raspberry Pi Zero 2 W launched! More processor power in a small package!

Raspberry Pi Zero 2 close-up, front side

Fans of the Raspberry Pi Zero can today get excited about a new Raspberry Pi product – the Raspberry Pi Zero 2 W.

TL;DR – it’s got the Pi 3B processor, 512MB of RAM and is a lotĀ speedier than the original Zero/Zero W.

New processor

The big headline improvement to the Zero is that the Pi Zero 2 W uses the same processor as the Raspberry Pi 3.

In technical terms, RP3A0 is a SiP that combines the BCM2710A1 die used in BCM2837A1 – which was used in the Raspberry Pi 3 – and a 4Gbit Micron LPDDR2 die along with the decoupling capacitors required to smooth the core supply voltage.

Raspberry Pi Zero 2 - chip package

The new Zero 2 chip is a quad-core, 64-bit processor, a massive increase in processor power over the original Zero/Zero W which uses the single core Raspberry Pi 1 processor. The processor is only set to 1 GHz to avoid overheating issues, but could be overclocked to 1.4 GHz (the same as the Pi 3B) with adequate heat dissipation. I’m sure someone will come out with a case that will allow you to do just that!

Same memory size, new package

Also on-board is 512MB of RAM. This has been packed into a new “substrate package” developed by Raspberry Pi to allow it to work with the new processor, making it their second home-grown silicon package (after the Raspberry Pi Pico).

The Pi Zero 2 W has WiFi onboard, but this time it is packaged into a “can” which means the whole Pi Zero 2 is conformance-tested, making it much easier to develop full products with the Zero 2 W embedded. As you can see from the image above, because the silicon package was developed by Raspberry Pi, you get the Pi logo on top, which is nice.

The board comes without a 40-pin header (so blame me for the soldering below!).

Raspberry Pi Zero 2 close up, reverse

Same form-factor

The form-factor of the Pi Zero 2 W is the same, although some components have moved so cases that expect components not to move (like the Pimoroni PiBow) might not fit properly. My usual choice for the Zero (the Ā£6 “Premium Raspberry Pi Zero Case” from The Pi Hut) housed the new board nicely, however, because it allows for flexibility of components.

I was also happy to see the continued inclusion of the Zero-sized camera connector. This is still a very fragile connector, although necessary because of the board size.

Existing models

It’s expected that the original Pi Zero and Pi Zero W will still be available to purchase for the near future.

Cost and getting the board

The baseline price of the Raspberry Pi Zero 2 W is $15 (because they source the parts in dollars, in case you didn’t know!). That works out to just over Ā£13 in the UK. They are limited to one-per-customer, as per usual with the Zero. This stops eBay scammers grabbing them all and makes things fairer, although this may be frustrating for some people, also as per usual with the Zero.

For those buying in the UK:

For those buying in the EU:

For those buying in North America:

For those buying in the Rest of the World

(I will add other resellers as I pick up their website addresses).

Extras

In practice

When I used the Pi Zero 2 W, I first of all did a full update/upgrade. Once that was done, it took less than 30 seconds to boot up into the desktop. Impressive considering the amount of memory on-board. Web browsing felt sluggish (because of the lack of memory), but I’m told that the Zero 2 isn’t really built for that (although it would do it at a push – I managed to post to the Raspberry Pi Forums from it without much bother!). I tested out the GPIO pins, including an I2C and SPI test and everything “just worked”.

I do wish that the Pi Zero 2 had 1GB, but maybe that would make it “too capable” and eat into the usage and sales of the Pi 4.

Bottom line: A very nice little board and is well worth the extra cost.

Other opinions and blog posts

As usual, I’ll pick up other coverage and link to it as the day progresses. Check back later for links to more opinions!

Review of the Cytron Technologies Maker Pi RP2040 – a motor controller board at the start of your robotics journey

Last in my mailbag of Cytron Technologies goodies, this is the Maker Pi RP2040. It has clearly been designed to get you going with your robotics journey and to give you the ability to add all sorts of inputs and outputs into a project, centered around the new RP2040 microcontroller chip from Raspberry Pi.

TL;DR – great for beginners, plenty of inputs and outputs, easy to power. Another winner from Cytron.

What’s in the box?

You get the Maker Pi RP2040 board and also 4 Grove-to-female-header cables, some rubber feet and, handily, a small Phillips screwdriver – that saved me digging my own screwdriver out, and it’s a nice something for the newbies who don’t have a collection of tools.

What can the board do?

Let’s first of all look at your input/output options:

  • 7 x Grove sockets – you can plug in any compatible Grove modules, or you can use the four included cables to hook up any generic sensors, outputs, inputs, whatever you like!
  • 2 x DC motor outputs – allowing you to drive 2 motor outputs forwards and backwards independently at up to 1A per channel (1.5A peak). Also on-board are four test buttons that allow you to get your motors moving and check they work without any programming.
  • 4 x servo motor outputs – for fine motor control.
  • 1 x piezo buzzer – so you can play beeps and other sounds (this can be disabled completely by an on-board switch).
  • 2 x Neopixel-style user-controllable RGB LEDs.
  • A collection of low-power LEDs in-line with all the input/output ports so that you know what has been activated (and who doesn’t like blinkies?)
  • 2 x user-defined buttons – perhaps you can use these to put your robot into different modes…?
  • A Run/reset button and a Boot button.

If you want to take a look at the Datasheet, you can find it here.

Let’s talk about power

In terms of powering the board and the motors, you haveĀ three options:

  1. Supply power through the microUSB socket (very handy when you’re testing things outĀ and programming at the same time).
  2. Supply power via a single-cell 3.7V LiPo battery. (The microUSB input will charge your LiPo as well, with overcharge and undercharge protection built-in).
  3. Supply power via the screw terminals between 3.6V and 6V (the max is equivalent to 4 x non-rechargeable AA batteries, but you can, of course, use rechargeable ones instead).

All the power is handily isolated by an on-board power switch so if you just need to shut the thing off, one click and you’re done.

Having a play

I hooked up two 6V DC motors to the board using the screw terminals (thanks again for the screwdriver!) then used the test buttons to make them run. Excitingly, it “just worked”. I then used the pre-loaded demo script to automatically “turn the robot on the spot” (which it would have done had I not elevated the wheels before I hit the button!). You can see a short demo video of it working below:

 

I also hooked up a servo and used the demo script to turn it back and forth. These are small steps, but vital ones, as it proves that everything works and you’re ready to embark on your future exploits.

Speaking of which, Cytron have provided a GitHub repository to help you, full of demo scripts for both CircuitPython (which is pre-installed) and MicroPython.

You can see the code for the pre-loaded demo here. As you can see, for DC motors you simply use the pwmio library to define the motors and then trigger them. For servos, they are using Adafruit’s adafruit_motor/servo library. Normally, I’d criticise them for not building their own library, except a) why re-invent the wheel? and b) they’re using CircuitPython for this anyway, and Adafruit invented that!

What did I think?

I think this is an excellent board for beginners. With the ability to hook motors up, provide power and then just click a button to test the motors out, you get the instant gratification that both kids and adults love. Programming the RP2040 is easy (especially using CircuitPython) and if you added on, say, a Cytron line following sensor (which might be a bit “cable-y” using the Grove sockets, admittedly), it’s clear to see how you could create a line following robot, as an example, quite simply.

The only caveat I really have with the board is that, because it’s all very centred around the 7 Grove sockets, there’s no direct access to the GPIO ports. Sometimes, you just want to hook something up without needing a special cable to do it. Now, they obviously provide 4 of these “special cables” to enable you to do that, which is very handy, but having (even unpopulated) headers for the pins might’ve made things even more flexible. Of course, the board is 85mm x 60mm (approx), and that small size is very attractive. There are always trade-offs, though, in any product, and it doesn’t stop me recommending it, it’s just something to be aware of.

The three power options are very flexible, and it’s good to see a LiPo port on there, even if beginners probably shouldn’t use them to start with. Adding the charging circuitry in, though? Very good move from Cytron, there.

I do keep mentioning that it is for beginners. Well, at 1A per channel, it’s not going to blow anyone away in terms of sheer power. Having said that, as a trade-off, you can run 6 motors independently, so one shouldn’t complain! They have a lovely range of other motor controller boards able to provide more grunt, so there is a clear path to change your method later when you get more advanced.

Overall, it’s a solid 9/10, I think, and a great use of the RP2040 chip.

Another winner from Cytron – highly recommended.

For another take on the same board, I suggest having a look at Les Pounder’s review over at Tom’s Hardware.

Where can I get it?

In Malaysia and surrounding countries, go directly to the Cytron website where the board is available for $9.90.

In the UK, I recommend The Pi Hut where it is listed at Ā£9, but I notice they are currently out of stock. ThereĀ is a notification email list, though.

In the USA, I suggest Adafruit who I believe are getting stock in very soon and selling for $12.50.

Other resellers are listed on the Cytron page near the bottom, above the reviews.

Review of the Maker Pi Pico from Cytron Technologies – a Raspberry Pi Pico project board worth your time

Also in my review box from Cytron (along with yesterday’s Edu:Bit) was a Maker Pi Pico. This is a board that houses a Raspberry Pi Pico and measures approximately 95mm x 70mm. The version I’ve got is the ordinary Maker Pi Pico, but there is also a Maker Pi Pico Base which just has a slot for a Pico that you may already own.

The idea of this board is to make it simpler to add components onto a project, but still keep it to a manageable size.

TL;DR – a great add-on home for your Raspberry Pi Pico at a great price. Find out where to buy it at the bottom of the post!

Features

It has the following features:

  • 6 Grove-compatible sockets for adding on inputs and outputs via Grove cables.
  • An SD card slot (so you can read and write files).
  • A 3.5mm audio output jack and a (fairly loud) buzzer for playing simple sounds (and, thank God, a small switch to disable the buzzer!).
  • Low-power LEDs on every pin so you can see what’s happening, input and output-wise.
  • A socket for an ESP-01 wi-fi module.
  • 3 user-defined buttons.
  • A RUN (reset) button.
  • A single Neopixel-style RGB LED.
  • A header that breaks out the debug pins and also allows you to wire up your own RUN/reset button.
  • All the Pico pins broken out as pin headers.

That’s quite a list, I think you’ll agree. They also provide lots of demo scripts to use when trying it out, so the software is “there”, too. These scripts come in both CircuitPython and MicroPython flavours, although the board is pre-loaded with CircuitPython. This is a wise choice – CP does give you a much easier way to develop – just drop the files on the “drive” that you get when the Pico is plugged in and away you go.

Initial impressions

What I liked about it most initially was that they have been very smart in deciding which pins to break out as Grove sockets. They have chosen lots of I2C but also chosen to break out two of the Pico’s analog pins as a Grove socket. Handy, because that means you can use Grove modules like a potentiometer. It’s an attractive board, too, in its purple colour and everything is well-labelled and well-engineered.

Experiments

Cytron were kind enough to send me a Grove OLED module and a Grove potentiometer module too, so I had a play with one of their demo scripts that shows the value of the pot on the OLED.

As you can see, the board also comes with a nifty pin reference card – very handy for looking up what pin does what quickly.

I did try and get the SD card reading and writing, but for some reason it didn’t recognise my card. I suspect this is just something to do with the card I was using, so I’ll try a different one at a later date.

The buzzer is certainly loud – the pre-loaded demo code plays the Mario theme immediately upon boot. Fortunately (and I can’t stress this enough), they’ve been good enough to provide a shut-off button for the buzzer! The audio jack is a nice touch for higher fidelity sound playing.

The three user-defined buttons, which are linked to GPIO pins 20, 21 and 22, are satisfyingly clicky and the Neopixel-like RGB LED is appropriately blinky. šŸ™‚

Experiments in Wi-Fi

Cytron also sent over a cheap ESP-01 module which allows you to connect the Maker Pi Pico to the Internet. There is a socket on-board (bottom-right) for the module. They were good enough to give me some support in getting it going and I was soon able to ping an IP address by providing wi-fi credentials in a supplied template “secrets” file.

Quick instructions: The demo scripts are on their GitHub here. I renamed simpletest.py to code.py, uploaded that and the lib folder to the Pico then altered secrets.py to give it my own credentials.

Overall

I had a very satisfying experience with this product, overall. The SD card issue I’m sure waseither down to the card, or the way it was formatted, so I will see about that later on. Price-wise, the board is very competitive. On the Cytron site, it’s $9.90 including the Pico. Elsewhere, it can be found on The Pi Hut’s site for Ā£11.50 with the Pico, or Ā£8 for the Base version. It is also available from Kitronik (just the Base version) and you can find other international resellers on the Cytron page, at the bottom.

It’s a great board at an attractive price and it gives you really great options for getting going with the Pico if you don’t want to use jumper cables on the Pico’s pins (although that option is still available to you!). Great features, great price, great idea. What’s not to like?!