Skip to content

Instantly share code, notes, and snippets.

@programmersommer
Last active November 25, 2017 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save programmersommer/6a7bd28284bebaa50ed929f04b20979a to your computer and use it in GitHub Desktop.
Save programmersommer/6a7bd28284bebaa50ed929f04b20979a to your computer and use it in GitHub Desktop.
Arduino Azure IoT hub communications
// Define the Model
BEGIN_NAMESPACE(IoTSample);
DECLARE_MODEL
(LightSensorDataModel,
WITH_DATA(ascii_char_ptr, deviceId),
WITH_DATA(int, iotdata),
WITH_ACTION(DuckAction, int, light)
);
END_NAMESPACE(IoTSample);
// for recieving message from IoT hub add action
// Could be called when you send JSON like {"Name" : "DuckAction", "Parameters" : {"light":1}}
EXECUTE_COMMAND_RESULT DuckAction(LightSensorDataModel* device, int light)
{
(void)printf("Duck action %d \r\n",light);
digitalWrite(2, light);
return EXECUTE_COMMAND_SUCCESS;
}
// for sending message to IoT hub
// add into void simplesample_http_run(void)
while (1)
{
myIoTdata->deviceId = "ArduinoAzureTwin";
myIoTdata->iotdata = analogRead(1); // getting some info from pin 1
unsigned char* destination;
size_t destinationSize;
if (SERIALIZE(&destination, &destinationSize, myIoTdata->deviceId, myIoTdata->iotdata) != CODEFIRST_OK)
{
(void)printf("Failed to serializern");
}
else
{
sendMessage(iotHubClientHandle, destination, destinationSize);
}
IoTHubClient_LL_DoWork(iotHubClientHandle);
ThreadAPI_Sleep(1000);
}
DESTROY_MODEL_INSTANCE(myIoTdata);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment