My smart home @ 433 MHz

In this post I want to share some details about my simple low budget smart home system based on 433.92 MHz receiver/transmitter controlled by Arduino. It can be easily extended with many cheap wireless devices, such as door bells, remote sockets, smoke alarms, leak detectors, etc. I’ll describe how to control remote socket, receive alarms from wireless smoke detector and draw a plot of room temperature obtained from regular wired sensor.

Arduino

I downloaded rc-switch and Adafruit DHT libraries and installed them via Arduino IDE. Both have nice tutorials if you need help with wiring and testing.

First I needed to decode commands used to control remote socket and find out smoke alarm code. To do that I used ReceiveDemo sketch which comes with rc-switch library. I just launched it, opened Serial Monitor and started pushing buttons on socket remote control and test button on the alarm. I made sure that the codes for the sockets work by modifying and running SendDemo sketch. In my case the tricky part was to use 180 ms pulse length instead of 178 as reported by ReceiveDemo.

Then I came up with very basic protocol for serial interface. To operate sockets, I send “sXu” and “sXd” strings to Arduino where X is socket number and “u” means “on” and “d” means “off”. For example, “s2d” turns off socket #2. When rc-switch receives an alarm, Arduino sends out “raY” string where Y is detector number.

smart_home.ino

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

#include <RCSwitch.h>

#include <DHT.h>

#define S1_ON 12345 // Remote socket codes obtained with ReceiveDemo.

#define S1_OFF 12346

#define A1 54321 // Alarm code obtained with ReceiveDemo.

#define DHTPIN 12 // DHT22 pin.

#define DHTTYPE DHT22 // DHT22 (AM2302), AM2321

RCSwitch mySwitch=RCSwitch();

DHT dht(DHTPIN,DHTTYPE);

Stringinput;// Received serial data.

voidsetup(){

Serial.begin(9600);

mySwitch.enableTransmit(10);// Transmitter pin.

mySwitch.setPulseLength(180);// Pulse length.

mySwitch.setRepeatTransmit(3);// Repeat transmission just in case.

mySwitch.enableReceive(0);// Receiver on inerrupt 0 => pin #2.

}

voidloop(){

if(mySwitch.available()){

intvalue=mySwitch.getReceivedValue();

if(value==0){

Serial.print("Unknown encoding");

}else{

switch(value){

caseS1_ON:// Remote control button was pressed.

Serial.println("rs1_on");

break;

caseS1_OFF:

Serial.println("rs1_off");

break;

caseA1:// Detector alarm went off.

Serial.println("ra1");

break;

default:// Unknown signal received.

Serial.print("ruk_");

Serial.println(value);

break;

}

}

mySwitch.resetAvailable();

}

input=Serial.readString();// Read the serial port.

if(input=="s1u"){// Turn socket 1 on.

mySwitch.send(S1_ON,24);

Serial.println("ss1_on");

}

if(input=="s1d"){// Turn socket 1 off.

mySwitch.send(S1_OFF,24);

Serial.println("ss1_off");

}

if(input=="t1r"){// Read temperature and print it back.

floatt=dht.readTemperature();

Serial.print("st1_");

Serial.println(t);

}

}

openHAB installation

There’s an openHAB repository for Raspbian, so installation is pretty easy.

Don’t forget to enable permissions for serial access as described in Wiki:

sudo usermod -a -G dialout openhab

openHAB configuration

This was the most complicated pаrt for me because openHAB is very complex and flexible. First one has to define items which are, for example, switches, temperature values and strings coming to and from serial interface. Then there are rules which tell openHAB what to do when something changes (commands are received, values change or scheduled time comes). To display something on the web page or mobile app one needs to setup a sitemap. Finally, to store values like temperatures and switches’ states, persistence is required.

Final touches

When everything is working as expected it’s recommended to reduce logging verbosity and config polling as described in Performance Tuning. You can also install mobile app with native interface. To be able to access your openHAB instance from the outside world, you can either set up port forwarding on your router or set up my.openHAB account. The latter is much easier and gives you additional features, like mobile app or SMS notifications. Happy hacking!