Guest User

Untitled

a guest
Aug 21st, 2016
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.16 KB | None | 0 0
  1. #define MY_DEBUG  
  2. #define MY_GATEWAY_W5100
  3.  
  4. #if !defined(MY_W5100_SPI_EN) && !defined(ARDUINO_ARCH_SAMD)
  5.   #define MY_SOFTSPI
  6.   #define MY_SOFT_SPI_SCK_PIN 14
  7.   #define MY_SOFT_SPI_MISO_PIN 16
  8.   #define MY_SOFT_SPI_MOSI_PIN 15
  9. #endif  
  10.  
  11. #ifndef MY_RF24_CE_PIN
  12.   #define MY_RF24_CE_PIN 5
  13. #endif
  14. #ifndef MY_RF24_CS_PIN
  15.   #define MY_RF24_CS_PIN 6
  16. #endif
  17.  
  18. #define MY_IP_ADDRESS 192,168,0,200  
  19. #define MY_PORT 5003      
  20. #define MY_MAC_ADDRESS 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  21. #define MY_LEDS_BLINKING_FEATURE
  22. #define MY_DEFAULT_LED_BLINK_PERIOD 300
  23. #define MY_INCLUSION_MODE_FEATURE
  24. #define MY_INCLUSION_BUTTON_FEATURE
  25. #define MY_INCLUSION_MODE_DURATION 60
  26. #if defined(MY_USE_UDP)
  27.   #include <EthernetUdp.h>
  28. #endif
  29. #include <SPI.h>
  30. #include <Ethernet.h>
  31. #include <MySensors.h>
  32. #include <DHT.h>
  33. #include <Bounce2.h>
  34.  
  35. #define RELAY_1  5
  36. #define RELAY_2  6// Arduino Digital I/O pin number for first relay (second on pin+1 etc)
  37. #define NUMBER_OF_RELAYS 2 // Total number of attached relays
  38. #define RELAY_ON 0  // GPIO value to write to turn on attached relay
  39. #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
  40. #define BUTTON_PIN  7
  41.  
  42. Bounce debouncer = Bounce();
  43. int oldValue=-1;
  44.  
  45. #define DHT_DATA_PIN 2
  46. #define SENSOR_TEMP_OFFSET 0
  47.  
  48. // Sleep time between sensor updates (in milliseconds)
  49. // Must be >1000ms for DHT22 and >2000ms for DHT11
  50. static const uint64_t UPDATE_INTERVAL = 100;
  51.  
  52. // Force sending an update of the temperature after n sensor reads, so a controller showing the
  53. // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
  54. // the value didn't change since;
  55. // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
  56. static const uint8_t FORCE_UPDATE_N_READS = 10;
  57.  
  58. #define CHILD_ID_HUM 0
  59. #define CHILD_ID_TEMP 1
  60. #define CHILD_ID 3
  61.  
  62. float lastTemp;
  63. float lastHum;
  64. uint8_t nNoUpdatesTemp;
  65. uint8_t nNoUpdatesHum;
  66. bool metric = true;
  67.  
  68. MyMessage msgHum(CHILD_ID_HUM, V_HUM);
  69. MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
  70. DHT dht;
  71.  
  72. // Change to V_LIGHT if you use S_LIGHT in presentation below
  73. MyMessage msg(CHILD_ID,V_TRIPPED);
  74.  
  75. void before() {
  76.   for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
  77.     // Then set relay pins in output mode
  78.     pinMode(pin, OUTPUT);  
  79.     // Set relay to last known state (using eeprom storage)
  80.     digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
  81.   }
  82. }
  83. void presentation()  
  84. {
  85.   // Send the sketch version information to the gateway
  86.   sendSketchInfo("S-tmp", "1.1");
  87.  
  88.   // Register all sensors to gw (they will be created as child devices)
  89.   present(CHILD_ID_HUM, S_HUM);
  90.   present(CHILD_ID_TEMP, S_TEMP);
  91. present(CHILD_ID, S_DOOR);
  92.     for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
  93.     // Register all sensors to gw (they will be created as child devices)
  94.     present(sensor, S_LIGHT);
  95.   }
  96.  
  97.   metric = getConfig().isMetric;
  98.  
  99.  
  100.   // Send the sketch version information to the gateway and Controller
  101.  // sendSketchInfo("Relay", "1.0");
  102.  
  103. }
  104.  
  105. void setup()
  106. {
  107.   dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
  108.   if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
  109.     Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
  110.   }
  111.   // Sleep for the time of the minimum sampling period to give the sensor time to power up
  112.   // (otherwise, timeout errors might occure for the first reading)
  113.   sleep(dht.getMinimumSamplingPeriod());
  114.     pinMode(BUTTON_PIN,INPUT);
  115.   // Activate internal pull-up
  116.   digitalWrite(BUTTON_PIN,HIGH);
  117.  
  118.   // After setting up the button, setup debouncer
  119.   debouncer.attach(BUTTON_PIN);
  120.   debouncer.interval(5);
  121. }
  122.  
  123.  
  124. void loop()      
  125. {  
  126.  
  127.   debouncer.update();
  128.   // Get the update value
  129.   int value = debouncer.read();
  130.  
  131.   if (value != oldValue) {
  132.      // Send in the new value
  133.      send(msg.set(value==HIGH ? 1 : 0));
  134.      //digitalWrite(5, value);
  135.      oldValue = value;
  136.      
  137.   }
  138.  
  139.  
  140.   // Force reading sensor, so it works also after sleep()
  141.   dht.readSensor(true);
  142.  
  143.   // Get temperature from DHT library
  144.   float temperature = dht.getTemperature();
  145.   if (isnan(temperature)) {
  146.     Serial.println("Failed reading temperature from DHT!");
  147.   } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
  148.     // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
  149.     lastTemp = temperature;
  150.     if (!metric) {
  151.       temperature = dht.toFahrenheit(temperature);
  152.     }
  153.     // Reset no updates counter
  154.     nNoUpdatesTemp = 0;
  155.     temperature += SENSOR_TEMP_OFFSET;
  156.     send(msgTemp.set(temperature, 1));
  157.  
  158.     #ifdef MY_DEBUG
  159.     Serial.print("T: ");
  160.     Serial.println(temperature);
  161.     #endif
  162.   } else {
  163.     // Increase no update counter if the temperature stayed the same
  164.     nNoUpdatesTemp++;
  165.   }
  166.  
  167.   // Get humidity from DHT library
  168.   float humidity = dht.getHumidity();
  169.   if (isnan(humidity)) {
  170.     Serial.println("Failed reading humidity from DHT");
  171.   } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
  172.     // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
  173.     lastHum = humidity;
  174.     // Reset no updates counter
  175.     nNoUpdatesHum = 0;
  176.     send(msgHum.set(humidity, 1));
  177.    
  178.     #ifdef MY_DEBUG
  179.     Serial.print("H: ");
  180.     Serial.println(humidity);
  181.     #endif
  182.   } else {
  183.     // Increase no update counter if the humidity stayed the same
  184.     nNoUpdatesHum++;
  185.   }
  186.  
  187.   // Sleep for a while to save energy
  188.   sleep(UPDATE_INTERVAL);
  189. }
  190.   void receive(const MyMessage &message) {
  191.   // We only expect one type of message from controller. But we better check anyway.
  192.   if (message.type==V_LIGHT) {
  193.      // Change relay state
  194.      digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
  195.      // Store state in eeprom
  196.      saveState(message.sensor, message.getBool());
  197.      // Write some debug info
  198.      Serial.print("Incoming change for sensor:");
  199.      Serial.print(message.sensor);
  200.      Serial.print(", New status: ");
  201.      Serial.println(message.getBool());
  202.    }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment