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) { 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; }