Azure IoT Hub : retrieving messages via browser-side javascript API - azure

Do you know if it is possible to retrieve device-to-cloud messages, with Microsoft Azure and IoT Hub, with a javascript client running in the browser (i.e. no node js) ?
It is my understanding that this is not currently possible, but I'd like to make sure (i.e. all current APIs can only work server side).
Edit1: REST APIs might be the obvious choice to attempt to do this; However, having tried these APIs via manual request, it seems the response does not provide the allow-access-control-origin header, so I can't call them from ajax due to CORS.

UPDATE: The example code was removed by requirement from Azure IoT Hub team, and they do not like users to connect to IoT Hub from browsers (https://github.com/Azure/azure-iot-sdk-node/issues/112#issuecomment-338073636).
Sorry for the broken links in this answer.
It seems this is an old question, however, I find there is still nobody can give the right answer.
In short, is there a way to read IoT Hub D2C messages in browser? The answer is absolute YES!
You shouldn't use RESTful API to loop checking messages, but use AMQP over WebSockets.
Before you start, you must know that IoT Hub has two sides, Device side and Service side. For Device side, you connect to IoT Hub using HTTP, AMQP and MQTT, however, for Service side you can ONLY connect to IoT Hub using AMQP.
Fortunately, Red Hat had already written a AMQP over WebSockets Node module, and you can do some simple work to make it working in browser.
You can learn more about the AMQP Node module from https://github.com/grs/rhea.
Also, we've made a web based tool for receiving D2C messages https://iotdevtool.com/service/, you can learn more about it from https://blogs.msdn.microsoft.com/zhiqing/2017/03/16/connect-to-azure-event-hub-in-browser-using-amqp-over-websockets/.

I haven't tried it myself but my understanding is that it's possible to retrieve cloud-to-device messages in JavaScript using the IoT Hub REST API.

Related

Mobile app to iot communication in azure iot hub

I want to implement the following system.
There are users and each user will have an IoT device. The user should be able to do the following:
Login with their email and password.
Control the IoT device in real time (the user will perform some operation on the IoT device and the result will be displayed instantly on the app).
See the state of IoT device in real time. If something changes on the IoT device it should reflect on the app in real time.
I was wondering if this is doable using azure IoT Hub. I came across an architecture where the device is connected to IoT hub and the app is connected to signalR. The messages from IoT device will go to IoT Hub which will update the app using signalR.
But is there any way we can not have additional component like signalR? Can IoT device and mobile app be connected directly to azure IoT hub and exchange data between them without an entity in between other than IoT hub?
Connecting mobile app to IoT hub seems like an option but I did not find any way to implement email and password based authentication to allow users to connect to IoT hub.
Any help in this regard is appreciated.
You could absolutely go without SignalR. You can use the IoT Hub Service SDK to send C2D messages to your device from your phone, and also listen to the device telemetry with the same SDK.
However, it would probably be a better idea to have some restrictions on what you can do with that SDK. If the user first has to log in, I take it that they don't get access to every device in your application? Using some kind of role-based access might be preferable, you would build an API for that, which then uses the Service SDK to give you the data.
Also, even though you could listen to the device data directly from your phone, that means that if you have bad reception/wifi, data might not be received. You might want to consider storing it somewhere? I don't know the details about your application, but if you would then want to listen to events from that storage and send it to your app... SignalR might not be the worst idea.
"See the state of IoT device in real time. If something changes on the IoT device it should reflect on the app in real time."
For this EventGrid integration (https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-event-grid ) would help. For realtime telemetry as Matthijs said SignalR would be a better choice.If you are fine with some delay (say 10 sec) then Azure Time Series Insights can also be used.

How to send events to a device in Azure IoT Hub using shared key for authentication

Is there a way to send data to a device in Azure IoT Hub using a shared key without first generating a SAS token? The reason I am trying to do this is because I want to send events via a web hook without writing code for it. The device is connected to our system and we're trying to use our web hooks functionality which can relay an event to a HTTP endpoint with preconfigured headers and format but it can't execute the code to get a temporary SAS token.
Alternatively can I make a SAS token that never expires?
Ended up using Azure IoT Central + the Azure IoT Central Bridge. The IoT Bridge is an Azure function where you put some JS code to convert your message to a format that IoT Central understands and then it deals with the tokens and eventual device registration. This is not an exact answer to the question since IoT Central is not exactly IoT Hub (it is a portal over it) but it turns out it works well enough for our case. Also I think the codebase of the IoT Bridge can be used with the IoT Hub if one wants to dig enough through it. Maybe this solution can serve someone else.

Receiving D2C messages from an IoT Hub in browser

I am working on a simple dashboard for showing IoT data in a browser.
So far, I was using the Paho MQTT client library and a public MQTT broker to receive data. However, I'd like to use the Azure IoT Hub from now on.
The data I'm sending is just simple temperature and humidity values.
I already found out that I can't connect to the IoT Hub via MQTT. So my question is:
Is there a possibility to connect (eg. using javascript) directly to IoT Hub and read the data?
If not, what's my best option?
I thought about saving the messages to an SQL Database and reading from that, but that seems quite complicated for what should be a simple IoT use case (showing real-time data)
Thank you in advance!
You can connect to IoT Hub via MQTT. Please take a look at this documentation for detail. We also have a Node.js SDK for connecting to IoT Hub if you don't want to deal with MQTT directly. I know you are working on your own dashboard, but have you looked at Azure IoT Solutions Accelerator? It's completely open source and you can modify it as you want.

Connect to Azure MQTT using SIM808

I want to send SIM808 GPS data to the Azure IoT hub using MQTT.
As mentioned in docs of azure, I have used the same parameters.
https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support#using-the-mqtt-protocol-directly
But it doesn't work. Did not published.
Can we connect to Azure MQTT using Username and password without attaching certificate? if not
How to create the certificate?
How to send SIM808 data to the Azure IoT hub using MQTT?
Suggest me a way to do it with an example.
Thanks!!!
Yes, you can do it. You can use tokens.
As a simplest alternative you may also use flespi telematics hub to reach same goal. Your device can send via HTTP or MQTT messages to flespi channel. And flespi will stream processed information to MS Azure and/or AWS in their specific protocols.

Creating a workflow for an IoT project

I was using ThingSpeak for an IoT project. I've decided to move on with Azure IoT. My project consists Raspberry Pi 3's sending data to an IoT platform using MQTT protocol which will be displayed over mobile app.
While using ThingSpeak, things were easier. I was sending data to IoT platform, ThingSpeak was storing them without me configuring almost anything and mobile app that I wrote was sending HTTP request to IoT platform with an interval. Then, I was parsing JSON response on mobile app to display important values in real time.
So far I've managed to send datas to IoT hub using Azure IoT C SDK. However I am very confused about how I am going to implement these on Azure IoT, what my workflow should be like.
Azure IoT Hub ingests data from your devices into the cloud and then delivers that data to other destinations for storage or processing. By default, IoT Hub delivers data from devices to an Event Hubs compatible end point (these Quickstarts illustrate this process: https://learn.microsoft.com/azure/iot-hub/quickstart-send-telemetry-node). IoT Hub can also use routing rules to send data to other locations, such as storage or a Service Bus queue. The following tutorial illustrates these options for you: https://learn.microsoft.com/azure/iot-hub/tutorial-routing.

Resources