Facebook
From driver, 2 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 227
  1. // Enable debug prints
  2. //MySensors library (1.6.0 beta)
  3. #define MY_DEBUG
  4. #define MY_GATEWAY_SERIAL
  5. // Enable and select radio type attached
  6. //#define MY_RADIO_NRF24
  7. //#define MY_RADIO_RFM69
  8. //#define MY_RS485
  9.  
  10. #include <SPI.h>
  11. #include <MySensors.h>
  12. #include <DHT.h>
  13. #include <DallasTemperature.h>
  14. #include <OneWire.h>
  15. #define COMPARE_TEMP 1
  16. #define ONE_WIRE_BUS 6 // Pin where dallase sensor is connected
  17. #define MAX_ATTACHED_DS18B20 16
  18.  
  19. // Set this to the pin you connected the DHT's data pin to
  20. #define DHT1_DATA_PIN 4
  21. #define DHT2_DATA_PIN 3
  22.  
  23. // Set this offset if the sensor has a permanent small offset to the real temperatures
  24. #define SENSOR_TEMP_OFFSET 0
  25.  
  26. // Sleep time between sensor updates (in milliseconds)
  27. // Must be >1000ms for DHT22 and >2000ms for DHT11
  28. static const uint64_t UPDATE_INTERVAL = 30000;
  29.  
  30. // Force sending an update of the temperature after n sensor reads, so a controller showing the
  31. // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
  32. // the value didn't change since;
  33. // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
  34. static const uint8_t FORCE_UPDATE_N_READS = 10;
  35.  
  36. #define CHILD_ID_HUM1 6
  37. #define CHILD_ID_TEMP1 7
  38. #define CHILD_ID_HUM2 8
  39. #define CHILD_ID_TEMP2 9
  40.  
  41.  
  42. float lastTemp1;
  43. float lastHum1;
  44. uint8_t nNoUpdatesTemp1;
  45. uint8_t nNoUpdatesHum1;
  46.  
  47. float lastTemp2;
  48. float lastHum2;
  49. uint8_t nNoUpdatesTemp2;
  50. uint8_t nNoUpdatesHum2;
  51. bool metric = true;
  52.  
  53. OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  54. DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature.
  55. float lastTemperature[MAX_ATTACHED_DS18B20];
  56. int numSensors=0;
  57. bool receivedConfig = false;
  58.  
  59.  
  60. MyMessage msgDallas(0,V_TEMP);
  61. MyMessage msgHum1(CHILD_ID_HUM1, V_HUM);
  62. MyMessage msgTemp1(CHILD_ID_TEMP1, V_TEMP);
  63. MyMessage msgHum2(CHILD_ID_HUM2, V_HUM);
  64. MyMessage msgTemp2(CHILD_ID_TEMP2, V_TEMP);
  65. DHT dht;
  66.  
  67. void before()
  68. {
  69.   // Startup up the OneWire library
  70.   sensors.begin();
  71. }
  72. void setup()
  73. {
  74. // requestTemperatures() will not block current thread
  75.   sensors.setWaitForConversion(false);
  76. }
  77. void presentation()
  78. {
  79.   // Send the sketch version information to the gateway
  80.   sendSketchInfo("DualDHTandDallas", "1.1");
  81.  
  82.   // Register all sensors to gw (they will be created as child devices)
  83.   present(CHILD_ID_HUM1, S_HUM);
  84.   present(CHILD_ID_TEMP1, S_TEMP);
  85.   present(CHILD_ID_HUM2, S_HUM);
  86.   present(CHILD_ID_TEMP2, S_TEMP);
  87.  
  88.   metric = getControllerConfig().isMetric;
  89.  
  90.    // Fetch the number of attached temperature sensors
  91.   numSensors = sensors.getDeviceCount();
  92. //numSensors = 2;
  93.   // Present all sensors to controller
  94.   for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {  
  95.      present(i, S_TEMP);
  96. }
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103. void loop()    
  104. {
  105.  
  106. // Fetch temperatures from Dallas sensors
  107.   sensors.requestTemperatures();
  108.  
  109.   // query conversion time and sleep until conversion completed
  110.   int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
  111.   // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
  112.   sleep(conversionTime);
  113.  
  114.   // Read temperatures and send them to controller
  115.   for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
  116.  
  117.     // Fetch and round temperature to one decimal
  118.    float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
  119.  
  120.      //Only send data if temperature has changed and no error
  121.     //#if COMPARE_TEMP == 1
  122.    // if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) {
  123.    // #else
  124.    // if (temperature != -127.00 && temperature != 85.00) {
  125.     //#endif
  126.  
  127.       // Send in the new temperature
  128.       send(msgDallas.setSensor(i).set(temperature,1));
  129.       Serial.print("Dallas");
  130.       Serial.println(i);
  131.       Serial.println(" :");
  132.     Serial.println(temperature);
  133.       // Save new temperatures for next compare
  134.       lastTemperature[i]=temperature;
  135.     //}
  136.   }
  137.  
  138. // Sleep for a while to save energy
  139.   sleep(UPDATE_INTERVAL);
  140.  
  141.    
  142.   dht.setup(DHT1_DATA_PIN); // set data pin of DHT1 sensor
  143.   if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
  144.     Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
  145.   }
  146.   // Sleep for the time of the minimum sampling period to give the sensor time to power up
  147.   // (otherwise, timeout errors might occure for the first reading)
  148.   sleep(dht.getMinimumSamplingPeriod());
  149.  
  150.   // Force reading sensor, so it works also after sleep()
  151.   dht.readSensor(true);
  152.  
  153.   // Get temperature from DHT library
  154.   float temperature1 = dht.getTemperature();
  155.   if (isnan(temperature1)) {
  156.     Serial.println("Failed reading temperature from DHT!");
  157.   } else if (temperature1 != lastTemp1 || nNoUpdatesTemp1 == FORCE_UPDATE_N_READS) {
  158.     // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
  159.     lastTemp1 = temperature1;
  160.     if (!metric) {
  161.       temperature1 = dht.toFahrenheit(temperature1);
  162.     }
  163.     // Reset no updates counter
  164.     nNoUpdatesTemp1 = 0;
  165.     temperature1 += SENSOR_TEMP_OFFSET;
  166.     send(msgTemp1.set(temperature1, 1));
  167.  
  168.     //#ifdef MY_DEBUG
  169.     //Serial.print("T1: ");
  170.     //Serial.println(temperature1);
  171.     //#endif
  172.   } else {
  173.     // Increase no update counter if the temperature stayed the same
  174.     nNoUpdatesTemp1++;
  175.   }
  176.  
  177.   // Get humidity from DHT library
  178.   float humidity1 = dht.getHumidity();
  179.   if (isnan(humidity1)) {
  180.     Serial.println("Failed reading humidity from DHT");
  181.   } else if (humidity1 != lastHum1 || nNoUpdatesHum1 == FORCE_UPDATE_N_READS) {
  182.     // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
  183.     lastHum1 = humidity1;
  184.     // Reset no updates counter
  185.     nNoUpdatesHum1 = 0;
  186.     send(msgHum1.set(humidity1, 1));
  187.    
  188.     #ifdef MY_DEBUG
  189.     Serial.print("H1: ");
  190.     Serial.println(humidity1);
  191.     #endif
  192.   } else {
  193.     // Increase no update counter if the humidity stayed the same
  194.     nNoUpdatesHum1++;
  195.   }
  196.  
  197.   // Sleep for a while to save energy
  198.   sleep(UPDATE_INTERVAL);
  199.  
  200.  
  201. dht.setup(DHT2_DATA_PIN); // set data pin of DHT2 sensor
  202.   if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
  203.     Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
  204.   }
  205.   // Sleep for the time of the minimum sampling period to give the sensor time to power up
  206.   // (otherwise, timeout errors might occure for the first reading)
  207.   sleep(dht.getMinimumSamplingPeriod());
  208.  
  209.   // Force reading sensor, so it works also after sleep()
  210.   dht.readSensor(true);
  211.  
  212.   // Get temperature from DHT library
  213.   float temperature2 = dht.getTemperature();
  214.   if (isnan(temperature2)) {
  215.     Serial.println("Failed reading temperature from DHT!");
  216.   } else if (temperature2 != lastTemp2 || nNoUpdatesTemp2 == FORCE_UPDATE_N_READS) {
  217.     // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
  218.     lastTemp2 = temperature2;
  219.     if (!metric) {
  220.       temperature2 = dht.toFahrenheit(temperature2);
  221.     }
  222.     // Reset no updates counter
  223.     nNoUpdatesTemp2 = 0;
  224.     temperature2 += SENSOR_TEMP_OFFSET;
  225.     send(msgTemp2.set(temperature2, 1));
  226.  
  227.     //#ifdef MY_DEBUG
  228.     //Serial.print("T2: ");
  229.     //Serial.println(temperature2);
  230.     //#endif
  231.   } else {
  232.     // Increase no update counter if the temperature stayed the same
  233.     nNoUpdatesTemp2++;
  234.   }
  235.  
  236.   // Get humidity from DHT library
  237.   float humidity2 = dht.getHumidity();
  238.   if (isnan(humidity2)) {
  239.     Serial.println("Failed reading humidity from DHT");
  240.   } else if (humidity2 != lastHum2 || nNoUpdatesHum2 == FORCE_UPDATE_N_READS) {
  241.     // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
  242.     lastHum2 = humidity2;
  243.     // Reset no updates counter
  244.     nNoUpdatesHum2 = 0;
  245.     send(msgHum2.set(humidity2, 1));
  246.    
  247.     #ifdef MY_DEBUG
  248.     Serial.print("H2: ");
  249.     Serial.println(humidity2);
  250.     #endif
  251.   } else {
  252.     // Increase no update counter if the humidity stayed the same
  253.     nNoUpdatesHum2++;
  254.   }
  255.  
  256.  
  257.   // Sleep for a while to save energy
  258.   sleep(UPDATE_INTERVAL);
  259.  
  260. }