LG201: IoT Prototype Development - IoT Capabilities

The next module of my MSc, LG202: IoT Cloud Server Project, will expand greatly on this area of development, but to prove my device can communicate with the internet, I will add Web Socket functionality to this website to demonstrate the communication, in both directions.

To get the device online and to view sensor output directly from the device we will also create a micro-website that will be hosted directly on the device.

4a. Embedded Webpages

The ESP32 has a mountable file system through SPI Flash File System or SPIFFS. The Hazzah is setup with 1.5Mbs for files, which is loads of space, but for one of the two configurations we won't have access to the internet so this needs to include any images, stylesheets and scripts.

We need two configurations, one for the Captive Portal where we will enter and store the network details and another for the Local Website that will display the sensor information.

8 files are all we need at this point, website requests can return files or interact directly with the sketch. I have used C++ to create JSON from the Sensor and to save network details to EEPROM.

The sketch loaded onto the board in the last section has been updated to include this functionality.

4b. Web Socket Server

This website is developed in .net core C# and is hosted on the Microsoft Azure Cloud Platform, .Net Core is an open-source version on .Net and can run on any operating system. It can be hosted in containers in Docker and even runs happily on a Raspberry Pi. Having worked in .Net for years building distributed systems, this is an incredibly powerful yet portable technology.

Core websites process requests by running through a number of services, called middleware, in a row, this is called the pipe. There are standard middleware items and you can write your own and add them to the pipe. So, the first thing we need to do is pick web socket requests and pass them to our middleware to process them.

The pipe is controlled by the StartUp.cs class, so we need to make a couple of amendments to that file. I won't go into to much detail but any requests to the URL /ws will be passed to our middleware WezSocketMiddleware.cs which will check they are Web Socket requests and put them back in the pipe if they are not. If they are it will pass them to WezSocketManager.cs which will keep track of them.

For this demonstration, WezSocketManager.cs will just echo any messages received to all connected clients. WezSocketManager contains a ConcurrentDictionary called WezSocketManager.sockets which will contain all the sockets using a session key or device key to keep track of them.

This is all working nicely, the next module will focus on expanding this into a secure and scalable solution. The files are attached below if you’re interested in them.

    Above: Put a Socket in it! All Web Socket messages are echoed here... open a couple of browser windows and have a chat with yourself.

    4c. Sensor Module

    The data from the sensor is available on the cloud and the local device. The local website uses a timed Ajax request through JQuery to update live content in the page. The data is made available to the cloud through Web Socket messages every 10 seconds. You can also send commands from the cloud to the device to stop and start the data.

    That’s it… my Thing is on the Internet of Things.