This repository was archived by the owner on Mar 24, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -60,12 +60,42 @@ void initTime()
60
60
static IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = NULL ;
61
61
static unsigned int iotHubMessageTrackingId = 0 ;
62
62
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
+
63
91
void initIoTHubClient ()
64
92
{
65
93
iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString (IOTHUB_CONNECTION_STRING, MQTT_Protocol);
66
94
if (iotHubClientHandle != NULL )
67
95
{
68
96
Serial.println (" IoTHubClient_LL_CreateFromConnectionString OK" );
97
+
98
+ IoTHubClient_LL_SetMessageCallback (iotHubClientHandle, receiveMessageCallback, NULL );
69
99
}
70
100
else
71
101
{
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change 4
4
5
5
using System ;
6
6
using System . Text ;
7
+ using Microsoft . Azure . Devices ;
7
8
using Microsoft . Azure . EventHubs ;
8
9
using Newtonsoft . Json ;
9
10
using Newtonsoft . Json . Linq ;
@@ -26,6 +27,14 @@ using SendGrid.Helpers.Mail;
26
27
humidity = humidity ,
27
28
} ;
28
29
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
+
29
38
SendGridMessage message = new SendGridMessage ( ) ;
30
39
message . AddTo ( new EmailAddress ( "{Your Email To Send}" ) ) ;
31
40
message . AddContent ( "text/plain" , $ "Temperature: { temperature } , Humidity: { humidity } ") ;
You can’t perform that action at this time.
0 commit comments