int main(void) { IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol = MQTT_Protocol; IOTHUB_MESSAGE_HANDLE message_handle; float telemetry_temperature; float telemetry_humidity; const char* telemetry_scale = "Celcius"; char telemetry_msg_buffer[80]; int messagecount = 0; printf("\r\nThis sample will send messages continuously and accept C2D messages.\r\nPress Ctrl+C to terminate the sample.\r\n\r\n"); IOTHUB_DEVICE_CLIENT_HANDLE device_handle; // Used to initialize IoTHub SDK subsystem (void)IoTHub_Init(); (void)printf("Creating IoTHub handle\r\n"); // Create the iothub handle here device_handle = IoTHubDeviceClient_CreateFromConnectionString(connectionString, protocol); if (device_handle == NULL) { (void)printf("Failure creating Iothub device. Hint: Check you connection string.\r\n"); } else { #ifdef SET_TRUSTED_CERT_IN_SAMPLES // Setting the Trusted Certificate. This is only necessary on system with without // built in certificate stores. (void)IoTHubDeviceClient_SetOption(device_handle, OPTION_TRUSTED_CERT, certificates); #endif // SET_TRUSTED_CERT_IN_SAMPLES int i = MESSAGE_COUNT; while(i--) { // Construct the iothub message telemetry_temperature = 20.0f + ((float)rand() / RAND_MAX) * 15.0f; telemetry_humidity = 60.0f + ((float)rand() / RAND_MAX) * 20.0f; sprintf(telemetry_msg_buffer, "{\"temperature\":%.3f,\"humidity\":%.3f,\"scale\":\"%s\"}", telemetry_temperature, telemetry_humidity, telemetry_scale); message_handle = IoTHubMessage_CreateFromString(telemetry_msg_buffer); (void)printf("\r\nSending message %d to IoTHub\r\nMessage: %s\r\n", (int)(messagecount + 1), telemetry_msg_buffer); IoTHubDeviceClient_SendEventAsync(device_handle, message_handle, send_confirm_callback, NULL); // The message is copied to the sdk so the we can destroy it IoTHubMessage_Destroy(message_handle); messagecount = messagecount + 1; ThreadAPI_Sleep(g_interval); } // Clean up the iothub sdk handle IoTHubDeviceClient_Destroy(device_handle); } // Free all the sdk subsystem IoTHub_Deinit(); return 0; }
int main(void) { TRANSPORT_HANDLE transport_handle; IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol; IOTHUB_DEVICE_CLIENT_HANDLE device_handle1; IOTHUB_DEVICE_CLIENT_HANDLE device_handle2; #ifdef SAMPLE_AMQP protocol = AMQP_Protocol; #endif // SAMPLE_AMQP #ifdef SAMPLE_AMQP_OVER_WEBSOCKETS protocol = AMQP_Protocol_over_WebSocketsTls; #endif // SAMPLE_AMQP_OVER_WEBSOCKETS (void)printf("Starting the IoTHub client sample C2D methods on AMQP over WebSockets with multiplexing ...\r\n"); // Used to initialize IoTHub SDK subsystem (void)IoTHub_Init(); if ((transport_handle = IoTHubTransport_Create(protocol, hubName, hubSuffix)) == NULL) { (void)printf("Failed to creating the protocol handle.\r\n"); } else { IOTHUB_CLIENT_CONFIG client_config1; client_config1.deviceId = deviceId1; client_config1.deviceKey = deviceKey1; client_config1.deviceSasToken = NULL; client_config1.iotHubName = hubName; client_config1.iotHubSuffix = hubSuffix; client_config1.protocol = protocol; client_config1.protocolGatewayHostName = NULL; IOTHUB_CLIENT_CONFIG client_config2; client_config2.deviceId = deviceId2; client_config2.deviceKey = deviceKey2; client_config2.deviceSasToken = NULL; client_config2.iotHubName = hubName; client_config2.iotHubSuffix = hubSuffix; client_config2.protocol = protocol; client_config2.protocolGatewayHostName = NULL; if ((device_handle1 = IoTHubDeviceClient_CreateWithTransport(transport_handle, &client_config1)) == NULL) { (void)printf("ERROR: iotHubClientHandle1 is NULL!\r\n"); } else if ((device_handle2 = IoTHubDeviceClient_CreateWithTransport(transport_handle, &client_config2)) == NULL) { (void)printf("ERROR: iotHubClientHandle1 is NULL!\r\n"); } else { // Set any option that are neccessary. // For available options please see the iothub_sdk_options.md documentation //bool traceOn = true; //IoTHubDeviceClient_SetOption(device_handle1, OPTION_LOG_TRACE, &traceOn); //IoTHubDeviceClient_SetOption(device_handle2, OPTION_LOG_TRACE, &traceOn); #ifdef SET_TRUSTED_CERT_IN_SAMPLES // Setting the Trusted Certificate. This is only necessary on system without // built in certificate stores. IoTHubDeviceClient_SetOption(device_handle1, OPTION_TRUSTED_CERT, certificates); IoTHubDeviceClient_SetOption(device_handle2, OPTION_TRUSTED_CERT, certificates); #endif // SET_TRUSTED_CERT_IN_SAMPLES /* Here subscribe for C2D methods */ if (IoTHubDeviceClient_SetDeviceMethodCallback(device_handle1, DeviceMethodCallback, (void*)deviceId1) != IOTHUB_CLIENT_OK) { (void)printf("ERROR: IoTHubClient_SetDeviceMethodCallback for the first device..........FAILED!\r\n"); } else if (IoTHubDeviceClient_SetDeviceMethodCallback(device_handle2, DeviceMethodCallback, (void*)deviceId2) != IOTHUB_CLIENT_OK) { (void)printf("ERROR: IoTHubClient_SetDeviceMethodCallback for the second device..........FAILED!\r\n"); } else { (void)printf("IoTHubClient_SetMessageCallback...successful.\r\n"); (void)printf("Waiting for C2D methods ...\r\n"); (void)printf("Press enter to close application\r\n"); (void)getchar(); } // Clean up the iothub sdk handle IoTHubDeviceClient_Destroy(device_handle1); IoTHubDeviceClient_Destroy(device_handle2); } IoTHubTransport_Destroy(transport_handle); // Free all the sdk subsystem IoTHub_Deinit(); } return 0; }
int main(void) { IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol; IOTHUB_MESSAGE_HANDLE message_handle; const char* telemetry_msg = "test_message"; char *cert_string = NULL; printf("This sample will send %d messages and wait for any C2D messages.\r\nPress the enter key to end the sample\r\n\r\n", MESSAGE_COUNT); // Select the Protocol to use with the connection #ifdef SAMPLE_MQTT protocol = MQTT_Protocol; #endif // SAMPLE_MQTT #ifdef SAMPLE_MQTT_OVER_WEBSOCKETS protocol = MQTT_WebSocket_Protocol; #endif // SAMPLE_MQTT_OVER_WEBSOCKETS #ifdef SAMPLE_AMQP protocol = AMQP_Protocol; #endif // SAMPLE_AMQP #ifdef SAMPLE_AMQP_OVER_WEBSOCKETS protocol = AMQP_Protocol_over_WebSocketsTls; #endif // SAMPLE_AMQP_OVER_WEBSOCKETS #ifdef SAMPLE_HTTP protocol = HTTP_Protocol; #endif // SAMPLE_HTTP IOTHUB_DEVICE_CLIENT_HANDLE device_handle; // Used to initialize IoTHub SDK subsystem (void)IoTHub_Init(); (void)printf("Creating IoTHub handle\r\n"); // Create the iothub handle here device_handle = IoTHubDeviceClient_CreateFromConnectionString(connectionString, protocol); if (device_handle == NULL) { (void)printf("Failure creating device client handle. Hint: Check you connection string.\r\n"); } else { // Setting message callback to get C2D messages (void)IoTHubDeviceClient_SetMessageCallback(device_handle, receive_msg_callback, NULL); // Setting connection status callback to get indication of connection to iothub (void)IoTHubDeviceClient_SetConnectionStatusCallback(device_handle, connection_status_callback, NULL); // Set any option that are necessary. // For available options please see the iothub_sdk_options.md documentation // Turn tracing on/off // bool traceOn = true; // (void)IoTHubDeviceClient_SetOption(device_handle, OPTION_LOG_TRACE, &traceOn); // Provide the Azure IoT device client with the Edge root // X509 CA certificate that was used to setup the Edge runtime cert_string = obtain_edge_ca_certificate(); (void)IoTHubDeviceClient_SetOption(device_handle, OPTION_TRUSTED_CERT, cert_string); #if defined SAMPLE_MQTT || defined SAMPLE_MQTT_OVER_WEBSOCKETS //Setting the auto URL Encoder (recommended for MQTT). Please use this option unless //you are URL Encoding inputs yourself. //ONLY valid for use with MQTT //bool urlEncodeOn = true; //(void)IoTHubDeviceClient_SetOption(device_handle, OPTION_AUTO_URL_ENCODE_DECODE, &urlEncodeOn); #endif for (size_t index = 0; index < MESSAGE_COUNT; index++) { // Construct the iothub message from a string message_handle = IoTHubMessage_CreateFromString(telemetry_msg); // Set Message property (void)IoTHubMessage_SetMessageId(message_handle, "MSG_ID"); (void)IoTHubMessage_SetCorrelationId(message_handle, "CORE_ID"); (void)IoTHubMessage_SetContentTypeSystemProperty(message_handle, "application%2fjson"); (void)IoTHubMessage_SetContentEncodingSystemProperty(message_handle, "utf-8"); // Add custom properties to message (void)IoTHubMessage_SetProperty(message_handle, "property_key", "property_value"); (void)printf("Sending message %d to Edge Hub\r\n", (int)(index + 1)); IoTHubDeviceClient_SendEventAsync(device_handle, message_handle, send_confirm_callback, NULL); // The message is copied to the sdk so the we can destroy it IoTHubMessage_Destroy(message_handle); } printf("\r\nPress any key to continue\r\n"); (void)getchar(); // Clean up the iothub sdk handle IoTHubDeviceClient_Destroy(device_handle); if (cert_string != NULL) { free(cert_string); cert_string = NULL; } } // Free all the sdk subsystem IoTHub_Deinit(); return 0; }