# Distributed with a free-will license.
# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
# SI7021
# This code is designed to work with the SI7021_I2CS I2C Mini Module available from ControlEverything.com.
# https://www.controleverything.com/content/Humidity?sku=SI7021_I2CS#tabs-0-product_tabset-2

# HF: Some minor modifications for Python 3. Tested to work with an Adafruit Si7021 sensor
# HF: Display of data (Temperature, Humidity, Date and Time) on Pimoroni Display-o-Tron 




import smbus
import time

from time import sleep
import datetime

import dothat.backlight as backlight
import dothat.lcd as lcd

# Get I2C bus
bus = smbus.SMBus(1)

# loop

i=0          # counter
i_max = 60   # number of measurements
dtime = 59   # seconds between measurements (n-1)

while i<i_max:
    

    # SI7021 address, 0x40(64)
    #		0xF5(245)	Select Relative Humidity NO HOLD master mode
    bus.write_byte(0x40, 0xF5)

    time.sleep(0.3)

    # SI7021 address, 0x40(64)
    # Read data back, 2 bytes, Humidity MSB first
    data0 = bus.read_byte(0x40)
    data1 = bus.read_byte(0x40)

    # Convert the data
    humidity = ((data0 * 256 + data1) * 125 / 65536.0) - 6

    time.sleep(0.3)

    # SI7021 address, 0x40(64)
    #		0xF3(243)	Select temperature NO HOLD master mode
    bus.write_byte(0x40, 0xF3)

    time.sleep(0.3)

    # SI7021 address, 0x40(64)
    # Read data back, 2 bytes, Temperature MSB first
    data0 = bus.read_byte(0x40)
    data1 = bus.read_byte(0x40)

    # Convert the data
    cTemp = ((data0 * 256 + data1) * 175.72 / 65536.0) - 46.85
    fTemp = cTemp * 1.8 + 32

    #get time
    date_time = (datetime.datetime.today().strftime("%Y/%m/%d %H:%M"))

    # Output data to screen
    humid = str(float ("%3.f" % humidity)) #define output formats for Humidity and Temperature
    cT = str(float ("%3.f" % cTemp))
    humval = (abs(humidity)/100)

    print ("Rel. Humidity: ", humid, "%") # print to screen
    print ("Temperature: ", cT, "°C")
    print (date_time)
    print ("")

    # Output data to display-o-tron

    Hum_String = "Humidity: "+ humid+" %" #define string for display-o-tron
    Temp_String = "Temp.: "+ cT+ " C"
    Time_String = date_time

    # Color indicator for Temperature
    
    lcd.clear() #clear display-o-tron

    if (cTemp < 20):
        backlight.hue(0.5) #blue bellow 20°C
    elif (cTemp > 23):
        backlight.hue (1) # red above 23°C
    else:
        backlight.hue(0.2) # green from 20 to 23°C

    # display humidity on bargraph (1 led per 16.7% humidity)   
      
    backlight.set_graph(humval)
    
    # display data on display-o-tron
    lcd.set_cursor_position(0,1)
    lcd.write(Hum_String)
    lcd.set_cursor_position(0,0)
    lcd.write(Temp_String)
    lcd.set_cursor_position(0,2)
    lcd.write(Time_String)

    print ('Press Ctrl+C to quit.\n')

    try:
       sleep(dtime)
       i=i+1

      
    except KeyboardInterrupt:
        # When Ctrl+C is pressed execution of the while loop is stopped
        print('Exit on request')
        
        break

print ("measurement circle done")
lcd.clear
backlight.off()


