#!/usr/bin/python
from HIH6130.io import HIH6130
from Adafruit_BMP085 import BMP085
import time
import MySQLdb

# ===========================================================================

bmp = BMP085(0x77)

# To specify a different operating mode, uncomment one of the following:
# bmp = BMP085(0x77, 0)  # ULTRALOWPOWER Mode
# bmp = BMP085(0x77, 1)  # STANDARD Mode
# bmp = BMP085(0x77, 2)  # HIRES Mode
# bmp = BMP085(0x77, 3)  # ULTRAHIRES Mode

temp1 = bmp.readTemperature()

# Read the current barometric pressure level
pressure = bmp.readPressure()

# To calculate altitude based on an estimated mean sea level pressure
# (1013.25 hPa) call the function as follows, but this won't be very accurate
altitude = bmp.readAltitude()
#rht = HIH6130()
#rht.read()

print "Temperature: %.2f C" % temp1
print "Pressure:    %.2f hPa" % (pressure / 100.0)
print "Altitude:    %.2f m" % altitude
temp2 = 26.3 
#temp2 = rht.t 
temp3 = 10.0
#humidity = rht.rh
humidity = 43.0
#print ("Timestamp: {0}\tRH: {1}\tTemp: {2} degC".format(rht.timestamp, rht.rh, rht.t))


db = MySQLdb.connect("localhost","matej","database112","data")
curs = db.cursor()

try:
    curs.execute ("""INSERT INTO sensor_data
            values(CURRENT_DATE() - INTERVAL 1 DAY, NOW(), %s, %s, %s, %s, %s)""",(temp1, temp2, temp3, pressure/100.0, humidity))

    db.commit()
    print "Data commited"

except:
    print "Error: the database is being rolled back"
    db.rollback()

curs.execute ("SELECT * FROM sensor_data")

print "\nDate     	Time		Temperature1   Temperature2     Temperature3    Pressure        Humidity" 
print "========================================================================================================="

for reading in curs.fetchall():
    print str(reading[0])+"	"+str(reading[1])+" 	"+str(reading[2])+"    	"+str(reading[3])\
			+"		"+str(reading[4])+"		"+str(reading[5])+"		"+str(reading[6])

db.close()


    
