Here is a sample C code to read temperature and humidity from 6 DHT22 sensors connected to GPIO ports 4, 5, 6, 23, 24, and 25, and write the data to separate CSV files named SensorX.csv:Make sure to install the `wiringPi` library and `dht` library before compiling and running the code. You can compile the code using the following command: ```bash gcc -o read_sensors read_sensors.c -lwiringPi -lpthread ``` This code will read temperature and humidity data from 6 DHT22 sensors connected to the specified GPIO pins and write the data to separate CSV files named SensorX.csv. Each CSV file will have a timestamp, temperature, and humidity data in a comma-separated format.#include <stdio.h> #include <wiringPi.h> #include <dht.h> #include <time.h> #define DHT_PIN_4 4 #define DHT_PIN_5 5 #define DHT_PIN_6 6 #define DHT_PIN_23 23 #define DHT_PIN_24 24 #define DHT_PIN_25 25 #define MAX_SENSORS 6 void writeToCSV(int pin, float temperature, float humidity) { char filename[20]; sprintf(filename, "Sensor%d.csv", pin); FILE *file = fopen(filename, "a"); if (file != NULL) { time_t now = time(NULL); struct tm *t = localtime(&now); fprintf(file, "%d-%02d-%02d %02d:%02d:%02d,%.2f,%.2f\n", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, temperature, humidity); fclose(file); } else { printf("Error opening file %s\n", filename); } } int main() { wiringPiSetup(); int pins[MAX_SENSORS] = {DHT_PIN_4, DHT_PIN_5, DHT_PIN_6, DHT_PIN_23, DHT_PIN_24, DHT_PIN_25}; float temperature, humidity; for (int i = 0; i < MAX_SENSORS; i++) { int dht22 = dhtInit(pins[i], DHT22); if (dht22 < 0) { printf("Failed to initialize DHT sensor on GPIO %d\n", pins[i]); } else { if (dhtRead(dht22, &temperature, &humidity) == DHT_SUCCESS) { writeToCSV(pins[i], temperature, humidity); } else { printf("Failed to read data from DHT sensor on GPIO %d\n", pins[i]); } delay(2000); // wait for 2 seconds before reading next sensor } } return 0; }
Here is a simple C library for DHT22 sensor that you can use to read temperature and humidity data from the sensor:You can use this library to read data from the DHT22 sensor by calling `read_dht_data()` function to read the sensor data, and then use `get_temperature()` and `get_humidity()` functions to get the temperature and humidity values respectively. Make sure to include the necessary headers and wiringPi library before compiling and using this library. Please note that this is a basic library and may need further modifications based on your specific requirements and use case.#include <wiringPi.h> #include <stdint.h> #include <stdio.h> #define MAX_TIMINGS 85 #define DHT_PIN 4 int data[5] = {0, 0, 0, 0, 0}; int read_dht_data() { uint8_t laststate = HIGH; uint8_t counter = 0; uint8_t j = 0, i; data[0] = data[1] = data[2] = data[3] = data[4] = 0; pinMode(DHT_PIN, OUTPUT); digitalWrite(DHT_PIN, LOW); delay(18); digitalWrite(DHT_PIN, HIGH); delayMicroseconds(40); pinMode(DHT_PIN, INPUT); for (i = 0; i < MAX_TIMINGS; i++) { counter = 0; while (digitalRead(DHT_PIN) == laststate) { counter++; delayMicroseconds(1); if (counter == 255) { break; } } laststate = digitalRead(DHT_PIN); if (counter == 255) { break; } if ((i >= 4) && (i % 2 == 0)) { data[j / 8] <<= 1; if (counter > 16) { data[j / 8] |= 1; } j++; } } if ((j >= 40) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))) { return 1; } else { return 0; } } float get_temperature() { float temperature = (float)((data[2] & 0x7F) * 256 + data[3]) / 10.0; if (data[2] & 0x80) { temperature *= -1; } return temperature; } float get_humidity() { return (float)((data[0] * 256 + data[1]) / 10.0); }
Max tokens (character): 1800 Modal: gpt-3.5-turbo Temperature: 0.5 Free to use by mbsTECH.dk changelog.txt webgpt_v1.2.5.rar