New IoT device with a servo motor

image

I had a recommendation to move to a different IoT device to make connection to Azure and programming easier going forward. The recommendations was to use a:

SparkFun Thing Plus – ESP32-S2 WROOM

So the first thing I needed to do was get the right board selection working with PlatformIO. The end result of this was the need to use this platformio.ini file in the project:

[env:sparkfun_esp32s2_thing_plus]
platform = espressif32
board = sparkfun_esp32s2_thing_plus
framework = arduino

and you can look at this for further reference:

https://docs.platformio.org/en/latest/boards/espressif32/sparkfun_esp32s2_thing_plus.html#board-espressif32-sparkfun-esp32s2-thing-plus

Once I had the board being recognised and accepting uploads the next challenge was to execute some code. I went back to start and did the standard stuff I’d done with other devices like:

External flashing LED

but with the SparkFun Thing Plus – ESP32-S2 WROOM device instead. All of that is pretty straight forward on any device, so I won’t repeat the details here. What I though I’d cover off is something new on attaching a servo motor to the device.

image

The configuration is pretty simple as shown above. I used a:

SG92R Micro server motor

that basically has three connections – Power, Ground and Control as shown above.

I found some simple example code that I could use here:

Using Servo Motors with ESP32

I have also put all the code I’ve actually used for my ESP32-S2 device here:

https://github.com/directorcia/Azure/tree/master/Iot/ESP32-S2

I also needed to add the

ESP32Servo

library from Kevin Harrington to the project.

The code that I used to make the servo ‘sweep’ backwards and forwards is here for your reference:

https://github.com/directorcia/Azure/blob/master/Iot/ESP32-S2/servo.cpp

and here’s what it looked like when it ran on the device:

servo

With this new device under control, it’s time to move onto bigger projects.

PlatformIO code compiles but fails to execute when uploaded

image

This is a really silly one but it tripped me up for far longer than I care to admit. In essence, the issues I was having is that I would successfully compile and upload my code in PlatformIO but for some reason it wouldn’t execute on the device. I tried many, many things, including a complete re-install of the environment to no avail.

The issue was that I was placing my code file at the root of the project file structure (where the red X is above) and not in the SRC directory (where the green tick is above). Thus, when I created a new project using PlatformIO, it created a new empty main.cpp in the SRC directory and was actually compiling that and uploaded that to my device. Because this default template effectively did nothing, the device looked as though it wasn’t working.

Without knowing this, I had create my code in a file, also called main.cpp, but at the root of the project structure that was never being compiled and uploaded! D’Oh!

Once I had my code in the main.cpp file in the SRC directory, it uploaded and executed on the device as expected. I probably should have read the PlatformIO documentation first:

PlatformIO IDE for VSCode

alas, I didn’t and thereby wasted hours trying to work out what was wrong! I’m glad that I’ve now worked it out and I’m sharing just in case someone else has the same issue, as I did spend heaps of time searching for a solution and found none that pointed out my error of file location.

Connecting to Azure IoT hub

The main aim of my dive into IoT was to get a remote device talking to Azure. After getting the IoT device connected to WiFi, flashing LEDs, accepting input from a button and capturing temperature data, it was now time to make that dream a reality.

There are different methods of connecting devices to Azure but for my first attempt I decided to use Azure IoT hub. The first step in that process is to login to the Azure portal and create a new IoT hub.

image

To do this, select the Create menu option in the top left of the Azure IoT hub blade

image

Once you have selected the Azure subscription and Resource group you’ll need to pick a Name for your Azure IoT hub. This name needs to be unique as a URL will be generated from this. Then select a Region and a Tier. You’ll notice that there is a Free tier, which I have selected for this example (very handy for tests like this).

image

Next, you can configure your networking. Because my device will just connect to a public Internet connection I selected Public access.

image

I left the management options as shown above.

image

The add-ons shown here are not available on the Free tier.

image

I didn’t need any tags.

image

I finally get a summary as shown. Note that the cost will be $0 because I am using the Free tier. Select Create to complete the process.

image

After a few minutes you should be able to see you IoT hub as shown above. Select Devices from the menu on the left.

image

Now select Add device from the menu on the right as shown above.

image

Give a the device a Name, and to keep things simple select Symmetric key for the Authentication type as shown. Ensure that the Auto-generate keys is select and that Connect this device to an IoT hub is set to Enable. Select Save to continue.

image

You should now see the device you just created listed as shown above. Select the name of the device to view it’s properties.

image

Here you will find the settings for your device. You’ll need to grab at least one Key and the matching Connection string to use when configuring your device.

With all of that information it’s time to head back and set up the device.

I have uploaded the code to get the device connected to Azure IoT hub here:

https://github.com/directorcia/Azure/blob/master/Iot/huzzah-iothub.ino

It is much more extensive that before and I will admit I am not yet 100% sure of what it all does but basically it connects the device to local Wifi then sends telemetry information to Azure IoT hub.

You’ll also need to have the file iot_config.h in the same directory when compiling your code. You can find an example of that here:

https://github.com/directorcia/Azure/blob/master/Iot/iot_configs.h

that file basically extracts all the unique security information like WiFi password, device keys and IoT Hub URL away from the main code. You’ll need to modify this file to suit your own environment before compiling.

The only other thing you’ll need to do is connect a single LED to pin 5 of the device to act as a diagnostic indicator. It will basically flash when data is sent to Azure IoT hub which gives a nice visual representation of something actually happening on the device.

image

When you compile the code you’ll also need to ensure all the appropriate libraries are available. Details of each of these is contained in the code.

With the compiled code uploaded to the device you should see the LED light start to flash after a few seconds indicating that data is being sent. If you look at the serial port you should see diagnostic data like so:

image

If you then look at the Overview page in the Azure IoT Hub you should the diagnostics reporting a number of messages increasing over time like so:

image

You can also download a tool called the Azure IoT explorer which you will find here:

https://github.com/Azure/azure-iot-explorer/releases

image

When you configure this for your IoT hub environment and drill down into the Device then Telemetry, as shown above, should allow to see the actual information being sent.

So there you have it. Once you have set up an Azure IoT hub and added a device to it you can grab the connection details and plug them into the code you use to configure your device. You can also use the Azure IoT Explorer to get more granular details of what your device is doing.

The next challenge is now to get the device working with Azure IoT central.

Adafruit Huzzah Temperature senor

image

Last project was:

Input from button

Next up now is connecting the DHT20 temperature sensor to the Adafruit Huzzah. The idea is to read data from the sensor and display it via the serial output.

The wiring diagram is shown above and is pretty straight forward. The main thing is to get the pin functions for the sensor. All 4 pins needs to be connected. On the DHT20 pin 1 goes to the 3V output on Huzzah. Next, the DHT20 pin 2 goes to ground on the Huzzah.

The final 2 pins (SCL and SDA) are for serial communications. Thus, DHT20 pin 2 goes to the SCL connection on the Huzzah. Finally, pin 4 from the DHT20 goes to SDA on the Huzzah.

The code is also very straight forward and I found it here:

https://learn.adafruit.com/adafruit-aht20/arduino

and my version is at:

https://github.com/directorcia/Azure/blob/master/Iot/huzzah-tempsens.ino

To make this work you’ll also need to add the following the library:

Adafruit AHTX0

Once you combine all these elements you can compile the code and upload it to the Huzzah. Now, the Huzzah should produce a serial output that looks like:

image

which shows the temperature and humidity of the room.

A pretty simple one when it comes to capturing temperature and humidity.

The next challenge will be to getting data in and out of Azure.