This writeup shows how you can use the Azure Sphere development kit and your personal phone to remotely control a power extension. I got inspired to build this project as I've seen little to no projects that involve a specialized mobile application interfaced with the Azure Sphere.
Here, I used a spare power extension integrated with the Azure Sphere to securely and remotely control household appliances powered by the extension. It's not much but a few seconds saved every day to switch ON/OFF an appliance goes a long way. I'll be updating this post here and then to include more sensors and cool stuff interfaced with the Azure sphere and the mobile application.
Also, if you didn't already know, Sphere + Extension = Sphextension 🤯
VideoDo turn on subtitles for the captions.
Project OverviewThere 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.
✔️ Mobile Application Development
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...
✔️ Cloud Integration
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 (10GB/month) for service usage was perfect for this project's use case.
✔️ AzureSphere Firmware
The Azure Sphere development board is a great device to work with. It is true that it takes a bit longer to get started with it than other platforms but since its debut, support, libraries, and tutorials have poured online and now it is easy to prototype an application on the board. Moving away from the Arduino ecosystem teaches you more of the low-level embedded development and this is exactly what you'll do with the Azure sphere.
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.
- 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.
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.
✔️ Expo framework
- 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.
✔️ Firebase
- Create a Firebase project.
- Get and save your Firebase configurations to interface Firebase Realtime Database with Expo Framework
✔️ Expo App with Firebase Integration
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.
✔️ Verifying data on Firebase Console
✔️ Azure Sphere Firmware
First, we set the app_manifest.json file to include capabilities to securely connect to the Firebase Realtime Database and to allow specific GPIO usage 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.
✔️ Putting it all together
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.
There'll be more documentation of the whole build process and improvements to follow. Stay tuned!
Comments