Turn Your Old Android Phone Into A Local Odd-Jobs Server Using Termux And Sinatra
Being the only software engineer in my family, I’m like the go-to guy for anything even remotely related to computers. Of course, I’ve always enjoyed solving problems…the first time at least. Repetition is something that all software engineers dislike. When we see some part of code repeating, we tend to throw it in a separate function. Likewise, when we are told to do some task over and over again, we try to automate it. And that is exactly what I did at my home to make my life and the life of those dependent on me a little easier.
When it comes to making a cheap local server that can run 24/7 and not put a strain on the electricity bill, a Raspberry Pi is the first thing that comes to mind. RasPis are great! They run linux, consume very little energy and are powerful and flexible enough to take care of most automation tasks that don’t require running complex deep learning models. In fact, a Raspberry Pi would have been perfect for the kind of automation I wanted to do. There was just one problem: I didn’t have one at my home. I was heading over to Amazon to buy one when I noticed my very first android phone, that I bought in 2014, glaring back at me. There lay another low-power linux device, collecting dust, that wouldn’t cost another cent.
Setting Up An Ideal Development Environment On Android
Android is technically running on a linux kernel but no shell environment or package manager is actually exposed to the user. The first thing we need to do is get a linux environment up and running so that we can install some linux cli-applications getting us closer to what a Raspberry Pi is.
- Install Termux from Github or the PlayStore. Termux is an open-source terminal application that provides a linux environment and a native package manager called pkg for Android. If you’ve used Ubuntu or OSX before, pkg is a package manager similar to apt and brew.
- Now that we have a linux environment up and running on Android, we need to decide which framework and language to use for developing our web server. Termux supports almost everything ranging from Python to Ruby to Node.js. I went with Sinatra, an open-source web-application library written in Ruby, as it is simple to use and lightweight enough to not put a strain on my six-year-old 1-GB-RAM phone. To get Sinatra up and running on Termux, run these three commands:
$ pkg install ruby$ gem install sinatra$ gem install thin
Make sure to install Ruby, Thin and Sinatra on your computer as well for ease of coding. I won’t stop you if you want to code on your phone using vim. Good luck with that!
Automating Stuff — The Fun Part
There are tons of things you can do with a local web server; it all depends on your specific use-case and what tasks you want to automate to simplify your life. For me, these are the tasks my family members often ask me to do:
Some of these tasks may seem trivial now but hold off on your judgement till I unravel exactly what is being done here. Before moving onto the implementation, make a new directory for this project inside Termux. For the sake of this tutorial, I’ll refer to this directory as old_phone_server. Keep it empty for now, we’ll populate it as and when needed.
Task 1: YouTube Media Downloader
Here’s some context: My dad owns a Windows laptop, an Android phone and an iPad. The request to download a video from YouTube can come from any of these three devices to me. Not only that, the quality at which the content is to be downloaded can vary from Full HD (to save the video in the gallery) to <16 MB video size (so that it can be forwarded via WhatsApp) to audio (to add to his music collection). There are many applications on Windows and Android that “claim” to do this job but often contain tons of bloatware and fail more often than they are successful (let’s forget about iOS). How do I do this? If you’re on a linux/unix based system, you may already be aware of this magic open-source cli-application called youtube-dl. With more than 70k stars on Github, this beautiful piece of software can do wonders when it comes to downloading stuff from YouTube and even other websites like Facebook and Reddit. So here’s the front end I made for my local app:
Neat, eh? Open the local page from any browser running on any device, select the quality, paste the link and bam, the content will be downloaded directly onto your device. Before jumping into the Sinatra code for this, install these dependencies on Termux first:
$ pkg install python$ pip install youtube-dl
Inside the old_phone_server directory, make two new directories namely views and downloads. The two .erb files shown below sit in this views directory. The downloads is where our downloaded YouTube media will be stored temporarily. The Ruby code sits in the root of the old_phone_server directory.
Both the .erb files have plain and super simple HTML code. The simple_webserver.rb is the Ruby-Sinatra code that takes care of the backend. Just try reading it and everything will make sense, I promise! After saving these files, go to the old_phone_server directory and run this command to get your server up and running:
$ ruby simple_webserver.rb -o 0.0.0.0
Don’t exit Termux after running this command. From the notification tile of Termux, choose “Acquire wakelock” to ensure that Android doesn’t kill Termux when the screen goes off.
To access this service, open <your_device’s_local_ip>:4567/youtube from any device connected to your router and enjoy the services provided by this application.
Task 2: PDF Compressor
One Google search will tell you that the internet is littered with online PDF compressors: just upload your PDF file on their website portal and they’ll give you a compressed version of it. Some of them are actually quite good. The only big concern here is privacy. PDF files can contain all sorts of private and classified data that you just don’t want anyone else to get their hands on. There’s no guarantee that what you just uploaded on these websites will be “deleted” as promised. Thankfully, there’s another famous open-source project that you can run on your terminal and get the same output as these websites (I’m pretty sure most of them are using this anyway): ghostscript. There may actually be a ghost involved here because this cli-application can shrink the size of PDFs to upto 15% of their original size with no visible loss in quality. The shrinkage, of course, matters from one file to another. If you make PDFs out of photos using applications like CamScanner and Adobe Scan, then this application can do wonders to reduce their size.
The frontend for my PDF compressor web app is pretty straightforward: choose a file, press “Upload PDF” and bam, a compressed PDF will be downloaded. Before moving onto the code for this, first install ghostscript on Termux:
$ pkg install ghostscript
Again, place the following .erb files in the views directory and save the Ruby code to the root of the old_phone_server directory (or append it to the previous simple_webserver.rb file if you want both functionalities). Also, make an empty directory by the name attachments. This is where the uploaded PDF files and the compressed files would be temporarily stored by Sinatra. Note that I’ve added the “get ‘/download’ ” code block again here just to make this gist a standalone piece of fully-functional code.
This can be run using the same command as before. The frontend for this application can be accessed by opening <your_device’s_local_ip>:4567/pdf on any device.
Task 3: Local Cloud For Local File Transfers
I once saw my sister upload a 2 GB file to GDrive from her computer and then download the same file on her phone 5 minutes later. 4 GB bandwidth wasted in mere minutes and for what, transferring files from one computer to another? Also, apparently emailing files to self is still a thing. Frankly, being so used to the KDE Connect ecosystem on linux, I failed to notice that local file transfer between different devices running other operating systems is still an issue in the real world. So, here’s my simple solution for this:
Select the user, upload the file and bam, it’ll be uploaded to the local server (good old Moto G). The front-end for the download service is almost identical to this upload one, just devoid of the “Choose file” button. Unlike the previous tutorials, no magic libraries are needed to accomplish this task, just the goodness of Sinatra.
Again, super simple frontend and backend codes. The way I’m implementing the storage and retrieval of latest files uploaded by a specific user is kind of a hack for now but hey, it works (in my defense, just 4 people are gonna use it and that too sparingly)! As for how to make this work, put the .erb files in the views directory as before, make an empty directory named uploads in the root of the project (this is where Sinatra will store the uploaded files) and run the Ruby code. To upload something to your local cloud, visit <your_device’s_local_ip>:4567/fileup from any device, choose the user and upload whatever you see fit. To download the recently uploaded file, visit <your_device’s_local_ip>:4567/filedown from any device, choose the user and download! No more messing around with Bluetooth compatibility of different devices or wasting precious internet bandwidth for file transfers.
Task 4: Shared Clipboard
How many times has it happened that you’re surfing the web on your tablet or your laptop and suddenly find some interesting text or link that you want to paste on your phone or vice-versa? This is something I’ve grown used to thanks to KDE Connect but for non-Linux devices, there’s no gold-standard solution for this. I’ve seen people messaging themselves or even using services like pastebin. This functionality is probably the simplest to implement among all the tasks I’ve laid down so far in this article, and also perhaps the most useful of them all!
The frontend for the homepage of paste is similar to the above copy one minus the textbox. Just select the user and the recently copied text will be rendered. Here’s the output of what I just copied rendered in a textbox:
Super simple frontend, eh? The textbox supports some rich-text features like rendering newline which is super important when copying loads of paragraphs off the internet. Here’s the super-simple code for doing this:
Eagle-eyed developers may have noticed that I’m using simple text files to store and retrieve text but you can use a database too. By the way, PostgreSQL and SQLite work charmingly in Termux. In time, I’ll probably also shift to the database approach for more complex text storage and retrieval operations but as of now, my customers seem pretty happy with what has been done. Also, about running this, well, figure it out! I’m not going to repeat the run instructions for the fourth time (DRY). Okay just one thing, make an empty clipboard directory for Sinatra to store text files. Okay, just one more thing, to access the copy service, visit <your_device’s_local_ip>:4567/copy and to access the paste service, visit <your_device’s_local_ip>:4567/paste from any device running any operating system.
Conclusion
Well folks, there you have it! A super simple way to not spend $35 on a Raspberry Pi and put your old android phone, that Amazon won’t take for exchange, to good use! Now that I think about it, I have a more recent android smartphone with a completely non-functional AMOLED display that would make for an even perfect device for this job! Oh wait, how will I install Termux and run the code without the screen giving me any visual cues? I guess I’ll find out and let you guys know in another Medium post! By the way, the purpose of this article was to give you a starting point and a direction. Adapt this system to your use-case(s) and share! Happy hacking :)
P.s. In case you’re wondering about how I’m managing storage space on my Moto G, I’ve created a cron job to clear out all the directories at 3 AM everyday.