D.I.Y. Arduino Timer Auto Lights Switch using arduino Nano,Ds3231rtc,relays v1.0 [OUT-DATED]


//*****************************************************************************************************************
//*****************************************************************************************************************


//************<<<< Arduino Nano, DS3231 Real Time Clock & Two Relay Module Lights Timer V1.0 >>>>******************


//*****************************************************************************************************************

//*****************************************************************************************************************





//UPDATED ON DEC 2017







//*****************************************************************************************************************


//*********************<<<< PLEASE VISIT / SUBSCRIBE MY YOUTUBE CHANNEL >>>>***************************************




//*********************<<<<       Youtube.com/CrazyGuyOfficial        >>>>*****************************************



//*****************************************************************************************************************











//**********************//**********************//**********************
//        PINS AND CONNECTIONS
//**********************//**********************//**********************

const byte lightone   =  5;       // Light one relay (LOW Level Relay)
const byte lighttwo   =  6 ;       // Light Two Relay
const byte lightthree =  4 ;       // Status Led    // 13 for arduino builtin LED 

       // DS3231 RTC "SDA to A4",  "SCL to A5"  "Vcc to 5V arduino pin Gnd to Gnd"


//**********************//**********************//**********************
//       Timer ON/OFF Time Settings
//**********************//**********************//**********************


const int OnTime  =  14 ;  // Hour when Light will turn ON (24 hr format NO zeros before single digits)
const int OffTime =  8 ;   // Hour when Light will turn OFF (24 hr format NO zeros before single digits)






//*****************************************************************************************************************


bool LightStatus  =  0 ;       






//*****************************************************************************************************************
// INCLUDE LIBRARIES
//*****************************************************************************************************************






#include <Time.h>        // Time Manipulation

#include <Wire.h>
#include <DS1307RTC.h>   // DS1307 RTC
#include <TimeAlarms.h>  // time alarm library //Changed Number of  alarms " dtNBR_ALARMS to 30 "
                         //"BY EDITING TIMEALARM.H FILE IN LIBRARIES FOLDER.
                         //increasing this number increases size of the sketch too





#define DS1307_ID 0x68    //Address of the RTC




//*****************************************************************************************************************
// SETUP BEGINS
//*****************************************************************************************************************


void setup() {

  pinMode(lightone,   OUTPUT);
  pinMode(lighttwo,   OUTPUT);
  pinMode(lightthree, OUTPUT);




  // THIS WILL PREVENT STARTUP RELAY TRIGGERING PROBLEM



  digitalWrite (lightone, HIGH);
  digitalWrite (lighttwo, HIGH);   // turn the relay "OFF" and turning the pin "LOW" turns the relay "ON"



















  //**********************//**********************//***********************//**********************
  //Check to see if the RTC is present.if yes then Set the ARDUINO's INTERNAL clock accordingly
  //**********************//**********************//***********************//**********************


  // when using timer library the Arduino's "internal clock " is used for getting "current time"
  // Arduino's internal clock is NOT so accurate and gets off time so we use time from RTC (which IS
  //accurate atleast for our use) and we sync Arduino's internal Clock with the time from RTC and we
  //have to do this periodically (atleast once in every 24 hrs )so our tasks can be completed according
  //to the correct time // we can put the sync command in the Loop and Alarm Functions too

  Wire.beginTransmission(DS1307_ID);
  Wire.write((uint8_t)0x00);


    setSyncProvider(RTC.get);         //Yes it did, Sync the time from the RTC

    setSyncInterval(60);

  
  
 
   



    Serial.begin(9600);              
    
  


















  //***********************//**********************//***********************//**********************

  //  TIMER ALARM THAT RUNS OVER AND OVER AFTER SECONDS (used for status led)

  //***********************//**********************//***********************//**********************



  Alarm.timerRepeat(10, Repeats);  // timer for every 10 seconds blinking LIGHT  (status LED)













}

//*****************************************************************************************************************
//                             SETUP END
//*****************************************************************************************************************










//***********************//**********************//***********************//**********************
//************// Function Called When Repeating Timer alarm triggers //*******************
//***********************//**********************//***********************//**********************
void Repeats() {


  Serial.println("******10 second timer******");

  digitalWrite(lightthree, HIGH); 

  Alarm.delay(30);

  digitalWrite(lightthree, LOW);

  
  
  digitalClockDisplay();      // Calls the function to Display  the Clock IN SERIAL MONITOR



if (LightStatus ==1) {
  
  Serial.println ("< Lights ON >");
  
  
  
  
  }
  
  
  else {
    
      Serial.println ("< Lights OFF >");

    
  }



}










//*****************************************************************************************************************


//                    LOOP BEGINS


//*****************************************************************************************************************




void loop() {


  Alarm.delay (2000);




  if (hour() == 0 && minute() == 0 && second() == 0)  // at 00:00:00   MIDNIGHT UPDATE the Arduino's INTERNAL Clock



  {
    setSyncProvider(RTC.get);                         //It's time, sync  the Arduino's INTERNAL Clock  to the RTC
  }










  //**********************************************************

  // Turn Lights On/Off

  //**********************************************************

  if (OffTime > OnTime) {     // Same Day


    if (hour() >= OnTime && hour () <= OffTime - 1)   { 
                              
                              // if current hour is equal or greater than ON time
                              // AND equal to or less than OFF time minus 1  then turn the
                              // lights ON (by turning pins LOW because of using LOW level Relays)
                              // Minus 1 from OFF time because we are not using Minutes here
                              // For Example turn off time is "16" (4 PM) the hour number will be "16" from
                              // 16:00 till 16:59 (4:00 till 4:59)

      digitalWrite(lightone, LOW);


      Alarm.delay (1000);


      digitalWrite(lighttwo, LOW);

       LightStatus  =  1 ; 
    
    
    }

    

    else {

      digitalWrite(lightone, HIGH);

      digitalWrite(lighttwo, HIGH);


      LightStatus  =  0 ;
   
    
    }

 
  
  }


  //**********************************************************


  if (OffTime < OnTime) {     // Different Day



    if (hour() >= OnTime || hour () <= OffTime - 1) {        
      
                              // if current hour is equal or greater than ON time
                              // OR equal to or less than OFF time minus 1  then turn the
                              // lights ON (by turning pins LOW because of using LOW level Relays)
                              // Minus 1 from OFF time because we are not using Minutes here
                              // For Example turn off time is "16" (4 PM) the hour number will be "16" from
                              // 16:00 till 16:59 (4:00 till 4:59)
                              // Minutes,Seconds even day,date,year can be used by adding code


      digitalWrite(lightone, LOW);



      Alarm.delay (1000);



      digitalWrite(lighttwo, LOW);



      LightStatus  =  1;


    }



    else {





      digitalWrite(lightone, HIGH);





      digitalWrite(lighttwo, HIGH);


      
      LightStatus  =  0 ;




    }


  
  
  
  
  
  }







  //*****************************************************************************************************************

  //    DAILY TIMED ALARMS THAT TRIGGER ON SPECIFIED TIMES

  // these alarms will trigger once and will do "stuff" mentioned in the alarm functions at the end of the loop


  //  IF Alarm function is used to Turn Pins ON/OFF it will but there's a Reset/Power Dwn/Up cycle it will not work after 
  // the Specified Alarm time has passed and pin will go back to it's initial state untill alarm changes it again 
  //*****************************************************************************************************************





  Alarm.alarmRepeat(3, 0, 0, ONAlarm); // daily Alarm 1  an alarm on ""03:00:00"" in the morning



  Alarm.alarmRepeat(4, 0, 0, ONAlarm); // daily Alarm 2  an alarm on ""04:00:00""  in the morning



  Alarm.alarmRepeat(5, 0, 0, ON2Alarm); // daily Alarm 3



  Alarm.alarmRepeat(6, 0, 0, ONAlarm); // daily Alarm 4



  Alarm.alarmRepeat(7, 0, 0, ONAlarm); // daily Alarm 5



  Alarm.alarmRepeat(8, 0, 0, ON2Alarm); // daily Alarm 6










  Alarm.alarmRepeat(9, 0, 0, ONAlarm); // daily Alarm 7



  Alarm.alarmRepeat(10, 0, 0, ONAlarm); // daily Alarm 8


  Alarm.alarmRepeat(11, 0, 0, ON2Alarm); // daily Alarm 9



  Alarm.alarmRepeat(12, 0, 0, ONAlarm); // daily Alarm 10


  Alarm.alarmRepeat(13, 0, 0, ONAlarm); // daily Alarm 11



  Alarm.alarmRepeat(14, 0, 0, ON2Alarm); // daily Alarm 12








  Alarm.alarmRepeat(15, 0, 0, ONAlarm); // daily Alarm 13



  Alarm.alarmRepeat(16, 0, 0, ONAlarm); // daily Alarm 14



  Alarm.alarmRepeat(17, 0, 0, ON2Alarm); // daily Alarm 15



  Alarm.alarmRepeat(18, 0, 0, ONAlarm); // daily Alarm 16



  Alarm.alarmRepeat(19, 0, 0, ONAlarm); // daily Alarm 17



  Alarm.alarmRepeat(20, 0, 0, ON2Alarm); // daily Alarm 18



  Alarm.alarmRepeat(21, 0, 0, OFFAlarm); // daily Alarm 19



  Alarm.alarmRepeat(22, 0, 0, OFFAlarm); // daily Alarm 20




  Alarm.alarmRepeat(23, 0, 0, OFF2Alarm); // daily Alarm 21



  Alarm.alarmRepeat(0, 0, 0, OFFAlarm); // daily Alarm 22



  Alarm.alarmRepeat(1, 0, 0, OFFAlarm); // daily Alarm 23



  Alarm.alarmRepeat(2, 0, 0, OFF2Alarm); // daily Alarm 24






}





//**********************//**********************//**********************
//                             LOOP END
//**********************//**********************//**********************








//**********************//**********************//**********************//**********************

//Functions called when daily time specific alarms (above in the loop) triggers:

//***********************************************//**********************//**********************


void ONAlarm() {


}


void ON2Alarm() {



  setSyncProvider(RTC.get);        //It's time, sync  the Arduino's INTERNAL Clock  to the RTC

  Serial.println("TIME SYNCHRONIZED");


}


void OFFAlarm() {

}



void OFF2Alarm() {


  setSyncProvider(RTC.get);       //It's time, sync  the Arduino's INTERNAL Clock  to the RTC

  Serial.println("TIME SYNCHRONIZED");

}





//**********************//**********************//**********************//**********************

//Function for digital clock display (in the SERIAL MONITOR ) 

//**********************//**********************//**********************//**********************




void digitalClockDisplay() {    // digital clock display of the time 24hr format






  Serial.print(hour());

  printDigits(minute());

  printDigits(second());

  Serial.println("");

  Serial.println(dayStr(weekday()));

  Serial.print(monthStr(month()));

  Serial.print("  ");

  Serial.print(day());

  Serial.print("  ");

  Serial.println(year());
}




void printDigits(int digits) {
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

//***********************************************//**********************//**********************



// Un comment the function below and comment the above only one can be used in the code or change names

/*void digitalClockDisplay()      //// digital clock display of the time 12hr format

  {


  Serial.print(hourFormat12());


  printDigits(minute());

  printDigits(second());

  Serial.println("");

  Serial.println(dayStr(weekday()));

  Serial.print(monthStr(month()));

  Serial.print("  ");

  Serial.print(day());

  Serial.print("  ");

  Serial.println(year());

  //Alarm.delay(1000);

  }



  void printDigits(int digits)
  {

  Serial.print(":");


  if (digits < 10) {


    Serial.print('0');


  }


  Serial.print(digits);


  }
*/

WiFi / Smart Ip Camera Tear down / a look from inside V380 smart ip camera type-01

 I bought my first Wifi/ip camera named " V380 Smart Wifi Camera ".

It is a very cheap camera (as compared to the others with similar functionality)but it is a 720p Day/Night Vision Camera with mechanical IR cut filter , LDR , multiple infrared L.E.D.s , On-board memory card slot , Mic/Speaker for two way audio , Alarm notifications and cloud based alarm pictures and it has Wifi which can work both in Station mode and AP mode.

First , I bought this, one camera (from one vendor) and used it for some time then after finding it useful I bought three more of these.(from the second guy)

After reading and watching videos on-line about "hacking ip cameras" and how they have "weak security" and "back-doors", I got really curious and first,I opened them all up to see "what's inside" as I am still new to these kind of electronics and SoCs.To be honest they are impressive and so tiny and beautiful. :-)  

Then after learning a little about "computer networking,ports and stuff",using Nmap on Computer and Fing,Connect-bot,Packet capture app on the phone I found a Telnet port (23), RTSP (554) and MMCC (5050).The MMCC(multimedia conference control) port only shows up in the Fing app when not streaming the camera video using the V380 camera app.

Using "Connect Bot" app I logged in as "Root" through the telnet port (23) using the login user name "root" and leaving the password empty.
It has some version of busy-box with "ash-shell".

I wasn't able to do much after that point except browsing the Linux file system which is "Read-only". All of this works only in the local network.

I also connected the USB to serial converter module (cp2102) to the Rx,Tx and Gnd (marked on the board) after soldering cut-out legs from some LEDs and using male to female jumper wires and if you turn the camera on after connecting the wires it doesn't work and nothing shows up in the serial monitor but if you connect the wires right after turning it on it boots up and it shows all the information in the serial monitor and if the Tx is also connected you can browse the file system as "root" right from the serial monitor but it's a read only system from what I understood because I tried a-lot of "things" but wasn't able to do/write/modify anything as it says "read-only".

However , I'll try to do it again after learning a little more.

When in Station Mode and connected to Internet the Camera automatically connects to the preprogrammed IPs/servers in China and the Android app also sends requests to multiple IPs and after the server responds it sends un-encrypted user name/password string (set from the app) for viewing the video.The server responds with an address and ports and the app connects to that and receives live images/audio.It also has alarm function which works only when connected to the Internet other than the alarm notifications/and alarm pictures (stored in the cloud) function, the live video works perfectly in the local network but all of this works using only the v380 app or the other ,comparatively less functional, apps from the same developer.

Following are the pictures of the first camera and I'll post the other camera's pictures too as there are many differences in their hardware and it seems like they are the later versions of this camera.
Although they all have the same main Soc.(the big grain media chip GM8135S)