📜 ⬆️ ⬇️

Remote control of VLC player using Arduino and Python

Good afternoon, dear readers.

I have long been interested in Arduino, and once decided to buy this wonderful platform. After a brief search, I purchased a small Arduino kit, which, among other things, had an IR sensor and a remote control for it. Having studied the examples from the manual, I realized that it was time to invent something of my own. In the end, I decided to make a remote control of the VLC player using the magic of Arduino and Python3.

Arduino magic

Connection diagram and sketch honestly taken without changes from the manual Arduino kit, downloaded from the manufacturer's website.


')
Sketch
/* Arduino Advanced Kit example Project 11 - IRRemote * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv * An IR detector/demodulator must be connected to the input RECV_PIN. * Version 0.1 July, 2009 * Copyright 2009 Ken Shirriff * http://arcfn.com */ #include <IRremote.h> int RECV_PIN = 7; IRrecv irrecv(RECV_PIN); decode_results results; void setup() { Serial.begin(9600); irrecv.enableIRIn(); // Start the receiver } void loop() { if (irrecv.decode(&results)) { Serial.println(results.value,HEX); // print received values irrecv.resume(); // Receive the next value } } 


As a result, the codes of keystrokes come to the port monitor. Experimentally, it was found that different IR remotes are suitable. Although this is a well-known fact, it was new to me. But further more interesting. VLC media player has a convenient web-interface. And you can manage it through the usual http-get requests of the form:

" Host : port / page.html? Var = value & var2 = value2 & ..."

More information can be found on videolan.org .

Python3 magic

Using Python3 and its rich libraries, it is possible to both send http-requests and read data from the serial port. That was enough for the execution of my ideas. The Python3 script is written using the OOP approach.

Briefly, the script is as follows:

1. Create an instance of the vlc_remote_contol class, pass the address of our Vlc player, the list with the key codes, and the name of the virtual port to the constructor.
2. Call the control method in which the key code is read from the port.
3. The key code is passed to the __switch_button method. where the __command method is called with certain parameters
4. __command directly makes a get request to the VLC player.

Script
 import getpass import time import subprocess import requests import serial import sys import lxml.etree as etree class vlc_remote_contol: def __init__(self,hst,blist,pname): self.host = hst self.btn_list = blist self.port = serial.Serial(pname, 9600, timeout = 0) self.__sess = requests.Session() self.__sess.auth = ('',getpass.getpass()) self.old_vol = self.__get_param(self.__sess.get(self.host+'/requests/status.xml').text, 'volume') self.curr_vol = 0 def __get_param(self,info,param): beg = info.find('<'+param+'>')+len('<'+param+'>') end = info.find('</'+param+'>') return int(info[beg:end]) def __command (self,host,comm,val): if (val == ''): req = self.__sess.get(host+'/requests/status.xml'+'?'+'command='+comm) else: req = self.__sess.get(host+'/requests/status.xml'+'?'+'command='+comm+'&val='+val) req.close() def __switch_button(self,btn_code): if (btn_code == 'FFC23D'): ## play/pause self.__command(self.host,'pl_pause','') elif (btn_code == 'FF906F'): ## fullscreen self.__command(self.host,'fullscreen','') elif (btn_code == 'FFE01F'): ## volume down self.__command(self.host,'volume','-10') elif (btn_code == 'FFA857'): ## volume up self.__command(self.host,'volume','+10') elif (btn_code == 'FF22DD'): ## rewind back self.__command(self.host,'seek','-0.5%') elif (btn_code == 'FF02FD'): ## rewind forward self.__command(self.host,'seek','+0.5%') elif (btn_code == 'FFA25D'): ## previous track self.__command(self.host,'pl_previous','') elif (btn_code == 'FFE21D'): ## previous next self.__command(self.host,'pl_next','') elif (btn_code == 'FF6897'): ## mute self.cur_vol = self.__get_param(self.__sess.get(self.host+'/requests/status.xml').text, 'volume') if (self.cur_vol == 0): self.__command(self.host,'volume',str(self.old_vol)) else: self.old_vol = self.cur_vol self.__command(self.host,'volume','0') else: pass def control(self): print('Success') while True: res = self.port.readline().strip().decode("UTF-8") if (res in self.btn_list): self.__switch_button(res) def main(): conf = etree.parse('conf.xml') vpath = conf.xpath('/document/vpath/text()')[0] vhost = conf.xpath('/document/vhost/text()')[0] try: subprocess.Popen([vpath]) except: input('VLC player not found') return try: buttons0 = ['FFA25D','FF629D','FFE21D','FF22DD','FF02FD','FFC23D','FFE01F','FFA857','FF906F','FF6897', 'FF9867','FFB04F','FF30CF', 'FF18E7','FF7A85','FF10EF','FF38C7','FF5AA5','FF42BD','FF4AB5', 'FF52AD'] vrc = vlc_remote_contol(vhost,buttons0,"COM3") vrc.control() except: input("Connection or authorization error.") return if __name__ == "__main__" : main() 



Conclusion

I understand that I did not invent anything original and new. This is my first experience in industrial programming, before that there were only laboratory work at the University and solving Olympiad problems. I look forward to a community of constructive criticism and tips to improve the script. So far there are plans to add key codes to the config.xml file, what other remotes were to use, and perhaps a simple graphical interface.

All sources are published on GitHub .

Source: https://habr.com/ru/post/232401/


All Articles