Setting up the Enviro+ Pi HAT's libraries

Matthew Wilkes on 2020-05-24


This video is a recording of me setting up the Enviro+ Pi HAT from Pimoroni on a Raspberry Pi in a virtual environment. As a follow-on to the example in my book, Advanced Python Development, I wanted to add support for the sensors on this board. This is the first step, using Jupyter notebooks and a remote kernel setup to try sample code.

Unfortunately, two problems bit me along the way.

  • A few dependencies required some development libraries to be installed on the Pi when installing with setuptools (and Pipenv), which aren't needed when installing the dependencies using the system package manager (which is the preferred installation method from Pimoroni, and makes sense for systems like the Raspberry Pi), but the apd.sensors installation on the Pi is using a virtual environment
  • There was a bug in release 0.0.3 of the interface library, which caused one dependency not to be installed.

This was a 40 minute programming and debugging session which I've condensed to 23 minutes by cutting out time I spent reading documentation and waiting for commands to execute. I have not cut any significant portions of mistakes I made in this process, as the idea is to demonstrate problem solving, rather than showing the final working outcomes.

The quality of this video has some things to be desired. The audio is noisy in places, the feed of my face ends at 15 minutes due to a problem with recording, and a small segment of the screen capture failed. Please bear with me, this was my first attempt at this kind of video, and the nature of the subject meant that I couldn't re-record to fix the problems without substantially changing the content.

Transcript


[00:00:00.06]
- Okay.

[00:00:01.06]
So I've bought this new Enviro+
board for the Raspberry Pi,

[00:00:05.05]
and I'm going to try setting it up

[00:00:07.00]
with the Advanced Python
development sensors code.

[00:00:10.02]
I have not tried anything with this yet.

[00:00:12.06]
I've had it plugged in but
I've not installed the code.

[00:00:15.05]
So as you can see, it's not
doing anything right now.

[00:00:20.09]
I'm starting off with
a connection in Jupyter

[00:00:25.03]
to this copy of the Pi,

[00:00:27.03]
which has the Advanced Python
development code installed.

[00:00:30.05]
And I can test things here.

[00:00:36.01]
All right, let's get started.

[00:00:38.01]
I think the first thing we need to do...

[00:00:44.02]
So the first thing we need to
do is get the code installed,

[00:00:47.07]
that's the interface code from Pimoroni.

[00:00:50.02]
so let's have a look at how you do that.

[00:00:55.00]
Pimoroni Enviro, code
examples, Python library.

[00:01:01.00]
And I know you can just pip install this,

[00:01:02.07]
but there's a lot of setup
with Raspberry Pi things

[00:01:05.03]
about making sure the interfaces
are enabled correctly.

[00:01:08.00]
So I'm going to see what
they have available.

[00:01:10.07]
Right, so they have a one-line install

[00:01:14.00]
that you can pipe into Bash.

[00:01:15.06]
Which is convenient, but it
always makes me a bit nervous,

[00:01:19.00]
(What does that do?)

[00:01:21.06]
(Leave that for now.)

[00:01:22.08]
Or install directly from GitHub

[00:01:25.00]
or from PyPI and configure manually.

[00:01:28.02]
Okay, it looks like there are
two commands we need to run

[00:01:33.00]
to enable I2C.

[00:01:36.09]
So let's do that.

[00:01:39.02]
As root.

[00:01:41.04]
And enable SPI.

[00:01:45.07]
Done, okay.

[00:01:48.05]
And I'm gonna pipenv install enviroplus.

[00:01:53.09]
What's this on the bottom?

[00:01:54.08]
Install additional dependencies.

[00:01:58.01]
Hm.

[00:01:59.06]
Does it not do that for me?

[00:02:03.02]
I'm gonna leave that for
now, see what happens.

[00:02:05.06]
So my pipfile is updating.

[00:02:08.03]
While that's working I'm gonna have a look

[00:02:11.02]
at the Enviro+ tutorial.

[00:02:16.04]
"What's on Enviro+?"

[00:02:19.02]
Lots of fun things that I
should probably understand

[00:02:20.07]
before I start coding for it, but...

[00:02:23.07]
How to attach to your Pi.

[00:02:26.05]
I've got it attached.

[00:02:30.01]
I guess we should look at the
example code, shouldn't we?

[00:02:36.05]
Well.

[00:02:40.01]
Let's run the code.

[00:02:42.03]
So we get the URL to the raw.

[00:02:46.00]
Oh, we can do this in Jupyter.

[00:03:12.06]
All right, so when I run
this it's not gonna work yet

[00:03:14.03]
because I haven't restarted the kernel.

[00:03:16.01]
So, no module named smbus.

[00:03:18.02]
So let's restart the kernel and run all,

[00:03:20.05]
and see if we do need to
install those additional things.

[00:03:24.06]
No module named smbus.

[00:03:25.08]
Okay.

[00:03:27.02]
All right.

[00:03:29.05]
Is that one of the things
that it told me to install?

[00:03:36.03]
Python-smbus, yep.

[00:03:38.04]
So I will do these.

[00:03:41.05]
Actually you know what,
I'm going to leave off

[00:03:46.07]
python-setuptools.

[00:03:49.09]
'Cause it should get that automatically,

[00:03:51.02]
and I do have it installed.

[00:03:58.04]
Apt update that because
it's missed some things.

[00:04:16.04]
There we go.

[00:04:17.07]
Okay, yes.

[00:04:24.00]
This is looking good.

[00:04:29.04]
Imagequant, libdef,
that's all kernel stuff.

[00:04:39.04]
Okay.

[00:04:41.06]
Let's try that notebook again.

[00:04:44.04]
Kernel, restart notebook.

[00:04:50.08]
No module smbus.

[00:04:52.01]
Yeah, yeah.

[00:04:55.07]
Pipenv run python.

[00:04:57.04]
Import smbus, no module smbus.

[00:05:02.03]
But yeah, so it's in the global.

[00:05:06.04]
So I've installed this globally,

[00:05:10.03]
which is of no help because
I've a pipenv install,

[00:05:13.07]
and that isn't pulling in
packages from the site-packages,

[00:05:18.06]
which is good 'cause it means
it's not getting polluted.

[00:05:22.06]
But it should've installed
those directly, really.

[00:05:29.02]
So python-smbus, what is that?

[00:05:35.02]
A drop-in replacement
for smbus-cff, smbus too.

[00:05:42.06]
Python smbus Ubuntu.

[00:05:45.00]
Why am I doing Ubuntu?

[00:05:46.01]
Well, that's close enough to Raspbian.

[00:05:51.06]
python-smbus 3.11,

[00:05:54.03]
enables smbus access.

[00:06:02.08]
Smbus...

[00:06:21.05]
Oh, what's that?

[00:06:22.06]
i2cdetect, let's see if it finds it.

[00:06:25.07]
Yeah, it found some things.

[00:06:27.04]
Cool.

[00:06:40.01]
Oh.

[00:06:42.08]
Eww.

[00:06:48.01]
Right, so it's installed
in Python 2 anyway.

[00:06:50.08]
That's horrid.

[00:06:52.05]
So let's try python3-smbus,

[00:06:56.05]
is that gonna let me? It's a thing!

[00:07:01.00]
Okay.

[00:07:03.00]
Let me do, import smbus, yep.

[00:07:07.04]
Pipenv run, right, here we'll see,

[00:07:09.02]
was it actually blocking this?

[00:07:12.06]
Yes it was.

[00:07:14.02]
Okay, so python3, import smbus,

[00:07:17.06]
smbus is that thing.

[00:07:22.07]
All right, so it looks
like it might be smbus2,

[00:07:25.00]
so let's give that a go.

[00:07:39.07]
So the problem we're having here

[00:07:40.08]
is that the instructions
tell you to install something

[00:07:42.09]
called python-smbus, which
controls the interface

[00:07:45.08]
between the Raspberry Pi
and the Enviro+ in Python.

[00:07:52.01]
The difficulty is, that's installed

[00:07:54.02]
through the operating
system package manager

[00:07:56.04]
which installs it into
the global namespace.

[00:07:58.06]
And we've got an independent
little pipenv environment.

[00:08:02.03]
We could've installed things
in the global namespace,

[00:08:04.04]
but then if we've got
multiple things installed

[00:08:06.02]
it's a bit tricky

[00:08:07.00]
and it's hard to keep
dependencies together.

[00:08:10.04]
Incidentally, this is one of the reasons

[00:08:11.08]
people don't like pipenv,

[00:08:13.01]
because they prefer to
just have containers

[00:08:15.05]
or virtual machines dedicated to a task

[00:08:18.03]
so they don't have this
problem of layering

[00:08:20.04]
between the system namespace
and the pipenv namespace.

[00:08:23.07]
But personally I quite like the idea

[00:08:25.04]
of having separate little
virtual environments.

[00:08:30.06]
So I think that smbus2
is the Python package

[00:08:35.01]
that is equivalent to
python-smbus in Raspbian.

[00:08:38.06]
So I'm now going to try that out.

[00:08:42.09]
Pipenv python.

[00:08:45.09]
So I cannot import smbus
but I can import smbus2.

[00:08:48.08]
So no, it's wrong.

[00:08:52.03]
I don't know why I didn't
spot that before, smbus-1.1.

[00:08:59.09]
So if I uninstall smbus2.

[00:09:04.02]
And I'm gonna skip the locking

[00:09:05.01]
because I'm about to install smbus

[00:09:07.04]
and that will cause it to relock anyway.

[00:09:12.09]
Okay.

[00:09:16.04]
The other things it wanted to
install were numpy and pil,

[00:09:20.00]
weren't they?

[00:09:22.06]
So I guess I should check
if those are installed

[00:09:24.09]
before I go trudge ahead.

[00:09:35.03]
Import numpy.

[00:09:37.03]
No pil, so...

[00:09:41.01]
So we're gonna install numpy

[00:09:42.04]
and I'm going to install Pillow,

[00:09:44.02]
which is Alex Clark's fork of
Pil, so let's install them.

[00:09:50.06]
Way back when you couldn't
install pil through setuptools.

[00:09:53.06]
I don't know if you can now.

[00:09:56.01]
Some people would just remember URLs

[00:09:58.05]
of repackaged versions of
pil that you could install.

[00:10:03.04]
Like dist.repos.org/pil-1.1.6.tar.gz.

[00:10:09.07]
Typed that so many times

[00:10:11.03]
before Alex stepped up and made Pillow.

[00:10:17.00]
There's also pil woTK

[00:10:19.02]
which was a variant of pil
that didn't have TK support,

[00:10:24.06]
and that was pip installable.

[00:10:28.06]
Should've known really.. "python-smbus"

[00:10:30.08]
Of course it's going to
be pip install smbus.

[00:10:39.01]
Let's have a look at this
example code while I'm waiting.

[00:10:42.04]
So.. oh, this does "from
smbus2 import smbus"!

[00:10:47.02]
So I could've used smbus2.

[00:10:48.05]
Well that was stupid of me, wasn't it?

[00:10:50.08]
Okay.

[00:10:54.03]
Should've had that.

[00:10:55.03]
Right, so use either smbus.

[00:10:59.01]
Then call BME280, give it the smbus object

[00:11:06.04]
and then you can get
temperature, pressure, humidity.

[00:11:08.05]
Okay.

[00:11:14.05]
So I don't actually
need any of these things

[00:11:15.09]
it's installing right now, I don't think,

[00:11:17.04]
because I'm not using the screen.

[00:11:20.04]
I'm not handling the data,

[00:11:21.04]
I just wanna get the value
of that BME280 sensor.

[00:11:28.05]
Can I be cheeky and just
restart and run all?

[00:11:33.05]
During the middle of the pip operation?

[00:11:38.09]
Logging is not defined.

[00:11:39.09]
Ah, brilliant.

[00:11:40.07]
Right, that's 'cause I removed it.

[00:11:43.04]
Take that.

[00:11:45.08]
Yeah!

[00:11:46.06]
Okay, so temperature, 24.11 degrees C,

[00:11:50.01]
pressure, 689.31 hPa.

[00:11:57.02]
hPa, what's that?

[00:12:01.03]
Relative humidity, 91.33%.

[00:12:05.03]
No, that sounds a bit high.

[00:12:07.01]
Okay.

[00:12:12.01]
Pressure in Leeds.

[00:12:15.03]
Weather forecast Leeds.

[00:12:22.04]
1014.2 hPa, humidity 46.6%.

[00:12:29.07]
Right, okay.

[00:12:33.09]
Bit dubious.

[00:12:44.03]
Okay, oh, that looks a lot better.

[00:12:46.03]
Look, temperature's too high.

[00:12:48.05]
So it's not 35 degrees.

[00:12:51.02]
Pressure should be over 1000

[00:12:53.03]
and this humidity should be, what?

[00:12:57.05]
Oh, I know.

[00:13:02.06]
Pi at rpi-lounge.

[00:13:06.05]
Rpi4-lounge.

[00:13:10.01]
Password.

[00:13:28.04]
Okay, relative humidity
downstairs is 43.5%,

[00:13:33.01]
it's glitched in this version,
and temperature's 20.8.

[00:13:36.02]
So I'm expecting humidity in the 40's

[00:13:40.08]
and temperature in the 20's.

[00:13:55.05]
It's possible this is wrong, but...

[00:13:59.03]
Hm.

[00:14:01.05]
Hm, not great.

[00:14:04.09]
See what else we can manage.

[00:14:09.08]
Weather and light, let's try that.

[00:14:12.08]
Ooh, that's long.

[00:14:19.04]
That's really long, that
does a lot of stuff.

[00:14:23.06]
Enviro+.

[00:14:26.02]
There's gotta be a simple
thing for me to look at.

[00:14:35.02]
Okay, I guess I'm gonna grab one of these.

[00:14:36.07]
Let's do All In One.

[00:14:39.06]
Raw.

[00:14:41.08]
And I'm gonna be lazy and
just dump it into this cell.

[00:14:46.07]
And we'll see what happens.

[00:14:49.04]
Eh, import from numpy C extensions failed.

[00:14:55.03]
Let's let that finish so I
can have the kernel restart.

[00:15:10.03]
Let's Google this error.

[00:15:31.02]
Libatlas-base-dev.

[00:15:32.05]
Apparently not libblas-dev.

[00:15:52.00]
I don't even know if pipenv install numpy

[00:15:53.09]
is gonna force it to rebuild.

[00:16:12.01]
'Kay, so let's try that.

[00:16:16.04]
Restart kernel, restart.

[00:16:20.07]
All right, let's try this one.

[00:16:23.07]
Libopenjp2 cannot open shared object.

[00:16:32.09]
Really we should be approaching
this with a bit more method.

[00:16:38.09]
No module named sounddevice.

[00:16:42.01]
That's interesting.

[00:16:47.04]
sounddevice...

[00:16:52.05]
What's sounddevice?

[00:17:03.03]
It's one of the dependencies
by the looks of it.

[00:17:07.07]
install_requires sounddevice, right.

[00:17:24.07]
There's no sounddevice there

[00:17:25.09]
but it's listed as install_requires.

[00:17:36.02]
Enviro+, all right.

[00:17:42.03]
Download files.

[00:17:45.02]
So this is the wheel.

[00:17:48.02]
Grab that.

[00:17:57.02]
And unzip it.

[00:18:00.02]
Enviroplus...

[00:18:07.05]
Get dist-info METADATA.

[00:18:16.07]
Requires-Dist,

[00:18:21.04]
there's no sounddevice there.

[00:18:25.00]
Why is there no sounddevice there?

[00:18:27.06]
Has this changed in the meantime?

[00:18:30.00]
17th of March, 16th of March.

[00:18:37.00]
What?

[00:18:38.08]
What's going on?

[00:18:45.02]
Library, setup.cfg, sounddevice.

[00:19:01.07]
Fix noise_floor bug.

[00:19:09.05]
Sounddevice, add deps
for noise measurements,

[00:19:11.03]
seven months ago.

[00:19:13.09]
All right.

[00:19:16.04]
Fine.

[00:19:24.04]
So why?

[00:19:37.06]
What?

[00:19:38.06]
Why is that not there?

[00:19:40.02]
Sounddevice.

[00:19:57.08]
Delete the egg-info directory.

[00:20:04.09]
Python setup.py bdist_wheel.

[00:20:09.05]
So we should re-generate
the egg-info directory

[00:20:11.06]
as well as the wheel.

[00:20:13.05]
Take a look at that.

[00:20:20.00]
And there's no sounddevice.

[00:20:29.09]
It's in setup.cfg, sounddevice.

[00:20:40.00]
Setup.cfg sounddevice
and a bunch of tests.

[00:20:49.07]
What's going on?

[00:21:04.04]
Ah, look at that!

[00:21:06.08]
There's a install_requires in setup.py

[00:21:12.03]
which doesn't include sounddevice.

[00:21:15.01]
So this is a bugged release.

[00:21:16.05]
All right, okay.

[00:21:20.03]
Issues, should've checked that.

[00:21:24.01]
Install misses package, yes.

[00:21:27.04]
No.

[00:21:44.03]
All right, I think released as of...

[00:22:01.02]
All right.

[00:22:02.02]
So that, move package
and requires to there,

[00:22:03.07]
which is marked the 24th.

[00:22:04.07]
So the 16th wasn't,

[00:22:08.04]
so that's eight days later this got fixed.

[00:22:17.09]
Packages, enviroplus.

[00:22:20.07]
Yep.

[00:22:41.06]
I'm just gonna double check
there's no existing issue here.

[00:22:49.08]
Support MEMS microphone, sounddevice...

[00:22:57.09]
Extra dependency needed,
examples LCD gives an error...

[00:23:04.01]
Okay, so I think I'm gonna
call that a day for now.

[00:23:10.02]
It's a little tricky to proceed

[00:23:13.02]
without the underlying library

[00:23:15.05]
being installed correctly.

[00:23:18.06]
I think next time I'm gonna try

[00:23:20.07]
installing sounddevice manually

[00:23:25.08]
and see what we can do.

[00:23:27.03]
So thanks for watching me get frustrated

[00:23:30.03]
and not knowing how to do

[00:23:32.08]
simple package installation in Python.