If you have been thinking about automating your home, then this article is for you. We will teach you how to automate your home using Raspberry Pi и Arduino. You will be able to manage the intercom, lighting, temperature, humidity, PlayStation 4, and get signals from the movement sensors. You can do all these with Apple HomeKit.
What do you need to have?
- RaspberryPi 3 - $42.50
- NodeMCU ESP-12E - $2.98
- Body Sensor HC SR501 - $1.39
- Temperature and Humidity Sensor DHT21 - $2.59
- 5V Relay Module, 10A 250V - $1.48x2
- MicroSD 8GB, 10 Class - $8.55
- Power Supply 5V 1-2A - $5.18
You can choose any version of Raspberry Pi. We prefer Raspberry Pi 3 as it has built-in WiFi and Bluetooth. Instead of NodeMCU you can use Arduino Nano and ESP8266 Module for WiFi. Solder all these and set up. To save you time and money, we rcommend using something readily available, like NodeMCU. Choose the number of relays you need. We are going to have three devices as we will use two relays with two separately regulated blocks.
Raspbian Jessie Setup
- Download the latest version of Raspbian Jessie Lite on Raspberry Pi official website. You can use the regular desktop version if you need it for some other purposes
- Format the SD card FAT32 or FAT.
- Follow the instructions to install the downloaded image on Windows, Linux or MacOS. We used ApplePi Baker to quickly edit a Mac OS X image in the graphic interface
- Insert the memory card into Raspberry Pi, connect a USB keyboard and HDMI monitor (you can use a TV set as well). Choose the settings you like
- Change the default password in paragraph 2 - Change User Password
- As you won't need a graphic interface choose 'Scratch' screen
- Change the host name in Advanced Options > Hostname
- Turn on SSH in Advanced Options > SSH to enable control from other computers
- That’s all! Press Finish to restart.
Setting Wifi on Raspberry Pi
- After Raspberry has loaded, open the interface file by entering:
sudo nano /etc/network/interfaces. Find iface wlan0 inet manual and change it to iface wlan0 inet static. Enter the static local address under it. This is what we got:
iface wlan0 inet static
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf address, netmask, gateway – of course you should write according to your router settings.
After you have made the changes, press CTRL+X, then Y, then ENTER. CTRL+X, then Y, then ENTER
- Open the supplicant file by entering into the command line the following:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf In the file write your WiFi settings as shown below. Here is what we got:
Instead of PerfectoWeb – will be your SSID, and instead of AcC3S4R@SBP1 will be your password.
After you have made the changes, press CTRL+X, then Y, then ENTER. CTRL+X, then Y, then ENTER
- Type into the command line: sudo ifdown wlan0 When the process is completed, enter: sudo ifup wlan0 Reload with the command: sudo reboot The Wifi is set and will automatically connect when Raspberry Pi is on.
- Before starting setup, it’s a good idea to do all the updates and upgrades. To do this, enter into the command line: sudo apt-get update
sudo apt-get upgrade
- If you are using something other than Raspbian Jessie, then you should install the necessary compiler. You can test the version via the command
g++-4.9 -v If you get gcc version 4.9.2 (Raspbian 4.9.2-10), then everything is all right. If you have an old version, or don’t have a compiler at all, install it with the command:
sudo apt-get install g++
You need to install Node.js version 4.0 or higher. Node.js started default support of ARM with this version. We will be using version 6.х, as most Homebridge plugins are not adapted for 8.х version. To install the latest suitable 6.х version perform the following command:
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs Check if the right version was installed:
nodejs -v In our case, we had version v6.11.2
- First you need to install Avahi and other dependences(зависимости???) for MDNS package from the HAP-NodeJS library. Perform the command:
sudo apt-get install avahi-daemon avahi-discover libnss-mdns libavahi-compat-libdnssd-dev
- Install Homebridge and all the dependences with the following sequence of commands:
sudo npm install -g --unsafe-perm homebridge hap-nodejs node-gyp
sudo npm install --unsafe-perm bignum
sudo node-gyp BUILDTYPE=Release rebuild
Проверяем тестовым запуском:
homebridge В ответ должны получить:
[2017-08-15 10:05:15] ---
[2017-08-15 10:05:15] config.json (/home/pi/.homebridge/config.json) not found.
Scan this code with your HomeKit App on your iOS device to pair with Homebridge:
│ 031-45-154 │
[2019-03-15 10:05:15] Homebridge is running on port 44278 It means the installation was successful and everything is working smoothly. You may have noted that Homebridge hasn’t found the configuration file config.json. Our next step will be creating this file.
- Create a configuration file with the command:
Пример с минимальной конфигурацией:
} } We recommend downloading our version with already written plugins to make sure you eventually get the same system we got.
- Make homebridge a daemon? and add it to the autoload list. To do this, create a file:
sudo nano /etc/init.d/homebridge Download an autoload script and copy its content into /etc/init.d/homebridge.
Save and perform the following commands
chmod 755 /etc/init.d/homebridge
update-rc.d homebridge defaults When Raspberry Pi is launched, Homebridge will start automatically with the root user’s name.
Homebridge Plugins Setup
Homebridge can control GPIO Raspberry Pi, IR, Wifi, and Bluetooth with plugins from different manufacturers. For our purpose we need the following plugins:
- Install cmdswitch2 to turn on/off devices with the script
sudo npm install -g homebridge-cmdswitch2
- Install homebridge-cmd_light_control for turning on/off and lighting dimmeration
sudo npm install -g homebridge-cmd_light_control
- Install HttpTemphum to get temperature and humidity data via HTTP in JSON
sudo npm install -g homebridge-httptemperaturehumidity
- Install homebridge-hc-sr501 to get data from movement sensors HC-SR501
sudo npm install -g homebridge-hc-sr501
- Install ps4-waker for turning on/off PlayStation 4
sudo npm install -g ps4-waker
This is the necessary set of plugins we need. You can find and modify the configurations in config.json, you downloaded earlier.
Then you need to connect the devices to Raspberry Pi pins, as shown in the image.
BE CAREFUL while working with the relay and high voltage! If you are not sure, don’t touch it!
When you have finished with the connections, download the scripts archive, unarchive it and upload it to /home/pi/ folder. All the files should have 0777 rights. To check this, for each file separately perform the following:
chmod 0777 /home/pi/light_bathroom_off.py
... Perform this command for all the files you have downloaded and uploaded to /home/pi/ folder.
Connecting to PlayStation 4
- Turn on PlayStation 4. Go to Settings > [Remote Play Connection Settings] > [Add Device]
- In the terminal RaspberryPi write
- The system will ask you to open the app PS4 on you cell phone and connect to ps4-waker.
- Then you will need to enter the pin-code of the monitor screen to which PS4 is connected. The connection will be established. The keys and certificates will be saved in RaspberryPi for further connections.
Setting the thermometer to Arduino
- Connect NodeMCU to the computer and open the official programmer. You need to install a driver for NodeMCU; in our case, it is built on CH340G chip. To do this, go to File > Preferences, and in the field Additional Boards Manager URLs, enter the link for downloading the alternative boards http://arduino.esp8266.com/stable/package_esp8266com_index.json, and press OK. Then go to Tools > Boards > Boards Manager, and in the search line enter esp8266, find esp8266 by ESP8266 Community and install it
- Install sensor control module DHT and module Adafruit Sensor master.
- Download sketch for HTTP server creation and data input from temperature and humidity sensor DHT21.
In the sketch write your IP address and WiFi connection settings. Fill the sketch and go on
Connect the sensor as shown in the image. We connected to the feet D5, GDN and 3.3V.Note that the sensor uses 3.3V, if you connect it to 5V, it may burn
- Nowadays a static IP address in NodeMCU doesn’t work correctly, which is very important for us. That’s why we went another way round by registering in the router the IP address 192.168.1.190 under MAC address of our NodeMCU. Now, when connecting to the network, NodeMCU always gets IP 192.168.1.190. Enter the same address in /home/pi/.homebridge/config.json on RaspberryPi for module HttpTemphum
- Clear the accessories cash and persist on Raspberry Pi with the following command:
rm -r /root/.homebridge/accessories
rm -r /root/.homebridge/persist
- Connect Arduino to USB 5V/1A and place it in the room where you want.
- Find the app Home on your iPad or iPhone. Tap on Add Accessory. Your tablet or smartphone should find. Install it and enjoy!
Hooray! Now you know how to automate your home appliances! You can control the lighting in the rooms and PlayStation 4, get data from movement sensors and be informed about the temperature and humidity. You can also automate some functions in Apple Home, like turning off the lights when you leave home, or turning on the fan when the temperature reaches certain degrees.
I hope you found this article useful.