Status Indicator for Your Door

Alex here. The first project I’ve decided to take on is one that I thought of right off the get-go. I thought, “wouldn’t it be nice to let people that come looking for me know where I am?” Well, the OBVIOUS solution is to create an overly complicated Raspberry Pi based system complete with a 16×2 LCD screen, Python backend, and PHP server… and that’s exactly what I’ve done.

Part 1: Desired Product

The end goal was to have a screen mounted to my door that would tell people coming by looking for me where they could find me (or if they should not bother me). I would update the screen by making a PHP web app that would be hosted on the RasPi itself. I could also program in times that I would always be busy (say for example, classes or work), but I’m too lazy.

An example use case is as follows

I leave my room, headed off to class. I open up the bookmarked web app on the home screen of my phone, and tap the class button. The PHP page runs a Python script on the Raspberry Pi that updates the screen. I go about my day.

Part 2: Equipment

To do this project like I did it (and you don’t have to), you’ll need the following items:

  1. Raspberry Pi 3B+
  2. 16×2 LCD Screen with I2C Backpack
  3. 4 DuPont Jumper Leads (F-F)
  4. (not an item, but) a WiFi connection
My Raspberry Pi Setup

Part 3: Execution

We start at the lowest level: updating the screen. I followed the tutorial by www.circuitbasics.com (linked here), but I’ll include the basics in this tutorial for simplicity’s sake. Connect the screen to your Raspberry Pi using the built-in I2C pins labelled SDA and SCL according to the online pinout. Don’t forget the 5V power and GND as well. Next, enable I2C on your Pi by using sudo raspi-config. Restart the Pi, and install i2c-tools and python-smbus using the built-in package manager, apt-get (for debian/raspbian). Now, glance over the library and get a feel for the functions – if you don’t have the know-how for this, you should follow the tutorial I linked above. Using those, test your display by writing a simple Python script in a separate file. It’ll be something along the lines of

import I2C_LCD_driver
from time import *

mylcd = I2C_LCD_driver.lcd()

mylcd.lcd_display_string("Sleepdrunk is", 1)
mylcd.lcd_display_string("cool", 2)

Great, now you have a working LCD. Now, make a Python script that takes an input of arguments and then outputs them on the display. Here’s my code:

import sys
import I2C_LCD_driver as lcdDriver

def update(text1, text2):
	"This will update both lines"
	if (validateString(text1) and validateString(text2)):
		lcd = lcdDriver.lcd()
		lcd.lcd_display_string(text1, 1)
		lcd.lcd_display_string(text2, 2)
		return True
	else:
		return False

def validateString(stringToValidate):
	"This will return true if a string is less than or equal to 16 characters"
	if len(stringToValidate) <= 16:
		return True
	else:
		return False

if len(sys.argv) == 1:
	update("", "")
elif len(sys.argv) == 2:
	condition = validateString(sys.argv[1])
	if condition:
		update(sys.argv[1], "")
	else:
		exit()
elif len(sys.argv) == 3:
	condition = validateString(sys.argv[1]) and validateString(sys.argv[2])
	if condition:
		update(sys.argv[1], sys.argv[2])
	else:
		exit()
else:
	exit()
The screen outside my door

Now- the most complicated part of this is the PHP. Why? Because PHP sucks ass. Don’t @ me. I’m not going to put all of my page code up here because it is a bit overkill and I think you should customize it how you want, but here’s the PHP itself.

<?php
	$strings = array(
		'here' => array(
			'line1' => 'Alex is here',
			'line2' => 'Come on in'
		),
		'busy' => array(
			'line1' => 'Alex is busy',
			'line2' => 'Please knock'
		)
		// Etc, add whatever you want in this format. Don't forget the comma after every middle entry
	);

	$mode = htmlspecialchars($_GET["mode"]);
	$customtop = htmlspecialchars($_GET["top"]);
	$custombottom = htmlspecialchars($_GET["bottom"]);

	$line1 = "";
	$line2 = "";

	if ($mode == '') {
		$jsonString = file_get_contents("/files/phpScreenData.json");
		$jsonData = json_decode($jsonString, true);
		$line1 = $jsonData["values"][0]["text1"];
		$line2 = $jsonData["values"][0]["text2"];
	} elseif ($mode == 'custom') {
		$line1 = $customtop;
		$line2 = $custombottom;
		update($line1, $line2);
	} else {
		$line1 = $strings[$mode]['line1'];
		$line2 = $strings[$mode]['line2'];
		update($line1, $line2);
	}

	$difference1 = 16 - strlen($line1);
	$difference2 = 16 - strlen($line2);

	for ($i = 0; $i < $difference1; $i += 1) {
		$line1 = $line1 . " ";
	}
	for ($i = 0; $i < $difference2; $i += 1) {
		$line2 = $line2 . " ";
	}
	echo $line1 . "<br>" . $line2;

    $top = "";
    $bottom = "";

	function update($topText, $bottomText) {
		$values[] = array('text1'=>$topText, 'text2'=>$bottomText);
		$json_data = json_encode(array('values'=>$values));
		file_put_contents('/files/phpScreenData.json', $json_data);

		$command = "sudo python /home/pi/screen/ScreenUpdate.py \"" . $topText . "\" \"" . $bottomText . "\"";
		exec($command);
	}
?>

This code is meant to run inside an element on the page that shows what the screen is currently displaying and it assumes a few things: that you have your python files in /home/pi/screen/ and that you’vechmod-ed a directory at /files/ to be 777. Again, if that sounds like alien speak to you, this tutorial might be a bit too far up of a place to start- familiarize yourself with Linux a bit first… not that I’m some Linux elitist, I promise I’m not; it’s just better for IOT applications.

Essentially what all that code does is that it looks for when the page is run with GET request arguments at the end, something like ?mode=here or, in mine, ?mode=custom&top=Sleepdrunk&bottom=is%20cool. They’re not supposed to be used for this purpose, but it’s just a whole lot more slick than using POST. You might’ve seen these before, namely on YouTube (/watch?v=VIDEO_CODE).

My final backend page

Part 4: Conclusion

After all that garbage, you have a working system… sick. It’s sorta cool, a little impractical, and not extremely useful, but hey, it isn’t a commercially available product and it fixes a problem that I had, so what’s the big deal? Anyway, I hope you hated PHP as much as I did (I mean seriously, who came up with error 500 anyways?) and that you enjoyed this read. Have a good one.

Alex
Sleepdrunk Host & Blog Contributor

About the author

Alex

I'm a college student with simple desires: happiness and freedom. Also, I have a passion for technology and engineering. They say electricity runs in my blood- it's a serious medical issue, and I need to go get it checked out.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *