Skip to content

Instantly share code, notes, and snippets.

@alexellis
Created September 1, 2020 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexellis/17ef3723e0acd1d608fa7e6ed60f80a4 to your computer and use it in GitHub Desktop.
Save alexellis/17ef3723e0acd1d608fa7e6ed60f80a4 to your computer and use it in GitHub Desktop.
Pair of Python3 programs to control an LED and sense the environment using the Pimoroni Blinkt and Envirophat
from flask import Flask
app = Flask(__name__)
import sys
import blinkt
blinkt.set_clear_on_exit(False)
def print_color(r,g,b):
print('Setting Blinkt to {r},{g},{b}'.format(r=r, g=g, b=b))
blinkt.set_all(r, g, b)
blinkt.show()
print_color(0,0,0)
@app.route('/')
def hello_world():
return 'Welcome to led1. Actions: <a href="/on">on</a> or <a href="/off">off</a>'
@app.route('/on')
def on_route():
print_color(255,0,0)
return 'OK on.. <a href="/">Home</a>'
@app.route('/off')
def off_route():
print_color(0,0,0)
return 'OK off.. <a href="/">Home</a>'
app.run(host= '0.0.0.0')
import sys
import time
import threading
from envirophat import light, weather, motion, analog
unit = 'hPa' # Pressure unit, can be either hPa (hectopascals) or Pa (pascals)
def read_sensor():
rgb = light.rgb()
analog_values = analog.read_all()
mag_values = motion.magnetometer()
acc_values = [round(x, 2) for x in motion.accelerometer()]
output = """
Sensor 1 outputs
Temp: {t:.2f}c
Pressure: {p:.2f}{unit}
Altitude: {a:.2f}m
Light: {c}
RGB: {r}, {g}, {b}
Heading: {h}
Magnetometer: {mx} {my} {mz}
Accelerometer: {ax}g {ay}g {az}g
Analog: 0: {a0}, 1: {a1}, 2: {a2}, 3: {a3}
""".format(
unit=unit,
a=weather.altitude(), # Supply your local qnh for more accurate readings
t=weather.temperature(),
p=weather.pressure(unit=unit),
c=light.light(),
r=rgb[0],
g=rgb[1],
b=rgb[2],
h=motion.heading(),
a0=analog_values[0],
a1=analog_values[1],
a2=analog_values[2],
a3=analog_values[3],
mx=mag_values[0],
my=mag_values[1],
mz=mag_values[2],
ax=acc_values[0],
ay=acc_values[1],
az=acc_values[2]
)
return output
from flask import Flask
app = Flask(__name__)
@app.route('/')
def on_route():
return read_sensor(), 200, {'Content-Type': 'text/plain'}
app.run(host= '0.0.0.0')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment