How to enable .py scripts in Apache2
By default Python is configured to only execute scripts with specific endings such as .cgi. In order to make Apache execute .py files, you need to edit a configuration file. In a terminal type this command:
This file contains settings for the default site (you can create other sites on the same server). Look for a section of code that looks like this:
…
</Directory>
Add the following line inside the above section of code:
In a terminal window, type this command to reload Apache’s configuration files:
Now type this code into a text editor, and save it as /usr/lib/cgi-bin/hello.py:
print “Content-type: text/html\n\n”
print “<h1>Hello World</h1>”
Make the file executable using this command:
As in the previous post, you can execute this script by accessing it through your browser. The IP address of my Raspberry Pi is 192.168.0.4, so I typed this address into my browser: http://192.168.0.4/cgi-bin/hello.py. Once again, you should see the words “Hello World” in large letters.
http://raspberrywebserver.com/cgiscripting/writing-cgi-scripts-in-python.html
Disable Serial Port Login
To enable the serial port for your own use you need to disable login on the port. There are two files that need to be edited
The first and main one is /etc/inittab
This file has the command to enable the login prompt and this needs to be disabled. Edit the file and move to the end of the file. You will see a line similar to
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
Disable it by adding a # character to the beginning. Save the file.
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
The simplest way to be able to see the temperature of your raspberry is to write it in the /var/www/index.html.
You will need to set up the webserver as described in the previous post. Then write a simple script that reads in the temperature and then prints it in the index.html. Then run the script automatically as frequently as you wish with the help of the crontab software. You can set it to every minute, every hour, every two days or whatever.
So,
- nano /usr/local/bin/getTemp.sh
#!/bin/bash
cpuTemp0=$(cat /sys/class/thermal/thermal_zone0/temp)
cpuTemp1=$(($cpuTemp0/1000))
cpuTemp2=$(($cpuTemp0/100))
cpuTempM=$(($cpuTemp2 % $cpuTemp1))
echo CPU temp"="$cpuTemp1"."$cpuTempM"'C" >> /var/www/index.html
echo GPU $(/opt/vc/bin/vcgencmd measure_temp) >> /var/www/index.html
timestamp() {
date +"%T"
}
TZ=":US/Central"
echo "Time: $(date)" >> /var/www/index.html
echo "<br>"/var/www/index.html
2. Then make it executable:
chmod a+x /usr/local/bin/getTemp.sh
3. Then add it to crontab:
crontab -e
and add to the file:
*/5 * * * * sudo /usr/local/bin/getTemp.sh
to run it every 5 minutes.
This is mostly a note to myself about what I do when setting up a web server on the pi.
- Install one
-
sudo apt-get install apache2 -y
-
- This time I won’t need php, so I will install that later.
- The wbserver started running automatically after installation, so just punch in the Pi’s IP address to the browser and it should send you to the index page of the Webserver.
After this, I want to make it available from the web too, not only my local network. It is connected to the router via an ethernet cable, but I think the router would assign a new IP to it after each restart.
So I need to give a static IP to it so it will be the same after every restart, so in the router settings I can set up port forwarding for it.
Setting the PI’s ip to 192.168.1.103 static (more info):
- sudo nano /etc/network/interfaces
- or sudo vi /etc/network/interfaces depending on editor preference
auto lo
iface lo inet loopback
iface eth0 inet static
address 192.168.1.103
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
Then it should take effect after restart:
- sudo reboot
Now for this IP we need to set up port forward in the routers setting page. Get the user manual of your router and set up http portforward for the Pi’s ip (in my case 192.168.1.103) on port 80.
After that, get your outside IP, type in the browser get my IP and then type that in to the browser.
Mine is:
76.84.48.83
You should see what is running on my PI’s webserver, by clicking on it.
This is how to do it the long way:
panelTemp=[]
d=0
for i in itotal:
panelTemp.append(float(dryBulbTemperature[d])+(i*40/1000))
d+=1
The short way:
panelTemp=[float(d)+(i*40/1000) for d,i in zip(dryBulbTemperature,itotal)]
For average of a list:
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print reduce(lambda x, y: x + y, l) / len(l)