The IoT Gateway
Making reference to the project flow one last time,
The implementation of the IOT Gateway is based on the Azure Event Hubs REST API’s “Send Event” endpoint.
The endpoint is in this format:
In my case:
Further, the send event endpoint accepts JSON-encoded string. The POST request should have the following headers:
- Timeout : This can be set to 2 sec or 3 sec – depending on what works best for you
- Content-Type : application/json
- Authorization : The generated SharedAccessSignature token value.
Generating Event Hubs SharedAccessSignature
If you recall from the previous blog, we created a Shared Access policy called DHTsender in the Event Hub configuration tab:
Copy the key to somewhere safe.
Next,download Sandrino’s RedDog Signature Generator, then follow the steps to generate a SharedAccessSignature signature:
I used 5 years TTL because I don’t want to regenerate this signature any time sooner; your mileage may differ.
First, this is the pseudocode so you can easily translate it to any programming language of choice:
Deploying the IoT Gateway
I deployed the rails app to a CentOS server running on Azure VM. I opened up port 3000 (because I reserved port 80 for something else). To address the HTTP ONLY security concern, I configured ACL to only permit traffic from my home network IP address, that way no one outside my home network can read/send garbage from/to my application.
The disadvantage with the ACL rule approach is, my router’s public IP may change if restarted.
Also, note the “/32” in the remote subnet; it is a hack (if you like) to comply with CIDR format if using a single IP address.
It’s finally time to do some interesting analytics!
To do this, we’d need to create a second stream analytics job called DHTAnalytics with two outputs – first out goes to SQL database and the second goes to PowerBI.
Once created, add an Input by navigating to Input -> Add Input -> Data Stream -> Event Hub; use DHTStreamAnalytics as the alias.
Next, add an SQL Database OUTPUT; use DHTAnalyticsDBOutput as the alias and DHTAnalytics as the table name.
Next,create a Power BI Output; Add Output -> Select Power BI. Once authenticated, use DhtPowerBIOutput as the alias and whatever you like for the dataset name and table name:
In all, your setup should look like:
Finally, copy/paste this query into the QUERY text area.
Note that the INTO clause is used to direct output to the DB and PowerBI outputs respectively. Directing outputs with a query is basically what you’d need to do if you have multiple outputs for one stream analytics job. Admittedly, it took me a while to figure that out.
Also, note that I am using a Tumbling Window of 1 hour, so I’d expect IoT to send ≈6 readings in a window.
Start the Job!
Connect Arduino VIN pin to a 5.5v power supply and watch your hard work pay off!
Clearly, I have high humidity problem.
Arduino Output (before unplugging it from USB):
The end 🙂
I ordered 2 of this Relay Module. Once they arrive I will hook it up to an extension box so I can control our De/Humidifier depending on the sensor readings. That is, if average humidity readings over X period of time is greater than %Y or lower than %Z turn it ON. Another idea is to be able to turn our heater ON/OFF from my mobile using one of those relay modules.