murkata86
Posts: 5
Joined: Sun May 15, 2016 10:46 pm

Displaying Sensor Readings in a Web Page

Sun May 15, 2016 11:01 pm

Hi All,

I have Raspberry Pi 2 with Raspbian OS. Recently I've purchased the Adafruit MCP9808 temperature sensor and wired it to the Pi. I've downloaded the library Adafruit has provided:

https://learn.adafruit.com/mcp9808-temp ... y/software

Everything is working fine, but I want to display the sensor readings in a web page and temperatur should be dynamically updated.

Does anyone know how this can be done or is there a similar example?

Thanks for your help!

Regards,
Ivan

User avatar
coltonj96
Posts: 26
Joined: Sat Jan 30, 2016 6:16 pm

Re: Displaying Sensor Readings in a Web Page

Mon May 16, 2016 2:02 am

One way is to output the data from the sensors to a sqlite or sql database via Python and have an AJAX enabled webpage retrieve and update the info on the page

murkata86
Posts: 5
Joined: Sun May 15, 2016 10:46 pm

Re: Displaying Sensor Readings in a Web Page

Mon May 16, 2016 6:42 am

Thank you for your response!

I was thinking about this as well, but won't this cause some performance issues as Python will be writing to the database while the app is running and with AJAX will be querying all the time as well.

I've shared my problem with a friend of mine and he mentioned something about sockets and I've checked in the Internet (as I'm not aware of the sockets at all), but all the examples were not working for me...

dddomodossola
Posts: 42
Joined: Thu Jan 22, 2015 8:56 am

Re: Displaying Sensor Readings in a Web Page

Mon May 16, 2016 6:57 am

Hello murkata86,

have you seen this library https://github.com/dddomodossola/remi ?
This allows to build a webinterface simply, without writing html and javascript. ;)

murkata86
Posts: 5
Joined: Sun May 15, 2016 10:46 pm

Re: Displaying Sensor Readings in a Web Page

Mon May 16, 2016 7:37 am

Well the thing is that at some point I'll need to write JavaScript and PHP....

dddomodossola
Posts: 42
Joined: Thu Jan 22, 2015 8:56 am

Re: Displaying Sensor Readings in a Web Page

Mon May 16, 2016 7:44 am

Why you plan to use javascript or PHP?

murkata86
Posts: 5
Joined: Sun May 15, 2016 10:46 pm

Re: Displaying Sensor Readings in a Web Page

Mon May 16, 2016 8:42 am

Because I want to build a more complex web interface and I think with JavaScipt and PHP I'll have more flexibility. Right now what I need is just to send data from Python to JavaScript or PHP, but after that I'll need to send data from JavaScript and/or PHP to Python as well. So basically the Python app will be sending and receiving data to and from PHP and/or JavaScript.

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: Displaying Sensor Readings in a Web Page

Mon May 16, 2016 8:51 am

murkata86 wrote:Thank you for your response!

I was thinking about this as well, but won't this cause some performance issues as Python will be writing to the database while the app is running and with AJAX will be querying all the time as well.

I've shared my problem with a friend of mine and he mentioned something about sockets and I've checked in the Internet (as I'm not aware of the sockets at all), but all the examples were not working for me...
For only a handful of sensors (20 or so) would not be a performance issue, even for a model A. You would still have a large overhead to work with.

Sockets will work, but that's a lot of work for what you want and there are easier ways to do it.

@dddomodossola suggest his remi module.
It will do what you want and is easy to use.

web.py is another option, http://webpy.org/
You write a little more code but it is way more flexible.

Some php with Apache, nGenx, or Lighttpd are the most complicated, but offer the most customization, but can also be the simplest way to go.

If you just want to get it done then follow one of the many tutorials for installing Apache and php, write a simple php script that calls your python script and outputs your results.

Code: Select all

<?php
    $output = shell_exec("your_script.py");
    echo "output :".$output.;
?>
Last edited by tom.slick on Mon May 16, 2016 10:51 pm, edited 1 time in total.

murkata86
Posts: 5
Joined: Sun May 15, 2016 10:46 pm

Re: Displaying Sensor Readings in a Web Page

Mon May 16, 2016 7:07 pm

Thanks! I'll look into webpy soon and will come back if I need more help.

duckvader
Posts: 13
Joined: Thu Apr 23, 2015 2:41 pm

Re: Displaying Sensor Readings in a Web Page

Tue May 24, 2016 1:11 am

I am currently using two Dallas 1-wire temp probes to read the outside temp as well as the temp of my pool water. The python script below reads the temp sensors and then converts it from C to F then inputs that data it into a mysql DB and at that point I just pull all the data via PHP on my website. It works really well.

Code: Select all

#!/usr/bin/python

import os
import time
import datetime
import glob
import MySQLdb
from time import strftime

#Uncomment for Live Temp Sensors
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

#Live Temp Sensors
poolTempSensor = '/sys/bus/w1/devices/28-0214632d15ff/w1_slave'
outsideTempSensor = '/sys/bus/w1/devices/28-03146315efff/w1_slave'

#Test Temp Sensors
#poolTempSensor = '/opt/code/tempSensor/devices/28-000003cee4ca/w1_slave' 
#outsideTempSensor = '/opt/code/tempSensor/devices/28-000003cee5ca/w1_slave'

# Variables for MySQL
db = MySQLdb.connect(host="localhost", user="XXXXXX",passwd="XXXXXX", db="XXXXXX")
cur = db.cursor()

#Reading Pool Temp Data  
def readPoolTempRaw():
    pt = open(poolTempSensor, 'r')
    lines = pt.readlines()
    pt.close()
    return lines
 
def readPoolTemp():
    lines = readPoolTempRaw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = readPoolTempRaw()
    poolTemp = lines[1].find('t=')
    if poolTemp != -1:
        tempString = lines[1][poolTemp+2:]
        poolTempC = float(tempString) / 1000.0
        poolTempF = poolTempC * 9.0 / 5.0 + 32.0
        return ("%.f" %poolTempF)

#Reading Outside Temp Data 
def readOutsideTempRaw():
    ot = open(outsideTempSensor, 'r')
    lines = ot.readlines()
    ot.close()
    return lines

def readOutsideTemp():
    lines = readOutsideTempRaw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = readOutsideTempRaw()
    outsideTempOutput = lines[1].find('t=')
    if outsideTempOutput != -1:
        tempString = lines[1][outsideTempOutput+2:]
        outsideTempC = float(tempString) / 1000.0
        outsideTempF = outsideTempC * 9.0 / 5.0 + 32.0
        return ("%.f" %outsideTempF)
        

while True:
        #print (readPoolTemp())
        #print (readOutsideTemp())      
        poolTemp = readPoolTemp()
        outsideTemp = readOutsideTemp()
        datetimeWrite = (time.strftime("%Y-%m-%d ") + time.strftime("%H:%M:%S"))
        
        # SQL Statement
        sql = ("""INSERT INTO tempLog (dateTime,poolTemp,outsideTemp) VALUES (%s,%s,%s)""",(datetimeWrite,poolTemp,outsideTemp))
        
        # Execute the SQL command
        cur.execute(*sql)
        
        # Commit your changes in the database
        db.commit()
        
        #cur.close()
        #db.close()
        time.sleep(3600)

User avatar
Gavinmc42
Posts: 4528
Joined: Wed Aug 28, 2013 3:31 am

Re: Displaying Sensor Readings in a Web Page

Tue May 24, 2016 2:41 am

I save temperature data to a log.csv file.
Then have a web server running this javascript
http://dygraphs.com/

It is just some simple HTML5 with one js file running on busybox-httpd server.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

clemaez
Posts: 25
Joined: Thu Apr 14, 2016 1:55 pm

Re: Displaying Sensor Readings in a Web Page

Thu May 26, 2016 8:17 pm

Hi,

A more effective way would be to use a Websocket. Just send a JSON formatted string that way you can easily parse it on Javascript side.

Your application architecture should look like:

server.py:
- Create a web socket server
- Create a loop who read your sensor regularly
- Format the data as a JSON formatted string (i.e: {"temperature": 0, "pressure": 0, "altitude": 0}
- Send this string to all Websocket client

Your web-application:
- Create a Websocket client
- Connect it to your websocket server
- Parse the receive data (i.e JSON.parse(data))
- Display them on your page


For Websocket server on python I would recommend http://websockets.readthedocs.io/en/stable/
And for Websocket client there is nothing specific, any recent web browser provides Websocket implementation (a documented websocket client is available in my previous link)

Kind regards,
Clément

texy
Forum Moderator
Forum Moderator
Posts: 5161
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Displaying Sensor Readings in a Web Page

Fri May 27, 2016 1:15 pm

murkata86 wrote:Hi All,

I have Raspberry Pi 2 with Raspbian OS. Recently I've purchased the Adafruit MCP9808 temperature sensor and wired it to the Pi. I've downloaded the library Adafruit has provided:

https://learn.adafruit.com/mcp9808-temp ... y/software

Everything is working fine, but I want to display the sensor readings in a web page and temperatur should be dynamically updated.

Does anyone know how this can be done or is there a similar example?

Thanks for your help!

Regards,
Ivan
The Foundations weather station does exactly what you want - you'll need to change it for the particular sensors you are using of course.
The setup for the sensors is here :
https://www.raspberrypi.org/learning/we ... e-setup.md

The setup for the webpage is here :
https://www.raspberrypi.org/learning/we ... mo-site.md

Hope this helps,
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Return to “Python”