static int runLongHaulSendTest(const LONGHAUL_SEND_TEST_PROFILE* testProfile) { int result; LONGHAUL_SEND_TEST_STATE test_state; test_state.profile = testProfile; test_state.sendFrequencyIndex = 0; test_state.timeUntilNextSendEventInSeconds = 0; LogInfo("Starting Long Haul send test \"%s\" (totalRunTimeInSeconds=%d)", test_state.profile->name, test_state.profile->totalRunTimeInSeconds); IOTHUB_CLIENT_CONFIG iotHubConfig; IOTHUB_CLIENT_HANDLE iotHubClientHandle; #ifdef MBED_BUILD_TIMESTAMP IOTHUB_CLIENT_RESULT client_result; #endif iotHubConfig.iotHubName = IoTHubAccount_GetIoTHubName(g_iothubAcctInfo); iotHubConfig.iotHubSuffix = IoTHubAccount_GetIoTHubSuffix(g_iothubAcctInfo); iotHubConfig.deviceId = IoTHubAccount_GetDeviceId(g_iothubAcctInfo); iotHubConfig.deviceKey = IoTHubAccount_GetDeviceKey(g_iothubAcctInfo); iotHubConfig.protocol = AMQP_Protocol; iotHubConfig.deviceSasToken = NULL; iotHubConfig.protocolGatewayHostName = NULL; if ((iotHubClientHandle = IoTHubClient_Create(&iotHubConfig)) == NULL) { LogError("Failed creating the IoT Hub Client."); result = __LINE__; } #ifdef MBED_BUILD_TIMESTAMP else if ((client_result = IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates)) != IOTHUB_CLIENT_OK) { LogError("Failed setting certificates on IoT Hub client [%i].", client_result); result = __LINE__; } #endif else { result = sendEventLoop(iotHubClientHandle, &test_state); } if (iotHubClientHandle != NULL) { IoTHubClient_Destroy(iotHubClientHandle); } LogInfo("Long Haul send test \"%s\" completed (result=%d)", test_state.profile->name, result); return result; }
static void remote_monitoring_run(void) { if (platform_init() != 0) { printf("Failed to initialize the platform.\r\n"); } else { if (serializer_init(NULL) != SERIALIZER_OK) { printf("Failed on serializer_init\r\n"); } else { IOTHUB_CLIENT_CONFIG config; IOTHUB_CLIENT_HANDLE iotHubClientHandle; config.deviceSasToken = NULL; config.deviceId = deviceId; config.deviceKey = deviceKey; config.iotHubName = hubName; config.iotHubSuffix = hubSuffix; #ifndef WINCE config.protocol = AMQP_Protocol; #else config.protocol = HTTP_Protocol; #endif iotHubClientHandle = IoTHubClient_Create(&config); if (iotHubClientHandle == NULL) { (void)printf("Failed on IoTHubClient_CreateFromConnectionString\r\n"); } else { #ifdef MBED_BUILD_TIMESTAMP // For mbed add the certificate information if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) { printf("failure to set option \"TrustedCerts\"\r\n"); } #endif // MBED_BUILD_TIMESTAMP Thermostat* thermostat = CREATE_MODEL_INSTANCE(Contoso, Thermostat); if (thermostat == NULL) { (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n"); } else { STRING_HANDLE commandsMetadata; if (IoTHubClient_SetMessageCallback(iotHubClientHandle, IoTHubMessage, thermostat) != IOTHUB_CLIENT_OK) { printf("unable to IoTHubClient_SetMessageCallback\r\n"); } else { /* send the device info upon startup so that the cloud app knows what commands are available and the fact that the device is up */ thermostat->ObjectType = "DeviceInfo"; thermostat->IsSimulatedDevice = false; thermostat->Version = "1.0"; thermostat->DeviceProperties.HubEnabledState = true; thermostat->DeviceProperties.DeviceID = (char*)deviceId; commandsMetadata = STRING_new(); if (commandsMetadata == NULL) { (void)printf("Failed on creating string for commands metadata\r\n"); } else { /* Serialize the commands metadata as a JSON string before sending */ if (SchemaSerializer_SerializeCommandMetadata(GET_MODEL_HANDLE(Contoso, Thermostat), commandsMetadata) != SCHEMA_SERIALIZER_OK) { (void)printf("Failed serializing commands metadata\r\n"); } else { unsigned char* buffer; size_t bufferSize; thermostat->Commands = (char*)STRING_c_str(commandsMetadata); /* Here is the actual send of the Device Info */ if (SERIALIZE(&buffer, &bufferSize, thermostat->ObjectType, thermostat->Version, thermostat->IsSimulatedDevice, thermostat->DeviceProperties, thermostat->Commands) != IOT_AGENT_OK) { (void)printf("Failed serializing\r\n"); } else { sendMessage(iotHubClientHandle, buffer, bufferSize); } } STRING_delete(commandsMetadata); } thermostat->Temperature = 50; thermostat->ExternalTemperature = 55; thermostat->Humidity = 50; thermostat->DeviceId = (char*)deviceId; while (1) { unsigned char*buffer; size_t bufferSize; float tempC = -300.0; float pressurePa = -300; float humidityPct = -300; int sensorResult = bme280_read_sensors(&tempC, &pressurePa, &humidityPct); if (sensorResult == 1) { thermostat->Temperature = tempC; thermostat->Humidity = humidityPct; printf("Humidity = %.1f%% Temperature = %.1f*C \n", humidityPct, tempC); pinMode(Grn_led_pin, OUTPUT); } else { thermostat->Temperature = 404.0; thermostat->Humidity = 404.0; printf("Unable to read BME280 on pin %i\n", Spi_channel); pinMode(Red_led_pin, OUTPUT); } (void)printf("Sending sensor value Temperature = %.1f*C, Humidity = %.1f%%\r\n", thermostat->Temperature, thermostat->Humidity); if (SERIALIZE(&buffer, &bufferSize, thermostat->DeviceId, thermostat->Temperature, thermostat->Humidity, thermostat->ExternalTemperature) != IOT_AGENT_OK) { (void)printf("Failed sending sensor value\r\n"); } else { sendMessage(iotHubClientHandle, buffer, bufferSize); } ThreadAPI_Sleep(1000); } } close_lockfile(Lock_fd); DESTROY_MODEL_INSTANCE(thermostat); } IoTHubClient_Destroy(iotHubClientHandle); } serializer_deinit(); } platform_deinit(); } }
void remote_monitoring_run(void) { if (serializer_init(NULL) != SERIALIZER_OK) { printf("Failed on serializer_init\r\n"); } else { IOTHUB_CLIENT_CONFIG config; config.deviceId = deviceId; config.deviceKey = deviceKey; config.iotHubName = hubName; config.iotHubSuffix = hubSuffix; config.protocol = AMQP_Protocol; IOTHUB_CLIENT_HANDLE iotHubClientHandle = IoTHubClient_Create(&config); if (iotHubClientHandle == NULL) { (void)printf("Failed on IoTHubClient_CreateFromConnectionString\r\n"); } else { unsigned int minimumPollingTime = 9; /*because it can poll "after 9 seconds" polls will happen effectively at ~10 seconds*/ if (IoTHubClient_SetOption(iotHubClientHandle, "MinimumPollingTime", &minimumPollingTime) != IOTHUB_CLIENT_OK) { printf("failure to set option \"MinimumPollingTime\"\r\n"); } #ifdef MBED_BUILD_TIMESTAMP // For mbed add the certificate information if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) { printf("failure to set option \"TrustedCerts\"\r\n"); } #endif // MBED_BUILD_TIMESTAMP Thermostat* thermostat = CREATE_MODEL_INSTANCE(Contoso, Thermostat); if (thermostat == NULL) { (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n"); } else { STRING_HANDLE commandsMetadata; if (IoTHubClient_SetMessageCallback(iotHubClientHandle, IoTHubMessage, thermostat) != IOTHUB_CLIENT_OK) { printf("unable to IoTHubClient_SetMessageCallback\r\n"); } else { /* send the device info upon startup so that the cloud app knows what commands are available and the fact that the device is up */ thermostat->ObjectType = "DeviceInfo"; thermostat->IsSimulatedDevice = false; thermostat->Version = "1.0"; thermostat->DeviceProperties.HubEnabledState = true; thermostat->DeviceProperties.DeviceID = (char*)deviceId; commandsMetadata = STRING_new(); if (commandsMetadata == NULL) { (void)printf("Failed on creating string for commands metadata\r\n"); } else { /* Serialize the commands metadata as a JSON string before sending */ if (SchemaSerializer_SerializeCommandMetadata(GET_MODEL_HANDLE(Contoso, Thermostat), commandsMetadata) != SCHEMA_SERIALIZER_OK) { (void)printf("Failed serializing commands metadata\r\n"); } else { unsigned char* buffer; size_t bufferSize; thermostat->Commands = (char*)STRING_c_str(commandsMetadata); /* Here is the actual send of the Device Info */ if (SERIALIZE(&buffer, &bufferSize, thermostat->ObjectType, thermostat->Version, thermostat->IsSimulatedDevice, thermostat->DeviceProperties, thermostat->Commands) != IOT_AGENT_OK) { (void)printf("Failed serializing\r\n"); } else { sendMessage(iotHubClientHandle, buffer, bufferSize); } } STRING_delete(commandsMetadata); } thermostat->Temperature = 50.0; thermostat->Humidity = 50.0; thermostat->DeviceId = (char*)deviceId; while (1) { unsigned char*buffer; size_t bufferSize; (void)printf("Sending sensor value Temperature = %02f, Humidity = %02f\r\n", thermostat->Temperature, thermostat->Humidity); if (SERIALIZE(&buffer, &bufferSize, thermostat->Temperature, thermostat->Humidity, thermostat->DeviceId) != IOT_AGENT_OK) { (void)printf("Failed sending sensor value\r\n"); } else { sendMessage(iotHubClientHandle, buffer, bufferSize); } ThreadAPI_Sleep(1000); } } DESTROY_MODEL_INSTANCE(thermostat); } IoTHubClient_Destroy(iotHubClientHandle); } serializer_deinit(); } }