code ..
/* By Roee Bloch Livolo Automatic Smart home turn on/off lights between definded hours! #define lock_time 30 // if it gets to 30 seconds it will shut off/on the light #define start_time 20 // it will start operation at 8:00 oclock evening #define stop_time 23 // till eleven oclock looks good to me All right Reserved Copyright (c) 2015 All Right Reserved, http://www.electronics-freak.com This source is subject to the Roee Bloch License. Please see the License.txt file for more information. All other rights reserved. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. </copyright> <author>Roee Bloch</author> <email>roeebloch@walla.co.il</email> <date>August 2015</date> Wifly automatic home control - using Telnet anywhere with home router address The commands are c1 activate GPIO4 to '1' c0 deactive GPIO4 to '0' c2 unlocked for 3 minuets without feedback after locked rr read status of gpio4 r0 reset internal counter to zero //PINS in USE are: //D4 - relay command //A4 - RTC DATA //A5 - RTC CLK //D6 - Reset pin to Wifly (to Wifly pin 5) //D10 - RX WIFLY (TX) //D11 - TX WIFLY (RX) // //need to connect resistor in series to change from 5V to 3.3V //suggest 1K, 2.2K while 2.2K to GND and 1K to D10,D11 // // // // using timer library from NET //https://github.com/JChristensen/Timer //to add it goto Sketch-> import library -> add librarry -> choose the ZIP file // //april 2015 - adding I2C RTC //on NANO A4=DATA, A5=CLK //another library for RTC: //RTC DS1307RTC */ #include <Wire.h> #include <Time.h> #include <DS1307RTC.h> #include <livolo.h> #define SIGNAL_IN 0 // INTERRUPT 0 = DIGITAL PIN 2 - use the interrupt number in attachInterrupt #define lock_time 30 // if it gets to 30 seconds it will shut off/on the light #define start_time 20 // it will start operation at 8:00 oclock evening #define stop_time 23 // till eleven oclock looks good to me volatile byte impulse = 0; // kolejny puls volatile int bufor[53]; volatile boolean header = false; volatile unsigned long StartPeriod = 0; // set in the interrupt volatile boolean stop_ints = false; Livolo livolo(8); // transmitter connected to pin #8 unsigned char mytime, myhours; char rxtx = 0; // reading digital pin 3 if zero will be ceceiver if 1 transmitter int my_lock_time = lock_time; // convert it to INT this is how it works int my_start_time = start_time; // convert it to INT this is how it works int my_stop_time = stop_time; // convert it to INT this is how it works char one_transmission_flag = 0; void setup() { Serial.begin(9600); pinMode(3, INPUT); pinMode(13, OUTPUT); // on board LED attachInterrupt(SIGNAL_IN, calcInput, CHANGE); while (!Serial) ; // wait for serial delay(200); Serial.println("DS1307RTC Read Test"); Serial.println("-------------------"); livolo.sendButton(2011, 106); // all lights off code } void loop() { tmElements_t tm; if (RTC.read(tm)) { Serial.print("Ok, Time = "); print2digits(tm.Hour); Serial.write(':'); print2digits(tm.Minute); Serial.write(':'); print2digits(tm.Second); Serial.print(", Date (D/M/Y) = "); Serial.print(tm.Day); Serial.write('/'); Serial.print(tm.Month); Serial.write('/'); Serial.print(tmYearToCalendar(tm.Year)); Serial.println(); mytime = tm.Second; myhours = (tm.Hour); Serial.println(mytime); Serial.print("hours is:"); Serial.println(myhours); if ((myhours > my_start_time - 1) && (myhours < my_stop_time)) { Serial.println("Working ......"); one_transmission_flag = 0; if ((mytime == my_lock_time) || ((mytime + 1) == my_lock_time)) { tx_onoff_light(random(3)); // pick random light } } else { if (one_transmission_flag == 0) { Serial.println("transmitting OFF"); one_transmission_flag = 1; livolo.sendButton(2011, 106); // all lights off code } } } else { if (RTC.chipPresent()) { Serial.println("The DS1307 is stopped. Please run the SetTime"); Serial.println("example to initialize the time and begin running."); Serial.println(); } else { Serial.println("DS1307 read error! Please check the circuitry."); Serial.println(); } delay(9000); } delay(1000); } void print2digits(int number) { if (number >= 0 && number < 10) { Serial.write('0'); } Serial.print(number); } void calcInput() { // get the time using micros unsigned int duration = (int)(micros() - StartPeriod); // save pulse length to bufor StartPeriod = micros(); //begin next impulse //Serial.println(StartPeriod); if (stop_ints) return; if ((duration < 90) || (duration > 600)) goto reset; //impulse not right bufor[impulse++] = duration; if (duration < 415) return; if (!header) { header = true; impulse = 0; return; } else { if ((impulse < 23) || (impulse > 52)) goto reset; //too long or too short info stop_ints = true; return; } reset: header = false; impulse = 0; return; } void txrx_blink(char param) { int mydelay; char myloop; if (param == 0) // Receiver { mydelay = 500; myloop = 2; } else { mydelay = 100; myloop = 3; } for (int i = 0; i < myloop; i++) { digitalWrite(13, 1); delay(mydelay); digitalWrite(13, 0); delay(mydelay); } } void receiver() { while (1) { if (stop_ints) //data in buffer { unsigned long binary = 1; //byte i = 0; for (byte j = 0; j < 46; j++) { //Serial.print(binary); if ((bufor[j] > 220) && (bufor[j] < 400)) { binary <<= 1; //binary |= 1; //i++; bitSet(binary, 0); } else if ((bufor[j] > 90) && (bufor[j] < 220) && (bufor[j + 1] > 90) && (bufor[j + 1] < 220)) { binary <<= 1; j++; } else if ((bufor[j] > 90) && (bufor[j] < 220) && (bufor[j + 1] > 220) && (bufor[j + 1] < 400)) { binary <<= 1; bitSet(binary, 0); //i += 2; j++; } else break; } //Serial.println(bitRead(binary,4)); if (bitRead(binary, 23)) { bitClear(binary, 23); Serial.print("remoteID:"); Serial.print((binary / 128) & 65535); Serial.print(" - "); Serial.print("key code:"); Serial.println(binary & 127); } else { Serial.println("wrong code "); Serial.println(binary, BIN); } delay (1000); txrx_blink(1); header = false; impulse = 0; stop_ints = false; // } } } } void transmit() // not in use this is part of other program { while (1) { // livolo.sendButton(6400, 120); // blink button #3 every 3 seconds using remote with remoteID #6400 txrx_blink(0); delay(2000); Serial.println("Transmitting key 1 NOW..."); livolo.sendButton(2011, 0); // my key 1 txrx_blink(0); delay(2000); Serial.println("Transmitting key 2 NOW..."); livolo.sendButton(2011, 96); // my key 2 txrx_blink(0); delay(2000); Serial.println("Transmitting key 3 NOW..."); livolo.sendButton(2011, 120); // my key 3 } } void tx_onoff_light(int x) // switch on/off button by value every number is different light { Serial.print("Transmitting now on/off...."); Serial.println(x); switch (x) { case 1: livolo.sendButton(2011, 0); // my key 1 break; case 2: livolo.sendButton(2011, 96); // my key 2 break; case 3: livolo.sendButton(2011, 120); // my key 3 break; } }
This Program is doing automatic on/off randomly, all you have to do is set the number of switch that you have and the time limits (start time and stop time) – between those times, it will automatically control the lights.
This uses the following:
Arduino Uno board
RTC DS1307 board – real time clock
Transmitter of 433Mhz
[adsenseyu6] |
[adsenseyu1]