## copyright KGB of rewindcameras.co.uk

## Voltage divider = Vout = (Vin*((R1+R2)/R2))
##  
analog_value = machine.ADC(28)



def voltage():

    volts=analog_value.read_u16()* (3.3 / 65535)
    print(volts)
    Vin = volts
    R1=  (22*1000) 
    R2=  (4.7*1000)

    Vout = (Vin*((R1+R2)/R2))

    print (str(Vout))
    return

import machine, onewire, ds18x20, time
from machine import Pin, I2C
import utime
#import ssd1306
from ssd1306 import SSD1306_I2C


analog_value = machine.ADC(28)

volts=analog_value.read_u16()

i2c=I2C(0,sda=Pin(20),scl=Pin(21), freq=400000)
oled = SSD1306_I2C(128, 64, i2c) # screen setup
pump = Pin(22, Pin.OUT) # pump relay pin


#temp_pin = machine.Pin(26, Pin.IN,Pin.PULL_UP) #temp sensor setup


temp_pin = machine.Pin(26)


ds_sensor = ds18x20.DS18X20(onewire.OneWire(temp_pin)) #temp sensor setup change pin on board !!!!! on analogue earth atm
#ds_sensor.convert_temp()
pump.value(0)
time.sleep(3)
oled.fill(0)
oled.text("SELF TESTING...",0,16)
oled.text("PLEASE WAIT",0,24)
oled.show()
time.sleep(2)
pump.value(0)
time.sleep(3)
#print("pump on")
wait_time =1 ###### CHANGE HERE FOR GLOBAL DELAY


#pump.value(0)


time.sleep(1)

max_temp =45
Btemp=0
Ptemp = 0
pump_on = False
pump.value(0)
start_temp = 20


oled.fill(0)
oled.text("WELCOME",7,16)
oled.text("SPARKS-FIELD",3,24)
oled.text("HEATING",5,32)
oled.text(" A KGB CREATION",0,40)
oled.text("HAPPY SHOWER :)",3,48)
oled.show()
time.sleep(4)
oled.fill(0)


def read():
    
    global rom
    global roms
    
    global Ptemp
    global Btemp
    
    while True:
        try:
            
            
            roms = ds_sensor.scan()
            time.sleep(1)
            for rom in roms:
     
        #print(rom)
                 ds_sensor.convert_temp()
                 
            
            time.sleep(1)
            
            time.sleep(1)
            oled.fill(0)
            Btemp=float(((ds_sensor.read_temp(roms[1]))))
            Ptemp=float(((ds_sensor.read_temp(roms[0]))))
            print('Found DS devices: ', str(roms))
            oled.text("STARTING...",0,16)
            oled.text("SENSORS FOUND",0,24)
            oled.text(str(roms[0]),0,32)
            oled.text(str(roms[1]),0,40)
            time.sleep(1)
            print(Btemp)
            print(Ptemp) 
            oled.show()
            break
        
        except:
                
            print("error finding sensors")
            oled.fill(0)
            oled.text("SENSOR ERROR 1",0,24)
            volts=analog_value.read_u16()
            oled.text(str(volts),0,40) # read votage
            oled.show()
            voltage()
            time.sleep(.5)
            read()
    main() #start main loop









def screen():
    
    
    
    
    print("in screen")
    global Btemp
    global Ptemp
    global roms
    
    global pump_on
    global max_temp
    #global count
    
    
   
    while True:
        try:
            time.sleep(1)
            roms = ds_sensor.scan()
            for rom in roms:
                time.sleep(1)
                ds_sensor.convert_temp()
            
            time.sleep(1)
            
            time.sleep(1)
            oled.fill(0)
            Btemp=float(((ds_sensor.read_temp(roms[1]))))
            Ptemp=float(((ds_sensor.read_temp(roms[0]))))
            print('Found DS devices: ', str(roms))
            oled.text("STARTING...",0,16)
            oled.text("SENSORS=",5,24)
            oled.text(str(roms[0]),0,32)
            oled.text(str(roms[1]),0,40)
            oled.text("PLEASE WAIT",5,48)
            time.sleep(1)
            print(Btemp)
            print(Ptemp) 
            oled.show()
            break
        
        except:
                
            print("error finding sensors")
            oled.fill(0)
            oled.text("SENSOR ERROR 1",0,24)
            oled.show()
            time.sleep(2)
            read()
         
    print("sensors found")
    print ("please wait...")
    time.sleep(2)
   
    oled.fill(0)
       
    print('Found DS devices: ', roms)
    print('boiler: ', str(Btemp))
    print('Pannel: ', str(Ptemp))
   
    print("debug in screen")
    time.sleep(1)
    oled.text("PAN TEMP=",0,16)
    oled.text(str(Ptemp),0,24)
    oled.text("WATER TEMP",0,32),
    oled.text(str(Btemp),0,40)
    oled.text("PUMP-ON",0,48)
      


    oled.show()
    time.sleep(1)
       
       
       
       
       
       #Ptemp= (Btemp+10) # will be [1]
    print("boiler temp = " + str(Btemp))
    print("Panel temp = " + str(Ptemp))
    #print(str(pump_on))
    print("debug after bug")
      
           
           
           
    if (Ptemp>(Btemp+4)) and (Ptemp > start_temp) and (Btemp<max_temp): # ideal situation
        
        print("good reading ")   
        pump_on =True
        pump.value(1)
        time.sleep(5)  
        oled.fill(0)
        
        print('Found DS devices: ', roms)
        print('panel: ', Ptemp)
        print('boiler: ', Btemp)
        print("debug in screen")
        time.sleep(1)
        oled.text("SOLAR TEMP",0,16)
        oled.text(str(Ptemp),0,24)
        oled.text("WATER TEMP",0,32),
        oled.text(str(Btemp),0,40)
        oled.text("* RUNNING *",0,48)
         
        oled.show()
         
        time.sleep(2)
        count = wait_time
         
        while count>0:
            oled.fill(0)
            time.sleep(1)
            oled.text("SOLAR TEMP=",0,16)
            oled.text(str(Ptemp),0,24)
            oled.text("WATER TEMP",0,32)
            oled.text(str(Btemp),0,40)
            oled.text("PUMP- ",0,48)
            oled.text("WAITING",0,56)
            oled.text(str(count),0,64)
            oled.show()
            count -=1
             
                 
        screen()
             
             
             
             
    if Ptemp<Btemp:
        pump_on = False
        pump.value(0)
        count = wait_time
          
        while count>0:
            oled.fill(0)
    
   
            time.sleep(1)
            oled.text("SOLOR TEMP=",0,16)
            oled.text(str(Ptemp),0,24)
            oled.text("WATER TEMP",0,32)
            oled.text(str(Btemp),0,40)
            oled.text("PUMP OFF ",0,48)
            oled.text("SOLAR TOO COLD",0,56)
            oled.text(str(count),8,64) 
            oled.show()
            count -=1
        
        screen()
   
    else:  ## why this ?
        oled.fill(0)
        
   
        time.sleep(1)
        oled.text("PUMP OFF",0,16)
        oled.text("MATCHING TEMPS",0,24)
        
        oled.text("SOLAR= ",0,32)
        oled.text(Ptemp,0,40)
        oled.text("WATER TEMP",0,56)
        oled.text(Btemp,0,64)
        
        
        print("not working")
        oled.show()
        time.sleep(2)
        read()
                         
def main():
    global Btemp
    global Ptemp
    global pump_on
    print("degug in main")
    global roms
    
    
   
    
    
        
        
        
        
    if (Ptemp>(Btemp+4)) and (Btemp<max_temp) and (Ptemp>start_temp):
        pump_on =True
        
        time.sleep(1)
        screen()
        
    if (Ptemp < Btemp) or (Btemp > max_temp)or (Ptemp<start_temp):
        print ("in panel lower loop")
        pump_on =False
        pump.value(0)
        time.sleep(5)
        
        if (Ptemp < Btemp and (Btemp <max_temp)):
            condition = "SOLAR TOO COLD"
        if (Btemp > max_temp):
            condition = "AT HEAT- PUMP OFF"
        if (Ptemp<start_temp):
            condition = "SOLAR TOO COLD"
        
        oled.fill(0)
                    
        
           
        print('Found DS devices: ', roms)
        print('Found Panel: ', Ptemp)
        print('Found boiler: ', Btemp)
        print("debug in screen")
        time.sleep(1)
        oled.fill(0)
    
   
        time.sleep(1)
        oled.text("SOLOR TEMP=",0,16)
        oled.text(str(Ptemp),0,24)
        oled.text("WATER TEMP",0,32)
        oled.text(str(Btemp),0,40)
        oled.text("PUMP OFF ",0,48)
        oled.text(condition,0,56)
        
        
            


        oled.show()
           
        time.sleep(2)
        
        count=wait_time
        while count > 0:
            oled.fill(0)
            time.sleep(1)
            oled.text("SOLAR TEMP",0,16)
            oled.text(str(Ptemp),0,24)
            oled.text("WATER TEMP",0,32)
            oled.text(str(Btemp),0,40)
            oled.text("PUMP OFF ",0,48)
            oled.text(condition,0,56)
            oled.text(str(count),0,64)


            oled.show()
            count -=1
        read()
        
        
        
        
        
    else:
        count=wait_time
        while count > 0:
            oled.fill(0)
            time.sleep(1)
            oled.text("PAN TEMP=",0,8)
            oled.text(str(Ptemp),0,16)
            oled.text("WATER TEMP",0,24)
            oled.text(str(Btemp),0,32)
            oled.text("PUMP - ",0,40)
            oled.text("WAITING",0,48)
            oled.text(str(count),0,56)


            oled.show()
            count -=1
            
        
        
        
        
        read()





read()
#  start the software    
      
 



count = 100

def test():
    global count
    while True:
            while count>0:
                print("Running count = ...")
                time.sleep(1)
                print(str(count))
                count-=1



NOCOMNCVCCGNDINLED1PWRRelay Module