Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Enable and select radio type attached
- #define MY_RADIO_NRF24
- // Enable debugging
- #define MY_DEBUG
- //#define MY_NODE_ID 1
- #include <DHT.h>
- #include <DHT_U.h>
- #include <MySensors.h>
- // Set this to the pin you connected the DHT's data pin to
- #define DHT_DATA_PIN 3
- // We're using a DHT22
- #define DHTTYPE DHT22
- // Set a child sensor id
- #define CHILD_ID_HUM 0
- #define CHILD_ID_TEMP 1
- // Set this offset if the sensor has a permanent small offset to the real temperatures
- #define SENSOR_TEMP_OFFSET 0
- // Sleep time between sensor updates (in milliseconds)
- // Must be >1000ms for DHT22 and >2000ms for DHT11
- static const uint64_t UPDATE_INTERVAL = 1001;
- DHT_Unified dht(DHT_DATA_PIN, DHTTYPE);
- uint32_t delayMS;
- float humidity;
- float temperature;
- MyMessage msgHum(CHILD_ID_HUM, V_HUM);
- MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
- bool initialValueSent = false;
- void setup()
- {
- dht.begin();
- sensor_t sensor;
- }
- void presentation()
- {
- sendSketchInfo("DHT22 Sensor", "0.1");
- present(CHILD_ID_TEMP, S_TEMP);
- present(CHILD_ID_HUM, S_HUM);
- }
- void loop()
- {
- // Read stuff from the DHT sensor
- sensors_event_t event;
- dht.temperature().getEvent(&event);
- if (isnan(event.temperature)) {
- Serial.println("Error reading temperature.");
- temperature = 0;
- }
- else {
- temperature = event.temperature;
- }
- // Get humidity event and print its value.
- dht.humidity().getEvent(&event);
- if (isnan(event.relative_humidity)) {
- Serial.println("Error reading humidity!");
- humidity = 0;
- }
- else {
- humidity = event.relative_humidity;
- }
- send(msgHum.set(temperature, 1));
- #ifdef My_DEBUG
- Serial.print("T: ")
- Serial.println(temperature);
- #endif
- send(msgTemp.set(humidity, 1));
- #ifdef My_DEBUG
- Serial.print("H: ")
- Serial.println(humidity);
- #endif
- // Sleep for a while to save energy
- sleep(UPDATE_INTERVAL);
- }
Advertisement
Add Comment
Please, Sign In to add comment