Skip to content

Instantly share code, notes, and snippets.

@kylejohnson
Created March 7, 2017 15:38
Show Gist options
  • Select an option

  • Save kylejohnson/1cc2681fbff54be2a4dfefb2d6179889 to your computer and use it in GitHub Desktop.

Select an option

Save kylejohnson/1cc2681fbff54be2a4dfefb2d6179889 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import Adafruit_DHT
sensor = Adafruit_DHT.DHT22
pin = 26
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
print('{0:0.0f}').format(humidity)
#print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
#!/bin/bash
humidity=$(python ~/h.py)
# Regex to make sure $humidity is what I want it to be.
re='^[0-9]+([.][0-9]+)?$'
if ! [[ "$humidity" =~ $re ]] ; then
echo "error: Not a number" >&2
exit 1
else
# Log a message to syslog
logger -t humidity "sending over $humidity"
if mosquitto_pub -h mqtt.home.gnulnx.net -p 8883 --cafile tls/ca.crt --cert tls/bedroom.crt --key tls/bedroom.key -t "environment/bedroom/humidity" -m "$humidity" -r; then
logger -t humidity 'success'
else
logger -t humidity 'failure'
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment