#Reading Sensors for the Portable Weather Station
#Sensors inclue:
#1-Sensirion SDP810-125 PA Diffirential Pressure/Gas Flow
#2-Adafruit BME280 Temp, barometric pressure and humidity sensor
#Developed by right J J Slabbert

from Adafruit_BME280 import *
import time
import smbus
import math

import Adafruit_CharLCD as LCD


# Raspberry Pi pin configuration: The pinouts on github does not corespond with the adafruit instructions
lcd_rs        = 25  
lcd_en        = 24
lcd_d4        = 23
lcd_d5        = 17
lcd_d6        = 21
lcd_d7        = 22
lcd_backlight = 4



# Define LCD column and row size for 16x2 LCD.
lcd_columns = 16
lcd_rows    = 2


# Initialize the LCD using the pins above.
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight)

bus=smbus.SMBus(1) #The default i2c bus
address=0x25
bus.write_i2c_block_data(0x25, 0x3F, [0xF9]) #Stop any cont measurement of the sensor
time.sleep(0.8)

#Read SDP810 Sensor

bus.write_i2c_block_data(address,0x36,[0x03]) #Start cont measurement
time.sleep(0.8)


while True:    
    #Reading SDP810
    reading=bus.read_i2c_block_data(address, 0, 9)
    reading0=reading[0]
    reading1=float(reading[1])    
    flowval=reading0+reading1/255    
    if flowval>=0 and flowval<128:
        flowrate=flowval
        wind_speed=math.sqrt(flowrate*0.72/0.625)
    elif flowval>128 and flowval<=256:
        flowrate=-(256-flowval)        
        wind_speed=-1*math.sqrt(-1*flowrate*0.72/0.625)
    elif flowval==128:
        wind_speed=9999999999
        print("Wind to Strong")
    if wind_speed<0.2 and wind_speed>-0.2:
        wind_speed=0

    #Reading BME280
    sensor = BME280(t_mode=BME280_OSAMPLE_16, p_mode=BME280_OSAMPLE_16, h_mode=BME280_OSAMPLE_16)
    temperature = sensor.read_temperature()
    pressure = sensor.read_pressure()
    humidity=sensor.read_humidity()
    dewpoint=temperature-((100-humidity)/5)
    altitude=(math.pow((101325/pressure),(1/5.257))-1)*(temperature+273.15)/0.0065

    
    f=open("data.txt", "a+")
    f.write(str(temperature)+", "+str(humidity)+", "+str(pressure)+", "+str(wind_speed)+", "+str(dewpoint)+", "+str(altitude)+"\n")
    f.close()
    print ("Time: "+time.strftime("%Y-%m-%d, %H:%M:%S")+"\n"+"Wind Speed:"+str(round(wind_speed,1))+" m/s, Temperature: " +str(round(temperature,1))
           +" Deg C, Barometric Pressure: "+str(round(pressure,1))+" PA, Relative Humidity: "+str(round(humidity,1))+" %"
           +",\n"+"Dew Point: "+str(round(dewpoint,1))+" Degrees Celcius, Altitude: "+str(round(altitude,1))+" meter \n")   
    lcd.clear()
    lcd.message("Temperature:"+"\n"+str(round(temperature,1))+" D Celcius")
    time.sleep(2.5)
    lcd.clear()
    lcd.message("Rel Humidity: "+"\n"+str(round(humidity,1))+" %")
    time.sleep(2.5)
    lcd.clear()
    lcd.message("Air Pressure: "+"\n"+str(round(pressure,1))+" PA")
    time.sleep(2.5)
    lcd.clear()
    lcd.message("Wind Speed: "+"\n"+str(round(wind_speed,1))+" m/s")
    time.sleep(2.5)
    lcd.clear()
    lcd.message("Dew Point: "+"\n"+str(round(dewpoint,1))+" D Celcius")
    time.sleep(2.5)
    lcd.clear()
    lcd.message("Altitude: "+"\n"+str(round(altitude,1))+" meters")
    time.sleep(2.5)
