Skip to content

Instantly share code, notes, and snippets.

@tomoto
Last active July 10, 2020 23:27
Show Gist options
  • Save tomoto/7c8103eb7119c2af6d286e210f4f7a73 to your computer and use it in GitHub Desktop.
Save tomoto/7c8103eb7119c2af6d286e210f4f7a73 to your computer and use it in GitHub Desktop.
Estimated WBGT meter (25±3℃) for CircuitPython on Particle Xenon
import time
import board
import pulseio
from adafruit_motor import servo
from adafruit_dht import DHT11
from collections import namedtuple
StatusLED = namedtuple('StatusLED', ['r', 'g', 'b'])
led = StatusLED(
pulseio.PWMOut(board.RGB_LED_RED, frequency=5000, duty_cycle=65535),
pulseio.PWMOut(board.RGB_LED_GREEN, frequency=5000, duty_cycle=65535),
pulseio.PWMOut(board.RGB_LED_BLUE, frequency=5000, duty_cycle=65535)
)
def setLED(r, g, b):
led.r.duty_cycle = int((1.0 - r) * 65535)
led.g.duty_cycle = int((1.0 - g) * 65535)
led.b.duty_cycle = int((1.0 - b) * 65535)
pwm = pulseio.PWMOut(board.D2, frequency=50)
s = servo.Servo(pwm, min_pulse=500, max_pulse=2350)
s.fraction = 0.5
dhts = [DHT11(board.D12), DHT11(board.D11)]
failure = 0
setLED(0.1, 0, 0.1)
time.sleep(2)
while True:
try:
t1, h1 = dhts[0].temperature, dhts[0].humidity
t2, h2 = dhts[1].temperature, dhts[1].humidity
t, h = (t1 + t2) / 2, h = (h1 + h2) / 2
# https://www.wbgt.env.go.jp/pdf/ic_rma/2301/mat05_3.pdf
wbgt = c.wbgt = 0.85 * t + 0.14 * h - 6.15
center = 25
rng = 3
r = min(max((center - wbgt) / rng, -1.0), 1.0) * 0.5 + 0.5
print("%f %f %f %f %f %f" % (t1, h1, t2, h2, wbgt, r))
setLED(0, 0, 0.1)
s.fraction = r
time.sleep(1)
s.fraction = None
setLED(0, 0.05, 0.05)
time.sleep(59)
failure = 0
except Exception as e:
print(e)
failure += 1
if failure >= 10:
print("Something is wrong. Reboot recommended")
setLED(0.1, 0, 0)
time.sleep(5)
else:
setLED(0.1, 0.1, 0)
time.sleep(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment