Use Tasker, MQTT, and openHAB to view mobile device charge status in a browser

Technical Information Type: 
Hack
Environment: 
IoT
Problem: 

I wanted to create a Web-accessible display that would show the current charging status of my mobile devices, beginning with their current battery charge and then adding more data.

Status: 
Fixed
The Fix: 

This hack involves using Tasker, the MQTT Publisher plugin for Tasker, mosquitto, and openHAB. You can view a live demo on this site that uses a bit more "glue" to display the device values here. That demo page has links to the technology resources I used.

Because this is a fairly complex setup, I won't go into deep detail for each step, as the explanation would be much longer than appropriate here. You can find helpful information about these steps via a search on the Web.

Here is an overview of how I achieved this goal. There are other ways, this is just my way (so far).

  1. Install Tasker on your phone. I'm assuming an Android phone here, I'm not sure if Tasker is available on other platforms. Also, when I say "your phone" I really mean "each of your devices that you want to keep an eye on."
  2. Install the MQTT Publisher plugin for Tasker on your phone.
  3. Unless you know of a suitable MQTT broker (reliable, trustworthy, etc.), install mosquitto on a computer that is always on -- a Raspberry Pi or similar computer might be a good choice.
  4. Install openHAB on a computer that is always on, which might be (but need not be) the same computer that mosquitto is running on.
  5. Configure mosquitto and, assuming you want to be able to get messages through when you're away from your network (e.g., via your phone's data plan), configure your router to open the port that mosquitto will be listening on and ensure it is routed to the always-on computer running the broker.
  6. Test your configuration by creating a task in Tasker that uses the MQTT Publisher to send a message to your broker. Verify (by querying the broker) that the message was received. I'm not a guru when it comes to MQTT, so take this with a grain of salt, but I'm using the "retain" flag and QoS of 1 for my Tasker MQTT telemetry reports.
  7. Configure openHAB to connect to the MQTT broker (e.g., your mosquitto instance).
  8. Experiment with defining an openHAB item that retrieves the state of a topic from the MQTT broker, using the test topic you sent from your phone (above).
  9. Decide on the topics you will use for MQTT. This is up to you, I went with something like: devices/status/WHICHSTATUS/WHICHDEVICE (replacing WHICHSTATUS and WHICHDEVICE with the status type -- like batterycharge or uptime -- and the device name (and yes, I name my devices just like I name my other computers). I recommend topics for battery level, AC/charging status, and last boot/shutdown.
  10. Define openHAB items that will be used to reflect the MQTT topics you just chose.
  11. If you use Tasker for other things (or think you might in the future), I recommend adding a Tasker Project on your phone now for your device telemetry profiles and tasks. I call mine Status Telemetry.
  12. Add these (or similar) tasks in Tasker on your phone:
    • Report battery charge (action: use MQTT Publisher plugin to send the payload of %BATT to your phone's battery-charge topic)
    • Report charging (action: use MQTT Publisher plugin to send the payload of Charging to your phone's charging-status topic )
    • Report unplugged (action: use MQTT Publisher plugin to send the payload of Unplugged to your phone's charging-status topic)
    • Report charging complete (action: use MQTT Publisher plugin to send the payload of Charged to 100% to your phone's charging-status topic; optional action: send yourself a text or email to let you know charging is done)
    • Report shutdown (action #1: use MQTT Publisher to send the payload Shut down %DATE %TIME to your phone's boot/shutdown topic; action #2: use MQTT Publisher to send the payload Off to your phone's charging-status topic)
    • Report device boot (action #1: use MQTT Publisher to send the payload Booted up %DATE %TIME to your phone's boot/shutdown topic; action #2: hmm... I'm missing this piece right now, but you can probably figure out something useful here!)
  13. Create the matching Tasker profiles to call the Tasker tasks you have created:
    • Create a profile that repeats every 5 minutes and triggers the report-battery-charge task.
    • Create a profile that occurs when the phone is charging; the entry task should be the report-charging task, the exit task should be the report-unplugged task.
    • Create a profile that occurs when the battery is done charging that triggers the report-charging-complete task.
    • Create a profile that occurs on device shutdown that triggers the report-shutdown task.
    • Create a profile that occurs on device boot that triggers the report-device-boot task.
  14. Manually fire off the various tasks and make sure the correct payloads are going to the correct MQTT topics.
  15. Add the openHAB items (defined above) to your openHAB sitemap.
  16. View your openHAB sitemap (display) in a browser and observe the values sent from your phone.

This may take some non-trivial troubleshooting to see why values don't appear correctly. I ran into various difficulties with mosquitto and openHAB, but it was just part of my learning process, not due to flaws in the software.

Ideas? Feedback? Critiques? Leave me a comment!

Fix License: 
Creative Commons: Attribution-ShareAlike