Smashed Toilet Phone Web Server
Behold my smashed Samsung a70 smartphone. It's seen better days but it's serving up this nice webpage for your enjoyment
Don't feel bad for this beat-up phone. Sure it's been smashed, then later dropped in a toilet before sitting for over a year in a bag of rice; whatever, that just adds character to the phone, right?
Life Signs
Our story begins after that year in a bag of rice when I found it by accident in the garage, and decided to see if there were any signs of life. After a long sit on the charger I was pleased to see that indeed there were life signs, and the readings were strong! Once charged back up to full, the phone booted up as if it were brand-new in the box! That's where the fun stopped however, as it immediately became clear that the touchscreen no longer functioned, and thus its days as a portable phone were long over.
You would not be foolish to think this should be the end of the story; how are you supposed to do anything with a device you can't control? It may seem counter-intuitive but actually Android devices have pretty good mouse and keyboard support; most now even support external monitors. It stems from a desire to make the smartphone the center of your computing life. The thinking from these big tech corps is that if your phone can do everything, why even both with a desktop in the first place. That logic is debatable depending on your needs, but it helps in this case so I'll take it!
The Plan
I've long been interested in the possibilities for old phones that have outlived their 'portable phone' days. It might be because I remember when computers ran at 8 MHZ with 640 KB of RAM, and could do all sorts of fun things. These 'old' phones by comparison are hundreds of times more powerful than those old computers, and have all sorts of sensors and networking abilities built-in. Surely they can serve a need other than food for the recycler.
While I have some neat ideas for remote control and other IoT applications, a web server is kind of like the "Hello World" of repurposing hardware. If you can get it to talk to other machines, all the other features can be bolted on afterwards.
Technology Auditions
The most critical part of any project, is the technology selection stage. You need to select a solid foundation for your project, that you won't regret later on. These days there are a dizzying amount of options, but surely an Android based web server isn't common, and will have fewer options.
That thought didn't age well. It seems I'm not the only one looking at hobby or even commercial applications for web servers on Android phones, as there are a surprising number of options. Aside from native Java or Kotlin, there are apps built with Apache and PHP built-in; just upload your scripts and start serving. You can also get a linux command line with Termux, and install any environment you're used to.
Hardware First
Let's take a break from the software side, and sort out hardware first instead. Normally this is just a big a challenge as software, but since the phone isn't modular there's actually less to worry about here. The phone's WiFi works just fine but we need wired ethernet for reliability. We also want power, as the phone battery isn't going to run forever. Here's what I came up with.
The USB-C hub provides power to the phone, while also supporting peripherals like the pictured USB ethernet adapter. With the other hub slots we could potentially add USB storage, cameras, printers, etc.
With some careful networking setup, the phone will switch seamlessly between WiFi and ethernet (keeping the same IP address). If it's unplugged the battery takes over as normal; it's got built-in redundancy!
About That Battery
Undoubtedly at least one of the readers of this story will raise alarms at the prospect of keeping a lithium battery on full charge all of the time. It's a fire hazard! It can explode!
I have a fair bit of experience with charging batteries of many types, and with confidence I can say that these concerns are largely overblown. Any appropriately designed battery charging system will stop charging the battery once it's fully charged. It's very easy for a charging circuit to detect when a lithium ion battery is fully charged (especially compared to NiMH), so there's little (not nothing, but little) to go wrong. Sure if the battery is already damaged that could be a problem, but then it would have been a problem anyway. There's also some suggestions out there that leaving a lithium ion battery fully charged will degrade its max capacity over time; I haven't seen any evidence of that in practice, but in our case we don't really care much about that anyway.
If you're interested in some more basic facts about batteries, check out my Battery Basics article.
Back to Software
The first software option was an app with an AMPP stack (Apache, MySQL, PHP). It was ultra easy to setup and load files, and it just worked. If all you want is something casual, then this is probably the way to go.
There were some issues with this approach, with the biggest being reliability. The app is solid but as with most apps, it will crash or get closed by android eventually. I needed something that could restart itself if it ever crashed. There's also the point about hardware I made earlier; I wanted control over the hardware in the phone (ie bluetooth, camera, SMS), and I wasn't going to get that from PHP on android.
I finally decide upon a far more custom solution, built with Cordova and a variety of native plugins. For the web server I used a plugin that wraps the NanoHttpd Java web server library. This approach gives me complete control over the request-response flow, while allowing me to build hardware interactions into my responses. Anyone want to send SMS remotely over a web interface?
How It's Going
I've been running this setup for over a year at time of writing, and it's exceeded all my expectations. Maybe if you're reading this it's exceeded your expectations as well, since that smashed toilet phone served up this content!
The app crashes or is closed around once per week, but the alarm plugin in the app on 2 minute intervals reboots the app if it's not already running. In between the reboots the web server is snappy and versatile. I have written a whole range of hardware integrations (ie Bluetooth, SMS, UART to USB Serial), and can remote control them from anywhere.
Currently I'm using Cloudflare as a proxy, but that's mostly to be on the safe side; I have a second phone I use for development, and I'm able to access it directly, with router port forwarding, or with my local linux proxy. Cloudflare also takes care of the SSL certificate for me, but I have a working prototype of an ACME client for Let's Encrypt certificates, so I may even switch to my own certs at some point.
Future Goals
I really want to see how far I can take this, and plan to try to replace most or all of my personal and development services with an equivalent that runs on this server. I've recently completed an FTP server and client, which should allow me to move much of my development work off of my linux box. I've got some basics for SMTP which allows me to send email, but having my own SMTP server would be even cooler!
I plan to open-source everything but the caveat as I see it is that it must be hosted on Git that is hosted by the phone. I've created a project I'm calling Qik to put everything under. If you're curious about how things are going, the Qik page sums everything up. If you interested in the web server portion, the Qik Node page is dedicated to the web server.
Menu