1#include "../include/sensor/sensor.h"
8#define SENSOR_PLUGIN_EXPORT __declspec(dllexport)
10#define SENSOR_PLUGIN_EXPORT
13#if defined(HAVE_WIRINGPI) && !defined(_WIN32)
20static int dht_pin(
void) {
21 const char *env = getenv(
"DHT22_PIN");
25 return (v >= 0) ? v : 7;
28static int dht22_data[5] = {0, 0, 0, 0, 0};
30static int dht22_read(
float *out_temp_c,
float *out_humidity_pct) {
31 if (!out_temp_c || !out_humidity_pct)
34 const int pin = dht_pin();
35 unsigned char laststate = HIGH;
36 unsigned char counter = 0;
40 dht22_data[0] = dht22_data[1] = dht22_data[2] = dht22_data[3] = dht22_data[4] = 0;
43 digitalWrite(pin, LOW);
45 digitalWrite(pin, HIGH);
46 delayMicroseconds(40);
49 for (i = 0; i < MAX_TIMINGS; i++) {
51 while (digitalRead(pin) == laststate) {
57 laststate = digitalRead(pin);
60 if ((i >= 4) && (i % 2 == 0)) {
61 dht22_data[j / 8] <<= 1;
63 dht22_data[j / 8] |= 1;
68 if ((j >= 40) && (dht22_data[4] == ((dht22_data[0] + dht22_data[1] + dht22_data[2] + dht22_data[3]) & 0xFF))) {
69 float h = (float)((dht22_data[0] << 8) + dht22_data[1]) / 10.0f;
71 h = (float)dht22_data[0];
72 float c = (float)(((dht22_data[2] & 0x7F) << 8) + dht22_data[3]) / 10.0f;
74 c = (float)dht22_data[2];
75 if (dht22_data[2] & 0x80)
79 *out_humidity_pct = h;
86static int dht22_init(
void) {
87 if (wiringPiSetup() == -1) {
88 fprintf(stderr,
"DHT22: wiringPi setup failed\n");
91 printf(
"DHT22 initialized (hardware mode, pin %d).\n", dht_pin());
95static int dht22_shutdown(
void) {
96 printf(
"DHT22 shutdown.\n");
103static int dht22_read(
float *out_temp_c,
float *out_humidity_pct) {
104 if (!out_temp_c || !out_humidity_pct)
108 float temp = 22.0f + (float)(rand() % 800) / 100.0f;
109 float hum = 40.0f + (float)(rand() % 4000) / 100.0f;
114 *out_humidity_pct = hum;
118static int dht22_init(
void) {
119 printf(
"DHT22 initialized (simulated mode). Set HAVE_WIRINGPI=1 on Linux to use hardware.\n");
123static int dht22_shutdown(
void) {
124 printf(
"DHT22 shutdown.\n");
130static void fill_timestamp(
char *buf,
size_t len) {
131 time_t now = time(NULL);
132 const struct tm *ptm = localtime(&now);
134 snprintf(buf, len,
"1970-01-01 00:00:00");
137 if (strftime(buf, len,
"%Y-%m-%d %H:%M:%S", ptm) == 0) {
138 snprintf(buf, len,
"1970-01-01 00:00:00");
146 float t = 0.0f, h = 0.0f;
147 if (dht22_read(&t, &h) != 0)
150 memset(out, 0,
sizeof(*out));
152 snprintf(out->
model,
sizeof(out->
model),
"DHT22");
169 .plugin_version =
"1.0.0",
170 .description =
"DHT22 plugin (hardware with wiringPi or simulated fallback).",
173 .read_sample = dht22_read_sample,
174 .shutdown = dht22_shutdown,
One air quality reading: pollutant values plus sensor/timestamp metadata.
char timestamp[32]
Reading timestamp, formatted "%Y-%m-%d %H:%M:%S".
char model[64]
Sensor model name, e.g.
int sensor_id
Numeric sensor identifier, matches SensorPlugin::sensor_id.
Runtime contract every sensor plugin (.so/.dylib/.dll) must implement.
int api_version
Must equal SENSOR_PLUGIN_API_VERSION at build time.