0

I want to display current temperature on a webpage.

I'm using Raspberry Pi 3 in Jessie.
I'm using Chromium as browser.

I placed the python script inside a loop for a countdown timer. I then trigger it every 5 seconds. I accompanied it with console.log("here") to check if the function is looping. It displays numerous "here" but I only get one value

  1. I have tried <?php shell_exec('sudo python /var/www/html/31856.py'); ?>
    The script adds temperature to a Mysql table but it only runs after I refresh the page.
  2. I have tried var tout = <?php echo shell_exec('sudo python /var/www/html/31856.py'); ?>
    console.log(tout); The script displays the temperature and is looping however the value does not change from the first value. It only change after refresh

1 & 2 is running (it gets temp value) and there is no error on /var/log/apache2 but it I can't get it to pass real time values.

Here are the codes:
1. #!/usr/bin/env python

from Adafruit_MAX31856 import max31856
import RPi.GPIO as GPIO


sensor = max31856.max31856(csPin=8, misoPin=9, mosiPin=10, clkPin=11)    
thermoTempC = sensor.readJunctionTemp()
st = "%.2f" % thermoTempC
print st

GPIO.cleanup()

2. #!/usr/bin/env python

from Adafruit_MAX31856 import max31856
from datetime import datetime

import RPi.GPIO as GPIO
import MySQLdb
import time


db = MySQLdb.connect(host="192.168.1.10", user="root", passwd="root", db="test")
cur = db.cursor()


data = ""
thermoTempC = 0.00



def send_temp():
    try:
        rt = "%.2f" % thermoTempC
        cur.execute("""INSERT INTO log (Cntl_Id, Temperature) VALUES (%s, %s)""", ("C01", rt))
        #print("Write to DB. Success")
        #print rt 
        db.commit()
    except:
        db.rollback()



try:              
    cur.execute("""SELECT Status FROM controller WHERE Number='C01'""")
    data = cur.fetchone()[0]
    #print data
    db.commit()
except:
    db.rollback()


if str(data) == "1":
    sensor = max31856.max31856(csPin=8, misoPin=9, mosiPin=10, clkPin=11)    
    thermoTempC = sensor.readJunctionTemp()
    send_temp()

    GPIO.cleanup()

Looping function

function timer(seconds) {
    var h = Math.floor(seconds / 3600), m = Math.floor(seconds / 60) % 60, s = seconds % 60;

    if (h < 10) { h = "0" + h; }
    if (m < 10) { m = "0" + m; }
    if (s < 10) { s = "0" + s; }


    if (seconds >= 0) {
            if ((seconds % 5) == 0) {
                //var tout = 0;
                //console.log(tout);
                var tout = <?php echo shell_exec('sudo python /var/www/html/31856.py'); ?>              
                console.log(tout);
                console.log("get temp");
            }

        $("#remaining_time").val(h + ":" + m + ":" + s);
    }
    else
    {
        $("#remaining_time").val("00:00:00");
    }
}
IyaSheep
  • 43
  • 1
  • 1
  • 10

1 Answers1

0

I cannot see a working Loop in your Javascript - You have to use setInterval (or something similar) to repeat your Code/Function/Method....

Try this:
javascript timer loop
or this:
https://raspberrypi.stackexchange.com/questions/12209/show-io-value-on-webpage-in-realtime

user966660
  • 634
  • 1
  • 8
  • 20