Thursday, June 8, 2017

Playing with the Adafruit Huzzah

The Internet of Things (IoT) involves hooking up everyday objects via wi-fi (or cellular) so that they can send or receive information.   This technology allows items to be "smart": to respond to remote commands or to update a user on status regardless of proximity.  Data from multiple IoT items can be aggregated to give the status of buildings or a region.

The Adafruit Huzzah is one of several products which use the ESP8266 wifi chip.  The Huzzah is "packaged" nicely with lots of support and a lower barrier of entry than the 8266 chip by itself.

I played with the Huzzah to create a proof of concept in order to understand its capabilities and ease of use.  It's pretty simple.  Much of the hard work is hidden in layers of various websites such as Adafruit IO and IFTTT.

WiFi Test

For a simple test, I first hooked up the Huzzah and ran a test program which gets you onto the Internet.   The program sends a GET request to the host

GET /testwifi/index.html HTTP/1.1\r\nHost:\r\nConnection: close\r\n\r\n

If that GET request works, you will see the contents of that webpage written to the serial monitor.  

This sketch also has two levels of diagnostics.  First, while the wifi connection is being made, you will see “.”  printed twice a second until a connection is made.  Afterwards, it will show you your IP address.  This diagnostic tells you if you have successfully chatted with the router whose SSID you provided. 

The second diagnostic level tells you whether you can make a TCP connection.  The Huzzah attempts to connect as a client to the host  If it successfully connects, it send the previously mentioned GET request, parses and displays the information sent back from the server.  Otherwise, it tells us that the "connection failed".   With one router, the test worked fine.  With a second, it failed, producing a mixture of gibberish and HEX information from the “stack”.

Adafruit IO

Next I played with Adafruit IO.  This gives the ability to set up feeds and dashboards which access the info in those feeds.   Adafruit IO runs using the MQTT protocol, and they have written nice libraries for the Huzzah.  I setup four testing feeds shown in the image below:

Each of these can be linked to a block on the dashboard as shown below:
Finally, we setup a dashboard as shown below.  I used a slider for the PWM feed, toggle switches for the LED and Huzzah Status (the former sending data and the latter receiving data from the Huzzah), and a textbox for the status of the switch (which is pulled HIGH on the Huzzah).  The dashboard can be accessed from anyplace which has Internet access and is the user's interface to the Huzzah.  I tested from a laptop, an old iTouch using wifi, and an Android device using cellular service.

In the code on the Huzzah, we either subscribe to feeds (test-LED and test-PWM) or publish to feeds (test-switch) depending on whether we are reading and acting on information from the user (subscribe) or sending information to the user (publish).  So if we want to turn on an appliance, turn off an alarm, or setup the sprinklers, we subscribe to an associated feed.  If we want to display information from a sensor, we publish to a feed.  The basic syntax is shown in the picture below:

In the setup function, we subscribe to to changes in the feeds that we have aliased above:

Then we see if a message has come on our subscription, determine whether its from the test-LED or test-PWM feed, parse the message to convert it to a number, and then act accordingly.  For the test-LED feed, this means turning on or off an LED.  For the test-PWM feed, this means analogWrite - ing to an LED to vary its brightness.

To publish our data, we use the .publish method for the class Adafruit_MQTT_Publish as shown below.
I played around with using a "will", which basically sends a final command if the connection terminates for some reason, but did not get consistent results with this protocol.

The following video shows the use of a laptop and an iTouch to control two LEDs.  The blue LED on the Huzzah is controlled by the test-LED feed and is either on or off.  The red LED is controlled by the slider linked to the test-PWM feed, which adjusts the LED brightness using PWM.  The Green wire shorts one of the Huzzah's inputs to ground which shows up in the feed test-switch and is displayed as a 1 or 0 under the heading Switch ON?  The switch updated consistently on my laptop and Android, but not on the iTouch.

Huzzah and IFTTT

The last experiment I did was to link up the Huzzah to a Twitter account to have it control some change in the physical world when a new tweet was received.  The connections are shown in the diagram below:

Everytime that something happens with the RedShiftFTC account, the Adafruit IO feed RedShiftTweeted is updated by IFTTT.  The Huzzah is monitoring this feed, and whenever the feed changes, the Huzzah just increments a variable called tweetsReceived.   I hooked up a LED matrix (LinkSprite LED Matrix kit using the Max7219 chip) and programmed a heart and a check symbol for the LED matrix.  The Huzzah causes the LED matrix to display the heart and check in succession, changing for the number of times that corresponds to the number of tweets received.  Full code is here.

Of minor interest (mostly for my records): The Huzzah runs at 3v3 while the LED matrix runs at 5V.  So I needed a level shifter between the two.  I originally worked with the 74LVC245 level shifter but found that it did not work reliably.  An oscilloscope trace showed that one of the outputs was behaving erratically.

I switched to the Sparkfun BOB-12009 level shift and got reliable results.

No comments:

Post a Comment