Skip to content

Instantly share code, notes, and snippets.

@JeffreySkid
Created September 10, 2018 15:40
Show Gist options
  • Select an option

  • Save JeffreySkid/1d408c0fdcbea97f6a1d0a36f1b17748 to your computer and use it in GitHub Desktop.

Select an option

Save JeffreySkid/1d408c0fdcbea97f6a1d0a36f1b17748 to your computer and use it in GitHub Desktop.
import Adafruit_DHT
import time
import json
from flask import Flask
app = Flask(__name__)
while 1:
print('test')
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, 4)
if humidity is not None and temperature is not None:
jsonobj = {
'temperature': '{0:0.1f}'.format(temperature),
'humidity': '{0:0.1f}'.format(humidity)
}
with open('info.json', 'w') as file:
file.write(json.dumps(jsonobj).encode())
time.sleep(5)
break
@app.route("/")
def index():
with open('info.json') as f:
data = json.load(f)
return data['temperature'] + '°C' + ' / ' + data['humidity'] + '%'
if __name__ == '__main__':
app.run(port=80)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment