Skip to content
This repository was archived by the owner on Mar 24, 2020. It is now read-only.

Commit 05ac09c

Browse files
authored
Send C2D (Cloud-to-Device) message to device (#11)
* Add C2D (Cloud-to-Device) message callback * Add Microsoft.Azure.Devices extension package * Send a C2D message to the configured device
1 parent 3b2b599 commit 05ac09c

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

arduino/arduino.ino

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,42 @@ void initTime()
6060
static IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = NULL;
6161
static unsigned int iotHubMessageTrackingId = 0;
6262

63+
IOTHUBMESSAGE_DISPOSITION_RESULT receiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void *userContextCallback)
64+
{
65+
const unsigned char *buffer;
66+
size_t bufferSize;
67+
68+
if (IoTHubMessage_GetByteArray(message, &buffer, &bufferSize) == IOTHUB_MESSAGE_OK)
69+
{
70+
char *command = (char *)malloc(bufferSize + 1);
71+
if (command == NULL)
72+
{
73+
return IOTHUBMESSAGE_ABANDONED;
74+
}
75+
76+
strncpy(command, (const char *)buffer, bufferSize);
77+
command[bufferSize] = '\0';
78+
Serial.print("receiveMessageCallback: ");
79+
Serial.println(command);
80+
81+
free(command);
82+
83+
return IOTHUBMESSAGE_ACCEPTED;
84+
}
85+
else
86+
{
87+
return IOTHUBMESSAGE_REJECTED;
88+
}
89+
}
90+
6391
void initIoTHubClient()
6492
{
6593
iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(IOTHUB_CONNECTION_STRING, MQTT_Protocol);
6694
if (iotHubClientHandle != NULL)
6795
{
6896
Serial.println("IoTHubClient_LL_CreateFromConnectionString OK");
97+
98+
IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, receiveMessageCallback, NULL);
6999
}
70100
else
71101
{

function/function.proj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>netstandard2.0</TargetFramework>
4+
</PropertyGroup>
5+
6+
<ItemGroup>
7+
<PackageReference Include="Microsoft.Azure.Devices" Version="1.17.1" />
8+
</ItemGroup>
9+
</Project>

function/run.csx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Text;
7+
using Microsoft.Azure.Devices;
78
using Microsoft.Azure.EventHubs;
89
using Newtonsoft.Json;
910
using Newtonsoft.Json.Linq;
@@ -26,6 +27,14 @@ using SendGrid.Helpers.Mail;
2627
humidity = humidity,
2728
};
2829

30+
string connectionString = System.Environment.GetEnvironmentVariable("iotHubConnectionString");
31+
string deviceId = System.Environment.GetEnvironmentVariable("iotHubDeviceId");
32+
using (ServiceClient serviceClient = ServiceClient.CreateFromConnectionString(connectionString))
33+
{
34+
Message commandMessage = new Message(Encoding.UTF8.GetBytes("hello world!"));
35+
serviceClient.SendAsync(deviceId, commandMessage).Wait();
36+
}
37+
2938
SendGridMessage message = new SendGridMessage();
3039
message.AddTo(new EmailAddress("{Your Email To Send}"));
3140
message.AddContent("text/plain", $"Temperature: {temperature}, Humidity: {humidity}");

0 commit comments

Comments
 (0)