Android smartphones can be made to partially work like Raspberry Pi like single board computers. That is probably useful for different reasons. To follow the steps of this guide, you need to read our previous guide for the basic setup with Termux. Although in that guide, we mainly focused on how to use Node-RED, there are is the installation of termux-api
and some useful links. Any Android device has restriction around using the GPIO. But actually, we can collect the sensor data using the termux-api
, install Python data science tools and also it is possible to indirectly control LED, servo motors connected with an ESP32 over Bluetooth. It will be a complicated idea to build Android-version of TJBot in this way.
We are completely avoiding the steps on how to add any IoT to a device on the IBM Watson IoT web interface. That is like we did with ESP32. You must complete adding the device step to get the auth token and other details. For port number 1883, you will have to set the policy as “TLS optional”. Many newbies forget to make “TLS optional” (as it is the developmental purpose) and failed to connect – it is a common matter with IBM Watson IoT.
Open Termux. Update it and run the below command to install Python :
---
1 2 3 | # pkg install clang python # |
The package includes pip
and probably python-dev
. Just to recall, on Raspberry Pi, we would run :
1 2 3 4 | # sudo apt-get install python-dev python-pip # |
Upgrade pip :
1 2 3 | # python -m pip install --upgrade pip # |
Install Watson IoT SDK, you can separately install the dependency paho-mqtt
:
1 2 3 4 | # pip install paho-mqtt pip install wiotp-sdk # |
IBM has one example repo for Raspberry Pi, you can wget
it for testing purpose :
1 2 3 4 5 6 7 8 9 | # apt install wget pip install psutil wget https://github.com/ibm-watson-iot/iot-python/archive/master.zip unzip master.zip cd iot-python-master/samples/psutil/src python iotpsutil.py --quickstart # |
psutil
will not work anymore on newer Android. You’ll get erroneous permission error against /proc/stat
. But, the output will assure you that the setup is otherwise perfect. But, you can look at the samples provided in the GitHub repo of IBM Watson IoT SDK (which you have installed a few minutes/steps before) :
1 2 3 4 5 | # https://github.com/ibm-watson-iot/iot-python # |
Like this example “Python Simple Device” (it connects & disconnects, which is possible to trace from IBM Watson IoT’s account dashboard) :
1 2 3 | # https://github.com/ibm-watson-iot/iot-python/tree/master/samples/simpleDevice # |
There are various ways to set up the environment to “remember” your credentials. One is running these commands (after edit) :
1 2 3 4 5 6 7 8 9 | # export WIOTP_IDENTITY_ORGID=myorgid export WIOTP_IDENTITY_TYPEID=mytypeid export WIOTP_IDENTITY_DEVICEID=mydeviceid export WIOTP_AUTH_TOKEN=myauthtoken # |
Another way for “Python Simple Device” like Python applications is to create a file named device.cfg
in the simpleDevice directory and add the org, device type, device id and the authentication token (edit it) :
1 2 3 4 5 6 7 | [device] org=$org type=$type id=$id auth-method=token auth-token=$token clean-session=true |
.
There is an example termux-api
wrapper for Python :
1 2 3 | # https://github.com/Ublimjo/termux # |
You can edit the “Python Simple Device” script and execute the termux-api
commands to grab data from the sensor.
This ends this small guide.
Tagged With android termux mqtt paho , how do i access Ibm cloud from termux , ibm cli on termux