This is only a preview of the September 2023 issue of Practical Electronics. You can view 0 of the 72 pages in the full issue. Articles in this series:
|
Make it with Micromite
Phil Boyce – hands on with the mighty PIC-powered, BASIC microcontroller
Part 49: PicoMite Smart light Controller – Part 4
L
ast month, we covered
extracting a list of available smart
lights (and smart sockets) from a
Hue Hub, and then how to sort them all
alphabetically by name – see Fig.1. These
names were passed in the JSON data that
the PicoMite received from the Hue Hub
once an initial request was sent by our
demo code. In addition to the names,
we also extracted other information,
such as the current smart light state
(on or off), each smart light’s unique ID
number, and also, for any smart light
with dimmable functionality, the last
setting of the brightness level.
This month, we conclude the project
with some program code that allows
control of selected smart lights via the
PicoMite’s touchscreen. We will not
go into every detail of the code, but
instead we will discuss the process
flow of the key functions, remembering
that the initial concept was to allow a
user to have access to control a limited
number of lights, rather than be able to
control all the lights in the setup. For
example, in my specific case it was to
allow my children to only be able to
control the lights in their individual
bedrooms, and not be able to annoy their
siblings (and parents!) by being able to
Fig.1. The ExtractJSON.txt
program from last month displays an
alphabetically sorted list of all available
smart lights (and smart sockets) on the
PicoMite’s Touchscreen.
Practical Electronics | September | 2023
n A
llow an admin user (eg, parent) to:
1. Fetch the most recent data from
the Hue Hub for all smart lights
and smart sockets.
2. See an alphabetically sorted list
of names for all smart lights and
smart sockets. If the list is too long
for the touchscreen, then show it
on multiple pages.
3. Select which smart lights and smart
sockets non-admin user(s) may
control. Selection is made using a
‘tick box’ against each name.
4. Refresh data from the Hue Hub if
new devices are added to the setup.
n A
llow a non-admin user (eg, child) to:
control every single light throughout
the house.
The project’s required program code
(PicoMiteSLCv2.txt) is available for
download from the September 2023 page
of the Practical Electronics website at:
https://bit.ly/pe-downloads
Remember, as always, the program
code presented is provided to you as
a working minimum for you to build
upon. You can easily customise it so that
it looks and works the way you want it
to. In addition, we would encourage you
to add new features so that the PicoMite
smart light controller meets your specific
needs; some ideas will be discussed at
the end. On the other hand, you may
simply wish to use this controller with
your Hue setup as an alternative to the
standard Hue switches and buttons.
Functional requirements
To allow control of certain specific smart
lights (and smart sockets) in the home,
the program code should provide the
following capabilities:
5. See only a chosen subset of names
for the smart light(s) and smart
socket(s) selected by the admin user
– again, if the list is too long then
use multiple touchscreen pages.
6. Switch any smart device ON or
OFF by pressing the appropriate
button – two buttons against each
name in the user’s list.
In the six key points listed above, there
will also need to be quite a lot of errortrapping. For example, in point 3 (admin
user selecting which devices a user can
control), if nothing has been selected,
then a warning message will need to be
shown. To make the above clear, let’s
work through each of these key points
in turn.
1. Fetch data
On running the program code, the
PicoMite will not contain any device
information (ie, no names for smart lights
or smart sockets). Therefore, the user
is prompted to search for devices – see
Fig.2. After pressing the ‘Search’ button,
a request is sent to the Hue Hub, and a
‘Please wait’ message will be displayed
while the request and response is
processed – see Fig.3. The request sent,
41
Fig.2. When the program code is RUN,
the PicoMite will need to search for a list
of devices – this is manually triggered.
Fig.6. Here, two smart devices have
been selected (‘Phil’s Lamp’ and ‘Sheila’s
Lamp’) as indicated by the ‘X’ in the
adjacent tick box. Note that the count
of the total number of smart devices
selected (from all pages) is shown in
green at the bottom of the screen.
pages (only four devices are displayed
per page). Figures 5a-5e show the five
pages required to display the 18 smart
devices as shown listed in Fig.1.
n A
page navigator is located at the
top of the touchscreen, comprising
the blue ‘<’ and ‘>’ buttons. Note
that the current page number and
the total number of pages is also
displayed (ie, ‘Page 1/5’ means the
touchscreen is showing the first page
of five pages).
Fig.3. After the search request is sent to
the Hub, and while any JSON response is
processed, this screen is displayed. Note
the Hub’s IP address is displayed at the
bottom of the screen.
3. Select devices
and the processing of the response back, is
based on the ExtractJSON code from last
month. If no devices are detected then the
variable L_DeviceQty will have a value
of 0, resulting in the warning message
shown in Fig.4. In this situation, a ‘Try
Again’ button will allow the admin user
to repeat the process. If the value of L_
DeviceQty is greater than 0, then its
value will represent the number of devices
that are configured in the Hue Hub.
n T
he tick box to the left of a device’s
name allows the adjacent device
to toggle between selected, and
unselected. When a device is
selected, an ‘X’ will appear in the
tick box (see Fig.6)
n T
he number of selected devices is
displayed (in green) in the bottomleft corner of the screen, along with
the total number of devices found.
n O
nce the required devices have been
selected, the green ‘Save’ button is
pressed. However, if no devices have
been selected then pressing ‘Save’ will
generate a warning message with an
option to take the admin user back to
the Select screen – see Fig.7.
2. Admin list (all devices)
Once valid information has been received
from the Hue Hub, the names of all smart
devices will be displayed alphabetically
on the touchscreen – see Fig.5a-e. There
are a few things to point out on this screen:
n I
f the list contains more than four
devices, then there will be multiple
Fig.4. If no smart devices are found after
the JSON processing, this screen is
displayed, with an option to ‘Try Again’.
42
Fig.5a-5e (top to bottom). Controllable
smart devices that have been configured
in the Hue App will be listed alphabetically
on the ‘selection screen’. Four smart
devices are listed per page, meaning
that five pages are needed to list all 18
devices from Fig.1. Note the blue page
navigator at the top, the green ‘Save’
button and the red ‘Refresh’ button.
Fig.7. If no smart devices are selected
by the admin user, then this screen is
displayed, with an option to jump back to
the ‘Select’ screen.
Practical Electronics | September | 2023
understanding these, along with looking at
the comments in the MMBASIC program
code, you should be able to follow how
everything works.
Remember that the program code is
there for you to add your own ideas.
Let’s discuss a few examples.
Possible upgrades
Fig.8. The main user screen allows simple
control of any listed smart device by
pressing the relevant on (1) or off (0) button.
4. Refresh admin list (from Hub)
n T
he red ‘Refresh’ button in the bottom-
right corner of the screen allows the
admin user to refresh the list (displayed
on the touchscreen) with all the devices
that are configured in the Hue Hub. This
is useful when new devices have been
added to the Hue setup (via the Hue
App), and which you subsequently
want to be able to control from the
PicoMite Smart light Controller.
5. User list (selected devices)
n T
he user will only see a list of devices
that they are able to control once a
minimum of one device has been
selected on the selection screen (and
the ‘Save’ button pressed).
n I
f the user’s list contains five devices
or less, then they will be listed, as
shown in Fig.8.
n I
f the user’s list contains more than
five devices, then multiple pages will
be displayed (this time with the page
navigator at the bottom of the screen)
– see Fig.9.
6. Control device
n T
o the left of each named device is a
red off button (marked ‘0’) and a green
on button (marked ‘1’) – see Fig.8 and
Fig.9. Pressing the appropriate button
is all that is needed to control the
relevant smart device.
The above six points are a summary of
the process flow of the program code. By
Fig.9. When the user has access to more
than five smart devices, a page navigator
appears at the bottom of the screen.
Practical Electronics | September | 2023
Once you start using the PicoMite Smart
light Controller, you will no doubt think
of many things that could be improved
or added. Here are some ideas:
n S
tore the admin and user list of
devices on either an SD card or in
Flash memory (rather than having to
perform the selection process each
time you RUN the program)
n D
istinguish between on/off-only devices
and smart lights that are dimmable (for
example, use different coloured names
to distinguish between them)
n F
or dimmable smart lights, allow a
percentage value (1-100) to be entered
(or maybe create a sliding-bar graphic)
n A
dd password protection to access
the admin list (accessed from the
user list by tapping a secretly placed
invisible touchscreen button)
n H
ave an admin menu to allow full
configuration (eg, inputs for: Wi-Fi
SSID, Wi-Fi Password, Hub IP address,
Hub API)
n
Auto switch-off touchscreen after a
period of non-use, and ‘wake up’ on
touch
n A
bility to rename a device via the
touchscreen.
The above should provoke some ideas
for you – and no doubt you can think
of many more. Please do send me an
email with a brief description of any
custom version of the PicoMite Smart
light Controller that you create – it is
always great to hear from readers!
Next time
We have had several enquiries about
whether there is a version of MMBASIC
that will run on the wireless version
of the Raspberry Pi Pico (rather than
having to use an ESP32 Wi-Fi module
connected to a standard Raspberry
Pi Pico). The good news is that there
is indeed a dedicated version of
firmware available. So, next time we
will discuss how to install MMBASIC
onto a Raspberry Pi Pico W module;
and we’ll cover the many features and
benefits it offers.
Until then, stay safe, and have FUN!
Questions? Please email Phil at:
contactus<at>micromite.org
www.poscope.com/epe
- USB
- Ethernet
- Web server
- Modbus
- CNC (Mach3/4)
- IO
- PWM
- Encoders
- LCD
- Analog inputs
- Compact PLC
- up to 256
- up to 32
microsteps
microsteps
- 50 V / 6 A
- 30 V / 2.5 A
- USB configuration
- Isolated
PoScope Mega1+
PoScope Mega50
- up to 50MS/s
- resolution up to 12bit
- Lowest power consumption
- Smallest and lightest
- 7 in 1: Oscilloscope, FFT, X/Y,
Recorder, Logic Analyzer, Protocol
decoder, Signal generator
43
|