Introduction: Home Automation With ATTiny and Mobile Phone

Picture of Home Automation With ATTiny and Mobile Phone

In our cottage in the woods we don't have a reliable internet connection. In the winter it is really cold when we enter and it takes a couple of hours before the house warms up when turning on the floor heating after we come in.

I wanted to be able to turn the heating on before we arrive, and enter a warm house. But the internet connection cannot be trusted. So I thought I would build a simple time delay switch which can be triggered by using a mobile phone. This circuit should be able to switch the central heating for a certain time when the phone number is called.

To know when phone is called and to select the time the heating would be turned on I wanted to use a simple microprocessor. As I had been experimenting with the ATTiny for a while, this was a great opportunity to make use of all the tests I have been doing. The switch is triggered by the light of the display of the phone when it receives a call.

The switch I used is a Solid State Relay Optocoupler the LH1540 which is capable of switching upto 350 VAC and up to 100 mA. This was enough for the central heating switch, but can also be used to switch other appliances.

If you want to switch higher Amps you should have the LH1540 SSR switch an additional relay.

As there were two pins not used I decided to use these to modify switching time by using jumpers. In the program the setup and times are explained.

If you like this 'ible, please click vote in the right top for a vote in the Home Automation contest.

Step 1: Setting Up and Testing the Circuit

Picture of Setting Up and Testing the Circuit

To test I created the circuit by putting all the parts on a breadboard. Using a ATTiny shield I build earlier I was able to program and test at the same time. (Great 'ible for the shield can be found here)

Materials:

  • ATTiny85
  • LH1540 Solid State Relay (see datasheet)
  • Perforated circuit board
  • Socket for 8 pin IC chip
  • LDR
  • Male headers
  • 2 jumpers (from old Harddrive)
  • 10 kOhm resistor
  • 100 Ohm resistor
  • (optional PCB connector)
  • Old mobile phone
  • Prepaid phone card (only for receiving calls, so no extra costs)
  • Case for the circuit and phone (light-tight) I used an cigar box for this

(total costs < 5 Euro's)

Tools used:

  • Soldering iron
  • Helping hand
  • Double sided tape

On the circuit schematic you can see the setup of all the components.

Step 2: Programming the ATTiny

Picture of Programming the ATTiny

To program the ATTiny you can find the instruction in the 'ible in the previous step.

Here is the program I used: (modified on advice of gulliverrr)

/*
ATTiny switch with timer (by Max Janssen june 2015)

The ATTiny timer is not very accurate, but enough to switch for a certain number of seconds

The digital switch is based upon a LDR and resistor. GND---LDR---(A1)---100KOhm---5V

The switch output goes to an LH1540 Optocoupler Solid State Relais to switch whatever. Pin1---100Ohm---Pin1(LH1540)

Pins 3 and 4 are used to jumper to GND and set different switching times

If 3 phonecalls are received minimum 30 seconds and maximum 2 minutes apart the switch is triggered *

/ These constants won't change. They're used to give names // to the pins used: const int ldrValue = A1; // Analog input pin that the LDR is attached to const int setPin1 = 3; // for setting time delay const int setPin2 = 4; const int outputPin = 1; // to switch (ss relais)

int sensorValue = 0; // value read from the LDR int timerValue = 0; int ldrTreshold = 600; //Treshold to switch

void setup() { // initialize serial communications at 9600 bps (not for ATTiny) // Serial.begin(9600); pinMode(setPin1, INPUT_PULLUP); pinMode(setPin2, INPUT_PULLUP); pinMode(outputPin, OUTPUT); digitalWrite(outputPin, LOW); }

void loop() { // read the LDR value: sensorValue = analogRead(ldrValue); if (sensorValue <= ldrTreshold) countRings(); // print the results to the serial monitor: /* Serial.print("Pin1 = " ); Serial.print(digitalRead(setPin1)); Serial.print(" Pin2 = " ); Serial.print(digitalRead(setPin2)); Serial.print(" sensor = " ); Serial.print(sensorValue); Serial.print(" timerValue = " ); Serial.println(timerValue); */

// wait before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(200); }

void countRings() { int ringCounter = 1; // phone has rung once unsigned long timeNow = millis(); delay(30000); //delay for 30 seconds while(millis() <= timeNow + 120000){ //check signal for 2 minutes sensorValue = analogRead(ldrValue); if (sensorValue <= ldrTreshold){ ringCounter = 2; break; } } if (ringCounter < 2) return; //the phone did not ring 2 times delay(30000); //delay for 30 seconds timeNow = millis(); while(millis() <= timeNow + 120000){ //check signal for 2 minutes sensorValue = analogRead(ldrValue); if (sensorValue <= ldrTreshold){ ringCounter = 3; break; } } if(ringCounter < 3) return; //the phone did not ring 3 times Switch(); // 3 rings => start the switching time }

void Switch(){ // set timer value. set = 0, not set = 1!!! // setPin2, setPin1, delay (millisec) // 1 1 300.000 (15 min) // 1 0 1200.000 (30 min) // 0 1 3600.000 (60 min) // 0 0 7200.000 (120 min) int timerState = (digitalRead(setPin2)*10)+digitalRead(setPin1); // Serial.println(timerState); switch(timerState){ case 11: timerValue = 300000; break; case 10: timerValue = 1200000; break; case 1: timerValue = 3600000; break; case 0: timerValue = 7200000; break; }

digitalWrite (outputPin, HIGH); delay(timerValue); digitalWrite (outputPin, LOW); }

When testing the program you can decrease the timerValue so you do not have to wait 2 hours to see if the last option works...

Because of the use of INPUT_PULLUP you can can connect a port to ground to us as a switch. Therefore the off state is 1 and the on state is 0. (see the use of setPin1 and setPin2)

The ldrTreshold can be determined by uploading the sketch to an Arduino Uno and using the Serial connection to look at the values. These values can be used in the ATTiny.

Of course you can change the values of timerValue to suit your need for delays.

Step 3: Soldering the Circuit

Picture of Soldering the Circuit

I used an 8-pin IC socket for the ATTiny so it can be easily removed and reprogrammed.

I tried to put all the parts on the perf board so it is easy to solder and only a few wires a necessary to connect everything. Not the most beautiful design and finish, but it works!

I cut and stripped the power supply line for the phone to use the 4.5 Volt to power the phone as well as the circuit. (not on the picture)

Finaly put the phone and the circuit in the case. I used velcro to stick the phone in the box so I can remove it if I want to see if the number of calls is ok and to call the voicemail every 3 months to make sure the prepaid card is still working.

Connect the wires from the connector to the Central Heating... done!

I found out that setting the timer to one hour is great for our cottage. So after a phonecall about two hours before we arrive we are welcomed in a warm house.

Comments

gulliverrr (author)2015-06-14

In my country even prepaid phones can receive advertising phone calls/sms and that would trigger the heating! Or simply a wrong number would be enough. If this was my only choice, I would build in a pattern that would need to be satisfied before turning on anything. A pattern could be 3 phonecalls no less than 1 minute apart and no more than 2 minutes apart. That way I could filter out unwanted calls :)

MaxJanssen (author)gulliverrr2015-06-15

We don't have that problem. But this is a very good idea! Thanks for the addition.

gulliverrr (author)MaxJanssen2015-06-15

what if power is cut? Upon reconnection the phone will start charging hence the screen will come on for some seconds. And what if the power is cut for some days and the phone dies. It will not start again even after the power is restored. I would definetely need "a filter" as described before. :)

MaxJanssen (author)gulliverrr2015-06-17

The powercuts are rare over here. For several days? Have never experienced that. But just to be sure, I changed the sourcecode so the phone has to ring 3 times. like you suggested.

Saiyam (author)2015-06-16

I like the idea that you have put in. At first I thought that you used a gsm module but later noticed this clever idea of saving your money as well as doing your job. Very well done.

But good points put up by gulliverrr.

MaxJanssen (author)Saiyam2015-06-16

Thanks very much! I agree that gulliverrr has good points. I modified the sourcecode so the phone will have to ring 3 times at least 30 seconds apart and in a maximum of 2 minutes timeslot.

When I use an old Nokia the phone will keep connection for 3 days on the battery. I have never had a power outage of 3 days. If so... we will enter a cold house....

skibum30 (author)2015-06-12

Brilliant idea to use the light from the phone as a trigger. Nicely done!

MaxJanssen (author)skibum302015-06-13

Thanks!