Files
old-tfm/controller/controller.py

39 lines
1.3 KiB
Python
Raw Normal View History

2015-12-27 07:12:51 +01:00
#!/usr/bin/python
import sys
import requests
import Adafruit_DHT
GPIO_PIN = 4
2015-12-29 23:25:11 +01:00
SENSORDATA_URL = 'http://tfm.meteor.com/api/sensorData'
2015-12-27 07:12:51 +01:00
SENSOR_TYPE = Adafruit_DHT.DHT11
if len(sys.argv) == 2:
owner = sys.argv[1]
else:
2015-12-29 23:25:11 +01:00
print 'usage: sudo ./controler.py [OWNER]#'
2015-12-27 07:14:50 +01:00
print 'example: sudo ./controller.py Senad - Send temperature as Senad'
2015-12-27 07:12:51 +01:00
sys.exit(1)
# Try to grab a sensor reading. Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
2015-12-27 07:16:27 +01:00
humidity, temperature = Adafruit_DHT.read_retry(SENSOR_TYPE, GPIO_PIN)
2015-12-27 07:12:51 +01:00
# Un-comment the line below to convert the temperature to Fahrenheit.
# temperature = temperature * 9/5.0 + 32
# Note that sometimes you won't get a reading and
# the results will be null (because Linux can't
# guarantee the timing of calls to read the sensor).
# If this happens try again!
2015-12-29 23:25:11 +01:00
if temperature is not None and humidity is not None:
response = requests.post(SENSORDATA_URL, json={"owner": owner, "temperatureValue": temperature, "humidityValue":humidity})
2015-12-27 07:12:51 +01:00
print 'Temp={0:0.1f}*C'.format(temperature)
2015-12-29 23:25:11 +01:00
print 'Humidity={0:0.1f}%'.format(humidity)
2015-12-27 07:12:51 +01:00
if response.status_code != 200:
print 'Failed to send temperature!'
sys.exit(2)
else:
print 'Failed to get reading. Try again!'
sys.exit(1)