TL;DR This Windows application written in C++ native code displays data from an environmental sensor connected to a Raspberry Pi.
The long story. Why would I want to do this? Well, I was stuck inside on a rainy spring day in 2022 in the Northwest and feeling nostalgic and reflective. This led me to check out what the Visual Studio team had done with the Microsoft Foundation Class C++ class library. The MFC classes were first developed approximately 3 decades ago! These classes were conceived and developed as a way for developers to bootsrap their apps in C++ on the Windows platform I believe in the Windows 3.1 timeframe. When I joined Microsoft in 1995, I joined as a support engineer for Visual C++ and quickly got addicted to programming in the Microsoft Foundation Classes. My work soon led me to the product team where I contributed my own work to the libraries. I also had the great honor to work on a modern C++ class library called Active Template Library (ATL) which was designed specifically for the COM programmers. There ended up being some convergence between MFC and ATL especially for the utility classes for the purpose of reuse and efficiency.
Fast forward to 2022 and I am pleased to find that Visual Studio Professional still includes support for building apps from the ground up using MFC. How fun! The app you will find in this repo was wizard generated from app templates that come with Visual Studio Professional 2022 :). Still going strong! The code in this repo will produce a desktop Windows App that I only tested on Windows 11. The experience of developing is so much more modern now. I run Visual Studio 2022 Professional in an Azure VM that exists somewhere in the cloud. The code is easily managed in GitHub with the excellent integration that exists in Visual Studio. Microsoft's optimizing C++ compiler is still blazing fast. The app that is produced is only 190K in size. We probably would have snickered about this 'humungous' app back in the day but today, this is tiny.
So now for the more modern pieces of the app. The app displays the current reading from a BMP180 temperature and pressure sensor that is connected to a Raspberry Pi Zero that sits on my desk in my home office. This device sends its sensor readings to my Azure IoT endpoint which broadcasts sensor events to my Azure Event Hub. I run an Event Hub listener in a container that is hosted in the new and very modern Azure Containers offering. This listener copies the events to a Cosmos DB Database that keeps about 3 days worth of sensor readings in the database. The sensor readings are exposed by my Azure App Service hosted service that is reachable from the domain mikedice.net. Everything that runs in my Azure services and on my Raspbery Pi was written in TypeScript and runs in NodeJS. So that is where the data comes from.
Reading the data, parsing it and using it in an app is trivial with robust and thoroughly documented platform and libraries based on NodeJS or .Net. However this app is neither of those. I know I could use .Net and 'Managed C++'. But that would defeat the challenge! What MFC is missing because nobody had the need for it is an async HTTP class to access data using REST over HTTP. It also lacks a JSON parser to parse the data that was produced in JavaScript into a model that can be used in MFC. I didn't want to stray too far from the original theme of MFC's WinInet based classes for accessing the data. But, these classes were all synchronous and strenuously avoided doing anything async. Async is absolute min bar so I wrote my own WinInet based Http client in class CHttpClient that encapsulates the callback form of WinInet. For those that don't know WinInet is the Windows client side C API for accssing resources over the Internet. This API was originally used to build Internet Explorer! It is still part of Windows and still works great for desktop apps. Once the HTTP requests are worked out then there is the matter of parsing the JSON into a model that can be used by a View in C++. Luckily there is an excellent JSON parser written in C++ that can be easily included into any project that needs the ability to parse JSON. The library can be found here: JSON for Modern C++.
The last interesting part is the rendering of the text in the view. For this I used the Windows GDI+ library which is an upgrade to the original Windows GDI graphics system API. Really not doing anything too earth shaking here. Just rendering text in a view but there is no real layout system so all of the calculations of the layout had to be done by hand. I kept them very simple.
There it is. If anyone ever reads this drop me a comment and let me know. Or even better if you sync the repo and give a run yourself let me know how you like it.
- Mike