-3

I want to read in a webapp some elements (CPU temperature, CPU usage, state of some sensors...) in real-time from my Raspberry Pi. I know Python instruction to do this, but I want to read these values (let's focus now only on CPU temperature, for example) from a PHP page in real-time, so continuously refreshing the page. I've found various methods on the internet, but there's here a SIMPLE mode to do this?

This is my code:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    temp="There I'll put instructions..."
    return render_template("index.php", temp=temp)


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')
Cœur
  • 37,241
  • 25
  • 195
  • 267
G. Ianni
  • 13
  • 6
  • Are you using Flask or PHP? – zmbq May 18 '17 at 21:07
  • I'm using Flask, I want PHP only for the renderer (I'm also a newbie, tell me if I'm doing wrong usage of these words :) ) – G. Ianni May 18 '17 at 21:15
  • It looks like you need to fetch a PHP-generated page from somewhere, parse it, and output your own page. Packages to consider are `requests` and `BeautifulSoup` (if it's a HTML page). – 9000 May 18 '17 at 21:22

2 Answers2

1

You could create a REST API using flask that will return sensor data upon request. Here's an example of what your server might look like:

@app.route('/sensor/<sensor>')
def get_sensor_data(sensor):
    sensor = sensor.lower()
    if sensor == 'cpu':
        sensor_data = cpu_sensor_data()
    elif sensor == 'ssd':
        sensor_data = ssd_sensor_data()
    # etc.

    return sensor_data

Your frontend would then need to make a request to an endpoint like http://domain_name.net/cpu and get the sensor data to display as needed.

notorious.no
  • 4,919
  • 3
  • 20
  • 34
0

I simply resolved adding <body onload="location=''"> in the HTML code of PHP page.

G. Ianni
  • 13
  • 6