EnviroDIY / ModularSensors

An Arduino library to give environmental sensors a common interface of functions for use with Arduino-framework dataloggers, such as the EnviroDIY Mayfly.
https://envirodiy.github.io/ModularSensors/
Other
81 stars 48 forks source link

Create `dataPublisher` for Azure Event Hub REST API #411

Open aufdenkampe opened 2 years ago

aufdenkampe commented 2 years ago

Add support for streaming data to a Microsoft Azure EventHub service REST API endpoint with authentication via a Shared Access Signature (SAS) token.

Documentation for Event Hubs service REST:

A example POST request looks like this:

POST https://your-namespace.servicebus.windows.net/your-event-hub/messages?timeout=60&api-version=2014-01 HTTP/1.1  
Authorization: SharedAccessSignature sr=your-namespace.servicebus.windows.net&sig=tYu8qdH563Pc96Lky0SFs5PhbGnljF7mLYQwCZmk9M0%3d&se=1403736877&skn=RootManageSharedAccessKey  
Content-Type: application/atom+xml;type=entry;charset=utf-8  
Host: your-namespace.servicebus.windows.net  

{ "DeviceId":"dev-01", "Temperature":"37.0" }

The key innovation here is to write a SAS signature, for which we first need to complete:

Generate a Shared Access Signature (SAS) for Azure Event Hub

SAS Format:

SharedAccessSignature sig=<signature-string>&se=<expiry>&skn=<keyName>&sr=<URL-encoded-resourceURI>

Where:

aufdenkampe commented 2 years ago

@SRGDamia1, I'm having a bunch of trouble getting this to work with the new EnviroDIY LTEBee (SIM7080), and I need your help.

The short story is that although I'm able to connect to the server and send a properly formatted POST Request (that works in Postman), on the Mayfly + SIM7080 modem, the data aren't going through and I'm not getting any response from the server.

Note that this uses HTTPS via port 443, which this modem seems to support. By specifying that port, I was able to get a connection to the endpoint server (but no connection with Port 80), so I think I'm OK on that.

To troubleshoot and solve this, I think I need to dive in to deeper use of the TinyGSM library functions, but I'm really new to that library and it's functions.

Can you offer suggestions?

aufdenkampe commented 2 years ago

@GArrigotti-cws, @GArrigotti, with the last commit 4b624c7, I also tested on a Digi radio and got the same result: no response from the server, even when the entire request is identical to the raw request that works in Postman.

In that last commit I included platformio.ini files that would allow you to test my two sketches (one for each radio) yourself.

aufdenkampe commented 2 years ago

In commit 8bc473c7b423dcb56a9c7582300efb511b435479 from work done on 4/20/22, I figured out the main issue here.

The key was to use TinyGsmClientSecure rather than TinyGsmClient in src/modems/SIMComSIM7080.h.

This fully enabled HTTPS. It turns out that specifying Port 443 in src/publishers/EventHubPublisher.cpp (rather than Port 80 which is used for HTTP) was not sufficient.

@SRGDamia1, let's connect to discuss ideas on how to best implement HTTPS across all modems and publishers in ModularSensors.

For example, it would be ideal to start migrating to HTTPS for new EnviroDIY stations posting to MonitorMyWatershed.org

aufdenkampe commented 1 year ago

@SRGDamia1, I would like to pick up this conversation about how to best architect the modems and publishers code to for a user to easily switch between HTTP and HTTPS.

As I mentioned above in https://github.com/EnviroDIY/ModularSensors/issues/411#issuecomment-1116524869, depending on the Publisher's endpoint, we need to instantiate the gsmClient within the Modem either using TinyGsmClient (as currently done for HTTP) or using TinyGsmClientSecure (for HTTPS).

I got this working, but only for src/publishers/EventHubPublisher.cpp.

That work is presently all in the hmac_auth branch.

I'll look at it more closely in the coming week or two and set up a time to discuss options with you.