daily pastebin goal
16%
SHARE
TWEET

Arduino Code

Kowlown Feb 12th, 2018 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Enable debug prints
  2. #define MY_DEBUG
  3. // #define MY_TRANSPORT_WAIT_READY_MS 1
  4. // Enable and select radio type attached
  5. #define MY_RADIO_NRF24
  6.  
  7. #include <SPI.h>
  8.  
  9.  
  10. #include <Adafruit_Sensor.h>
  11.  
  12. #include <DHT.h>
  13. #include <DHT_U.h>
  14.  
  15. #include <MyConfig.h>
  16. #include <MySensors.h>
  17.  
  18. #define DHTPIN 2
  19. #define DHTTYPE           DHT22     // DHT 22 (AM2302)
  20.  
  21. // Set this offset if the sensor has a permanent small offset to the real temperatures
  22. #define SENSOR_TEMP_OFFSET 0
  23.  
  24. #define CHILD_ID_HUM 0
  25. #define CHILD_ID_TEMP 1
  26.  
  27.  
  28. // Sleep time between sensor updates (in milliseconds)
  29. // Must be >1000ms for DHT22 and >2000ms for DHT11
  30. static const uint64_t UPDATE_INTERVAL = 3000;
  31.  
  32. DHT_Unified dht(DHTPIN, DHTTYPE);
  33. uint32_t delayMS;
  34. bool metric = true;
  35.  
  36.  
  37.  
  38. MyMessage msgHum(CHILD_ID_HUM, V_HUM);
  39. MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
  40.  
  41.  
  42. void presentation()
  43. {
  44.   // Send the sketch version information to the gateway
  45.   sendSketchInfo("TemperatureAndHumidity", "1.1");
  46.  
  47.   // Register all sensors to gw (they will be created as child devices)
  48.   present(CHILD_ID_HUM, S_HUM);
  49.   present(CHILD_ID_TEMP, S_TEMP);
  50.  
  51.   metric = getControllerConfig().isMetric;
  52. }
  53.  
  54.  
  55. void setup() {
  56.  
  57.   // Initialize device.
  58.   dht.begin();
  59. //  Serial.begin(115200);
  60.   Serial.println("DHTxx Unified Sensor Example");
  61.   // Print temperature sensor details.
  62.   sensor_t sensor;
  63.   dht.temperature().getSensor(&sensor);
  64.   Serial.println("------------------------------------");
  65.   Serial.println("Temperature");
  66.   Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  67.   Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  68.   Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  69.   Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" *C");
  70.   Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" *C");
  71.   Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" *C");  
  72.   Serial.println("------------------------------------");
  73.   // Print humidity sensor details.
  74.   dht.humidity().getSensor(&sensor);
  75.   Serial.println("------------------------------------");
  76.   Serial.println("Humidity");
  77.   Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  78.   Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  79.   Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  80.   Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println("%");
  81.   Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println("%");
  82.   Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println("%");  
  83.   Serial.println("------------------------------------");
  84.   // Set delay between sensor readings based on sensor details.
  85.   delayMS = UPDATE_INTERVAL;
  86.  
  87. }
  88.  
  89. void loop() {
  90.   // Delay between measurements.
  91.   delay(delayMS);
  92.   // Get temperature event and print its value.
  93.   sensors_event_t event;  
  94.   dht.temperature().getEvent(&event);
  95.   if (isnan(event.temperature)) {
  96.     Serial.println("Error reading temperature!");
  97.   }
  98.   else {
  99.     Serial.print("Temperature: ");
  100.     Serial.print(event.temperature);
  101.     Serial.println(" *C");
  102.     float temperature = event.temperature;
  103.     send(msgTemp.set(temperature, 1));
  104.   }
  105.   // Get humidity event and print its value.
  106.   dht.humidity().getEvent(&event);
  107.   if (isnan(event.relative_humidity)) {
  108.     Serial.println("Error reading humidity!");
  109.   }
  110.   else {
  111.     Serial.print("Humidity: ");
  112.     Serial.print(event.relative_humidity);
  113.     Serial.println("%");
  114.     float humidity = event.relative_humidity;
  115.     send(msgHum.set(humidity, 1));
  116.   }
  117. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top