OSMC and TV Controlled by Flic Smart Button
by Acmecorporation in Circuits > Raspberry Pi
3287 Views, 35 Favorites, 0 Comments
OSMC and TV Controlled by Flic Smart Button
This project starts from a specific need, simplfy some repeated actions.
I have got OSMC media center Raspberry Pi based that i use every evening when i want to watch a documentary on my old plasma TV (without HDMI) before sleep.
The actions that i perform when going in bed are:
- turn on Raspberry Pi
- turn on TV
- set shutdown timer on OSMC
- set shutdown timer on TV
For the first step, the only solution find is to unplug and replug Raspberry Pi PS.
Steps from 2 to 4 are grouped into an only action, Flic smart button pressing.
As showed in my previous Instructables, i've tested Flic wireless smart button with RPi. Starting from that point, i've developed this project that, as per my settings, do this:
Flic press = turn on TV, set 50min shutdown timer for RPI and TV.
Flic double press = set more 45min shutdown timer for RPI and TV.
Flic hold = RPI and TV shutdown.
How Do It Works
Flic button pushing action sends an information to Flic smartphone app that perform a HTTP request to a listening RPi webserver, that's all. On RPi a simple script reads HTTP request content and interact with OSMC api and a relay module.
What Do You Need
To perform the project you need:
hardware
Raspberry Pi B (at least) with PS RPi
Relay module Songle
Flic smart button
Smartphone or tablet
TV (*)
other specific hardware
PC power cable
PC extension power cable C13 C14 Female sockets
C13 & C14 Female panel mount plugs
A plastic box
Three jumpers
smartphone/tablet apps
Flic app for IOS or Android
Kodi remote for IOS or Android (optional)
(*)Your TV must support Auto power on functionality. When you disconnect the power cord and re-connect it, the TV will be powered on automatically.
Start
Assuming that you have installed OSMC on your RPi with static IP address and SSH is enabled, open Putty (user & password: osmc) on your PC and follow these steps (or do that directly on LX Terminal):
update Raspberry Pi
sudo apt-get update
install Python pip
sudo apt-get install build-essential python-dev python-pip gcc
install GPIO module for Python
sudo pip install -U RPi.GPIO
install Flask webserver
sudo pip install flask
Install crontab
sudo apt-get install cron
create a directory and navigate inside it
mkdir flic
cd flic
with text editor create a file osmc.py
nano osmc.py
copy and paste following code or download the attached file:
from flask import Flask, render_template, request
app = Flask(__name__)
import os import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
@app.route("/")
def action(buttonPress):
if buttonPress == "click":
GPIO.setup(22, GPIO.OUT)
message = "pressed click"
os.system("xbmc-send --action='XBMC.AlarmClock(shutdowntimer,XBMC.Shutdown(),55)'") # change here shutdown timer
if buttonPress == "dclick":
message = "pressed double click"
os.system("xbmc-send --action='XBMC.AlarmClock(shutdowntimer,XBMC.Shutdown(),45)'") #change here more timer function setting
if buttonPress == "hold":
message = "pressed hold"
os.system("xbmc-send --action='XBMC.ShutDown'")
templateData = { 'message' : message }
return render_template('main.html', **templateData)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5050, debug=True) # change here port if you need
As you can see, this script define actions to be performed when there is a HTTP GET request to Flask webserver on RPi at port 5050.
first case - single click :
When RPi receive HTTP GET request on the address YOUROSMCADDRESS:5050/click, perform an OSMC api .
GPIO.setup(22, GPIO.OUT) .... xbmc-send --action='XBMC.AlarmClock(shutdowntimer,XBMC.Shutdown(),55)'
this instruction give power to my relay module, then TV turn on
Once TV is on, it set 55 minutes of shutdown timer for OSMC . When OSMC timer expires, RPi is turned OFF then relay module disconnects TV power cable
second case - double click
When GET request is YOUROSMCADDRESS:5050/dclick, it sets more minutes to the shutdown timer
third case - hold
GET request is YOUROSMCADDRESS:5050/hold shutdowns RPi then disconnect relay contacts.
Give now permissions to the script
sudo chmod +x osmc.py
you need to create another folder inside pervious
mkdir templates
download and unzip attached file named main.zip and save inside this directory
and now
sudo crontab -e
add at the end
@reboot sudo python /home/osmc/flic/osmc.py
so anytime RPi starts, the script run automatically.
Relay Box
My projects policy is: when possible, recycle .
Starting from a low voltage broken power supply i've recycled the enclosure box.
Two sockets come from a PC PSU.
Then with Dremel tool i've molded the plastic box.
Relay is screwed inside it and VCC and GND are connected respectively to PRi pin 2 and pin 9, relay SIG to pin 15.
Relay used was not exactly correct for my purpose.
When it is powered, NO contact closes and i'm not able to control it when is powered by 5V RPi pin . If i connect it to 3.3V RPi pin, i can control but, sometimes, it jams (*) and NO contacts remain closed.
Then "GPIO.setup(22, GPIO.OUT)" instruction inside script give power to relay and it close NO contact.
The only way to open NO contacts to turn TV off is to poweroff RPi. That's all what i need.
Weld three cables between sockets. Cut one of them (not the yellow cable, it's ground connection) and connect the two ends to NO and COM on relay module.
Be carefully when you connect all to 110/220V AC, don't touch nothing if don't want an high voltage electric shock .
Now you have only to connect two TV power cables to relay box then, once everything is connected plug to AC wall socket.
(*) I casually discovered that moving strong magnets around a jammed relay, sometimes it unlocks
Flic Smartphone App
Assuming that you have already paired you Flic button, for each button pressing add a HTTP GET requests (set "don't show response") with these features :
Click
http//YOUROSMCIPADDRESS:5050/click
Double Click
http//YOUROSMCIPADDRESS:5050/dclick
Hold
http//YOUROSMCIPADDRESS:5050/hold
End
This project is very useful for my need.
Next evolution will be to install on RPi the Bluetooth dongle (BCM20702 chip) so Flic smart button and Raspberry Pi can communicate directly, without any smartphone app.
Thanks for reading and sorry for my English.
Thanks to gearbest.com for supporting.