Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define MY_DEBUG
- #define MY_GATEWAY_W5100
- #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
- #ifndef MY_RF24_CE_PIN
- #define MY_RF24_CE_PIN 5
- #endif
- #ifndef MY_RF24_CS_PIN
- #define MY_RF24_CS_PIN 6
- #endif
- #define MY_IP_ADDRESS 192,168,0,200
- #define MY_PORT 5003
- #define MY_MAC_ADDRESS 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
- #define MY_LEDS_BLINKING_FEATURE
- #define MY_DEFAULT_LED_BLINK_PERIOD 300
- #define MY_INCLUSION_MODE_FEATURE
- #define MY_INCLUSION_BUTTON_FEATURE
- #define MY_INCLUSION_MODE_DURATION 60
- #if defined(MY_USE_UDP)
- #include <EthernetUdp.h>
- #endif
- #include <SPI.h>
- #include <Ethernet.h>
- #include <MySensors.h>
- #include <DHT.h>
- #include <Bounce2.h>
- #define RELAY_1 5
- #define RELAY_2 6// Arduino Digital I/O pin number for first relay (second on pin+1 etc)
- #define NUMBER_OF_RELAYS 2 // Total number of attached relays
- #define RELAY_ON 0 // GPIO value to write to turn on attached relay
- #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
- #define BUTTON_PIN 7
- Bounce debouncer = Bounce();
- int oldValue=-1;
- #define DHT_DATA_PIN 2
- #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 = 100;
- // 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
- #define CHILD_ID 3
- 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;
- // Change to V_LIGHT if you use S_LIGHT in presentation below
- MyMessage msg(CHILD_ID,V_TRIPPED);
- void before() {
- for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
- // Then set relay pins in output mode
- pinMode(pin, OUTPUT);
- // Set relay to last known state (using eeprom storage)
- digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
- }
- }
- void presentation()
- {
- // Send the sketch version information to the gateway
- sendSketchInfo("S-tmp", "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);
- present(CHILD_ID, S_DOOR);
- for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
- // Register all sensors to gw (they will be created as child devices)
- present(sensor, S_LIGHT);
- }
- metric = getConfig().isMetric;
- // Send the sketch version information to the gateway and Controller
- // sendSketchInfo("Relay", "1.0");
- }
- 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());
- pinMode(BUTTON_PIN,INPUT);
- // Activate internal pull-up
- digitalWrite(BUTTON_PIN,HIGH);
- // After setting up the button, setup debouncer
- debouncer.attach(BUTTON_PIN);
- debouncer.interval(5);
- }
- void loop()
- {
- debouncer.update();
- // Get the update value
- int value = debouncer.read();
- if (value != oldValue) {
- // Send in the new value
- send(msg.set(value==HIGH ? 1 : 0));
- //digitalWrite(5, value);
- oldValue = value;
- }
- // 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);
- }
- void receive(const MyMessage &message) {
- // We only expect one type of message from controller. But we better check anyway.
- if (message.type==V_LIGHT) {
- // Change relay state
- digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
- // Store state in eeprom
- saveState(message.sensor, message.getBool());
- // Write some debug info
- Serial.print("Incoming change for sensor:");
- Serial.print(message.sensor);
- Serial.print(", New status: ");
- Serial.println(message.getBool());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment