USING an MQTT Broker
The idea of using a relatively old smartphone, such as a Samsung Galaxy Nexus for example, is not new, however not many information are available.
To achieve this goal these are the elements of the solution:
- Hardware:
- A small server to collect the sensor data running Linux or Windows.
- It can be an x86 PC desktop/ server or an SBC like the Raspberry PI.
- An old android smartphone, with as many sensors as possible.
- The Samsung Galaxy Nexus has the advantage of being equipped with Barometer, Accelerometer, Gyroscope, Compass and Proximity sensors, but any old high end android smartphone would be usable.
- A small server to collect the sensor data running Linux or Windows.
- Software:
- An Open Source MQTT Broker like MOSQUITTO.
- As an MQTT broker it takes care of receiving and dispatching MQTT messages.
- An Open Source MQTT Subscriber like the MOSQUITTO Sub.
- It will be used to pass the subscribed MQTT messages to another processing element.
- An android MQTT Publisher like Sensor Node.
- It will take care of transforming the smartphone’s sensor data to MQTT messages to be sent to the listening broker.
- An Open Source MQTT Broker like MOSQUITTO.
HOW TO DO IT
The MOSQUITTO Broker
Installing MOSQUITTO onto a system running Ubuntu Linux, is as easy as:
sudo apt-get install mosquitto mosquitto-clients
# after the installation completes then edit the mosquitto config file
# /etc/mosquitto/mosquitto.conf
# This is an example configuration:
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
# -----------Start of configuration---------------------
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
log_type error
log_type warning
log_type notice
log_type information
connection_messages true
log_timestamp true
store_clean_interval 120
include_dir /etc/mosquitto/conf.d
# -------------End of configuration---------------------
# With this configuration the mosquitto broker will listen on port 1883 TCP
# Then restart the mosquitto service:
sudo service mosquitto restart
# and check it with:
sudo service mosquitto status
If everything will be ok you should see something like this:
● mosquitto.service - LSB: mosquitto MQTT v3.1 message broker
Loaded: loaded (/etc/init.d/mosquitto; bad; vendor preset: enabled)
Active: active (running) since Mon 2017-01-09 16:10:57 CET; 36s ago
Docs: man:systemd-sysv-generator(8)
Process: 7690 ExecStop=/etc/init.d/mosquitto stop (code=exited, status=0/SUCCESS)
Process: 7704 ExecStart=/etc/init.d/mosquitto start (code=exited, status=0/SUCCESS)
Tasks: 1
Memory: 660.0K
CPU: 59ms
CGroup: /system.slice/mosquitto.service
└─7718 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
The Smartphone
Before installing Sensor Node, it is strongly suggested to upgrade the android system to Lollipop (5.0 – 5.1.1) or Marshmallow (6.0 – 6.0.1), if feasible. A search through XDA Developers Forum will definitely help on this matter
Then install Sensor Node from the android Play Store as any other android app. There are two version of Sensor Node, the free version and the paid version. The free version would be just fine for getting acquainted to the set up and do some testing. The Sensor Node app is not a fully foolproof application and bear with it some glitches: it doesn’t support SSL/TLS, it may behave strangely if the smartphone is not kept constantly alive and it may also hangs up sometime, but it would suffice for the casual user. After all it was a “dust collecting” smartphone, revived… If, by whatever reasons you cannot install the Sensor Node through the Google’s Play Store, here you can download the apk file and then install it manually on the smartphone.
|
|||
Once Sensor Node is installed it is possible to check the sensors supported. Those indicated with N/A are either non-existent with the smartphone hardware or may require a non standard or proprietary API to be utilized. In this example, the Samsung Galaxy Nexus has the following sensors:
|
|||
It is also possible to view the sensor values in graphical mode, although by stopping the MQTT transmission, which is a very convenient way to check the sensors capabilities before using them. | |||
Once the MQTT Stream is configured with:
(!!important!! the “tcp” protocol name shall be lowercase for the stream to work correctly)
(i.e. “sensornode/livestream/CENX1”) Select the sensor/s you want to stream through the MQTT protocol and by pressing the lower button, to change it from “off” to “on”, the MQTT messages of the smartphone will start flowing to the MQTT Broker. |
|||
The MOSQUITTO SubscriberOnce the MQTT Stream is flowing from the smartphone to the broker, you can receive the MQTT messages with an MQTT subscriber client, like the MOSQUITTO Subscriber client: |
|||
Sensor Data only
|
Topic with Sensor Data
|
||
For more information please visit the MOSQUITTO Documentation pages or the relevant “man” pages |
USING a TCP STREAM
Something very similar to what has been described so far, using an MQTT Broker, can be achieved by using a different APP on the smartphone and by using “netcat” or “socat” onto the receiving server.
The SENSOR FUSION App
The SENSOR FUSION App was developed by Gustaf Hendeby as part of introducing the app as part of a lab in the Sensor Fusion course at University of Linköping the spring of 2013, and it was meant for exchanging sensor data with MATLAB. However the app can gather data from the smartphone sensors and send the data through a TCP stream to a configurable port. The App is pretty straight forward to configure and use. Here follows some screenshots:
To access the settings you have to click on the “red gear” in the bottom right corner of the screen, which is a little counter intuitive at the beginning. |
Before hitting the “Start” button on the “Log Data” screen, make sure that you have the receiving end, ready to accept the data, by listening on the configured TCP port, in this example “TCP 3400”. You can achieve this by running “netcat” onto a terminal of the server, as follows:
sudo netcat -k -l -p 3400
# the "-l"option means "listen" on the port (TCP by default)
# the "-k" option means "keep listening" on the port
# the "-p XXXXXX" select the port number to listen to
# once the smartphone App start sending data you should see the following on the server terminal
2769197442 ACC -1.3024458 5.93762 7.728483
2769197445 GYR 0.057421334 0.064751714 -0.014660766
2769197451 ACC -1.2737153 5.9184666 7.8912888
2769197454 GYR 0.030543262 0.0 -0.02321288
2769197461 ACC -1.2449849 5.995081 7.9966335
2769197464 GYR 0.0 -0.07452556 -0.02321288
2769197471 ACC -1.2354081 6.033388 7.8912888
2769197474 GYR -0.014660766 -0.14782938 -0.015882496
2769197481 ACC -1.1779473 5.899313 7.469909
2769197484 GYR 0.0061086523 -0.16615535 -0.007330383
One thought on “REVIVE AN OLD SMARTPHONE AS AN IOT SENSOR”
Comments are closed.