Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The MySensors Arduino library handles the wireless radio link and protocol
- * between your home built sensors/actuators and HA controller of choice.
- * The sensors forms a self healing radio network with optional repeaters. Each
- * repeater and gateway builds a routing tables in EEPROM which keeps track of the
- * network topology allowing messages to be routed to nodes.
- *
- * Created by Henrik Ekblad <[email protected]>
- * Copyright (C) 2013-2015 Sensnology AB
- * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
- *
- * Documentation: http://www.mysensors.org
- * Support Forum: http://forum.mysensors.org
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * version 2 as published by the Free Software Foundation.
- *
- *******************************
- *
- * REVISION HISTORY
- * Version 1.0 - Henrik EKblad
- * Contribution by a-lurker and Anticimex,
- * Contribution by Norbert Truchsess <[email protected]>
- * Contribution by Tomas Hozza <[email protected]>
- *
- *
- * DESCRIPTION
- * The EthernetGateway sends data received from sensors to the ethernet link.
- * The gateway also accepts input on ethernet interface, which is then sent out to the radio network.
- *
- * The GW code is designed for Arduino 328p / 16MHz. ATmega168 does not have enough memory to run this program.
- *
- * LED purposes:
- * - To use the feature, uncomment WITH_LEDS_BLINKING in MyConfig.h
- * - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
- * - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
- * - ERR (red) - fast blink on error during transmission error or recieve crc error
- *
- * See http://www.mysensors.org/build/ethernet_gateway for wiring instructions.
- *
- */
- // Enable debug prints to serial monitor
- #define MY_DEBUG
- // Enable and select radio type attached
- //#define MY_RADIO_NRF24
- //#define MY_RADIO_RFM69
- // Enable gateway ethernet module type
- #define MY_GATEWAY_W5100
- // W5100 Ethernet module SPI enable (optional if using a shield/module that manages SPI_EN signal)
- //#define MY_W5100_SPI_EN 4
- // Enable Soft SPI for NRF radio (note different radio wiring is required)
- // The W5100 ethernet module seems to have a hard time co-operate with
- // radio on the same spi bus.
- #if !defined(MY_W5100_SPI_EN) && !defined(ARDUINO_ARCH_SAMD)
- #define MY_SOFTSPI
- #define MY_SOFT_SPI_SCK_PIN 14
- #define MY_SOFT_SPI_MISO_PIN 16
- #define MY_SOFT_SPI_MOSI_PIN 15
- #endif
- // When W5100 is connected we have to move CE/CSN pins for NRF radio
- #ifndef MY_RF24_CE_PIN
- #define MY_RF24_CE_PIN 5
- #endif
- #ifndef MY_RF24_CS_PIN
- #define MY_RF24_CS_PIN 6
- #endif
- // Enable to UDP
- //#define MY_USE_UDP
- #define MY_IP_ADDRESS 192,168,0,200 // If this is disabled, DHCP is used to retrieve address
- // Renewal period if using DHCP
- //#define MY_IP_RENEWAL_INTERVAL 60000
- // The port to keep open on node server mode / or port to contact in client mode
- #define MY_PORT 5003
- // Controller ip address. Enables client mode (default is "server" mode).
- // Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
- //#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 254
- // The MAC address can be anything you want but should be unique on your network.
- // Newer boards have a MAC address printed on the underside of the PCB, which you can (optionally) use.
- // Note that most of the Ardunio examples use "DEAD BEEF FEED" for the MAC address.
- #define MY_MAC_ADDRESS 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
- // Flash leds on rx/tx/err
- #define MY_LEDS_BLINKING_FEATURE
- // Set blinking period
- #define MY_DEFAULT_LED_BLINK_PERIOD 300
- // Enable inclusion mode
- #define MY_INCLUSION_MODE_FEATURE
- // Enable Inclusion mode button on gateway
- #define MY_INCLUSION_BUTTON_FEATURE
- // Set inclusion mode duration (in seconds)
- #define MY_INCLUSION_MODE_DURATION 60
- // Digital pin used for inclusion mode button
- #define MY_INCLUSION_MODE_BUTTON_PIN 3
- // Uncomment to override default HW configurations
- //#define MY_DEFAULT_ERR_LED_PIN 7 // Error led pin
- //#define MY_DEFAULT_RX_LED_PIN 8 // Receive led pin
- //#define MY_DEFAULT_TX_LED_PIN 9 // the PCB, on board LED
- #include <SPI.h>
- #if defined(MY_USE_UDP)
- #include <EthernetUdp.h>
- #endif
- #include <Ethernet.h>
- #include <MySensors.h>
- #include <DHT.h>
- // Enable debug prints
- #define MY_DEBUG
- #define MY_GATEWAY_SERIAL
- // Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
- #if F_CPU == 8000000L
- #define MY_BAUD_RATE 38400
- #endif
- // Flash leds on rx/tx/err
- #define MY_LEDS_BLINKING_FEATURE
- // Set blinking period
- #define MY_DEFAULT_LED_BLINK_PERIOD 300
- // Inverses the behavior of leds
- //#define MY_WITH_LEDS_BLINKING_INVERSE
- // Enable inclusion mode
- #define MY_INCLUSION_MODE_FEATURE
- // Enable Inclusion mode button on gateway
- #define MY_INCLUSION_BUTTON_FEATURE
- // Inverses behavior of inclusion button (if using external pullup)
- //#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP
- // Set inclusion mode duration (in seconds)
- #define MY_INCLUSION_MODE_DURATION 60
- // Digital pin used for inclusion mode button
- #define MY_INCLUSION_MODE_BUTTON_PIN 3
- // Enable and select radio type attached
- //#define MY_RADIO_NRF24
- //#define MY_RADIO_RFM69
- #include <SPI.h>
- #include <MySensors.h>
- // Set this to the pin you connected the DHT's data pin to
- #define DHT_DATA_PIN 2
- // Set this offset if the sensor has a permanent small offset to the real temperatures
- #define SENSOR_TEMP_OFFSET 0
- // Sleep time between sensor updates (in milliseconds)
- // Must be >1000ms for DHT22 and >2000ms for DHT11
- static const uint64_t UPDATE_INTERVAL = 20000;
- // Force sending an update of the temperature after n sensor reads, so a controller showing the
- // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
- // the value didn't change since;
- // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
- static const uint8_t FORCE_UPDATE_N_READS = 10;
- #define CHILD_ID_HUM 0
- #define CHILD_ID_TEMP 1
- float lastTemp;
- float lastHum;
- uint8_t nNoUpdatesTemp;
- uint8_t nNoUpdatesHum;
- bool metric = true;
- MyMessage msgHum(CHILD_ID_HUM, V_HUM);
- MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
- DHT dht;
- void presentation()
- {
- // Send the sketch version information to the gateway
- sendSketchInfo("TemperatureAndHumidity", "1.1");
- // Register all sensors to gw (they will be created as child devices)
- present(CHILD_ID_HUM, S_HUM);
- present(CHILD_ID_TEMP, S_TEMP);
- metric = getConfig().isMetric;
- }
- void setup()
- {
- dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
- if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
- Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
- }
- // Sleep for the time of the minimum sampling period to give the sensor time to power up
- // (otherwise, timeout errors might occure for the first reading)
- sleep(dht.getMinimumSamplingPeriod());
- }
- void loop()
- {
- // Force reading sensor, so it works also after sleep()
- dht.readSensor(true);
- // Get temperature from DHT library
- float temperature = dht.getTemperature();
- if (isnan(temperature)) {
- Serial.println("Failed reading temperature from DHT!");
- } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
- // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
- lastTemp = temperature;
- if (!metric) {
- temperature = dht.toFahrenheit(temperature);
- }
- // Reset no updates counter
- nNoUpdatesTemp = 0;
- temperature += SENSOR_TEMP_OFFSET;
- send(msgTemp.set(temperature, 1));
- #ifdef MY_DEBUG
- Serial.print("T: ");
- Serial.println(temperature);
- #endif
- } else {
- // Increase no update counter if the temperature stayed the same
- nNoUpdatesTemp++;
- }
- // Get humidity from DHT library
- float humidity = dht.getHumidity();
- if (isnan(humidity)) {
- Serial.println("Failed reading humidity from DHT");
- } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
- // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
- lastHum = humidity;
- // Reset no updates counter
- nNoUpdatesHum = 0;
- send(msgHum.set(humidity, 1));
- #ifdef MY_DEBUG
- Serial.print("H: ");
- Serial.println(humidity);
- #endif
- } else {
- // Increase no update counter if the humidity stayed the same
- nNoUpdatesHum++;
- }
- // Sleep for a while to save energy
- sleep(UPDATE_INTERVAL);
- }
Advertisement
Add Comment
Please, Sign In to add comment