1

I have created a program with the ESP8266 to periodically request to my flask application a integer variable to turn on and off a LED.

When I run it locally the app works as it should but when I deploy it on Heroku, the first time I change the value of the status it starts swaping from 1 to 0 randomly forever.

Here is the code of the Flask app

from flask import Flask, jsonify, render_template, redirect
import time

##########################################################

app = Flask(__name__)

ledStatus = 1

##########################################################


@app.route('/led-status')
def getStatus():
    return jsonify (ledStatus = ledStatus)



@app.route('/button')
def getButton():
    return render_template('button.html', ledStatus=ledStatus)


@app.route('/led-swap')
def swapStatus():
    global ledStatus

    if ledStatus == 1:
        ledStatus = 0
    else:
        ledStatus = 1

    time.sleep(0.5)
    return redirect("/button")


##########################################################

if __name__ == "__main__":
    app.run()

Thanks in advance.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
  • 1
    The deployment seems to use multiple processes and therefore has multiple versions of the `ledStatus` variable. You have to find a way to share the status between the processes, like a database or a key value store. – Klaus D. Jun 21 '18 at 20:38
  • Specifically, some way of sharing state that's not a global variable. – Dave W. Smith Jun 21 '18 at 20:51

0 Answers0