This is a simple Node.js and Python script for Broadlink SP2 Smart Plug It will set up a web server on port 3000 and will listen to GET requests It will send the request to the Python script and using the python-broadlink will turn on/off the SP2 smart plug.
node.js
node.js express
node.js python-shell
python
(2.7)python-broadlink
git clone https://github.com/NightRang3r/Broadlink-NodeSP2
sudo pip install broadlink
npm install python-shell
npm install express
You will need to edit the "main.js" file lines 11 and 12 with the python and script path:
pythonPath: '/usr/bin/python', scriptPath: '/home/pi/sp2',
You can test SP2 directly from the python script, it expects 3 arguments: IP, MAC, STATE:
python sp2.py "SP2 IP ADDRESS" "SP2 MAC ADDRESS" "STATE"
ON:
python sp2.py 10.0.0.4 b443000000 1
OFF:
python sp2.py 10.0.0.4 b443000000 0
pi@raspberrypi:~/sp2 $ node main.js
curl "http://[SERVERIP]:3000/?ip=["SP2 IP ADDRESS"]&mac=["SP2 MAC ADDRESS"]&state=[1]"
ON:
curl "http://10.0.0.20:3000/?ip=10.0.0.4&mac=b443000000&state=1"
OFF:
curl "http://10.0.0.20:3000/?ip=10.0.0.4&mac=b443000000&state=0"
Create a file named NodeSP2.service:
[Unit] Description=NodeSP2 Wants=network.target After=network.target [Service] Type=simple ExecStart=/usr/local/bin/node /home/pi/sp2/main.js [Install] WantedBy=multi-user.target
cp NodeSP2.service /etc/systemd/system
systemctl daemon-reload
systemctl enable NodeSP2
systemctl start NodeSP2