The first thought that came to mind about this "Home Is Where the Hack Is" Challenge was to build and improve on some appliances I had lying around my home with the goal of fun usability.
I had a spare unused power extension and thought it would be awesome to integrate the Azure Sphere with it to securely and remotely control household appliances powered by the extension, thus giving birth to the Sphextension (Sphere + Extension)!
Here's the project demonstration, do turn on subtitles for some of the comments.
There are three main parts to this project which are the customized mobile application for the control interface, cloud integration for remote control of the appliance, and finally the firmware on the Azure sphere itself.
For this part, I had the aim of building on a platform that does not require installing Xcode or Android Studio for application development. Aside from the additional installation of these IDE's, there are big learning curves associated with building on those platforms. I've had prior experience with building web applications on the React framework and thought it would translate nicely to develop for a React-native mobile application in this challenge. So after digging around, I found Expo to be the perfect framework and platform for this project due to the fact that I can write and deploy code right from the Visual Studio Code development platform. Visual Studio Code is also used as the platform for firmware development on the Azure Sphere and thus building on Expo makes it convenient to develop and deploy code right from the same platform.
However, there are some caveats to building on this platform which is explained in the next part...
I initially planned to integrate the Azure IoT SDK within the custom mobile application, but with the Expo framework being relatively new and Azure having limited support for native mobile applications as of now, I had to settle with a cloud service that can achieve the project's functionality while still coupled to the Expo framework. I used Firebase, specifically the Realtime Database service to connect the mobile application and the Azure Sphere. Having good documentation, support for the Expo framework and a generous free allocation every month for service usage was perfect for this project's use case.
For this project's use case, there are two functionalities required. The first is having the capability to access the Cloud service in which the Azure sphere collects data on the state (ON/OFF) for each appliance connected to the power extension. The second is having the capability to send the appropriate signal to GPIO's to switch ON/OFF an appliance based on the state obtained from the cloud. The two Azure Sphere code sample links below were used and modified to achieve these functionalities. Visual Studio Code was the main development platform used to develop and deploy code to the Azure Sphere.
- https://github.com/Azure/azure-sphere-samples/tree/master/Samples/HTTPS/HTTPS_Curl_Easy - to periodically obtain appliance state data (ON/OFF) from the cloud set by the mobile application.
- https://github.com/Azure/azure-sphere-samples/tree/master/Samples/GPIO/GPIO_HighLevelApp - to set the appropriate signal to the GPIO for switching ON/OFF based on the obtained state data.
Before diving into the build process, here's the hardware used for the project.
- Power Extension - to power the appliances
- Relays - to safely switch ON/OFF high voltage appliances.
- Azure Sphere - a secure high-level application platform with built-in communication and security features for internet-connected devices.
Here are the prerequisites before starting the project.
Visual Studio & Azure Sphere
- Visual Studio development platform installed with the C++ environment.
- The Azure Sphere SDK installed in your system.
- The Azure Sphere Extension installed on the Visual Studio platform.
- Claim the Azure Sphere for our tenant and connect it to your WiFi network.
- Download NodeJS in your system.
- Install the Expo command-line tool.
- The Expo Client on your mobile platform for realtime debugging & development. IOS or Android.
- Create a Firebase project.
- Get and save your Firebase configurations to interface Firebase Realtime Database with Expo Framework
We start by developing the mobile application first. Once the 'expo start' command is run on the terminal, a local development server is initialized where the QR code can be scanned by your phone to view and debug in realtime the code developed for the application.
A simple user interface is designed on the application to control appliances such as Table Lamps and Electric Fans.
And here's a code snippet to store the state based on button presses to the Firebase Realtime Database. A switch case to determine what appliance is being switched ON/OFF where tl is the Table Lamp, ef is the Electric Fan and ps is an additional Power socket to be controlled.
First, we set the app_manifest.json file to include capabilities to connect to the Firebase Realtime Database and to allow signaling to the GPIO's used in the project.
Next, the CMakeLists.txt is modified to include the hardware descriptors of the Azure Sphere as well as including the Root Certificate of the web service that is being accessed.
Here's the code snippet that checks whether the obtained data from Firebase (obtained via the libcurl sample code) is to set an appliance ON/OFF. Raw string comparison isn't probably the best for comparison of data and a JSON library such as this is much better for tokenization since the data obtained from Firebase is JSON formatted. That will be for future improvements but for now, this approach works out.
Here are the disassembly and connections of the Relay modules, Power Extension and the Azure Sphere.
And after all the wire configurations have been made and tested, the Azure Sphere and the Relay modules will be nicely tucked away when the cover is placed.
I do hope to document the whole build process even further as I only knew about this challenge one week before the deadline. Anyways, cheers to the organizers for coming up with this challenge, it was definitely one!