// Enable debug prints to serial monitor //#define MY_DEBUG // Enable and select radio type attached //#define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 // Set LOW transmit power level as default, if you have an amplified NRF-module and // power your radio separately with a good regulator you can turn up PA level. //#define MY_RF24_PA_LEVEL RF24_PA_LOW // Enable serial gateway #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 // Uncomment to override default HW configurations //#define MY_DEFAULT_ERR_LED_PIN 4 // Error led pin //#define MY_DEFAULT_RX_LED_PIN 6 // Receive led pin //#define MY_DEFAULT_TX_LED_PIN 5 // the PCB, on board LED #include #include #include #include #include // Set this to the pin you connected the DHT's data pin to #define DHT_DATA_PIN 7 // Set this offset if the sensor has a permanent small offset to the real temperatures. // In Celsius degrees (as measured by the device) #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 = 60000; // 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 33 #define CHILD_ID_TEMP 34 float lastTemp; float lastHum; uint8_t nNoUpdatesTemp; uint8_t nNoUpdatesHum; bool metric = true; Timer timer; MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); DHT dht; // Enable repeater functionality for this node #define MY_REPEATER_FEATURE #define RELAY_L_1 22 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define RELAY_L_2 23 #define RELAY_L_3 24 #define RELAY_L_4 25 #define RELAY_L_5 26 #define RELAY_L_6 27 #define RELAY_L_7 28 #define RELAY_L_8 29 #define RELAY_L_9 30 #define RELAY_L_10 31 #define RELAY_L_11 32 #define RELAY_L_12 33 #define RELAY_L_13 34 #define RELAY_L_14 35 #define RELAY_L_15 36 #define RELAY_L_16 37 #define RELAY_H_17 38 #define RELAY_H_18 39 #define RELAY_H_19 40 #define RELAY_H_20 41 #define RELAY_H_21 42 #define RELAY_H_22 43 #define RELAY_H_23 44 #define RELAY_H_24 45 #define RELAY_H_25 46 #define RELAY_H_26 47 #define RELAY_H_27 48 #define RELAY_H_28 49 #define RELAY_H_29 50 #define RELAY_H_30 51 #define RELAY_H_31 52 #define RELAY_H_32 53 #define NUMBER_OF_RELAYS_L 16 // Total number of attached relays #define RELAY_L_ON 0 // GPIO value to write to turn on attached relay #define RELAY_L_OFF 1 // GPIO value to write to turn off attached relay #define NUMBER_OF_RELAYS_H 16 // Total number of attached relays #define RELAY_H_ON 1 // GPIO value to write to turn on attached relay #define RELAY_H_OFF 0 // GPIO value to write to turn off attached relay // docelowo stan wysoki (HIGH) - dotykowe, narazie niski - poki niema czujników #define BUTTON1_PIN A0 #define BUTTON2_PIN A1 #define BUTTON3_PIN A2 #define BUTTON4_PIN A3 #define BUTTON5_PIN A3 // LOW - zwykłe #define BUTTON6_PIN 8 #define BUTTON7_PIN 9 #define BUTTON8_PIN 10 #define BUTTON9_PIN 11 #define BUTTON10_PIN 12 void before() { for (int sensor = 1, pin = RELAY_L_1; sensor <= NUMBER_OF_RELAYS_L; 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_L_ON : RELAY_L_OFF); } for (int sensor = 17, pin = RELAY_H_17; sensor <= NUMBER_OF_RELAYS_H+NUMBER_OF_RELAYS_L; 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_H_ON : RELAY_H_OFF); } } Bounce debouncer1 = Bounce(); Bounce debouncer2 = Bounce(); Bounce debouncer3 = Bounce(); Bounce debouncer4 = Bounce(); Bounce debouncer5 = Bounce(); Bounce debouncer6 = Bounce(); Bounce debouncer7 = Bounce(); Bounce debouncer8 = Bounce(); Bounce debouncer9 = Bounce(); Bounce debouncer10 = Bounce(); 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()); // Setup locally attached sensors delay(10000); // Setup the button. pinMode(BUTTON1_PIN, INPUT_PULLUP); pinMode(BUTTON2_PIN, INPUT_PULLUP); pinMode(BUTTON3_PIN, INPUT_PULLUP); pinMode(BUTTON4_PIN, INPUT_PULLUP); pinMode(BUTTON5_PIN, INPUT_PULLUP); pinMode(BUTTON6_PIN, INPUT_PULLUP); pinMode(BUTTON7_PIN, INPUT_PULLUP); pinMode(BUTTON8_PIN, INPUT_PULLUP); pinMode(BUTTON9_PIN, INPUT_PULLUP); pinMode(BUTTON10_PIN, INPUT_PULLUP); // After setting up the button, setup debouncer. debouncer1.attach(BUTTON1_PIN); debouncer1.interval(5); debouncer2.attach(BUTTON2_PIN); debouncer2.interval(5); debouncer3.attach(BUTTON3_PIN); debouncer3.interval(5); debouncer4.attach(BUTTON4_PIN); debouncer4.interval(5); debouncer5.attach(BUTTON5_PIN); debouncer5.interval(5); debouncer6.attach(BUTTON6_PIN); debouncer6.interval(5); debouncer7.attach(BUTTON7_PIN); debouncer7.interval(5); debouncer8.attach(BUTTON8_PIN); debouncer8.interval(5); debouncer9.attach(BUTTON9_PIN); debouncer9.interval(5); debouncer10.attach(BUTTON10_PIN); debouncer10.interval(5); } void presentation(){ // Send the sketch version information to the gateway and Controller sendSketchInfo("ARDUINO_1", "1.3"); for (int sensor = 1, pin = RELAY_L_1; sensor <= NUMBER_OF_RELAYS_L; sensor++, pin++) { // Register all sensors to gw (they will be created as child devices) present(sensor, S_LIGHT); } for (int sensor = 17, pin = RELAY_H_17; sensor <= NUMBER_OF_RELAYS_H+NUMBER_OF_RELAYS_L; sensor++, pin++) { // Register all sensors to gw (they will be created as child devices) present(sensor, S_LIGHT); } // 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 = getControllerConfig().isMetric; } MyMessage msg1(1, V_LIGHT); MyMessage msg2(2, V_LIGHT); MyMessage msg3(3, V_LIGHT); MyMessage msg4(4, V_LIGHT); MyMessage msg5(5, V_LIGHT); MyMessage msg6(6, V_LIGHT); MyMessage msg7(7, V_LIGHT); MyMessage msg8(8, V_LIGHT); MyMessage msg9(9, V_LIGHT); MyMessage msg10(10, V_LIGHT); MyMessage msg11(11, V_LIGHT); MyMessage msg12(12, V_LIGHT); MyMessage msg13(13, V_LIGHT); MyMessage msg14(14, V_LIGHT); MyMessage msg15(15, V_LIGHT); MyMessage msg16(16, V_LIGHT); MyMessage msg17(17, V_LIGHT); MyMessage msg18(18, V_LIGHT); MyMessage msg19(19, V_LIGHT); MyMessage msg20(20, V_LIGHT); MyMessage msg21(21, V_LIGHT); MyMessage msg22(22, V_LIGHT); MyMessage msg23(23, V_LIGHT); MyMessage msg24(24, V_LIGHT); MyMessage msg25(25, V_LIGHT); MyMessage msg26(26, V_LIGHT); MyMessage msg27(27, V_LIGHT); MyMessage msg28(28, V_LIGHT); MyMessage msg29(29, V_LIGHT); MyMessage msg30(30, V_LIGHT); MyMessage msg31(31, V_LIGHT); MyMessage msg32(32, V_LIGHT); void loop() { if(timer.state() == STOPPED) { timer.start(); } if (timer.read() > 60000) { // 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; // apply the offset before converting to something different than Celsius degrees temperature += SENSOR_TEMP_OFFSET; if (!metric) { temperature = dht.toFahrenheit(temperature); } // Reset no updates counter nNoUpdatesTemp = 0; 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++; } timer.stop(); // Sleep for a while to save energy //sleep(UPDATE_INTERVAL); //wersja dla samych czujników (bez przekaźników) } // Send locally attached sensor data here if (debouncer6.update()) { // Get the update value. int value6 = debouncer6.read(); // Send in the new value. if (value6 == LOW) { saveState(17, !loadState(17)); digitalWrite(RELAY_H_17, loadState(17) ? RELAY_H_ON : RELAY_H_OFF); send(msg17.set(loadState(17))); } } if (debouncer7.update()) { int value7 = debouncer7.read(); if (value7 == LOW) { saveState(19, !loadState(19)); digitalWrite(RELAY_H_19, loadState(19) ? RELAY_H_ON : RELAY_H_OFF); send(msg19.set(loadState(19))); } } if (debouncer8.update()) { int value8 = debouncer8.read(); if (value8 == LOW) { saveState(21, !loadState(21)); digitalWrite(RELAY_H_21, loadState(21) ? RELAY_H_ON : RELAY_H_OFF); send(msg21.set(loadState(21))); } } if (debouncer9.update()) { int value9 = debouncer9.read(); if (value9 == LOW) { saveState(13, !loadState(13)); digitalWrite(RELAY_L_13, loadState(13) ? RELAY_L_ON : RELAY_L_OFF); send(msg13.set(loadState(13))); } } // if (debouncer1.update()) { // int value1 = debouncer1.read(); // if (value1 == HIGH) { // saveState(3, !loadState(3)); // digitalWrite(RELAY_L_3, loadState(3) ? RELAY_L_ON : RELAY_L_OFF); // send(msg3.set(loadState(3))); // } // } } void receive(const MyMessage &message) { // We only expect one type of message from controller. But we better check anyway. if (message.type == V_LIGHT) { if (message.sensor < 17) { // Change relay state digitalWrite(message.sensor - 1 + RELAY_L_1, message.getBool() ? RELAY_L_ON : RELAY_L_OFF); // Store state in eeprom saveState(message.sensor, message.getBool()); // Write some debug info #ifdef MY_DEBUG Serial.print("Incoming change for sensor:"); Serial.print(message.sensor); Serial.print(", New status: "); Serial.println(message.getBool()); #endif } else { // Change relay state digitalWrite(message.sensor - 17 + RELAY_H_17, message.getBool() ? RELAY_H_ON : RELAY_H_OFF); // Store state in eeprom saveState(message.sensor, message.getBool()); // Write some debug info #ifdef MY_DEBUG Serial.print("Incoming change for sensor:"); Serial.print(message.sensor); Serial.print(", New status: "); Serial.println(message.getBool()); #endif } } }