Introduction: 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

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

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

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.

Home Automation

Participated in the
Home Automation