Guest User

Untitled

a guest
Aug 18th, 2016
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.82 KB | None | 0 0
  1.  
  2. /**
  3.  * The MySensors Arduino library handles the wireless radio link and protocol
  4.  * between your home built sensors/actuators and HA controller of choice.
  5.  * The sensors forms a self healing radio network with optional repeaters. Each
  6.  * repeater and gateway builds a routing tables in EEPROM which keeps track of the
  7.  * network topology allowing messages to be routed to nodes.
  8.  *
  9.  * Created by Henrik Ekblad <[email protected]>
  10.  * Copyright (C) 2013-2015 Sensnology AB
  11.  * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
  12.  *
  13.  * Documentation: http://www.mysensors.org
  14.  * Support Forum: http://forum.mysensors.org
  15.  *
  16.  * This program is free software; you can redistribute it and/or
  17.  * modify it under the terms of the GNU General Public License
  18.  * version 2 as published by the Free Software Foundation.
  19.  *
  20.  *******************************
  21.  *
  22.  * REVISION HISTORY
  23.  * Version 1.0 - Henrik EKblad
  24.  * Contribution by a-lurker and Anticimex,
  25.  * Contribution by Norbert Truchsess <[email protected]>
  26.  * Contribution by Tomas Hozza <[email protected]>
  27.  *
  28.  *
  29.  * DESCRIPTION
  30.  * The EthernetGateway sends data received from sensors to the ethernet link.
  31.  * The gateway also accepts input on ethernet interface, which is then sent out to the radio network.
  32.  *
  33.  * The GW code is designed for Arduino 328p / 16MHz.  ATmega168 does not have enough memory to run this program.
  34.  *
  35.  * LED purposes:
  36.  * - To use the feature, uncomment WITH_LEDS_BLINKING in MyConfig.h
  37.  * - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
  38.  * - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
  39.  * - ERR (red) - fast blink on error during transmission error or recieve crc error
  40.  *
  41.  * See http://www.mysensors.org/build/ethernet_gateway for wiring instructions.
  42.  *
  43.  */
  44.  
  45. // Enable debug prints to serial monitor
  46. #define MY_DEBUG
  47.  
  48. // Enable and select radio type attached
  49. //#define MY_RADIO_NRF24
  50. //#define MY_RADIO_RFM69
  51.  
  52. // Enable gateway ethernet module type
  53. #define MY_GATEWAY_W5100
  54.  
  55. // W5100 Ethernet module SPI enable (optional if using a shield/module that manages SPI_EN signal)
  56. //#define MY_W5100_SPI_EN 4  
  57.  
  58. // Enable Soft SPI for NRF radio (note different radio wiring is required)
  59. // The W5100 ethernet module seems to have a hard time co-operate with
  60. // radio on the same spi bus.
  61. #if !defined(MY_W5100_SPI_EN) && !defined(ARDUINO_ARCH_SAMD)
  62.   #define MY_SOFTSPI
  63.   #define MY_SOFT_SPI_SCK_PIN 14
  64.   #define MY_SOFT_SPI_MISO_PIN 16
  65.   #define MY_SOFT_SPI_MOSI_PIN 15
  66. #endif  
  67.  
  68. // When W5100 is connected we have to move CE/CSN pins for NRF radio
  69. #ifndef MY_RF24_CE_PIN
  70.   #define MY_RF24_CE_PIN 5
  71. #endif
  72. #ifndef MY_RF24_CS_PIN
  73.   #define MY_RF24_CS_PIN 6
  74. #endif
  75.  
  76. // Enable to UDP          
  77. //#define MY_USE_UDP
  78.  
  79. #define MY_IP_ADDRESS 192,168,0,200   // If this is disabled, DHCP is used to retrieve address
  80. // Renewal period if using DHCP
  81. //#define MY_IP_RENEWAL_INTERVAL 60000
  82. // The port to keep open on node server mode / or port to contact in client mode
  83. #define MY_PORT 5003      
  84.  
  85. // Controller ip address. Enables client mode (default is "server" mode).
  86. // Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
  87. //#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 254  
  88.  
  89. // The MAC address can be anything you want but should be unique on your network.
  90. // Newer boards have a MAC address printed on the underside of the PCB, which you can (optionally) use.
  91. // Note that most of the Ardunio examples use  "DEAD BEEF FEED" for the MAC address.
  92. #define MY_MAC_ADDRESS 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  93.  
  94. // Flash leds on rx/tx/err
  95. #define MY_LEDS_BLINKING_FEATURE
  96. // Set blinking period
  97. #define MY_DEFAULT_LED_BLINK_PERIOD 300
  98.  
  99. // Enable inclusion mode
  100. #define MY_INCLUSION_MODE_FEATURE
  101. // Enable Inclusion mode button on gateway
  102. #define MY_INCLUSION_BUTTON_FEATURE
  103. // Set inclusion mode duration (in seconds)
  104. #define MY_INCLUSION_MODE_DURATION 60
  105. // Digital pin used for inclusion mode button
  106. #define MY_INCLUSION_MODE_BUTTON_PIN  3
  107.  
  108. // Uncomment to override default HW configurations
  109. //#define MY_DEFAULT_ERR_LED_PIN 7  // Error led pin
  110. //#define MY_DEFAULT_RX_LED_PIN  8  // Receive led pin
  111. //#define MY_DEFAULT_TX_LED_PIN  9  // the PCB, on board LED
  112.  
  113. #include <SPI.h>
  114.  
  115. #if defined(MY_USE_UDP)
  116.   #include <EthernetUdp.h>
  117. #endif
  118. #include <Ethernet.h>
  119. #include <MySensors.h>
  120. #include <DHT.h>
  121.  
  122.  
  123. // Enable debug prints
  124. #define MY_DEBUG
  125. #define MY_GATEWAY_SERIAL
  126. // Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
  127. #if F_CPU == 8000000L
  128. #define MY_BAUD_RATE 38400
  129. #endif
  130.  
  131. // Flash leds on rx/tx/err
  132. #define MY_LEDS_BLINKING_FEATURE
  133. // Set blinking period
  134. #define MY_DEFAULT_LED_BLINK_PERIOD 300
  135.  
  136. // Inverses the behavior of leds
  137. //#define MY_WITH_LEDS_BLINKING_INVERSE
  138.  
  139. // Enable inclusion mode
  140. #define MY_INCLUSION_MODE_FEATURE
  141. // Enable Inclusion mode button on gateway
  142. #define MY_INCLUSION_BUTTON_FEATURE
  143.  
  144. // Inverses behavior of inclusion button (if using external pullup)
  145. //#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP
  146.  
  147. // Set inclusion mode duration (in seconds)
  148. #define MY_INCLUSION_MODE_DURATION 60
  149. // Digital pin used for inclusion mode button
  150. #define MY_INCLUSION_MODE_BUTTON_PIN  3
  151. // Enable and select radio type attached
  152. //#define MY_RADIO_NRF24
  153. //#define MY_RADIO_RFM69
  154.  
  155. #include <SPI.h>
  156. #include <MySensors.h>
  157.  
  158.  
  159. // Set this to the pin you connected the DHT's data pin to
  160. #define DHT_DATA_PIN 2
  161.  
  162. // Set this offset if the sensor has a permanent small offset to the real temperatures
  163. #define SENSOR_TEMP_OFFSET 0
  164.  
  165. // Sleep time between sensor updates (in milliseconds)
  166. // Must be >1000ms for DHT22 and >2000ms for DHT11
  167. static const uint64_t UPDATE_INTERVAL = 20000;
  168.  
  169. // Force sending an update of the temperature after n sensor reads, so a controller showing the
  170. // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
  171. // the value didn't change since;
  172. // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
  173. static const uint8_t FORCE_UPDATE_N_READS = 10;
  174.  
  175. #define CHILD_ID_HUM 0
  176. #define CHILD_ID_TEMP 1
  177.  
  178. float lastTemp;
  179. float lastHum;
  180. uint8_t nNoUpdatesTemp;
  181. uint8_t nNoUpdatesHum;
  182. bool metric = true;
  183.  
  184. MyMessage msgHum(CHILD_ID_HUM, V_HUM);
  185. MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
  186. DHT dht;
  187.  
  188.  
  189. void presentation()  
  190. {
  191.   // Send the sketch version information to the gateway
  192.   sendSketchInfo("TemperatureAndHumidity", "1.1");
  193.  
  194.   // Register all sensors to gw (they will be created as child devices)
  195.   present(CHILD_ID_HUM, S_HUM);
  196.   present(CHILD_ID_TEMP, S_TEMP);
  197.  
  198.   metric = getConfig().isMetric;
  199. }
  200.  
  201.  
  202. void setup()
  203. {
  204.   dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
  205.   if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
  206.     Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
  207.   }
  208.   // Sleep for the time of the minimum sampling period to give the sensor time to power up
  209.   // (otherwise, timeout errors might occure for the first reading)
  210.   sleep(dht.getMinimumSamplingPeriod());
  211. }
  212.  
  213.  
  214. void loop()      
  215. {  
  216.   // Force reading sensor, so it works also after sleep()
  217.   dht.readSensor(true);
  218.  
  219.   // Get temperature from DHT library
  220.   float temperature = dht.getTemperature();
  221.   if (isnan(temperature)) {
  222.     Serial.println("Failed reading temperature from DHT!");
  223.   } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
  224.     // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
  225.     lastTemp = temperature;
  226.     if (!metric) {
  227.       temperature = dht.toFahrenheit(temperature);
  228.     }
  229.     // Reset no updates counter
  230.     nNoUpdatesTemp = 0;
  231.     temperature += SENSOR_TEMP_OFFSET;
  232.     send(msgTemp.set(temperature, 1));
  233.  
  234.     #ifdef MY_DEBUG
  235.     Serial.print("T: ");
  236.     Serial.println(temperature);
  237.     #endif
  238.   } else {
  239.     // Increase no update counter if the temperature stayed the same
  240.     nNoUpdatesTemp++;
  241.   }
  242.  
  243.   // Get humidity from DHT library
  244.   float humidity = dht.getHumidity();
  245.   if (isnan(humidity)) {
  246.     Serial.println("Failed reading humidity from DHT");
  247.   } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
  248.     // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
  249.     lastHum = humidity;
  250.     // Reset no updates counter
  251.     nNoUpdatesHum = 0;
  252.     send(msgHum.set(humidity, 1));
  253.    
  254.     #ifdef MY_DEBUG
  255.     Serial.print("H: ");
  256.     Serial.println(humidity);
  257.     #endif
  258.   } else {
  259.     // Increase no update counter if the humidity stayed the same
  260.     nNoUpdatesHum++;
  261.   }
  262.  
  263.   // Sleep for a while to save energy
  264.   sleep(UPDATE_INTERVAL);
  265. }
Advertisement
Add Comment
Please, Sign In to add comment