static int remote_monitoring_init(void) { int result; Lock_fd = open_lockfile(LOCKFILE); if (setuid(getuid()) < 0) { perror("Dropping privileges failed. (did you use sudo?)n"); result = EXIT_FAILURE; } else { result = wiringPiSetup(); if (result != 0) { perror("Wiring Pi setup failed."); } else { result = wiringPiSPISetup(Spi_channel, Spi_clock); if (result < 0) { printf("Can't setup SPI, error %i calling wiringPiSPISetup(%i, %i) %sn", result, Spi_channel, Spi_clock, strerror(result)); } else { int sensorResult = bme280_init(Spi_channel); if (sensorResult != 1) { printf("It appears that no BMP280 module on Chip Enable %i is attached. Aborting.\n", Spi_channel); result = 1; } else { // Read the Temp & Pressure module. float tempC = -300.0; float pressurePa = -300; float humidityPct = -300; sensorResult = bme280_read_sensors(&tempC, &pressurePa, &humidityPct); if (sensorResult == 1) { printf("Temperature = %.1f *C Pressure = %.1f Pa Humidity = %1f %%\n", tempC, pressurePa, humidityPct); result = 0; } else { printf("Unable to read BME280 on pin %i. Aborting.\n", Spi_channel); result = 1; } } } } } 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 simplesample_amqp_run(void) { if (platform_init() != 0) { printf("Failed to initialize the platform.\r\n"); exit(1); } if (serializer_init(NULL) != SERIALIZER_OK) { (void)printf("Failed on serializer_init\r\n"); } else { /* Setup IoTHub client configuration */ IOTHUB_CLIENT_HANDLE iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol); srand((unsigned int)time(NULL)); int avgMTemperature = 10; if (iotHubClientHandle == NULL) { (void)printf("Failed on IoTHubClient_Create\r\n"); } else { #ifdef MBED_BUILD_TIMESTAMP // For mbed add the certificate information if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) { (void)printf("failure to set option \"TrustedCerts\"\r\n"); } #endif // MBED_BUILD_TIMESTAMP ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer); if (myWeather == NULL) { (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n"); } else { unsigned char* destination; size_t destinationSize; if (IoTHubClient_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK) { printf("unable to IoTHubClient_SetMessageCallback\r\n"); } else { int Lock_fd__i = open_lockfile(LOCKFILE); if (setuid(getuid()) < 0) { perror("Dropping privileges failed. (did you use sudo?)\n"); exit(EXIT_FAILURE); } int Result__i = wiringPiSetup(); if (Result__i != 0) exit(Result__i); int Spi_fd__i = wiringPiSPISetup(Spi_channel__i, Spi_clock__i); if (Spi_fd__i < 0) { printf("Can't setup SPI, error %i calling wiringPiSPISetup(%i, %i) %s\n", Spi_fd__i, Spi_channel__i, Spi_clock__i, strerror(Spi_fd__i)); exit(Spi_fd__i); } int Init_result__i = bme280_init(Spi_channel__i); if (Init_result__i != 1) { printf("It appears that no BMP280 module on Chip Enable %i is attached. Aborting.\n", Spi_channel__i); exit(1); } pinMode(Red_led_pin__i, OUTPUT); pinMode(Grn_led_pin__i, OUTPUT); //////////////// // Read the Temp & Pressure module. float Temp_C__f = -300.0; float Pressure_Pa__f = -300; float Humidity_pct__f = -300; Result__i = bme280_read_sensors(&Temp_C__f, &Pressure_Pa__f, &Humidity_pct__f); if (Result__i == 1) { printf("Temperature = %.1f *C Pressure = %.1f Pa Humidity = %1f %%\n", Temp_C__f, Pressure_Pa__f, Humidity_pct__f); } else { printf("Unable to read BME280 on pin %i\n", Spi_channel__i); } char buff[11]; int timeNow = 0; int c; while (1) { timeNow = (int)time(NULL); sprintf(buff, "%d", timeNow); myWeather->DeviceId = "raspy"; myWeather->EventTime = buff; if (Result__i == 1) { myWeather->MTemperature = Temp_C__f; printf("Humidity = %.1f%% Temperature = %.1f*C \n", Humidity_pct__f, Temp_C__f); } else { myWeather->MTemperature = 404.0; printf("Unable to read BME280 on pin %i\n", Spi_channel__i); pinMode(Red_led_pin__i, OUTPUT); } if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->EventTime, myWeather->MTemperature) != IOT_AGENT_OK) { (void)printf("Failed to serialize\r\n"); } else { sendMessage(iotHubClientHandle, destination, destinationSize); } delay(5000); } /* wait for commands */ // (void)getchar(); close_lockfile(Lock_fd__i); } DESTROY_MODEL_INSTANCE(myWeather); } IoTHubClient_Destroy(iotHubClientHandle); } serializer_deinit(); } }