int main ( int argc, char *argv[] ) { int lockfd; uint8_t rErr = 0; if ( argc != 2 ) printf ( "%s <pin>\n pin : wiringPi pin No (default %d)\n",argv[0], DHTPIN ); else DHTPIN = atoi( argv[1] ); lockfd = open_lockfile( LOCKFILE ); if ( wiringPiSetup () == -1 ) exit( EXIT_FAILURE ) ; if ( setuid( getuid() ) < 0 ) { perror( "Dropping privileges failed\n" ); exit( EXIT_FAILURE ); } while ( read_dht22_dat() == 0 && rErr++ < 10 ) { delay( 1000 ); // wait 1sec to refresh } delay( 1500 ); // No retry within 1 sec close_lockfile( lockfd ); return 0 ; }
int main (int argc, char *argv[]) { int lockfd; if (argc != 2) printf ("{\"err\": {\"message\": \"NOT_ENOUGH_ARGUMENTS\"}}\n"); else DHTPIN = atoi(argv[1]); lockfd = open_lockfile(LOCKFILE); if (wiringPiSetup () == -1) exit(EXIT_FAILURE) ; if (setuid(getuid()) < 0) { perror("{\"err\": {\"message\": \"DROPPING_PRIVILEGES_FAILED\"}}\n"); exit(EXIT_FAILURE); } while (1) { read_dht22_dat(); fflush(stdout); delay(500); // wait 0.5s to refresh } delay(500); close_lockfile(lockfd); return 0 ; }
int main (void) { int lockfd; lockfd = open_lockfile(LOCKFILE); if (wiringPiSetup () == -1) exit(EXIT_FAILURE) ; if (setuid(getuid()) < 0) { perror("Dropping privileges failed\n"); exit(EXIT_FAILURE); } while (read_dht22_dat() == 0) { delay(1000); // wait 1sec to refresh } delay(1500); close_lockfile(lockfd); return 0 ; }
int main (int argc, char *argv[]) { int lockfd = 0; //initialize to suppress warning int tries = 100; int lock = 1; if (argc < 2) printf ("usage: %s <pin> (<tries> <lock>)\ndescription: pin is the wiringPi pin number\nusing 7 (GPIO 4)\nOptional: tries is the number of times to try to obtain a read (default 100)\n lock: 0 disables the lockfile (for running as non-root user)\n",argv[0]); else DHTPIN = atoi(argv[1]); if (argc >= 3) tries = atoi(argv[2]); if (tries < 1) { printf("Invalid tries supplied\n"); exit(EXIT_FAILURE); } if (argc >= 4) lock = atoi(argv[3]); if (lock != 0 && lock != 1) { printf("Invalid lock state supplied\n"); exit(EXIT_FAILURE); } printf ("Raspberry Pi wiringPi DHT22 reader\nwww.lolware.net\n") ; if(lock) lockfd = open_lockfile(LOCKFILE); if (wiringPiSetup () == -1) exit(EXIT_FAILURE) ; if (setuid(getuid()) < 0) { perror("Dropping privileges failed\n"); exit(EXIT_FAILURE); } while (read_dht22_dat() == 0 && tries--) { delay(1000); // wait 1sec to refresh } delay(1500); if(lock) close_lockfile(lockfd); return 0 ; }
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(); } }