web server that displays temp, humitdy etc

Gents

I am stuck on how to do this

I would like to create a web server that shows Motion web cam that has the following information overlaid on the screen using the Pi camera. (i have motion currently working ) Air Temp Humidity Air Pressure Water Temp water level

the following python code works but how do i run it on a web page updating every 10 seconds.

I already have Apache running how do i get it to run my python code or how do i get the information into html?

#!/usr/bin/env python # -*- coding: utf-8 -*-

# Can enable debug output by uncommenting: #import logging #logging.basicConfig(level=logging.DEBUG)

import Adafruit_BMP.BMP085 as BMP085 import sys import Adafruit_DHT import os import glob import time

# Default constructor will pick a default I2C bus.

sensor1 = BMP085.BMP085() sensor2 = Adafruit_DHT.AM2302 pin = 17

# Optionally you can override the bus number: #sensor = BMP085.BMP085(busnum=2) os.system('modprobe w1-gpio') os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/' device_folder = glob.glob(base_dir + '28*')[0] device_file = device_folder + '/w1_slave'

def read_temp_raw(): f = open(device_file, 'r') lines = f.readlines() f.close() return lines

def read_temp(): lines = read_temp_raw() while lines[0].strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw() equals_pos = lines[1].find('t=') if equals_pos != -1: temp_string = lines[1][equals_pos+2:] temp_c = float(temp_string) / 1000.0 #temp_f = temp_c * 9.0 / 5.0 + 32.0 return temp_c

while True: print 'Box Temp = {0:0.2f} *C'.format(sensor1.read_temperature()) print 'Pressure = {0:0.2f} Pa'.format(sensor1.read_pressure()) print 'Altitude = {0:0.2f} m'.format(sensor1.read_altitude()) print 'Sealevel Pressure = {0:0.2f} Pa'.format(sensor1.read_sealevel_pressure()) humidity, temperature = Adafruit_DHT.read_retry(sensor2, pin) print 'Air Temp= ',temperature print 'Humidity= %',humidity print 'Water Temp = ',(read_temp()) print time.sleep(5)

Reply to
Glen_Ossman
Loading thread data ...

Have your Python code write to a file that Apache can serve up. Plain text to start then add the HTML. If you are running this from a cron job just redirect the output to a file. If the python code needs to run as root then use root's cron.

--

Knute Johnson
Reply to
Knute Johnson

That's one way. The other way is to get apache to call the python which writes html and javascript to stdout: that is then is your 'web page'

Use a cron on server with heavy traffic but on a hobby server call python directly.

formatting link

is a good guide as to how to get apache to execute python scripts.

--
New Socialism consists essentially in being seen to have your heart in  
the right place whilst your head is in the clouds and your hand is in  
someone else's pocket.
Reply to
The Natural Philosopher

Just to be clear you want the temp/humidty ("weather") information embedded into the streamed moving image from the Pi camera? Or at least the appearance of that.

Does the "weather" information need to update in realtime, set intervals or not at all?

--
Cheers 
Dave.
Reply to
Dave Liquorice

Apache is rather heavy for what you want. You have a script running in Python, so would it not be easier for your Python script to sever the webpage directly?

I suggest you look into using bottle.

formatting link

Reply to
mm0fmf

Flask is another way to serve HTTP directly from a simple Python script. If your traffic load increases beyond what Flask can handle directly, it can team up with a higher-capacity web server.

HTH

--
Robert Riches 
spamtrap42@jacob21819.net 
(Yes, that is one of my email addresses.)
Reply to
Robert Riches

Gents

you gave me some good ideas that Py code can't be run by a web page Access denied you can only SUDO the code

so i am thinking that root cron every 10 minuets and then read the file using html or PHP

I am a net coder and example or help would be appreciated

motion is up and running on port 8081 and can be accessed by 127.0.0.1:8081

so i need a frame to run it in full page ? with the text on both sides pulled from the file ?

i think that will work?

Reply to
Glen_Ossman

That can most likely be fixed by sorting out the read and execute permissions for the Python code file. Alternatively, follow what the Apache manual says in the "CGI: Dynamic Content" manual about running scripts.

If you do that, don't run it as root: no user programs should be given that amount of access to the system.

Instead 'sandbox' it by making another user to run it in. The crontab file you'll use to run it every 10 minutes has the ability to run programs under any user you specify

When a new file has been created, you have two choices:

1) create it in the /home/pyuser/public_html directory (you'll have to create that) and configure Apache to read the file from there,

2) copy the file to some place in the main Apache web page storage area.

In either case your script must set the output file's permissions so Apache can read it.

No, your Python code just needs to write a text file containing the HTML needed to format the file, i.e it should start with:

page title

... the text and formatting that forms the page content goes here ...

Do yourself a favour by downloading and installing the 'tidy' package ("apt-get install tidy"). tidy is a program that, checks HTML pages for errors and/or tidies up the HTML text. In this case its the fast way to make sure your Python code is generating valid HTML. Running "tidy -e myoutput.html" will report any problems with the HTML in 'myoutput.html' - using this while you're writing your program will be a lot quicker (and find more gotchas) than looking at the HTML file in a browser, though of course you need to look at the output with a browser as well.

If you haven't got an HTML book yet and think you need one, "HTML and CSS: Visual QuickStart Guide" by Elizabeth Castro is worth a look. I have her "HTML for the World Wide Web" 5th edition and would recommend that except that it deals with 2003 vintage HTML 4 while "HTML and CSS" covers the current HTML 5 flavour.

Either running your Python program from Apache on demand or doing the 10 minute cron update should do what you want.

But, see how long the program takes to rebuild its webpage: if it takes more than a second to do so, then running it on demand may give a noticeable delay for the browser user while running it as a cron job won't provided you run the Python from a shell script which does this the following:

- run Python code to create the file with a name that Apache doesn't know

- use chmod to set the files permissions for Apache access

- use cp or mv to replace the old file with the new one because this reduces the time needed to swap the file served up by Apache to an absolute minimum.

--
martin@   | Martin Gregorie 
gregorie. | Essex, UK 
org       |
Reply to
Martin Gregorie

Why does it have to be run as root? You should find out why and fix it by using the correct accounts and permissions.

Can I suggest you also use Python3 not Python2. There's nothing wrong with Python2, I used it myself for everything until relatively recently. But moving to Python3 means you are facing the future not the past.

Reply to
mm0fmf

My guess would be because it is accessing I2C (from the OP's original post) and so it is the permissions somewhere down the /sys/bus/wl/devices directory tree that will need fixing.

Reply to
David James

can't be done this is why

formatting link

"This means that as a rule, you should not be able to directly access hardware, like the physical memory of the computer. So that's why /dev/mem is protected so that normal users cannot access it.

Now "/dev/mem" allows you much, much more "mischief" than just changing a GPIO. So that's why /dev/mem must be protected against normal users. "

Reply to
Glen_Ossman

At this point I'll leave you to enjoy learning about web security and Unix permissions. You probably don't want to have the webserver running as root. Why? That's left as an exercise for the reader! But even if you only intend the webserver to be accessed on your own network you should still get the security model correct from the start. If you always design the security in from the start you wont find something obvious biting you in the backside later on.

Reply to
mm0fmf

if i use a cron job to SUDO Python file_name and it creates a text file in my home directory then have Apache read the text file every time it is accessed. i am not sure i understand my web security issue ??

Reading from text files

Reply to
Glen_Ossman

As I said above, compartmentalise for good security. I know Windows often makes this impossible, but it works well with Linux and is something you really should get used to doing.

Run the Python code in its own user so it is walled off from everybody else and (carefully, sparingly) let that user inherit any extra permissions in needs to access stuff such as I2C ports. Run it with a cron script that copies each newly produced web page to wherever Apache expects to find it.

Reply to
Martin Gregorie

This is almost correct for general GIO (there are utilities taht make GPIO available at user level) and it is possible to configure i2c & spi such that root permissions are unnecessary.

--
I hope something GOOD came in the mail today so I have a REASON to live!!
Reply to
alister

I've not read that thread, but it's very possible to access the GPIO without sudo.

One way is to use a set-uid root program, but that may be considered cheating and unless you know what you're doing then might fall foul, however my wiringPi library can be used by set-uid programs then subsequently relinquish root privs once the /dev/mem device has been opened. I do this in my RTB BASIC interpreter so it can access the GPIO and then open/close files as the calling user once it drops root privs.

Another way is to export the GPIO pins required using the sysfs interface. The export operation needs to be done via a root program (either via sudo or suid), then your user level program can access them that way. The down-side is that it's slower. Not a problem for simple LEDs and buttons though, and again wiringPi provides mechanisms to let you do this.

The SPI and I2C interfaces can also be accessed directly from user-land, all that needs to be done is to make sure the /dev/ device names have the right permissions (and I think they now use the 'gpio' group for this and the default 'pi' user is in the gpio group, so it should "just work"...

I think there was/is talk of creating a special /dev/gpio device that only allows mapping of the gpio hardware area - and usable from user-land, but I've not been keeping up with Pi developments as of late.

Gordon

Reply to
Gordon Henderson

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.