Lesson 7: Internet of Things
Table of Contents
- IoT Platforms
- Adafruit IO
- Let’s make something!
- Previous lesson
The ESP32 is exciting not just because of its speed, memory, and GPIO capabilities but also because it is truly a modern Internet of Things (IoT) board with Wi-Fi and Bluetooth support. And it’s never been easier to get data off of these Internet-connected devices and into “the cloud” (for better or worse). :)
There are a ton of IoT platforms—significantly more than even a few years ago—including Adafruit IO, Blynk, ThingsSpeak, and Losant. See Hackaday and bigmessowires for reviews.
If you don’t want to dev out your own backend or you simply want a platform to rapidly prototype an idea, then these IoT sites are convenient, easy-to-use, flexible, and fairly feature rich. You can get real-time displays of your data, control and interact with this data and Internet-connected devices, connect your projects to web services like Twitter, etc. Many of the IoT platforms also connect with services like IFTTT and/or offer their own event triggering to, for example, email you when an anomaly is detected.
And many of these platforms support both REST APIs—which you’re likely familiar with if you’ve done any web dev—as well as the Message Queuing Telemetry Transport (MQTT) protocol, which is an open standard for lightweight, publish-subscribe networking. See MQTT.
For our lesson, we’ll be using Adafruit.io, which greatly simplifies interfacing with a cloud backend from the ESP32 and provides a rich array of features. The free tier provides 10 feeds, 5 dashboards, an upload rate limit of 30 data points/minute, 30 day storage, and privacy controls. The paid tier, called Adafruit IO Plus, is $99/year and provides unlimited feeds, unlimited dashboards, 60 data points/minute, and 60 days of storage.
You must register for Adafruit IO on their website. Follow their step-by-step instructions here. If you don’t rate throttle your uploads to a maximum of 1 upload every 2 seconds (30 uploads/minute), you’ll receive this warning and a temporary block from Adafruit IO
Install Adafruit IO in Arduino IDE
To install the Adafruit IO library for Arduino, open the Arduino IDE and go to Tools -> Library -> Manage Libraries. When the Library Manager opens, search for “Adafruit IO Arduino” and scroll to find the match:
When asked to install dependencies, click on “Install All”
Using Adafruit IO
Adafruit has published a 7-step guide that covers everything from uploading sensor data and viewing it on a dashboard (took us ~5 minutes to setup) to sending data from Adafruit IO to control an RGB LED or servo motor. You can access these examples (and many more) within the Arduino IDE by going to File -> Examples -> Adafruit IO Arduino:
Let’s make something!
We suggest following the Adafruit IO guide to learn all facets of the IoT platform. However, as a start, we created a simple real-time sensor feed based on the Analog Input Adafruit example. Their source code is here or you can access it via File -> Examples -> Adafruit IO Arduino -> adafruitio_08_analog_in.
Because the max upload rate is 30 data points/minute (1 data point every two seconds), we throttle our uploads. If the photoresistor value has changed, we upload at a maximum rate of every two seconds. If the photoresistor value has not changed, we upload at a rate of once every 10 seconds.
We have a photoresistor in a voltage divider with a 10k resistor connected to
A7 (we can only use ADC1 pins because we’re going to use WiFi). The analog input voltage will increase as the brightness level increases. We drive a PWM output on pin GPIO 21 whose duty cycle is inversely proportional to the light level thereby turning on an LED brighter as the light level decreases.
The code is on github. Note: there are two files. The IotPhotoresistorLed.ino file and the config.h file.
config.h file, you must change the following:
And here’s the full IotPhotoresistorLed.ino code:
This source code is on GitHub.
Here’s a brief video demonstration showing a workbench recording paired with a screen recording of Adafruit IO.
Previous: Capacitive Touch Sensing