Introduction: Live Streaming Programmable LCD Screen Via Raspberry PI and PI Camera

A while back I hooked up a small 16x2 LCD screen to my Raspberry Pi. I pointed my Pi camera at the screen, wrote some PHP to update it from a web interface and unleashed it upon the world. As of the time of this writing, it is still live and online. To view the end result of this project, visit http://slasho.me For some reason, a lot of people really liked it and wanted to know how I did it.

I've never really done an instructable, nor am I sure if I remember 100% of the process. But in this instructable, I'm going to do my best to guide you through the process so that you too can recreate this project.

Let's get started!

Step 1: Introduction / Materials

Step 2: Prepare the Raspberry Pi

First of all, you'll want to install the latest version of Raspian on your Raspberry Pi. It can be found here. While downloading/writing this to your microSD card is simple, it's beyond the scope of this instructable. Fortunately, there is a guide to accomplishing this task on the official Raspberry Pi website here

Step 3: Connecting Your LCD Screen to Your Raspberry Pi

I used a module 1602 LCD screen, and although I linked to a pre-soldered one in step one, you can find unsoldered screens if you are looking to save some money. Also, some have a control board on the back which seems to eliminate the need for a breadboard setup, but I haven't personally tried this method.

To connect your Raspberry Pi to the LCD screen, follow the video linked in this step. It should be noted also that I installed a potentiometer on mine. This helps tremendously with the contrast. You can find a cheap one here.

Step 4: Test Your LCD Screen

It's a good time to test your screen to be sure you can write to it. Power on your Pi. (I suggest a good 2.0A power brick, as the screen will draw power from the Pi, as well as anything else you're using, such as a wifi adapter, etc)

Connect your Raspberry Pi to an HDMI screen and plug in a USB keyboard. Once booted up, there's a few pieces of software you should install.

sudo apt-get update
sudo apt-get install build-essential python-dev python-smbus python-pip git python-dateutil

You'll also want to install the Adafruit CharLCD module

sudo pip install adafruit-charlcd

Your screen should be powered on, and you should be ready to write to it. type "vi test.py" (or use your Linux editor of choice" and paste in the following code:

from time import sleep
from Adafruit_CharLCD import Adafruit_CharLCD
# instantiate lcd and specify pins
lcd = Adafruit_CharLCD(rs=26, en=19, d4=13, d5=6, d6=5, d7=11, cols=16, lines=2) 
lcd.clear() 
# display text on LCD display \n = new line 
lcd.message('Adafruit CharLCD\n  Raspberry Pi')

You should see your message displayed at this point on the screen.

Step 5: Misc. Software Setup

There are a few more things that need to be addressed on the software side while you're booted into the Linux console.

Stop the graphical desktop from loading

This will be a headless project, so there's no need for the Pixel UI to be loaded, consuming resources.

sudo raspi-config

Choose option 3 - Boot options. Then the first option "Desktop/CLI". Choose "Console", hit tab and select "OK"

Enable the camera

As long as we're in raspi-config, there are a couple more things we need to do. On the main screen choose option 5. "Enable Camera".

Enable SSH

Also, our primary means of communication from here on out will be via SSH, so choose option 7 "Advanced Options", then choose "SSH", and choose "Yes".

Expand the filesystem

Also, while you're in raspi-config, you -may- want to expand the filesystem to use the entire microSD card, but this is optional. If so, select option 1 on the main menu.

When you exit, you may be asked if you want to reboot. Choose no. We still need to do a couple more changes, potentially.

Setting a static IP address

You will probably want to set a static IP for this project, to ensure you always have the same IP address. In the past, this was done by editing /etc/network/interfaces, however it should be noted at the time of this instructable the process is changed. Use vi (or your favorite editor) to edit /etc/dhcpcd.conf and go to the bottom of the file and add the following information:

interface eth0 
static ip_address=(YOUR IP ADDRESS HERE)/24
static routers=(IP OF YOUR ROUTER/AC/GATEWAY)
static domain_name_servers=(IP OF YOUR DNS SERVER, PROBABLY SAME AS ABOVE)

(Note the interface will be different if you are using wifi, and some additional files will need to be edited to tell your wifi adapter which SSID to connect to and any passwords it may need. Configuring this is beyond the scope of this instructable. For more information on setting up wifi on the Raspberry Pi, see the official instructions here.)

This should give you a static IP when you reboot. Go ahead and reboot your Pi and try to login. If you're on a windows client, you can use software such as PuTTY. Your default login is pi, default password is raspberry.

NOTE: It is recommended you change this password. To do this type:

passwd

You will be prompted for your old password, and then asked to enter a new one.

Step 6: Installing the Streaming Camera Software

This project uses a modified version of the RPi-Cam-Web-Interface software. It is very robust, but due to the nature of the project, many of its features will be stripped away. Installing this software is accomplished with the following commands:

git clone https://github.com/silvanmelchior/RPi_Cam_Web_Interface.git
cd RPi_Cam_Web_Interface
chmod u+x *.sh
./install.sh

You'll be asked a few choices during the install. I recommend going with the defaults. As for the webserver, make sure you choose Apache. This (should) install apache, php, and several other packages that are needed to make the software work.

At this point, you should be able to point your camera at your screen and open a web browser pointed to your IP address. http://(RASPBERRY PI IP ADDRESS) If all has gone well up to this point, you will see a live feed of your camera in the web page.

Step 7: Locking Things Down

The camera software is very robust, too robust if you intend to open this up for the world. It needs to be locked down considerably. Below are some links to files that need to be replaced.

To simplify this, I've made a tarball of my directory. You can download it here.

In the /var/www/html directory, remove all files and directories except for status_mjpeg.txt, raspimjpeg and cam.jpg, which are links to other files and devices. Then extract the tarball into /var/www/html

There are also a number of helper scripts that run. This includes a python socket server that interfaces with the screen, a script that counts the daily users and a few other scripts that help keep spam to a minimum. These files can be downloaded here and should be placed in /usr/local/bin.

Next, use vi (or your favorite text editor) and edit /etc/rc.local to add the following lines:

python /usr/local/bin/server.py &
/usr/local/bin/clearspam.sh &

This ensures the socket server and spam helper scripts start at boot.

Next, enter the following commands:

    sudo mkdir /var/log/lcd
    sudo chown www-data:www-data /var/log/lcd
    cd /var/log/lcd
    sudo touch last20.txt lcd_screen.log lcd_spam.log spammer.log
    sudo chown www-data:www-data *

    You're almost done. Reboot your Raspberry Pi.

    Step 8: Setting Up Logrotate (optional)

    Logrotate is a great piece of software and semi-important to this project. It ensures your log files don't get too large and unwieldy. But also for this project it makes sure you have good, reliable data when you type @users to find out the daily users of your LCD screen.

    If you want to set this up, first 'cd /etc/logrotate.d' and make a new file called 'lcd'. Then copy/paste the following into that file:

    /var/log/lcd/lcd_screen.log {
    	daily
    	missingok 
    	rotate 14
    	create 640 www-data www-data 
    }
    /var/log/lcd/lcd_spam.log {
    	daily
    	missingok
    	rotate 14
    	create 640 www-data www-data
    } 

    Step 9: Final Step / Thoughts

    You'll want to secure your setup in such a way that it won't be easily moved around. As you can see in the picture, I've used a simple box and zip ties to get the job done. When the lid is closed, the camera lines up with the screen.

    If all has gone well, once you reboot your Raspberry Pi, you should be done. Point your camera at the LCD screen and in a browser, go to http://(RASPBERRY PI IP ADDRESS) . You -SHOULD- see your screen and 2 input lines, along with some other information. Try typing into the lines and hit "Submit It". If your LCD screen updates and displays your text, then you're good to go. Don't forget that if you want the outside world to be able to access your creation, some port forwarding to your Raspberry Pi may be needed on your router. The webserver uses the standard port 80.

    Congratulations!

    Troubleshooting

    If your LCD screen doesn't update, there are a few things you can check. Look in /var/log/apache2/error.log to see if there are any errors. Also, do a 'ps -edf | grep server.py' to ensure the listening server is running. If it isn't, try to start it manually with 'python /usr/local/bin/server.py' and then look for any errors and try submitting again.

    If you can't see an image on the screen, ensure your camera is set up correctly. Try typing 'raspistill -o cam.jpg' and then view the cam.jpg image. It should be a still capture from your camera. If this doesn't work, your camera may not be set up correctly. Try following the instructions here. Also, raspimjpeg may not be running. This was installed with the RPi camera software. do a 'ps -edf | grep raspimjpeg' to see if the process is running. If for some reason it isn't, start it manually. Double check /etc/raspimjpeg to make sure it is set to autostart. Look for the line 'autostart standard' and ensure it's not commented out.

    Final Thoughts

    Initially this project received a lot of traffic. Spam was a big problem, so I tried a lot of different methods, including Apache modules which never seemed to really work out too well. For the record, I tried spamhaus and evasion. It could be they are helping, but until I rolled my own solution, spammers plagued me pretty badly.

    The code could be cleaned up CONSIDERABLY, but it's good enough for now. The reason I'm putting this out there, aside from several people asking about it, is that I know the concept can be improved upon. I get by with python and php, but I know my methods could likely be done better, and more efficiently. Maybe with some luck, someone will pick this up and release a better, 2.0 version. For now though, I'm happy with the code and what it does.

    The main workhorse of this project are the files server.py and sendToLCD.php. server.py is a socket server that runs on port 9997 and is the only thing that writes to the LCD screen.

    Most of the sexy code happens in sendToLCD.php, including checking for spammers, bans and admin messages. A note on admin messages: In sendToLCD.php is a location containing IP addresses I am usually coming from. This information can be changed to your needs. This is what will put the little 'x' on the first block of the LCD screen when you post, letting ppl know that the poster is, in fact, the admin.

    That's it. If you have any questions or get stuck along the way, I'll help out if I can. Feel free to contact me at hiro24@gmail.com

    Good luck!