iPhone remote controlled lamp
This project uses the iPhone and a raspberry pi to remote control the lamp. Both the iPhone and the raspberry pi are connected to the wifi router. The lamp on/off commands are sent from iPhone and received by the raspberry pi embedded system. Then the raspberry pi triggers the IO to control the lamp through a relay. The lamp can be replaced by other home appliances. The software is written in Python and it can be easily port to other devices.
Step 1: System diagram
The system diagram is shown in this figure. The data flow is straightforward. The iPhone is connected to the wifi router wirelessly while the Raspberry pi is connected to the wifi router through a lan cable (this can change to wireless with a wifi adapter installed in the Raspberry pi). One of the Raspberry pi's IO is connected to a relay board. Since the relay board control voltage is 5V and the Raspberry pi output voltage is 3.3V, a transistor is used to perform the voltage transition; the detail will be introduced in step 3. Then the lamp can be controlled through the relay board.
Both the iPhone and the Raspberry pi need to run a software for communication. In this project, Python is used as the programming language. In iPhone, Pythonista is installed and run the script, a UI is also designed. In Raspberry pi, a Python script is run automatically after booting the system. The communication is based on socket TCP mode.
Step 2: Electronics
Here are the electronics parts.
A Raspberry pi board
A relay board
A universal board for connecting the Raspberry pi and the relay board
A wireless router
A USB charger for powering up the Raspberry pi
A transistor 8050
Two resistor (1K ohm)
Lan cable and USB cable and
Of course an iPhone as the remote control
Step 3: Circuit
Since the output voltage of the Raspberry pi is 3.3V and the control signal voltage of the relay board is 5V, we need to change the 3.3V signal to 5V signal. A voltage converter circuit is shown in this picture. A 8050 transistor is used to perform the transition. When the output GPIO of the Raspberry pi is low, the relay board input signal is high, and the relay is on, the lamp will turn on. When the output GPIO of the Raspberry pi is high, the relay board input signal is low, and the relay is off, the lamp will turn off.
Step 4: Software-Raspberry Pi
Some modification of the Raspberry pi is needed to perform this project's task.
Details are list below:
- Auto login
edit /etc/inittab, change
"1:2345:respawn:/sbin/getty --noclear 38400 tty1" into
"1:2345:respawn:/bin/login -f pi tty1 /dev/tty1 2>&1"
- Static IP
set the IP as "192.168.1.200"
edit /etc/network/interfaces, change
"iface eth0 inet dhcp" into
"iface eth0 inet static
- Auto-run script
edit .bashrc, add at the bottom:
"sudo python server_5.py"
server_5.py is the script to serve the lamp control command
- GPIO control
in order to contol the IO, RPi.GPIO is installed,
note: the version is changed to the latest one
- Socket server
the Raspberry pi is acting as a socket server
the Python script is attached as server_5.py.
Step 5: Software-iPhone
Pythonista is installed in the iPhone to run the Python script, it also provides an UI design function.. The script is attahed as lamp.py.
Step 6: Connection and Testing
modify the lamp, break the live wire and connect into the relay board, one end connect to the com port, and another end connect to the normal open port.
use the GPIO 11 of the Raspberry pi to control the relay
connect the iPhone and the Raspberry pi to the wifi router
the screen of the iPhone is captured after running the script, there will be pop-up notifications after press On and Off buttons.
two demo videos are also embedded
It can be easily modified to control other home appliances. And it is quite convenient since you can control it everywhere when you can receive the wifi signal.
It would be possible to use the button titles (or button names) to simplify
import console, socket, ui def lamp_switch(sender): '''Assumes the button titles are 'On' and 'Off' and that both of their actions are set to lamp_switch. You could use button name instead of title.''' s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('192.168.1.200', 8888)) s.sendall('led1 ' + sender.title.lower()) s.close() console.hud_alert('Turned ' + sender.title) v = ui.load_view('lamp') if ui.get_screen_size().h > 768: v.present('popover') else: v.present()