1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
/* 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]