My Netduino and Arduino devices can’t do https so I had been looking at different approaches for uploading sensor data to a Microsoft Azure Event Hub. In a previous post I published the “simplest” possible useful program (a console application) which could upload data and this code builds on that.
In this proof of concept I have integrated the core of the console application code into an ASP.NET MVC WebAPI 2 project which acts as a service gateway. My Netduino clients now use a website hosted on my Essentials 2012 home server to forward the requests to a Microsoft Azure Event Hub .
For more detail about how to program the energy monitor shield see these posts about the Nokia 5110 display, nrf24L01 wireless link, and non invasive current sensor algorithm optimisations.
try { using (HttpWebRequest request = (HttpWebRequest)WebRequest.Create( AzureGatewayUrl )) { string payload = @"{""DeviceId"":" + deviceId + @",""Usage"":" + value + "}"; byte[] buffer = Encoding.UTF8.GetBytes(payload); request.Method = "POST"; request.ContentLength = buffer.Length; request.ContentType = "text/csv"; request.KeepAlive = false; request.Timeout = 5000; request.ReadWriteTimeout = 5000; using (Stream stream = request.GetRequestStream()) { stream.Write(buffer, 0, buffer.Length); } using (var response = (HttpWebResponse)request.GetResponse()) { Debug.Print("HTTP Status" + response.StatusCode + " : " + response.StatusDescription); } } } catch (Exception ex) { Debug.Print(ex.Message); }
Azure Service Bus Explorer by Paolo Salvatori is great for debugging and testing Service Bus applications like this.
Bill of materials (prices as at Feb 2015)
- Netduino Plus 2 USD60 NZD108
- Energy Monitor Shield USD23 NZD55.37
- Non Invasive Current Clamp 30A USD7.98 100A NZD20.70
- Passive PoE cable set USD4.50
The Azure Event Hub Service GatewayV0.1 is pretty basic with, no security, doesn’t have a lot of logging. wouldn’t scale terribly well (Though most home systems wouldn’t have a lot of sensors) and is hosted in Internet Information Server(IIS),
In future posts I’ll fix these limitations and make the service gateway secure, easier to install, configure and operate. But, this proof of concept proves the approach is viable
// POST api/eventhub public void Post(HttpRequestMessage request) { try { string connectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"]; string eventHubName = ConfigurationManager.AppSettings["Microsoft.ServiceBus.EventHub"]; NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString); EventHubClient client = EventHubClient.Create(eventHubName); EventData data = new EventData(request.Content.ReadAsByteArrayAsync().Result); // Set user properties if needed data.Properties.Add("UploadedAtUTC", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss")); data.Properties.Add("UploadedBy", "devMobileAzureEventHubGateway"); client.Send(data); } catch (Exception ex) { Debug.WriteLine("Send failed " + ex.Message); } }
Pingback: Azure Event Hub Service Gateway V0.1 | Arduino,...
Pingback: Azure Event Hub Service Gateway V0.5 | devMobile's blog