Playing with my new toy – Pi-mote Control starter kit with 2 sockets.
Sample code – turn on all plugs for 5 seconds.
import RPi.GPIO as GPIO from time import sleep GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(17,GPIO.OUT) GPIO.setup(22,GPIO.OUT) GPIO.setup(23,GPIO.OUT) GPIO.setup(27,GPIO.OUT) GPIO.setup(24,GPIO.OUT) GPIO.setup(25,GPIO.OUT) GPIO.output(17,True) GPIO.output(22,True) GPIO.output(23,False) GPIO.output(27,True) sleep(0.1) GPIO.output(25,True) sleep(0.25) GPIO.output(25,False) sleep(5) GPIO.output(17,True) GPIO.output(22,True) GPIO.output(23,False) GPIO.output(27,False) sleep(0.1) GPIO.output(25,True) sleep(0.25) GPIO.output(25,False)
Complicated? There is an easier way, by using Energenie library.
Installing Energenie library
For Python 3
sudo apt-get install python3-pip sudo pip-3.2 install energenie
For Python 2
sudo apt-get install python-pip sudo pip install energenie
* pip is a package management system used to install and manage software packages written in Python.
Sample code – turn on all plugs for 5 seconds
from energenie import switch_on, switch_off from time import sleep switch_on() sleep(5) switch_off()
It is much easier now, doesn’t it?
Updated: 11/9/2016
To control each switch separately, hold down the green button on one switch for 10-15 seconds, send channel 1 on command, hold down the green button on the other switch for 10-15 seconds, send channel 2 on command.
Now I can switch on/off the plug separately.
from energenie import switch_on, switch_off from time import sleep switch_on(1) sleep(3); switch_off(1) sleep(3); switch_on(2) sleep(3); switch_off(2)
Now I’ll be able to automatically turn on my radio every morning to wake me up, or remotely turn on my rice cooker before I arrive at home 😉
Reference
Controlling electrical sockets with Energenie Pi-mote – Raspberry Pi
Energenie Documentation
Energenie Python library source code