Categories
Linux Ubuntu

CMUS – A command line music player.

cmus is command line based music player for linux based operation system. It supports various output methods by output-plugins. It can be controlled from outside using cmus-remote.

We can use cmus for playing audio on local drive, audio hosted on network or any live stream. I have used cmus to play online radio stations.

There are 7 views in cmus. Press keys 1-7 to change active view.

Library view (1) , Sorted library view (2) , Playlist view (3) , Play Queue view (4) , Browser (5) ,Filters view (6) , Settinsgs view (7)                                                                                                                               Multiple Views of cmus

cmus_all_views

Cmus Control                                                                                                                                       cmus is conrolled using commands and key presses. Press “:” to enter comand line and “Esc” to come out of command mode. Commands can be auto completed by pressing “tab”.

Press “a” in command mode to add any music to library. Use following key presses to copy marked or selected tracks from views 1-5

a copy tracks to the library in view (1-2)                                                                                          y copy tracks to the playlist in view(3)                                                                                                e append tracks to the play queue in view (4)                                                                                  E prepend tracks to the play queue in view (4)

Common cmus commands

q quit -i
:q exit cmus.
b player-next
c player-pause
x player-play
z player-prev
v player-stop
+ volume +10%
– volume -10%

cmus-remote

This is a utility to control running cmus from outside cmus process.  If no argument is given it connect to local cmus socket (~/.cmus/socket). It can also connect o external cmus using  “–server SOCKET”  argument.

Some common commands to control are

-p Start playing.
-u Toggle pause.
-s Stop playing.
-n Skip forward in playlist.
-r Skip backward in playlist.
-R Toggle repeat.
-S Toggle shuffle.
-v VOL Change volume.
-k SEEK Seek.
-Q, Get player status information
-l Modify library instead of playlist.
-P Modify playlist (default).

 

cmus playlist

You can create a playlist and put in cmus home. It will be visible in playlist view(3). Some of the bollywood channels are listed below.

pi@raspberrypi:~/.cmus $ cat > playlist.pl

http://50.7.77.114:8296/

http://174.36.206.197:8198/

http://198.100.149.27:8012/

http://uk2.internet-radio.com:8043/live

http://69.64.61.173:8057/stream

http://50.7.77.114:8296

http://5.9.66.201:8896

http://185.66.249.48:9600/stream

http://69.162.111.146:9960

http://192.240.102.133:8512/stream

 

 

Categories
Linux Networking Python Ubuntu

MQTT Python Client

In this article, I want to create a python module which would connect to mosquitto server. We can connect a MQTT client as subscriber or publisher of message.

Mosquitto python module has been donated to Eclipse Paho project, which is open-source client implementations of MQTT.  This module can be installed using pip command.

$ sudo pip install paho-mqtt

Subs.py – The code given below is subscribe module. You can look at comment to understand in detail.


#The line imports python mqtt module.
import paho.mqtt.client as mqtt

#Here we initilize variables used in the code.
server = "192.168.0.XXX"
port = 8883
keep_live = 45
topic = "msgTopic"

# Define routines to register.
# Client - The instance at the client side.
# userdata - Data to be passed by the user.
# flags - response flag sent by the server.
# rc - Return code, 0 is successful connection.
# mid - message id
# granted_qos - Quality of service granted by the server, default 0.
# payload - the message received on the topic.
def on_connect(client, userdata, flags, rc):
 mqttc.subscribe(topic, 0)
def on_subscribe(client, userdata, mid, granted_qos):
 print("Subscribed to: "+str(mid)+" "+str(granted_qos))
def on_message(client, userdata, msg):
 print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))

# Initilize a client instance and ser password for the connection.
mqttc = mqtt.Client()
mqttc.username_pw_set(username="username",password="password")

# register the routines.
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe

# connect to the server and wait for message.
mqttc.connect(server, port, keep_live)
mqttc.loop_forever()

Pubs.py – The code given below is publisher module. You can look at comment to understand in detail.

#The line imports python mqtt module.
import paho.mqtt.client as mqtt

#Here we initilize variables used in the code.
server = "192.168.0.191"
port = 8883
keep_live = 45
topic = "msgTopic"

# Connect and publish modules
def on_connect(client, userdata, flags, rc):
 mqttc.subscribe(topic, 0)
def on_publish(client, userdata, mid):
 print "Message Published..."

# Initilize a client instance and ser password for the connection.
mqttc = mqtt.Client()
mqttc.username_pw_set(username="mqtt",password="mqtt")

# register the routines.
mqttc.on_publish = on_publish
mqttc.on_connect = on_connect

# Connect and publish the message on a topic.
mqttc.connect(server, port, keep_live)
mqttc.publish(topic,"Hello! How are you?")
mqttc.disconnect()
 Output – Below is the Publish and Subscribe module in action on raspberry pi.
subs_pub
Categories
Linux Ubuntu

MQTT

MQTT is simple publish subscribe message broker, which is used for IOT based machine to machine (M2M) communication. It is simple to use. It needs a MQTT server, which can be hosted on any linux or windows machine.

Execute The following line with root privilege, it would import security code and add to local key repository and install mosqitto service.

$wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
$sudo apt-key add mosquitto-repo.gpg.key
$cd /etc/apt/sources.list.d/
$sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
$sudo apt-get install mosquitto

Making mosquitto secure – Add below lines to the file /etc/mosquitto/mosquitto.conf. This would make the mosquitto listen on port(8883) at all the interfaces connected to Raspberry pi.

listener 8883 0.0.0.0
password_file /etc/mosquitto/pwfile

Update the password file with the username you want to connect to the mosquitto service and restart the mosquitto service.

$ sudo mosquitto_passwd -c /etc/mosquitto/pwfile mqtt
Password:
Reenter password:
$ sudo service mosquitto restart
$ ps -aef | grep mosq
mosquit+ 2150 1 0 09:55 ? 00:00:00 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

 

Testing the service – You can use many clients, e.g. python paho-mqtt, android based client, online client. I have used chrome based app called MQTTLens. It can be found on chrome app store.

Add a New connection

connection

Subscribe & Publish

publish_subscribe

To remove mosquitto run apt-get purge.

$sudo apt-get purge mosquitto

There can be multiple use of MQTT, I will use this to communicate with raspberry-pi, ESP8266 and Arduino modules in coming posts.