#!/usr/bin/env python
import RPi.GPIO as GPIO
import datetime
import time
import sys
import Adafruit_DHT
from twython import Twython

CONSUMER_KEY = 'XXXXX'
CONSUMER_SECRET = 'XXXXX'
ACCESS_KEY = 'XXXXX'
ACCESS_SECRET ='XXXXX'

api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)

sensor = Adafruit_DHT.DHT22

pin = '4'

humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

if humidity is not None and temperature is not None:
	humid = '{0:0.1f}'.format(humidity)
	tempc = '{0:0.1f}'.format(temperature)
	tempf = temperature * 1.8 + 32
	enviro = tempc + 'C, ' + "%.2f" %tempf + 'F, ' + humid + '% humidity.' 
	print(enviro)
else:
	enviro = 'Failed to get reading.'

GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

input_state = GPIO.input(18)

if input_state == False:
	api.update_status(status='MITERS door is open!   %s ({:%Y-%m-%d %H:%M:%S})'.format(datetime.datetime.now()) % enviro)
	time.sleep(0.2)
elif input_state == True:
	api.update_status(status='MITERS door is closed! %s ({:%Y-%m-%d %H:%M:%S})'.format(datetime.datetime.now()) % enviro)
	time.sleep(0.2)
