-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule_DHT22.ino
More file actions
73 lines (62 loc) · 1.87 KB
/
Module_DHT22.ino
File metadata and controls
73 lines (62 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <Arduino.h>
#include <Wire.h>
#include "mdht.h"
#include "Serial/packaging.h"
#include "Serial/flags.h"
using namespace CS;
PackagedWired* wire;
mDHT* dht = nullptr;
constexpr int port_DHT22 = GPIO_NUM_14;
const auto this_device = device_id::DHT22_SENSOR;
void callback(void*, const uint8_t, const char*, const uint8_t);
void setup()
{
Serial.begin(115200);
dht = new mDHT(port_DHT22);
wire = new PackagedWired(config()
.set_slave(this_device)
.set_slave_callback(callback)
.set_led(2)
);
}
void callback(void* rw, const uint8_t expects, const char* received, const uint8_t length)
{
if (length != sizeof(Requester)) return;
PackagedWired& w = *(PackagedWired*) rw;
Requester req(received);
switch(req.get_offset()) {
case 0:
{
FlagWrapper fw;
if (dht->has_issues()) fw |= device_flags::HAS_ISSUES;
if (dht->has_new_data_autoreset()) fw |= device_flags::HAS_NEW_DATA;
Command cmd("#FLAGS", (uint64_t)fw);
w.slave_reply_from_callback(cmd);
}
break;
case 1:
{
const float val = dht->get_temperature();
Command cmd("/dht/temperature", val);
w.slave_reply_from_callback(cmd);
//Serial.printf("Received request {%zu}\nReplying with temperature %.3f\n", req.get_offset(), val);
}
break;
case 2:
{
const float val = dht->get_humidity();
Command cmd("/dht/humidity", val);
w.slave_reply_from_callback(cmd);
//Serial.printf("Received request {%zu}\nReplying with humidity %.3f\n", req.get_offset(), val);
}
break;
default:
{
Command cmd; // invalid
w.slave_reply_from_callback(cmd);
//Serial.printf("Received request {%zu}\nConsidered invalid!\n", req.get_offset());
}
}
}
// unused
void loop() { vTaskDelete(NULL); }