In today's tutorial, we'll show you how to use Azure's IoT hub in Ubuntu core to develop our apps. Azure IoT Hub currently provides a framework to manage our IoT devices and to showcase our data through a pre-built solution. In today's article, we'll show you how to connect our devices to a remote monitoring preconfigured solution.
1) Provision remote monitoring pre-configuration solution
We can follow the official documents in Microsoft:
https://www.azure.cn/documentation/articles/iot-suite-connecting-devices-linux/
To create a pre-configured solution for us, and eventually form the following configuration:
The name of my solution here is called " sensors".
If there is data coming up in our IoT device, we can see that data in the telemetry history on the right. These data are usually expressed in the form of curves. In the process of creating the device, we need to record the data in the following screen so that we can use it in the code implementation:
We can also open azure.cn to see all the resources we have created:
The "Connection string-master key" shown here is very useful for our future programming. Need special attention.
2) Generate a snap application developed in C language
In this section, we will show you how to develop a client using the C language and eventually form a snap application. This application will communicate with the remote monitoring preconfigured solution that we formed in the previous section. We develop SNAP applications in desktop for Ubuntu 16.04. If you are not familiar with the installation of SNAP development, please refer to my article to install your environment. As described in the article, we will install the necessary component packages first:
$ sudo apt-get install cmake gcc g++
To add an Azureiot repository to your computer:
$ sudo add-apt-repository ppa:aziotsdklinux/ppa-azureiot
$ sudo apt-get update
To install the Azure-iot-sdk-c-dev package:
$ sudo apt-get install-y Azure-iot-sdk-c-dev
In this way, we have installed the component packages we need. For some reason, there are some errors in compiling our routines, so we have to do the following manual modifications:
/usr/include/azureiot/inc$ sudo mv Azure_c_shared_utility.
With this change, you can make sure that we do not see the header file in the following compilation.
Let's take a look at one of the projects I've done: https://github.com/liu-xiao-guo/remote-monitor
Snapcraft.yaml
Name:remote-monitor
version: ' 0.1 '
summary:this is a remote-monitor snap for Azure
Description: |
This was a Remote-monitor sample snap for Azure
grade:stable # must was ' stable ' to release into Candidate/stable Chann Els
confinement:strict # use ' strict ' once your right plugs and slots
apps:
remote-monitor:
comma Nd:bin/sample_app
plugs: [Network]
parts:
Remote:
plugin:cmake
Source:./src
This project is a CMake project. Since the name of our package is the same as the name of our app, when we run our app, we can directly enter Remote-monitorming to run our app. Before making any changes, we open the Remote_monitoring.c file and note the code:
static const char* DEVICEID = "Mydevice";
static const char* Devicekey = "[Device Key]";
static const char* Hubname = "sensorsf8f61";
static const char* Hubsuffix = "azure-devices.cn";
The explanation here is:
static const char* deviceId = "[Device Id]";
static const char* Devicekey = "[Device Key]";
static const char* Hubname = "[Iothub Name]";
static const char* Hubsuffix = "[Iothub Suffix, i.e. azure-devices.net]";
We need to replace these values with our own account. In practical use, we can modify the following code in REMOTE_MONITORING.C:
while (1)
{
unsigned char*buffer;
size_t buffersize;
Srand (Time (NULL));
int r = rand ()%;
int r1 = rand ()%;
int r2 = rand ()%;
printf ("R:%d, R1:%d, R2:%d\n", R, R1, r2);
Thermostat->temperature = R;
Thermostat->externaltemperature = R1;
thermostat->humidity = R2;
(void) printf ("Sending sensor value temperature =%d, humidity =%d\r\n", Thermostat->temperature, thermostat-> humidity);
if (SERIALIZE (&buffer, &buffersize, Thermostat->deviceid, Thermostat->temperature, thermostat-> Humidity, thermostat->externaltemperature)! = Codefirst_ok)
{
(void) printf ("Failed Sending sensor value \ r \ n ");
}
...
}
To pass on the data we need. Here, we randomly write some random data.
Note that the "Devicekey" here is the "Device Key" that we showed in the picture in the previous section. We enter the following commands directly in the termnial:
$ snapcraft
This forms the snap package for our project. We can install it by using the following command:
liuxg@liuxg:~/snappy/desktop/azure/remote-monitor$ sudo snap install Remote-monitor_0.1_amd64.snap--dangerous
[sudo] password for liuxg:
remote-monitor 0.1 installed
liuxg@liuxg:~$ Snap list
Name Version Rev Developer Notes
Azure 0.1 x1 -
Core 16.04.1 714 canonical -
Hello 1.0 x1 DEVMODE
Hello-world 6.3 canonical -
Hello-xiaoguo 1.0 X1 -
Remote-monitor 0.1 x2
Obviously our Remote-monitor has been successfully installed. We enter the following command in the terminal:
liuxg@liuxg:~$ Remote-monitor
Iothubclient accepted the message for delivery
r:30, r1:37, r2:4
sending Sens or value temperature = Humidity = 4
iothubclient accepted the message for delivery
r:45, r1:23, r2:35
S Ending sensor value temperature = $, Humidity =
Iothubclient accepted the message for delivery
r:16, r1:39, R2:25
sending sensor value temperature = Humidity =
Iothubclient accepted the message for delivery
R: R1:33, r2:14
sending sensor value temperature = +, Humidity = Iothubclient accepted the
message for D Elivery
r:20, r1:29, r2:32
Obviously our client app is constantly sending data to the Azure IoT hub. We can view the data we have received through https://www.azureiotsuite.cn/.
In the following we can see the maximum value of the device data and the change of the minimum value. If you want to run this app on an arm device like Raspberry Pi, see my article "How to install Ubuntu core for Raspberry Pi and compile it in the snap system". The exact installation is the same as described here. Ask the developer to try it out for themselves.
3) Generate SNAP applications developed with Nodejs
In this section, we'll show you how to use Nodejs to develop our snap application. We can refer to the article "Getting Started with Azure IoT hub for node. js." As described in this article, we are most interested in the third console application Simulateddevice.js introduced in it.
Simulateddevice.js
#!/usr/bin/env node var clientfromconnectionstring = require (' AZURE-IOT-DEVICE-AMQP '). clientfromconnectionstring; var Message = require (' Azure-iot-device ').
Message; var connectionString = ' Hostname=sensorsf8f61.azure-devices.cn;deviceid=mydevice;
Sharedaccesskey={device Key} ';
var client = clientfromconnectionstring (connectionString); Function Printresultfor (OP) {return function Printresult (err, res) {if (err) Console.log (op + ' ERROR: ' + Err.tos
Tring ());
if (res) Console.log (op + ' status: ' + Res.constructor.name);
};
} var connectcallback = function (err) {if (err) {Console.log (' Could not connect: ' + err.amqperror);
} else {Console.log (' Client connected '); Create a message and send it to the IoT hub every second setinterval (function () {var temp = ten + (Math.rand
Om () * 4);
var windspeed = ten + (Math.random () * 4);
var data = json.stringify ({deviceId: ' Mydevice ', Temp:temp, windspeed:windspeed}); var mesSage = new Message (data);
Console.log ("Sending message:" + message.getdata ());
Client.sendevent (Message, printresultfor (' send '));
}, 5000);
}
};
Client.open (Connectcallback);
Note In the above code, we need to manually modify the following connectionstring:
var connectionString = ' Hostname=sensorsf8f61.azure-devices.cn;deviceid=mydevice; Sharedaccesskey={yourdevicekey} ';
As described in the article, it is defined as:
var connectionString = ' Hostname={youriothostname};D eviceid=myfirstnodedevice; Sharedaccesskey={yourdevicekey} ';
We need to modify the above string according to the parameters we set in the first section. You can refer to my project:
Https://github.com/liu-xiao-guo/azurenode-snap
Snapcraft.yaml
Name:azure
version: ' 0.1 ' # Just for humans, typically ' 1.2+git ' or ' 1.3.2 ' summary:this are an
Azure snap App
description: |
This was an Azure client snap to send a message
grade:stable # must was ' stable ' to release into Candidate/stable Chann Els
confinement:strict # use ' strict ' once your right plugs and slots
apps:
Azure:
command:bin/s End
plugs: [Network]
parts:
node:
plugin:nodejs
Source:.
Similarly we can enter the Snapcraft command to produce the corresponding package and install it:
liuxg@liuxg:~/snappy/desktop/azurenode-snap$ Snap list
Name Version Rev Developer Notes
Azure 0.1 x2 -
core 16.04.1 714 canonical -
Hello 1.0 x1 devmode
hello-world 6.3 canonical -
Hello-xiaoguo 1.0 X1 -
remote-monitor 0.1 x2
We can run the Azure command directly:
liuxg@liuxg:~/snappy/desktop/azurenode-snap$ Azure Client Connected Sending message: {"deviceId": "Mydevice", "temp" : 11.826184131205082, "windspeed": 11.893792165443301} send status:messageenqueued Sending message: {"DeviceId": " Mydevice "," temp ": 10.594819721765816," windspeed ": 10.54138664342463} send status:messageenqueued Sending message: {" DeviceId ":" Mydevice "," temp ": 11.27814894542098," windspeed ": 10.962828870862722} send status:messageenqueued sending Message: {"deviceId": "Mydevice", "temp": 13.068702490068972, "windspeed": 10.28670579008758} Send Status:
Messageenqueued Sending message: {"deviceId": "Mydevice", "temp": 11.723079251125455, "windspeed": 12.173830625601113} Send status:messageenqueued Sending message: {"deviceId": "Mydevice", "temp": 12.595101269893348, "windspeed" : 12.120747512206435} send status:messageenqueued Sending message: {"deviceId": "Mydevice", "temp": 11.431507185101509, "Windspeed": 11.76255036983639} send status:messageenqueued Sending message: {"deviceId": "Mydevice", "temp":12.488932724110782, "windspeed": 13.200456796213984} Send status:messageenqueued
We can view the data we have received via https://www.azureiotsuite.cn/:
We can see the curve of temp and wind speed above. Similarly, if you want to run this app on an arm device like Raspberry Pi, see my article "How to install Ubuntu core for Raspberry Pi and compile it in the snap system". Ask the developer to try it out for themselves.