Azure webhook triggers a IoT Hub - azure-iot-hub

I want to make a webhook that can be triggered by either get/post and triggers connected IoT devices to a WebSocket.
So, I thought Azure might help to automize this process, instead of writing everything from scratch and run it on a webserver.
I am very new in the Azure world, I found it very complicated to make it working on Azure.
Can you point me to any simple to make it work?

The first thing to do is to decide where you want to connect your devices to. Generally, you'll either use Azure IoT Hub or IoT Central (which uses an IoT Hub anyway). Your question doesn't include any details about your devices, or whether you're developing them yourself, but I'll assume you can connect to either.
If you go for Azure IoT Central, the easiest way to get a webhook going is to create a Logic App and link nodes together to trigger a Command to your device. You can use an HTTP request as the trigger, and use the built-in IoT Central node to trigger your command, example:
If you end up connecting your devices to IoT Hub, you can use Direct Methods, Cloud to Device messages or even Device Twins to communicate with your devices. You can leverage these by creating an Azure Function with an HTTP trigger, and you program the logic to call IoT Hub, which will communicate with your devices. There are a lot of samples on the web that show how to create Functions, or control your device remotely.
There are a lot of ways to go about your scenario, if you need some help from the Microsoft community, you can get a lot of help on Microsoft Q&A.
Update based on the comments:
In this case, the devices are connected to IoT Hub. You can use the Service SDK to run a job that fires a Direct Method to all currently connected devices. This doc describes the process.

Related

Send and receive iot-hub messages

I am working on a project based on the azure sphere kit where I want to control a motor from a web application. I managed to send the command to the iot hub and the build in monitor shows that the message is received by the iot hub. Now, my question is: How can I forward this message to the board? I have to mention that this is my first experience with the iot-hub so please dont judge me if it is a stupid question.:) Here is the confirmation that the message is received by the iothub
If you want to communicate with your Azure Sphere from Azure IoT Hub, you're talking about Cloud-to-device communication. There are three different ways to do this communication:
Direct Methods
Device Twin Desired Properties
Cloud-to-device (C2D) messages
Microsoft documented them here. This document also lists the different considerations for each of the three methods.
To forward the IoT Hub message that you mentioned in your question, a good start would be to write an Azure Function that listens to the IoT Hub output and send that to the device using one of the three methods above. You can find an example for the Azure Function IoT Hub trigger here
Adding few more detailed options in this scenario to the above responses.
Scenario 1: You may be interested in configuring your devices from your back-end service(s) as your web application. To synchronize state information between a device and an IoT hub, you use device twins. A desired property is set by a back-end application and read by a device. A reported property is set by a device and read by a back-end application. A tag is set by a back-end application and is never sent to a device. You use tags to organize your devices. Long-running commands intended to put the device into a certain desired state. For example, set the telemetry send interval to 30 minutes. This completes your E2E state synchronization scenario : Web-Application<=>IoT Device, Ref Link
Question: How can I forward this message to the board?
Scenario 2: You may be interested in direct method to control a device connected to your IoT hub. You can use direct methods to remotely change the behavior of devices connected to your IoT hub. Commands that require immediate confirmation, such as turning on a fan.
Requirements:
-->IoT Device: To receive the direct method calls, the applications running on IoT Device connect to a device-specific endpoint on your IoT hub.
HostName={YourIoTHubName}.azure-devices.net;DeviceId=MyNodeDevice;SharedAccessKey={YourSharedAccessKey}
-->Web-Application: To call a direct method on a device, your Web application connects to service-side endpoint on your IoT hub.
HostName={YourIoTHubName}.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey={YourSharedAccessKey}
Direct Method calls: Points to be noted. Please visit the link: detailed comparison of the various cloud-to-device communication options.
The maximum direct method payload size is 128 KB.
Disconnected devices are not contacted. The solution back end is notified that the device is not connected.
Ref Link
If you need further help in this matter, please comment in the below section and we will be happy to help you on this forum.

Is it possible to subscribe to Azure IOT Edge Device

Hopefully a simple question, but with all the reading I have done about IOT Edge Devices, I still cannot find an answer.
For my application, I would like to have a local offline view of all the data going into the edge device and an online view from a website. Is it possible to subscribe to all the events in the Edge Device, just like how I am able to subscribe to all the events in the IOT Hub?
Hopefully something like this
Device->IOTEdge->IOTHub->Website
_______________->Local View (offline)
From what I have seen I can use the components to create a website and do this.
If you need a local Azure IoT Edge webpage dashboard then look at Sander van de Velde blog for details and ideas. Sander has also a lot of other very good examples.
https://sandervandevelde.wordpress.com/2018/12/07/create-your-own-local-azure-iot-edge-dashboard/
https://sandervandevelde.wordpress.com/2018/04/20/visualizing-azure-iot-edge-using-local-dashboard/
You just need one nodejs module listening messages from edgeHub and sending messages using websocket connection. Then you can use same or another module to serve static webpage aka your dashboard. You can access this local dashboard from the same local network without any connection to internet. And you can also send your messages to upstream (IoTHub in cloud) and do the same there. I have used nodejs websocket locally in the edge and SignalR to stream data to cloud dashboards.

How to connect non-programmable (no SDK) devices to IoT Hub

I am building an IoT solution based on devices that are not programmable (3rd party devices).
I can just configure the server they must connect to and I have the messaging (sending messages and receiving commands).
What is the best way to integrate them in an Azure IoT Solution (IoT Hub) ?
It looks like we can use Azure IoT Edge as Gateway (https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/iot-edge/iot-edge-as-gateway.md) in a "Identity translation" pattern, but I still not figure out how to do it.
I also find Azure IoT Protocol Gateway (https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-protocol-gateway) that seems to do the job but it is not sufficiently clear for me what to do.
Thanks in advance for your help.
The question is which protocols your devices support and how configurable your devices are. For instance you might be able to talk MQTT directly to the IoT Hub without using the SDK: https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support#using-the-mqtt-protocol-directly-as-a-device
Same is possible for HTTP using the REST API directly: https://learn.microsoft.com/en-us/rest/api/iothub/device
If neither is an option then yes, you need some kind of gateway - like in the examples which you linked.

Is LoRaWan only accessible with an internet connection?

I'm planning to build an IoT project for an oil palm plantation through the use of an Arduino and an Android Mobile application for my final year project in University. As plantations have low to no communication signals which includes wifi, it is possible to implement LoRaWAN without access to the internet/use/ of a web-based application?
The LoRaWAN node does not need any other communications channel aside from LoRaWAN, of course. Would not make any sense otherwise. ;-)
The gateway however does need a connection to the server application that is to be used as a central instance for your use case. Usually this is an existing LoRaWAN cloud service such as The Things Network (TTN) with your application connected behind, but in theory you could connect the gateway to your very own central, making your whole network independent. This is possible because LoRa uses frequency bands free for use (ISM bands) so anyone can become a „network operator“. The TTN software is available as Open Source, for example.
Connection from the gateway to the central is usually done via existing Ethernet/WiFi infrastructures or mobile internet (3G/4G), whatever suits best.
Besides, the LoRa modules available for Arduinos can be used for a low-level, point-to-point LoRa (not LoRaWAN) connection between two such modules. No gateway here. Maybe that is an option, too, for your use case.
The LoraWAN is using the Gateway connected to some kind of cloud, for example the TTN network which is community based. If you live in a bigger city you have good chances to have a TTN Gateway in your area.
You can however connect two Lora nodes together to get a point to point connection. You can send data from Node1, which is connected to some kind of sensor and batterypowered, to Node2, which is stationary and stores all the data to a flashdrive for example. From this flashdrive you can import the data to a website or you could use an application like Node-Red to display the data on a Dashboard.
Here you will find instructions on how to send Data from one Lora-Node to another.
Here you will find instuctions on how to use Node-Red to display your Lora-Data. You will have to change the input from the TTN-Cloud to a textfile on your Raspberry, or whatever gateway you use. (Optional)

Initial Device ID registration to Azure IoT Hub by the device itself

In order to send messages to the Azure IoT Hub, you first need to register the devices to the IoT Hub registry, though how can you make the devices register its' ID by themselves in a safe way?
My intention of this question is about the security problem.
I am thinking of implementing them on apps such as Androids, iOS, etc. which will be used by many users.
If you give those devices a read/write permission to the registry (which the shared access key is stored in the app), there will be a risk that a malicious user finds out the shared access key and use it to abuse the system.
Are there any other solutions besides preparing a public server that provides a function to register a device and give them back the per-device access key? Or, shouldn't I use the IoT Hub and find another service?
If you want users to be able to register their own devices with your IoT hub, you should create a separate service that can be called from the device and that returns a key. The service should register the device with IoT Hub and provide some logic that limits the number of devices that any user can register.
This is the approach taken in the MyDriving sample solution here that allows users to register their phones as devices connected to an IoT hub.
You can create multiple keys, so using the way Azure Storage works - have a primary and a secondary.