____ _ _ ____ ___
/ ___| _ __ ___ __ _ _ __| |_| |__ ___ _ __ _ __ _ _| _ \_ _|
\___ \| '_ ` _ \ / _` | '__| __| '_ \ / _ \ '__| '__| | | | |_) | |
___) | | | | | | (_| | | | |_| |_) | __/ | | | | |_| | __/| |
|____/|_| |_| |_|\__,_|_| \__|_.__/ \___|_| |_| \__, |_| |___|
|___/
SmartberryPI is a simple Node.js library for building a task scheduler for Automation and IoT installations based on Raspberry PI.
In a recent installation we had the need to create effects of light and switch devices randomly. Objects in our installation had to "animate" in particular time intervals, and we planned to manage the whole effects using Raspberry PI, with some software written from scrach in Node.js that allow you to define one or more daily programs "on" and "off" with ignition scheduled. Defining different programs, each day is selected randomly from the program actually run, so visitors could see different behaviour every day.
The status of the devices can be seen in a simple web interface that display the status of all devices (current on/off) and with a master button is it possible to switch ON / OFF the whole process.
For this setup you need:
- A RaspberryPI (I used RaspberryPI 3 Model B)
- A set of plugs remote controlled, in my setup I used Etekcity® Remote Control Electrical Plug & Power Outlet
In my case the remote has one button for power ON and another button for power OFF for each plugs. This means that we need a couple of GPIO pins for each plugs, in order to simulate the button pressed for ON / OFF operation.
Clone the git repo in your RaspberryPI directory, like:
mkdir /opt/smartberrypi
git clone https://github.com/fabvla/SmartberryPI.git .
npm install
NOTE: If you're trying SmartberryPI on Windows, please read carefully the node-gyp installation prerequisites:
https://github.com/nodejs/node-gyp
I suggest to use "Windows -> Option 1":
npm install --global --production windows-build-tools
that works perfectly for me.
First of all, configures your devices (each device corresponds to one or two GPIO Pins, depends on how your remote works):
- Copy
config.json.sample
toconfig.json
- Edit
config.json.sample
and map every device with a Name and GPIO PIN:
{
"port": 5000,
"enabled": true,
"debug": true,
"devices": {
"device_1": {
"name": "Lamp 1",
"driver": "remote_onoff",
"options": {
"pin_on": 17,
"pin_off": 18
}
},
"device_2": {
"name": "Lamp 2",
"driver": "remote_onoff",
"options": {
"pin_on": 22,
"pin_off": 23
}
},
"device_3": {
"name": "Lamp 3",
"driver": "remote_onoff",
"options": {
"pin_on": 24,
"pin_off": 25
}
},
"device_4": {
"name": "Table",
"driver": "remote_onoff",
"options": {
"pin_on": 5,
"pin_off": 6
}
},
"device_5": {
"name": "Floor",
"driver": "remote_onoff",
"options": {
"pin_on": 16,
"pin_off": 26
}
}
}
}
port
: Web Server HTTP portenabled
: True if you want to start SmartberryPI enabled, false otherwise (you can switch enable/disable by web interface)debug
: True for print debug messages on logs, false otherwise. We prefer to log less as possible due to writes limitations of the SD card.devices
: Device list
Note: I made a driver for my specific remote controller, and is it possible to specify a different driver for each device. This means that you can control different type of device (ie: a smart plug, an RF433MHz device etc...).
Actually I only tested a remote with a pear of ON / OFF buttons for each device. I have a remote that can handle 5 device, so I have 10 buttons corresponding to 10 different GPIO output pin.
Generic Schema for connections between a remote and GPIO port:
This is a picture of my implementation with my Etekcity kit:
Before plug remote to GPIO, is better to set all used PINs to 'out', like:
gpio -g mode 17 out
gpio -g mode 18 out
gpio -g mode 22 out
gpio -g mode 23 out
gpio -g mode 24 out
gpio -g mode 25 out
gpio -g mode 5 out
gpio -g mode 6 out
gpio -g mode 16 out
gpio -g mode 26 out
Than verify the system manually by switching ON / OFF every plugs with command line:
gpio -g write 17 1
gpio -g write 17 0
gpio -g write 18 1
gpio -g write 18 0
...
gpio -g write 16 1
gpio -g write 16 0
gpio -g write 26 1
gpio -g write 26 0
You can configure one or more programs, you can see an example on programs/program.json.sample
.
- Copy
programs/program.json.sample
toprograms/program_1.json
- Edit
programs/program_1.json
and define for each device one or more timer status.
You can define one or more programs, if there are many programs, every day one will be choosed randomly.
For each device you can define multiple time and status in this way {"at": "9:50", "status": true}
.
For example, you can define that device_1 is switched "on" at 9:50, switched off at 11:12 and so on.
If you want to switch on for all the day one device, you can simply add only one row like: {"at": "00:00", "status": true},
.
Here an example:
{
"device_1": [
{"at": "9:10", "status": "on"},
{"at": "11:12", "status": "off"},
{"at": "12:50", "status": "on"},
{"at": "13:20", "status": "off"},
{"at": "17:30", "status": "on"},
{"at": "23:59", "status": "off"}
],
"device_2": [
{"at": "9:00", "status": "on"},
{"at": "10:05", "status": "off"},
{"at": "12:10", "status": "on"},
{"at": "13:36", "status": "off"},
{"at": "17:35", "status": "on"},
{"at": "21:00", "status": "off"},
{"at": "21:10", "status": "on"}
],
"device_3": [
{"at": "9:40", "status": "on"},
{"at": "11:20", "status": "off"},
{"at": "13:05", "status": "on"},
{"at": "16:21", "status": "off"},
{"at": "18:00", "status": "on"},
{"at": "18:10", "status": "off"},
{"at": "19:05", "status": "on"},
{"at": "20:30", "status": "off"},
{"at": "21:10", "status": "on"}
],
"device_4": [
{"at": "19:30", "status": "on"},
{"at": "20:55", "status": "off"},
{"at": "21:30", "status": "on"}
],
"device_5": [
]
}
Actually status value can be set to:
on
: Power On the deviceoff
: Power Off the devicetoggle
: Switch status of the device
Connect to your Raspberry PI web server:
http://raspberrypi/
to see the schedule status. You can also switch ON / OFF the scheduler for the current day.
Every day, at midnight when reset task runs, it reset all the switches and re-set the application to default values (active=true) and with a new daily program.
I used linux tool Supervisord. To install run following command:
sudo apt-get install supervisor
sudo service supervisor restart
Than, create a new configuration file inside:
/etc/supervisor/conf.d/smartberrypi.conf
sudo touch /etc/supervisor/conf.d/smartberrypi.conf
sudo vi /etc/supervisor/conf.d/smartberrypi.conf
and put this content:
[program:smartberrypi]
command=node /opt/smartberrypi/app.js
directory=/opt/smartberrypi/
user=pi
stderr_logfile=/opt/smartberrypi/logs/smartberry_err.log
stdout_logfile=/opt/smartberrypi/logs/smartberry_out.log
autostart=true
autorestart=true
After that, reboot Raspberry PI for test the scripts.
If you want to verify the status of the process, use this command:
pi@raspberrypi:$ sudo supervisorctl
smartberrypi RUNNING pid 2435, uptime 0:01:01
For start / stop smartberrypi manually, use:
sudo supervisorctl stop smartberrypi
sudo supervisorctl start smartberrypi
and look log files with tail:
tail -f /opt/smartberrypi/logs/smartberry_out.log
- Thanks to Rocco Musolino, we inspired our project by his IOT-433 Project
- onoff, a Node.js interface for GPIO ports