Encoding data
- Introducing JSON
- JSON — 7159
- Python 2
json
- Python 3
json
- Python 2 data structures
- Python 3 data structures
- Trying out JSON
Tasks
Represent the following with Python/JSON.
- Voltage
- Color as RGB values
- Set of pins to turn on
- List of pushed buttons
Getting a web site working
No matter which web server you download, you will need to add the
www-data
user to the
gpio
and i2c
groups.
You should also add your own account to the
www-data
group.
There are a few choices for web servers. (I think) You can install more than one web server, just be sure that only one is enabled at a time.
Apache
This is the old standard. We run it at www.cs.unca.edu . Exploring BeagleBone, the BeagleBone version of the textbook, which is available on-line from the UNC Asheville library, has a section of its chapter 10, which show you how to install the Apache web server.
Here are more links to installing Apache with CGI scripts.
- Setting up an Apache Web Server on a Raspberry PiK — skip PHP
- Put files in /var/www/html or allow user directories
- Enable CGI
with files in /usr/lib/cgi-bin/
sudo a2enmod cgid; sudo service apache2 restart
lighttpd
Many think the Apache is too heavy, especially for a Raspberry Pi. In the Fall 2016 CSCI 178 we used lighttpd. It worked quite well. You can follow these installation steps.
NGINX
The textbook has detailed instructions (pp 488–498) on using NGINX (engine-X) which is becoming popular. You can also follow a tutorial on NGINX and CGI to install it.
Before going on...
Regardless of the route you take, make sure your web server is working with CGI before proceeding. I suggest you try installing the shell script listi2c.cgi in your cgi-bin directory as a test.
You should also be able to connect to the I2C display on uncacsci-pi-j.cs.unca.edu.
Back to the Pi
Grab a simple I2C board with an Adafruit library, such as a 9DOF or BMP180, and install the hardware and associated Adafruit library.
Go the the examples directory for your I2C device and look for the simpletest (or simpletest.py) program.
Copy the program and modify it as follows:
- Add the appropropriate hashbang to the beginning of the program.
import json
- Remove any loops. Make the program only take a single reading
- Store the reading in a Python dictionary
- Use
json.dump
to encode the dictionary - Print the following three lines
- Content-Type: application/json
- a blank line
- the JSON-encoded reading
After testing the program:
- Rename the program to have the extension .cgi
- Make the program executable
- Move the prgram to /usr/lib/cgi-bin
If all else fails
Read my history from two days ago.
Tasks for the next couple of weeks
Pi Wi-Fi
Take your favorite Pi home. Grab one of those USB WiFI adapters and try to connect your Pi to your home WiFi. I just followed the instructions in the Setting WiFi up via the command line page.
Skim through the following sections of the textbook Exploring Raspberry Pi. You can also read very similar material in Exploring Beagle which is available on-line from the UNC Asheville library.
- Chapter 3 — section on Managing Linux Systems
- Chapter 4 — Interfacing Electronics
- Chapter 5 — very short section on on JavaScript and Java
- Chapter 8 — section on I2C