Raspberry Pi Pico-W : I finally plugged one in

Well, it is kind of nice to get back to doing something that is not related to Solar Panels!

I guess you could be reading this and thinking, "Raspberry Pi PICO W(ireless) edition - Tony, you are so out of date, that thing came out ages ago, what are you thinking?!?"

Well the truth is, like most things, I get stuff, I put said stuff to one side, I then get distracted by a variety of things (not just solar panels, but sometimes work/work gets in the way) and then I either forget about things and "re-find" them months or even years later - usually discover that 3 more versions have come out by now and my thing is out of date, so I go buy another version and repeat the above cycle, or, I put them to one side and think to myself, I really should do something with that...

The Raspberry Pi Pico and Pico-W is no exception here.  I bought the Pico when it was first released, didn't even make it out of the packaging, it was stuffed into a "get around to that soon" box... never did.  Then when the Pico-W was released I bought 2 of them and the reason for buying 2 is obvious, apart from the low-price, it's because I always manage to lose one of them...and guess what, yep - I can only find one.  But one is good enough.    (update: I found the other one! and the normal Pico too!)

I did originally buy a load of arduino soil sensors (about 10 or 12 of them) a while back, okay, some of them are about 3years old, some about 3months old...and when I went off travelling in the motorhome around Wales, I had aspirations to setup the Pico-W, along with the sensors and some adafruit trinketry, such as the feather huzzah esp32 board and the lithium batteries etc... no, they didn't make it out of the box.  I did however purchase, print out (yes, all 400 pages as I don't actually like reading digital PDFs) and went through the Random Nerd Tutorials Smarthome automation book - whilst I did read it all, I didn't actually "do it".

Alas, no more! I finally gathered the un-opened Pico-W and a usb-cable and my trusty HONOR laptop and set about setting it up for usage....

And then I found out that the usb cable (that was brand new) was useless, it did not do data connection - I eventually found my single trusty cable that I always use for my Android phone and plugged that in... sorted... that works.

Then I was over to DuckDuckGo and was searching away how to setup this thing.

Turns out to be quite simple really.  Just plug it in, press the button on it whilst doing so, it gets detected as a drive on the LINUX laptop and you're away....well, I say, "you're away", you have 2 files shown and they are pretty unhelpful.

At this point, you go back to searching and you may even find the official documentation helpful at this point (shock!horror! Tony using & following instructions?! he must have just had his Covid booster and be feeling slightly unwell - that is actually true)

https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html#what-is-on-your-pico

Keep that unwellness in mind, I, yes, I...then install MicroPython onto the device... did I mention I was slightly unwell?

It's pretty simple, as it says here, just download the UF2 file and drag & drop

https://www.raspberrypi.com/documentation/microcontrollers/micropython.html#drag-and-drop-micropython


Once that is done, best option is to install THONNY IDE.

As I'm on Ubuntu, simple as:

$sudo apt-get install thonny

Then when starting up Thonny, it detected that I had a RPi Pico-W plugged in and asked which version I wanted to use.  3 guesses what I selected? HINT: you'll only need 2 guesses :-D

I did find a nice article that told me to create a secrets.py file to drop the following into:

SSID = "nameOfWifi"

PASSWORD="password"

and then within Thonny, create some code to SAVE to the device and RUN it.

Obviously, I kept it simple.... yeah, right, as always, went beyond a Hello World.... The code is quite simple, it connects the WiFi adapter to the WiFi, gets a connection, outputs some debug info. then connects to the internet, gets some random data about the International Space Station & what Astronauts are currently in space and in what craft and then outputs that info...and whilst doing so, it flashes the onboard LED.  simplez.


The screenshot has some trimmed code, so here's the full code output:

import network

import secrets

import time

import urequests

import machine

import utime

import ubinascii


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


if hasattr(network, "WLAN"):

   # the board has WLAN capabilities - therefore it is a Pico W

   wlan = network.WLAN(network.STA_IF)

   wlan.active(True)

   wlan.connect(secrets.SSID, secrets.PASSWORD)

   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:

      raise RuntimeError("wifi comms failed")

   else:

      print("connected to WiFi = " + str(wlan.isconnected()))

      status = wlan.ifconfig()

      print("IP ADDR = " + status[0])

      mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()

      print("MAC     = " +mac)

      print("CHANNEL = "+str(wlan.config('channel')))

      print("SSID    = "+str(wlan.config('essid')))

      print("TX PWR  = "+str(wlan.config('txpower')))

      #where is the ISS right now?

      iss = urequests.get("http://api.open-notify.org/iss-now.json").json()

      print("ISS is currently located at:"+iss['iss_position']['longitude']+","+iss['iss_position']['latitude'])

      #{"message": "success", "iss_position": {"longitude": "22.4391", "latitude": "40.6717"}, "timestamp": 1668960520}

      #fetch a list of astronauts currently in space from the internet

      print("These people are in SPACE atm:")

      astronauts = urequests.get("http://api.open-notify.org/astros.json").json()

      number = astronauts['number']

      for i in range(number):

         print(astronauts['people'][i]['name']+" - "+astronauts['people'][i]['craft'])

         led.value(1)

         utime.sleep(1)

         led.value(0)

         utime.sleep(1)

      #should flash LED when printing each name out

      

And the output (minus the green flashing LED on the device - maybe I'll make a thrilling 10 second video of that?)

connected to WiFi = True

IP ADDR = 192.168.0.85

MAC     = 28:cd:c1:04:45:d7

CHANNEL = 8

SSID    = VM8885728_24

TX PWR  = 31

ISS is currently located at:51.3264,50.4143

These people are in SPACE atm:

Cai Xuzhe - Tiangong

Chen Dong - Tiangong

Liu Yang - Tiangong

Sergey Prokopyev - ISS

Dmitry Petelin - ISS

Frank Rubio - ISS

Nicole Mann - ISS

Josh Cassada - ISS

Koichi Wakata - ISS

Anna Kikina - ISS


What next?  well.... you don't think I'm leaving at with Python on it do you?...

https://datasheets.raspberrypi.com/picow/connecting-to-the-internet-with-pico-w.pdf

Next step will be to put the C/C++ SDK in place and do some PROPER coding on it, along with sensors and stuff.

My missus quite likes the idea of the OLED smiley faced watering plant pots that are available - I do have all the right components, it's just a matter of put them together.

https://wonderfulengineering.com/this-smart-pot-has-an-animated-face-that-tells-you-how-your-plant-is-feeling/




I'm sure I can make quite a few of these & hook them all up together and then set up and automated watering system mechanism, based on stored rainfall and a motorised valve (stored at over head height, so it'll be gravity fed, therefore not requiring a pump)... and yes, I do have all of those component parts knocking around...someplace...


Comments