Advertisement
chrisdaloa

Mysensors Gateway + dht22

Nov 11th, 2018
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.42 KB | None | 0 0
  1. /**
  2.  * The MySensors Arduino library handles the wireless radio link and protocol
  3.  * between your home built sensors/actuators and HA controller of choice.
  4.  * The sensors forms a self healing radio network with optional repeaters. Each
  5.  * repeater and gateway builds a routing tables in EEPROM which keeps track of the
  6.  * network topology allowing messages to be routed to nodes.
  7.  *
  8.  * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
  9.  * Copyright (C) 2013-2015 Sensnology AB
  10.  * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
  11.  *
  12.  * Documentation: http://www.mysensors.org
  13.  * Support Forum: http://forum.mysensors.org
  14.  *
  15.  * This program is free software; you can redistribute it and/or
  16.  * modify it under the terms of the GNU General Public License
  17.  * version 2 as published by the Free Software Foundation.
  18.  *
  19.  *******************************
  20.  *
  21.  * REVISION HISTORY
  22.  * Version 1.0 - Henrik EKblad
  23.  * Contribution by a-lurker and Anticimex,
  24.  * Contribution by Norbert Truchsess <norbert.truchsess@t-online.de>
  25.  * Contribution by Ivo Pullens (ESP8266 support)
  26.  *
  27.  * DESCRIPTION
  28.  * The EthernetGateway sends data received from sensors to the WiFi link.
  29.  * The gateway also accepts input on ethernet interface, which is then sent out to the radio network.
  30.  *
  31.  * VERA CONFIGURATION:
  32.  * Enter "ip-number:port" in the ip-field of the Arduino GW device. This will temporarily override any serial configuration for the Vera plugin.
  33.  * E.g. If you want to use the default values in this sketch enter: 192.168.178.66:5003
  34.  *
  35.  * LED purposes:
  36.  * - To use the feature, uncomment any of the MY_DEFAULT_xx_LED_PINs in your sketch, only the LEDs that is defined is used.
  37.  * - RX (green) - blink fast on radio message received. In inclusion mode will blink fast only on presentation received
  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 receive crc error
  40.  *
  41.  * See https://www.mysensors.org/build/connect_radio for wiring instructions.
  42.  *
  43.  * If you are using a "barebone" ESP8266, see
  44.  * https://www.mysensors.org/build/esp8266_gateway#wiring-for-barebone-esp8266
  45.  *
  46.  * Inclusion mode button:
  47.  * - Connect GPIO5 (=D1) via switch to GND ('inclusion switch')
  48.  *
  49.  * Hardware SHA204 signing is currently not supported!
  50.  *
  51.  * Make sure to fill in your ssid and WiFi password below for ssid & pass.
  52.  */
  53.  
  54. // Enable debug prints to serial monitor
  55. #define MY_DEBUG
  56.  
  57. // Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
  58. #define MY_BAUD_RATE 9600
  59.  
  60. // Enables and select radio type (if attached)
  61. #define MY_RADIO_NRF24
  62. //#define MY_RADIO_RFM69
  63. //#define MY_RADIO_RFM95
  64.  
  65. #define MY_GATEWAY_ESP8266
  66.  
  67. #define MY_WIFI_SSID "WIFI1612"
  68. #define MY_WIFI_PASSWORD "********"
  69.  
  70. // Enable UDP communication
  71. //#define MY_USE_UDP  // If using UDP you need to set MY_CONTROLLER_IP_ADDRESS below
  72.  
  73. // Set the hostname for the WiFi Client. This is the hostname
  74. // it will pass to the DHCP server if not static.
  75. #define MY_HOSTNAME "sensor-gateway"
  76.  
  77. // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
  78. //#define MY_IP_ADDRESS 192,168,178,87
  79.  
  80. // If using static ip you can define Gateway and Subnet address as well
  81. //#define MY_IP_GATEWAY_ADDRESS 192,168,178,1
  82. //#define MY_IP_SUBNET_ADDRESS 255,255,255,0
  83.  
  84. // The port to keep open on node server mode
  85. #define MY_PORT 5003
  86.  
  87. // How many clients should be able to connect to this gateway (default 1)
  88. #define MY_GATEWAY_MAX_CLIENTS 5
  89.  
  90. // Controller ip address. Enables client mode (default is "server" mode).
  91. // Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
  92. //#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 68
  93.  
  94. // Enable inclusion mode
  95. //#define MY_INCLUSION_MODE_FEATURE
  96.  
  97. // Enable Inclusion mode button on gateway
  98. //#define MY_INCLUSION_BUTTON_FEATURE
  99. // Set inclusion mode duration (in seconds)
  100. //#define MY_INCLUSION_MODE_DURATION 60
  101. // Digital pin used for inclusion mode button
  102. //#define MY_INCLUSION_MODE_BUTTON_PIN D1
  103.  
  104. // Set blinking period
  105. //#define MY_DEFAULT_LED_BLINK_PERIOD 300
  106.  
  107. // Flash leds on rx/tx/err
  108. // Led pins used if blinking feature is enabled above
  109. //#define MY_DEFAULT_ERR_LED_PIN 16  // Error led pin
  110. //#define MY_DEFAULT_RX_LED_PIN  16  // Receive led pin
  111. //#define MY_DEFAULT_TX_LED_PIN  16  // the PCB, on board LED
  112.  
  113. #if defined(MY_USE_UDP)
  114. #include <WiFiUdp.h>
  115. #endif
  116.  
  117. #include <SPI.h>
  118. #include <ESP8266WiFi.h>
  119. #include <MySensors.h>
  120.  
  121.  
  122. // BLOCCO DHT //
  123. #include <DHT.h>
  124. // Set this to the pin you connected the DHT's data pin to
  125. #define DHT_DATA_PIN 3
  126.  
  127. // Set this offset if the sensor has a permanent small offset to the real temperatures.
  128. // In Celsius degrees (as measured by the device)
  129. #define SENSOR_TEMP_OFFSET 0
  130.  
  131. // Sleep time between sensor updates (in milliseconds)
  132. // Must be >1000ms for DHT22 and >2000ms for DHT11
  133. static const uint64_t UPDATE_INTERVAL = 60000;
  134.  
  135. // Force sending an update of the temperature after n sensor reads, so a controller showing the
  136. // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
  137. // the value didn't change since;
  138. // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
  139. static const uint8_t FORCE_UPDATE_N_READS = 10;
  140.  
  141. #define CHILD_ID_HUM 0
  142. #define CHILD_ID_TEMP 1
  143.  
  144. float lastTemp;
  145. float lastHum;
  146. uint8_t nNoUpdatesTemp;
  147. uint8_t nNoUpdatesHum;
  148. bool metric = true;
  149.  
  150. MyMessage msgHum(CHILD_ID_HUM, V_HUM);
  151. MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
  152. DHT dht;
  153.  
  154. // FINE BLOCCO DHT
  155.  
  156. void setup()
  157. {
  158.     // Setup locally attached sensors
  159.   // *** BLOCCO DHT ***
  160.   dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
  161.   if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
  162.     Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
  163.    
  164.   }
  165.   // Sleep for the time of the minimum sampling period to give the sensor time to power up
  166.   // (otherwise, timeout errors might occure for the first reading)
  167.   sleep(dht.getMinimumSamplingPeriod());
  168.   // *** FINE BLOCCO DHT ***
  169.  
  170. }
  171.  
  172. void presentation()
  173. {
  174.     // Present locally attached sensors here
  175.   // *** BLOCCO DHT **
  176.   // Send the sketch version information to the gateway DHT
  177.   sendSketchInfo("TemperatureAndHumidity", "1.1");
  178.  
  179.  
  180.   // Register all sensors to gw (they will be created as child devices)
  181.   present(CHILD_ID_HUM, S_HUM);
  182.   present(CHILD_ID_TEMP, S_TEMP);
  183.  
  184.   metric = getControllerConfig().isMetric;
  185.  
  186.   // *** FINE BLOCCO DHT **
  187. }
  188.  
  189. void loop()
  190. {
  191.  
  192.     // *** BLOCCO DHT ***
  193.     // Send locally attached sensors data here
  194.  
  195.     // Force reading sensor, so it works also after sleep()
  196.   dht.readSensor(true);
  197.  
  198.   // Get temperature from DHT library
  199.   float temperature = dht.getTemperature();
  200.   if (isnan(temperature)) {
  201.    // Serial.println("Failed reading temperature from DHT!");
  202.       Serial.println(dht.getTemperature());
  203.   } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
  204.     // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
  205.     lastTemp = temperature;
  206.  
  207.     // apply the offset before converting to something different than Celsius degrees
  208.     temperature += SENSOR_TEMP_OFFSET;
  209.  
  210.     if (!metric) {
  211.       temperature = dht.toFahrenheit(temperature);
  212.     }
  213.     // Reset no updates counter
  214.     nNoUpdatesTemp = 0;
  215.     send(msgTemp.set(temperature, 1));
  216.  
  217.     #ifdef MY_DEBUG
  218.     Serial.print("T: ");
  219.     Serial.println(temperature);
  220.     #endif
  221.   } else {
  222.     // Increase no update counter if the temperature stayed the same
  223.     nNoUpdatesTemp++;
  224.   }
  225.  
  226.   // Get humidity from DHT library
  227.   float humidity = dht.getHumidity();
  228.   if (isnan(humidity)) {
  229.     Serial.println("Failed reading humidity from DHT");
  230.   } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
  231.     // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
  232.     lastHum = humidity;
  233.     // Reset no updates counter
  234.     nNoUpdatesHum = 0;
  235.     send(msgHum.set(humidity, 1));
  236.  
  237.     #ifdef MY_DEBUG
  238.     Serial.print("H: ");
  239.     Serial.println(humidity);
  240.     #endif
  241.   } else {
  242.     // Increase no update counter if the humidity stayed the same
  243.     nNoUpdatesHum++;
  244.   }
  245.  
  246.   // *** FINE BLOCCO DHT ***
  247.  
  248.   // Sleep for a while to save energy
  249.   sleep(UPDATE_INTERVAL);
  250.  
  251.  
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement