I am trying to read various values from a Sense HAT by creating a CGI script that measures the values and returns them in the form of a JSON document.
This is the code running in the script.
#!/usr/bin/python3
# Script to read sensor data from the sense hat and return it as a JSON document
import sense_hat
import json
import cgitb
cgitb.enable()
# Set up sensehat.
sense = sense_hat.SenseHat()
# Calling these functions because the get functions will sometimes return zero when used for the first time.
sense._init_humidity()
sense._init_pressure()
# Set up json document
output = {
"humidity": sense.get_humidity(),
"pressure": sense.get_pressure(),
"temperature" : sense.get_temperature()
}
# Encode json to string and return to STDOUT
print("Content-Type: application/json\n")
print(json.dumps(output))
I have set up the apache2 webserver to accept .py files in the /var/www/cgi-bin folder. A simple hello world has verified that this is working.
When attempting to access the script through a browser, an Internal Server Error is reported. I've manually executed the script as www-data (the user that runs cgi-scripts) and I think that user doesn't have enough permissions to use the sense hat.
Traceback (most recent call last):
File "./throw.py", line 12, in <module>
sense = sense_hat.SenseHat()
File "/usr/lib/python3/dist-packages/sense_hat/sense_hat.py", line 92, in __init__
self._stick = SenseStick()
File "/usr/lib/python3/dist-packages/sense_hat/stick.py", line 57, in __init__
self._stick_file = io.open(self._stick_device(), 'rb', buffering=0)
PermissionError: [Errno 13] Permission denied: '/dev/input/event0'
How can I best go about this?