I'm struggling with importing the variables from my Python Script (pulsecount.py) into my Flask App (routes.py). If I simply do it like the following script, the code is executed, but it only shows the print outputs from the While Loop in pulsecount.py in the terminal and the Flask app won't run. If I delete the While Loop, the Flask App is working but I can't get the updated variable value, which in this case is conValue.
I want to show the newest output of the Raspberry Pi on the Browser, how do I do that?
pulsecount.py:
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.IN)
global revcount
global coinValue
global Balance
revcount = 0
coinValue = float(0.00)
Balance = float(0.00)
def increaserev(self):
global revcount
global coinValue
global Balance
revcount += 1
if revcount == 2:
coinValue = float(1.00)
else:
coinValue = float(0.5)
return coinValue
GPIO.add_event_detect(27, GPIO.RISING, callback=increaserev)
while True:
if revcount > 0:
sleep(2)
Balance += coinValue
print "Coin Value is {0}".format(coinValue)
print "Balance is {0}".format(Balance)
revcount = 0
coinValue = 0
routes.py:
from flask import Flask
from flask import render_template
from pulsecount import coinValue
app = Flask(__name__)
@app.route("/")
def home():
return render_template("home.html")
@app.route("/getbitcoin/")
def getbitcoin():
coinvalue = coinValue
return render_template("getbitcoin.html", coinvalue = coinvalue)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0")
getbitcoin.html:
{% extends "template.html" %}
{% block head %}
<script type="text/javascript">
$("document").ready(function(){
setInterval(function(){
$.ajax({
url:"/getbitcoin/",
success:function(result){
$(".get-bitcoin").html(result);
}
});
}, 2000);
});
</script>
{% endblock %}
{% block content %}
<a id="link" href="/total/">
<div class="get-bitcoin">
<div class="get-bitcoin-button">
<h1 class="bitcoin-header">{{ coinvalue }}</h1>
<!-- <h1 id="result"></h1> -->
</div>
</div>
</a>
{% endblock content %}