• Benvenuti su RaspberryItaly!
Benvenuto ospite! Login Login con Facebook Registrati Login with Facebook


Valutazione discussione:
  • 0 voto(i) - 0 media
  • 1
  • 2
  • 3
  • 4
  • 5

[-]
Tags
in python lettura lunghi con x tempi temperatura ds18b20

tempi lunghi x lettura temperatura con ds18b20 in python
#11
Per capire dove sbagli, mettici gli script.. (a proposito, sono nella stessa directory?)
Per quanto riguarda lo scrivere in Python in modo che abbia un funzionamento asincrono, significa imparare a programmare a livello avanzato, non intendere lo script solo come "una lista di cose da fare in sequenza", ma scoprire come far fare più cose contemporaneamente all'interno del programma.. qui ad esempio viene introdotto l'argomento.
Chiaramente, in questo caso specifico, fai prima con uno degli altri metodi proposti, ma puoi usarlo come scusa per approfondire le tue conoscenze, se ne hai voglia. Credo RaspberryPi serva anche a questo.
Risposta
#12
(28/08/2018, 20:56)Zzed Ha scritto: Per capire dove sbagli, mettici gli script.. (a proposito, sono nella stessa directory?)
Per quanto riguarda lo scrivere in Python in modo che abbia un funzionamento asincrono, significa imparare a programmare a livello avanzato, non intendere lo script solo come "una lista di cose da fare in sequenza", ma scoprire come far fare più cose contemporaneamente all'interno del programma.. qui ad esempio viene introdotto l'argomento.
Chiaramente, in questo caso specifico, fai prima con uno degli altri metodi proposti, ma puoi usarlo come scusa per approfondire le tue conoscenze, se ne hai voglia. Credo RaspberryPi serva anche a questo.

Grazie per il link, lo studierò molto volentieri nel tempo libero.
Intanto ti allego il semplice codice dello script di lettura temp (per adesso prevedevo semplicemente un print, ovviamente in seguito avrei dovuto scrivere su un file che sarebbe stato poi letto dallo script principale)

Codice:
from w1thermsensor import W1ThermSensor
sensor=W1ThermSensor()
while True:
   temp= sensor.get_temperature()
   temp=temp*1000
   temp=int(temp/100)
   temp=temp/10
   print("temp %s" % temp)
   time.sleep(10)

mentre questo sotto è il codice, un pò lunghetto, dello script principale. Ovviamente i due script sono nella stessa directory.
grazie mille
paolo


Codice:
#*********************** COLORI ***********************
red=255,0,0
green=0,255,0
blue=0,0,255
moon=0,162,232
white=255,255,255
black=0,0,0
yellow=255,255,0
magenta=255,0,255
navy=0,0,128
grey=128,128,128
orange=255,165,0
cyan=0,255,255
back_txt_color=black
pesca=255,187,0





import pygame
import time
from pygame.locals import *
import os
from time import sleep
from w1thermsensor import W1ThermSensor
sensor=W1ThermSensor()
os.system('date -s "08 feb 2018 20:20:20"')
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(32, GPIO.OUT) #day
GPIO.setup(33, GPIO.OUT) #moon
GPIO.setup(35, GPIO.OUT) #heater
GPIO.setup(36, GPIO.OUT) #co2
GPIO.setup(37, GPIO.OUT) #o2
GPIO.setup(38, GPIO.OUT) #food
#pin 40 (gpio21)=1wire pin (W1)

d=GPIO.PWM(32,100)
m=GPIO.PWM(33,100)
d.start(0)
m.start(0)
GPIO.output(35, GPIO.HIGH)
GPIO.output(36, GPIO.HIGH)
GPIO.output(37, GPIO.HIGH)
GPIO.output(38, GPIO.HIGH)
#GPIO.output(40, GPIO.HIGH)


pygame.init ()
screen = pygame.display.set_mode ((320,480),pygame.NOFRAME)

lcd_rotate=1
display_rotate=1


#************************** inizializza *********************************
ec=422
ph=6.8

pwm1_a=0
pwm2_a=255


in_file = open("data.txt","r")
eeprom = in_file.read()
in_file.close()
print(eeprom)

auto1=bool(int(eeprom[38]))
auto2=bool(int(eeprom[39]))
auto3=bool(int(eeprom[40]))
auto4=bool(int(eeprom[41]))
auto5=bool(int(eeprom[42]))
on1=bool(int(eeprom[47]))
on2=bool(int(eeprom[48]))
on3=bool(int(eeprom[49]))
on4=bool(int(eeprom[50]))
on5=bool(int(eeprom[51]))
pwm1_m=int(eeprom[83]+eeprom[84]+eeprom[85])
pwm2_m=int(eeprom[119]+eeprom[120]+eeprom[121])
hh_day_on=int(eeprom[59]+eeprom[60]);min_day_on=int(eeprom[61]+eeprom[62])
hh_day_off=int(eeprom[71]+eeprom[72]);min_day_off=int(eeprom[73]+eeprom[74])
hh_moon_on=int(eeprom[94]+eeprom[95]);min_moon_on=int(eeprom[96]+eeprom[97])
hh_moon_off=int(eeprom[107]+eeprom[108]);min_moon_off=int(eeprom[109]+eeprom[110])
hh_co2_on=int(eeprom[129]+eeprom[130]);min_co2_on=int(eeprom[131]+eeprom[132])
hh_co2_off=int(eeprom[141]+eeprom[142]);min_co2_off=int(eeprom[143]+eeprom[144])
hh_o2_on=int(eeprom[151]+eeprom[152]);min_o2_on=int(eeprom[153]+eeprom[154])
hh_o2_off=int(eeprom[162]+eeprom[163]);min_o2_off=int(eeprom[164]+eeprom[165])
hh_food1_on=int(eeprom[173]+eeprom[174]);min_food1_on=int(eeprom[175]+eeprom[176])
hh_food2_on=int(eeprom[184]+eeprom[185]);min_food2_on=int(eeprom[186]+eeprom[187])
hh_food3_on=int(eeprom[195]+eeprom[196]);min_food3_on=int(eeprom[197]+eeprom[198])
hh_food4_on=int(eeprom[206]+eeprom[207]);min_food4_on=int(eeprom[208]+eeprom[209])
food1_pass=int(eeprom[221])
food2_pass=int(eeprom[222])
food3_pass=int(eeprom[223])
food4_pass=int(eeprom[224])



Tx=float(eeprom[226]+eeprom[227]+eeprom[228]+eeprom[229]);

x_date=0;y_date=20
x_time=125;y_time=20
x_ph=244;y_ph=20

x_temp=0;y_temp=70
x_heater=125;y_heater=70
x_ec=220;y_ec=70

y_day=120
y_day_bar=175
y_moon=200
y_moon_bar=255
y_co2=280
y_o2=345
y_food=410
hbar=20

x_switch_onoff=200
x_led_onoff=270

cont=0

auto_1 = pygame.image.load('auto135.png')
auto_2 = pygame.image.load('auto235.png')
on=pygame.image.load('on.png')
off=pygame.image.load('off.png')

led_on=pygame.image.load('led_on20.png')
led_off=pygame.image.load('led_off20.png')
led_on8= pygame.image.load('led_on8.png')  
led_off8= pygame.image.load('led_off8.png')
setup = pygame.image.load('setup.png')
taste_add=pygame.image.load('+taste.png')
taste_sub=pygame.image.load('-taste.png')
save35=pygame.image.load('save35.png')
date35 = pygame.image.load('date40.png')
time35 = pygame.image.load('time40.png')
ec35 = pygame.image.load('ec40.png')
temp35 = pygame.image.load('temp40.png')
heater = pygame.image.load('heater44.png')
ph35 = pygame.image.load('ph40.png')
daylight = pygame.image.load('daylight70.png')
moonlight = pygame.image.load('moonlight70.png')
co2 = pygame.image.load('co264.png')
o2 = pygame.image.load('o263.png')
food = pygame.image.load('food68.png')
ledred20= pygame.image.load('ledred20.png')
ledgreen20=pygame.image.load('ledgreen20.png')
steps=pygame.image.load('steps.png')
step0=pygame.image.load('0.png')
step1=pygame.image.load('1.png')
step2=pygame.image.load('2.png')
step3=pygame.image.load('3.png')
ledon10=pygame.image.load('ledon10.png')
ledoff10=pygame.image.load('ledoff10.png')
fish=pygame.image.load('fish.png')

fnt16 = pygame.font.SysFont("Arial", 14, bold=True)
fnt16b = pygame.font.SysFont("Arial", 16, bold=True)
fnt17 = pygame.font.SysFont("Times New Roman", 16)
fnt14 = pygame.font.SysFont("Times New Roman", 14)
fnt10 = pygame.font.SysFont("Arial", 10)
fnt12 = pygame.font.SysFont("Arial", 12, bold=True)
fnt24 = pygame.font.SysFont("Times New Roman", 24)
fnt22 = pygame.font.SysFont("Times New Roman", 22)
fnt20 = pygame.font.SysFont("Times New Roman", 20)
fnt18b = pygame.font.SysFont("Times New Roman", 18,bold=True)
fnt32 = pygame.font.SysFont("Times New Roman", 32)
fnt48 = pygame.font.SysFont("Times New Roman", 48)
fnt72 = pygame.font.SysFont("Times New Roman", 72)


#******************************** avvio ********************************
def avvio():
   screen.fill(black)
   x=30
   y=210
   rit=0.005
   
   sleep(0.5)

   for i in range (1,320):
       pygame.draw.line(screen, grey, (320-i,180),(310-i,180),1)
       pygame.draw.line(screen, grey, (i,200),(i+10,200),1)
       
       pygame.draw.line(screen, grey, (i,300),(i+10,300),1)
       pygame.draw.line(screen, grey, (320-i,280),(310-i,280),1)

       pygame.draw.line(screen, grey, (60,0),(60,i/1.6),1)
       pygame.draw.line(screen, grey, (80,200),(80,200-i/1.6),1)
       #pygame.draw.line(screen, grey, (80,280),(80,280+i/1.6),1)
       pygame.draw.line(screen, grey, (260,280),(260,280+i/1.6),1)
       pygame.draw.line(screen, grey, (240,480),(240,480-i/1.6),1)

       
       surf_text = fnt48.render("A", True, grey)
       if i>40:
           surf_text = fnt48.render("Aq", True, grey)
       if i>70:
           surf_text = fnt48.render("Aqu", True, grey)
       if i>100:
           surf_text = fnt48.render("Aqua", True, grey)
       if i>130:
           surf_text = fnt48.render("AquaC", True, grey)
       if i>160:
           surf_text = fnt48.render("AquaCo", True, grey)
       if i>190:
           surf_text = fnt48.render("AquaCon", True, grey)
       if i>220:
           surf_text = fnt48.render("AquaCont", True, grey)
       if i>250:
           surf_text = fnt48.render("AquaContr", True, grey)
       if i>280:
           surf_text = fnt48.render("AquaContro", True, grey)
       if i>310:
           surf_text = fnt48.render("AquaControl", True, grey)
           
       screen.blit(surf_text, (x, y))
       #screen.blit(fish, (100-i,320))
       screen.blit(fish, (400-i,50))
       pygame.display.update()
       sleep(rit)
   pygame.display.update()
       
   sleep(2)
   screen.fill(black)
   pygame.display.update()
   sleep(0.3)

#************************** disegna una tantum *********************************


def fix():
   screen.fill(black)
   pygame.draw.rect(screen, grey, (0,0,320,20),0)
   surf_text = fnt16.render("Paolo AquaControl - rev. 9.2", True, black)
   screen.blit(surf_text, (70, 2))
   #*******************stampa icone***************************************
   screen.blit(date35, (x_date+5,y_date+5))
   screen.blit(time35, (x_time+5,y_time+5))
   screen.blit(ec35, (x_ec+5,y_ec+5))
   screen.blit(temp35, (x_temp+5,y_temp+5))
   screen.blit(heater, (x_heater+5,y_heater+5))
   screen.blit(ph35, (x_ph+5,y_ph+5))
   screen.blit(daylight, (5,y_day+5))
   screen.blit(moonlight, (5,y_moon+5))  
   screen.blit(co2, (0,y_co2+1))
   screen.blit(o2, (0,y_o2+1))
   screen.blit(food, (1,y_food+3))
   #*******************stampa riquadri***************************************
   '''pygame.draw.rect(screen, white, (0,y_day,320,68),1)
   pygame.draw.rect(screen, white, (70,y_day+48,248,16),1) #barra pwm1
   pygame.draw.rect(screen, white, (0,y_moon,320,68),1)
   pygame.draw.rect(screen, white, (70,y_moon+48,248,16),1) #barra pwm2
   pygame.draw.rect(screen, white, (0,y_co2,320,54),1)
   pygame.draw.rect(screen, white, (0,y_o2,320,54),1)
   pygame.draw.rect(screen, white, (0,y_food,320,54),1)'''
   pygame.draw.line(screen, grey, (0,y_temp),(320,y_temp),1)
   pygame.draw.line(screen, grey, (0,y_day),(320,y_day),1)
   pygame.draw.line(screen, grey, (0,y_moon),(320,y_moon),1)
   pygame.draw.line(screen, grey, (0,y_co2),(320,y_co2),1)
   pygame.draw.line(screen, grey, (0,y_o2),(320,y_o2),1)
   pygame.draw.line(screen, grey, (0,y_food),(320,y_food),1)
   pygame.draw.rect(screen, white, (70,y_day_bar,248,hbar),1) #barra pwm1
   pygame.draw.rect(screen, white, (70,y_moon_bar,248,hbar),1) #barra pwm2



#**************  hh-mm 2 cifre  *****************
def stringa2(hm):
   stringa=""
   if hm<10:
       stringa=stringa+"0"
   stringa=stringa+str(hm)
   return (stringa)
   
def stringa5 (hh,min):
   stringa=""
   if hh<10:
       stringa=stringa+"0"
   stringa=stringa+str(hh)+":"
   if min<10:
       stringa=stringa+"0"
   stringa=stringa+str(min)
   return (stringa)


#*******************   SETUP  DATE-TIME-TEMP   ********************
def setup_1(n):
   global Tx,ora,minuti
   screen.fill(black)
   pygame.draw.line(screen, white, (60,25),(260,25),1)
   pygame.draw.line(screen, white, (60,53),(260,53),1)
   screen.blit(setup, (5, 20))
   #header(0)

 

#***scrive intestazione e icone          
   if n==1:
       surf_text = fnt16b.render("SETUP TIME", True, white)
       screen.blit(time35, (270, 20))
   if n==2:
       surf_text = fnt16b.render("SETUP TEMPERATURE", True, white)
       screen.blit(temp35, (270, 20))
   screen.blit(surf_text, surf_text.get_rect(center=(160, 40)))
#***scrive SAVE
   pygame.draw.rect(screen, white, (0,440,320,40),0)
   surf_text4=fnt24.render("S A V E", True, black)
   screen.blit(save35, (90, 442))
   screen.blit(surf_text4, surf_text4.get_rect(center=(180, 460)))  
#***scrive tasti + e -
   screen.blit(taste_add, (60, 100))
   screen.blit(taste_sub, (60, 350))      
#***scrive TIME - TEMP
   if n==1:
       surf_text1 = fnt72.render(stringa5(ora,minuti), True, white)
   else:
       surf_text1 = fnt72.render("{0:.2f}".format(Tx), True, white)
   screen.blit(surf_text1, surf_text1.get_rect(center=(160, 250)))
       
   pygame.display.update()
   

   yy=0
   while yy<440:
       print("secondo get")
       for event in pygame.event.get():
           if(event.type is MOUSEBUTTONDOWN):
               pos = pygame.mouse.get_pos()
               xx,yy = pos
               print (pos)
#***************selezionato + 0 -
               if yy>100 and yy<160:
                   if n==1:
                       minuti=minuti+1
                       if minuti==60:
                           minuti=0
                           ora=ora+1
                           if ora==24:
                               ora=0
                   if n==2:
                       Tx=Tx+0.1
                   pygame.draw.rect(screen, black, (78,218,165,80),0)
                                             
               if yy>350 and yy<410:
                   if n==1:
                       minuti=minuti-1
                       if minuti==-1:
                           minuti=59
                           ora=ora-1
                           if ora==-1:
                               ora=23
                   if n==2:
                       Tx=Tx-0.1
                   pygame.draw.rect(screen, black, (78,218,165,80),0)
                       
               if n==1:
                   surf_text1 = fnt72.render(stringa5(ora,minuti), True, white)
               if n==2:
                   surf_text1 = fnt72.render("{0:.2f}".format(Tx), True, white)
               screen.blit(surf_text1, surf_text1.get_rect(center=(160, 250)))

               pygame.display.update()
                   
   pygame.display.update()
   return()



#*******************   SETUP  LIGHTS-CO2-O2   ********************
def setup_2(n):
   global hh_on, hh_off, min_on, min_off
   screen.fill(black)
   pygame.draw.line(screen, white, (60,25),(260,25),1)
   pygame.draw.line(screen, white, (60,53),(260,53),1)
   pygame.draw.line(screen, pesca, (0,260),(320,260),1)
   screen.blit(setup, (5, 20))
   surf_text = fnt16b.render("ON", True, pesca)
   screen.blit(surf_text, (15, 210))
   surf_text = fnt16b.render("OFF", True, pesca)
   screen.blit(surf_text, (15, 290))
   #header(0)
#***scrive intestazione e icone                            
   if n==1:
       surf_text = fnt16b.render("SETUP DAYLIGHT", True, white)
       screen.blit(daylight, (265, 0))
   if n==2:
       surf_text = fnt16b.render("SETUP MOONLIGHT", True, white)
       screen.blit(moonlight, (265, 0))
   if n==3:
       surf_text = fnt16b.render("SETUP CO2", True, white)
       screen.blit(co2, (265, 0))
   if n==4:
       surf_text = fnt16b.render("SETUP O2", True, white)
       screen.blit(o2, (265, 0))
   screen.blit(surf_text, surf_text.get_rect(center=(160, 40)))
#***scrive SAVE
   pygame.draw.rect(screen, white, (0,440,320,40),0)
   surf_text4=fnt24.render("S A V E", True, black)
   screen.blit(save35, (90, 442))
   screen.blit(surf_text4, surf_text4.get_rect(center=(180, 460)))
#***scrive tasti + e -
   screen.blit(taste_add, (60, 100))
   screen.blit(taste_sub, (60, 350))
#***seleziona default hh_on
   pygame.draw.rect(screen, pesca, (78,180,76,74),0)
   regola=1
#***scrive HH:MM ON e OFF
   surf_text1 = fnt72.render(stringa5(hh_on,min_on), True, white)
   surf_text2 = fnt72.render(stringa5(hh_off,min_off), True, white)
   screen.blit(surf_text1, (79, 175))
   screen.blit(surf_text2, (79, 260))
   

   pygame.display.update()
   sleep(1)
   

   yy=0
   while yy<440:
       print("secondo get")
       for event in pygame.event.get():
           if(event.type is MOUSEBUTTONDOWN):
               pos = pygame.mouse.get_pos()
               xx,yy = pos
               print (pos)
#***************selezionato + 0 -
               if yy>100 and yy<160:
                   if regola==1:
                       hh_on=hh_on+1
                       if hh_on==24:
                           hh_on=0
                       pygame.draw.rect(screen, black, (78,180,76,74),0)
                       pygame.draw.rect(screen, pesca, (78,180,76,74),0)
                   if regola==2:
                       hh_off=hh_off+1
                       if hh_off==24:
                           hh_off=0
                       pygame.draw.rect(screen, black, (78,268,76,74),0)
                       pygame.draw.rect(screen, pesca, (78,268,76,74),0)
                   if regola==3:
                       min_on=min_on+1
                       if min_on==60:
                           min_on=0
                           hh_on=hh_on+1
                       pygame.draw.rect(screen, black, (78,180,76,74),0)
                       pygame.draw.rect(screen, black, (170,180,76,74),0)
                       pygame.draw.rect(screen, pesca, (170,180,76,74),0)
                   if regola==4:
                       min_off=min_off+1
                       if min_off==60:
                           min_off=0
                           hh_off=hh_off+1
                       pygame.draw.rect(screen, black, (78,268,76,74),0)
                       pygame.draw.rect(screen, black, (170,268,76,74),0)
                       pygame.draw.rect(screen, pesca, (170,268,76,74),0)
                       
               if yy>350 and yy<410:
                   if regola==1:
                       hh_on=hh_on-1
                       if hh_on==-1:
                           hh_on=23
                       pygame.draw.rect(screen, black, (78,180,76,74),0)
                       pygame.draw.rect(screen, pesca, (78,180,76,74),0)
                   if regola==2:
                       hh_off=hh_off-1
                       if hh_off==-1:
                           hh_off=23
                       pygame.draw.rect(screen, black, (78,268,76,74),0)
                       pygame.draw.rect(screen, pesca, (78,268,76,74),0)
                   if regola==3:
                       min_on=min_on-1
                       if min_on==-1:
                           min_on=59
                           hh_on=hh_on-1
                       pygame.draw.rect(screen, black, (78,180,76,74),0)
                       pygame.draw.rect(screen, black, (170,180,76,74),0)
                       pygame.draw.rect(screen, pesca, (170,180,76,74),0)
                   if regola==4:
                       min_off=min_off-1
                       if min_off==-1:
                           min_off=59
                           hh_off=hh_off-1
                       pygame.draw.rect(screen, black, (78,268,76,74),0)
                       pygame.draw.rect(screen, black, (170,268,76,74),0)
                       pygame.draw.rect(screen, pesca, (170,268,76,74),0)


               if xx>80 and xx<160 and yy>180 and yy<260:
#***************selezionato hh_on:
                   regola=1
                   pygame.draw.rect(screen, black, (78,180,76,74),0)
                   pygame.draw.rect(screen, black, (78,268,76,74),0)
                   pygame.draw.rect(screen, black, (170,180,76,74),0)
                   pygame.draw.rect(screen, black, (170,268,76,74),0)
                   
                   pygame.draw.rect(screen, pesca, (78,180,76,74),0)
                   surf_text1 = fnt72.render(stringa5(hh_on,min_on), True, white)
                   screen.blit(surf_text1, (79, 175))
               

               if xx>80 and xx<160 and yy>260 and yy<340:
#***************selezionato hh_off
                   regola=2
                   pygame.draw.rect(screen, black, (78,180,76,74),0)
                   pygame.draw.rect(screen, black, (78,268,76,74),0)
                   pygame.draw.rect(screen, black, (170,180,76,74),0)
                   pygame.draw.rect(screen, black, (170,268,76,74),0)
                   
                   pygame.draw.rect(screen, pesca, (78,268,76,74),0)
                   surf_text2 = fnt72.render(stringa5(hh_off,min_off), True, white)
                   screen.blit(surf_text2, (79, 260))


               if xx>160 and xx<240 and yy>180 and yy<260:
#***************selezionato min_on
                   regola=3
                   pygame.draw.rect(screen, black, (78,180,76,74),0)
                   pygame.draw.rect(screen, black, (78,268,76,74),0)
                   pygame.draw.rect(screen, black, (170,180,76,74),0)
                   pygame.draw.rect(screen, black, (170,268,76,74),0)
                   
                   pygame.draw.rect(screen, pesca, (170,180,76,74),0)
                   surf_text1 = fnt72.render(stringa5(hh_on,min_on), True, white)
                   screen.blit(surf_text1, (79, 175))


               if xx>160 and xx<240 and yy>260 and yy<340:
#***************selezionato min_off
                   regola=4
                   pygame.draw.rect(screen, black, (78,180,76,74),0)
                   pygame.draw.rect(screen, black, (78,268,76,74),0)
                   pygame.draw.rect(screen, black, (170,180,76,74),0)
                   pygame.draw.rect(screen, black, (170,268,76,74),0)
                   
                   pygame.draw.rect(screen, pesca, (170,268,76,74),0)
                   surf_text2 = fnt72.render(stringa5(hh_off,min_off), True, white)
                   screen.blit(surf_text2, (79, 260))

                               
               surf_text1 = fnt72.render(stringa5(hh_on,min_on), True, white)
               surf_text2 = fnt72.render(stringa5(hh_off,min_off), True, white)
               #pygame.draw.rect(screen, black, (78,180,76,80),0)
               screen.blit(surf_text1, (79, 175))
               screen.blit(surf_text2, (79, 260))
               pygame.display.update()
   
   return()

def regola_food(box):
   pygame.draw.rect(screen, black, (73,172,49,40),0)
   pygame.draw.rect(screen, black, (73,217,49,40),0)
   pygame.draw.rect(screen, black, (73,262,49,40),0)
   pygame.draw.rect(screen, black, (73,307,49,40),0)
   pygame.draw.rect(screen, black, (130,172,49,40),0)
   pygame.draw.rect(screen, black, (130,217,49,40),0)
   pygame.draw.rect(screen, black, (130,262,49,40),0)
   pygame.draw.rect(screen, black, (130,307,49,40),0)
   if box ==1:
       xbox=73;ybox=172;hh=hh_food1_on;min=min_food1_on
   elif box==3:
       xbox=130;ybox=172;hh=hh_food1_on;min=min_food1_on
   elif box==2:
       xbox=73;ybox=217;hh=hh_food2_on;min=min_food2_on
   elif box==4:
       xbox=130;ybox=217;hh=hh_food2_on;min=min_food2_on
   elif box==5:
       xbox=73;ybox=262;hh=hh_food3_on;min=min_food3_on
   elif box==7:
       xbox=130;ybox=262;hh=hh_food3_on;min=min_food3_on
   elif box==6:
       xbox=73;ybox=307;hh=hh_food4_on;min=min_food4_on
   else:
       xbox=130;ybox=307;hh=hh_food4_on;min=min_food4_on
   pygame.draw.rect(screen, pesca, (xbox,ybox,49,40),0)
   surf_text = fnt48.render(stringa5(hh,min), True, white)
   screen.blit(surf_text, (72, ybox-7))

def regola_piu_meno(box,segno):
   global hh_food1_on, hh_food2_on, hh_food3_on, hh_food4_on
   global min_food1_on, min_food2_on, min_food3_on, min_food4_on
   
   print(hh_food1_on, hh_food2_on)
   if box==1:
       xreg=73; yreg=172; hm="h"; hh=hh_food1_on
   if box==2:
       xreg=73; yreg=217; hm="h"; hh=hh_food2_on
   if box==5:
       xreg=73; yreg=262; hm="h"; hh=hh_food3_on
   if box==6:
       xreg=73; yreg=307; hm="h"; hh=hh_food4_on
   if box==3:
       xreg=73; yreg=172; hm="m"; min=min_food1_on; hh=hh_food1_on
   if box==4:
       xreg=73; yreg=217; hm="m"; min=min_food2_on; hh=hh_food2_on
   if box==7:
       xreg=73; yreg=262; hm="m"; min=min_food3_on; hh=hh_food3_on
   if box==8:
       xreg=73; yreg=307; hm="m"; min=min_food4_on; hh=hh_food4_on
   
   pygame.draw.rect(screen, black, (xreg,yreg,49,40),0)
   pygame.draw.rect(screen, black, (xreg+57,yreg,49,40),0)
   
   if hm=="h":
       if segno=="+":
           hh=hh+1
           if hh==24: hh=0
       if segno=="-":
           hh=hh-1
           if hh==-1: hh=23
       pygame.draw.rect(screen, pesca, (xreg,yreg,49,40),0)
   else:
       if segno=="+":
           min=min+1
           if min==60: min=0; hh=hh+1
       if segno=="-":
           min=min-1
           if min==-1: min=59; hh=hh-1
       pygame.draw.rect(screen, pesca, (xreg+57,yreg,49,40),0)

   if box==1: hh_food1_on=hh
   elif box==2: hh_food2_on=hh
   elif box==3: min_food1_on=min
   elif box==4: min_food2_on=min
   elif box==5: hh_food3_on=hh
   elif box==6: hh_food4_on=hh
   elif box==7: min_food3_on=min
   elif box==8: min_food4_on=min
   
   return()

   


#*******************   SETUP  FOOD   ********************
def setup_3():
   global hh_food1_on,hh_food2_on,hh_food3_on,hh_food4_on
   global min_food1_on,min_food2_on,min_food3_on,min_food4_on
   global food1_on,food2_on,food3_on,food4_on
   global food1_pass,food2_pass,food3_pass,food4_pass
   screen.fill(black)
   pygame.draw.line(screen, white, (60,25),(260,25),1)
   pygame.draw.line(screen, white, (60,53),(260,53),1)
   pygame.draw.line(screen, pesca, (0,214),(320,214),1)
   pygame.draw.line(screen, pesca, (0,259),(320,259),1)
   pygame.draw.line(screen, pesca, (0,304),(320,304),1)
   surf_text = fnt16b.render("ON 1", True, pesca)
   screen.blit(surf_text, (15, 180))
   surf_text = fnt16b.render("ON 2", True, pesca)
   screen.blit(surf_text, (15, 225))
   surf_text = fnt16b.render("ON 3", True, pesca)
   screen.blit(surf_text, (15, 270))
   surf_text = fnt16b.render("ON 4", True, pesca)
   screen.blit(surf_text, (15, 315))
   
   
   screen.blit(setup, (5, 20))
   screen.blit(food, (265, 2))
   surf_text = fnt16b.render("SETUP FOOD", True, white)
   screen.blit(surf_text, surf_text.get_rect(center=(160, 40)))          
   #header(0)
                   
   
#***scrive SAVE
   pygame.draw.rect(screen, white, (0,440,320,40),0)
   surf_text4=fnt24.render("S A V E", True, black)
   screen.blit(save35, (90, 442))
   screen.blit(surf_text4, surf_text4.get_rect(center=(180, 460)))    
#***scrive tasti + e -
   screen.blit(taste_add, (20, 100))
   screen.blit(taste_sub, (20, 350))
#***seleziona default hh_on
   pygame.draw.rect(screen, pesca, (73,172,49,40),0)
   regola=1
#***scrive HH:MM ON
   surf_text1 = fnt48.render(stringa5(hh_food1_on,min_food1_on), True, white)
   surf_text2 = fnt48.render(stringa5(hh_food2_on,min_food2_on), True, white)
   surf_text3 = fnt48.render(stringa5(hh_food3_on,min_food3_on), True, white)
   surf_text4 = fnt48.render(stringa5(hh_food4_on,min_food4_on), True, white)
   screen.blit(surf_text1, (72, 165))
   screen.blit(surf_text2, (72, 210))
   screen.blit(surf_text3, (72, 255))
   screen.blit(surf_text4, (72, 300))

   screen.blit(steps, (230, 125))
   screen.blit(step0, (220, 175))
   screen.blit(step1, (245, 175))
   screen.blit(step2, (270, 175))
   screen.blit(step3, (295, 175))
   screen.blit(step0, (220, 220))
   screen.blit(step1, (245, 220))
   screen.blit(step2, (270, 220))
   screen.blit(step3, (295, 220))
   screen.blit(step0, (220, 265))
   screen.blit(step1, (245, 265))
   screen.blit(step2, (270, 265))
   screen.blit(step3, (295, 265))
   screen.blit(step0, (220, 310))
   screen.blit(step1, (245, 310))
   screen.blit(step2, (270, 310))
   screen.blit(step3, (295, 310))
   if food1_pass==0: screen.blit(ledoff10, (225, 200))
   if food1_pass>0: screen.blit(ledon10, (250, 200))
   if food1_pass>1: screen.blit(ledon10, (275, 200))
   if food1_pass>2: screen.blit(ledon10, (300, 200))

   if food2_pass==0: screen.blit(ledoff10, (225, 245))
   if food2_pass>0: screen.blit(ledon10, (250, 245))
   if food2_pass>1: screen.blit(ledon10, (275, 245))
   if food2_pass>2: screen.blit(ledon10, (300, 245))

   if food3_pass==0: screen.blit(ledoff10, (225, 290))
   if food3_pass>0: screen.blit(ledon10, (250, 290))
   if food3_pass>1: screen.blit(ledon10, (275, 290))
   if food3_pass>2: screen.blit(ledon10, (300, 290))

   if food4_pass==0: screen.blit(ledoff10, (225, 335))
   if food4_pass>0: screen.blit(ledon10, (250, 335))
   if food4_pass>1: screen.blit(ledon10, (275, 335))
   if food4_pass>2: screen.blit(ledon10, (300, 335))
   

   pygame.display.update()

   yy=0
   while yy<440:
       #print("secondo get")
       for event in pygame.event.get():
           if(event.type is MOUSEBUTTONDOWN):
               pos = pygame.mouse.get_pos()
               xx,yy = pos
               print (pos)
#***************selezionato +
               if yy>100 and yy<160 and xx>20 and xx<220:
                   if regola==1: regola_piu_meno(1,"+")
                   elif regola==2: regola_piu_meno(2,"+")
                   elif regola==3: regola_piu_meno(3,"+")
                   elif regola==4: regola_piu_meno(4,"+")
                   elif regola==5: regola_piu_meno(5,"+")
                   elif regola==6: regola_piu_meno(6,"+")
                   elif regola==7: regola_piu_meno(7,"+")
                   elif regola==8: regola_piu_meno(8,"+")
                       
#***************selezionato  -
               if yy>350 and yy<410 and xx>20 and xx<220:
                   if regola==1: regola_piu_meno(1,"-")  
                   elif regola==2: regola_piu_meno(2,"-")
                   elif regola==3: regola_piu_meno(3,"-")
                   elif regola==4: regola_piu_meno(4,"-")
                   elif regola==5: regola_piu_meno(5,"-")
                   elif regola==6: regola_piu_meno(6,"-")
                   elif regola==7: regola_piu_meno(7,"-")
                   elif regola==8: regola_piu_meno(8,"-")

               
                 
               if xx>70 and xx<125 and yy>180 and yy<220:
#***************selezionato hh_food1_on
                   regola=1;regola_food(1)

               if xx>70 and xx<125 and yy>220 and yy<260:
#***************selezionato hh_food2_on
                   regola=2;regola_food(2)
                   
               if xx>125 and xx<185 and yy>180 and yy<220:
#***************selezionato min_food1_on
                   regola=3;regola_food(3)
                   
               if xx>125 and xx<185 and yy>220 and yy<260:
#***************selezionato min_food2_on
                   regola=4;regola_food(4)

               if xx>70 and xx<125 and yy>260 and yy<300:
#***************selezionato hh_food3_on
                   regola=5;regola_food(5)

               if xx>70 and xx<125  and yy>300 and yy<340:
#***************selezionato hh_food4_on
                   regola=6;regola_food(6)
                   
               if xx>125 and xx<185 and yy>260 and yy<300:
#***************selezionato min_food3_on
                   regola=7;regola_food(7)
                   
               if xx>125 and xx<185 and yy>300 and yy<340:
#***************selezionato min_food4_on
                   regola=8;regola_food(8)
                               
               surf_text1 = fnt48.render(stringa5(hh_food1_on,min_food1_on), True, white)
               surf_text2 = fnt48.render(stringa5(hh_food2_on,min_food2_on), True, white)
               surf_text3 = fnt48.render(stringa5(hh_food3_on,min_food3_on), True, white)
               surf_text4 = fnt48.render(stringa5(hh_food4_on,min_food4_on), True, white)

               screen.blit(surf_text1, (72, 165))
               screen.blit(surf_text2, (72, 210))
               screen.blit(surf_text3, (72, 255))
               screen.blit(surf_text4, (72, 300))
               pygame.display.update()

               if xx>220 and xx<240: #food_pass =0
                   if yy>180 and yy<200: food1_pass=0
                   if yy>225 and yy<245: food2_pass=0
                   if yy>270 and yy<295: food3_pass=0
                   if yy>320 and yy<340: food4_pass=0
                   setup_3()

               if xx>245 and xx<265: #food_pass =1
                   if yy>180 and yy<200: food1_pass=1
                   if yy>225 and yy<245: food2_pass=1
                   if yy>270 and yy<295: food3_pass=1
                   if yy>320 and yy<340: food4_pass=1
                   setup_3()

               if xx>270 and xx<290: #food_pass =2
                   if yy>180 and yy<200: food1_pass=2
                   if yy>225 and yy<245: food2_pass=2
                   if yy>270 and yy<295: food3_pass=2
                   if yy>320 and yy<340: food4_pass=2
                   setup_3()

               if xx>295 and xx<315: #food_pass =3
                   if yy>180 and yy<200: food1_pass=3
                   if yy>225 and yy<245: food2_pass=3
                   if yy>270 and yy<295: food3_pass=3
                   if yy>320 and yy<340: food4_pass=3
                   setup_3()
                 
   
   return()



#**************************     INIZIO       ***********************            
avvio()
fix()
while True:
   cont=cont+1
   
#******************    DATE     ************************
   localtime = time.asctime( time.localtime(time.time()) )
   pygame.draw.rect(screen, back_txt_color, (x_date+47,y_date+12,75,30),0)
   surf_text = fnt16.render(localtime[0]+localtime[1]+localtime[2]+localtime[3]+localtime[8]+localtime[9]+" "+localtime[4]+localtime[5]+localtime[6],True, white)
   screen.blit(surf_text, (x_date+50, y_date+18))

#******************    TIME     ************************
   ora= int(localtime[11]+localtime[12])
   minuti=int(localtime[14]+localtime[15])
   secondi=int(localtime[17]+localtime[18])
   pygame.draw.rect(screen, back_txt_color, (x_time+47,y_time+12,70,30),0)
   surf_text = fnt16.render(localtime[11]+localtime[12]+":"+localtime[14]+localtime[15]+":"+localtime[17]+localtime[18], True, white)
   screen.blit(surf_text, (x_time+50, y_time+18))
#******************     EC      ************************
   pygame.draw.rect(screen, back_txt_color, (x_ec+47,y_ec+12,60,30),0)
   surf_text = fnt16.render(str(ec)+" uS", True, white)
   screen.blit(surf_text, (x_ec+50,y_ec+18))
#******************    TEMP     ************************
   temp= sensor.get_temperature()
   temp=temp*1000
   temp=int(temp/100)
   temp=temp/10
   print("temp %s" % temp)
   pygame.draw.rect(screen, back_txt_color, (x_temp+47,y_temp+12,57,30),0)
   surf_text = fnt16.render(str(temp)+"°C", True, white)
   screen.blit(surf_text, (x_temp+50, y_temp+18))
#******************    HEATER     ************************
   pygame.draw.rect(screen, back_txt_color, (x_heater+53,y_heater+5,25,43),0)
   if T<Tx:
       screen.blit(led_on, (x_heater+52,y_heater+12))
       surf_text = fnt12.render("ON", True, green)
       GPIO.output(35, GPIO.LOW)
   else:
       screen.blit(led_off, (x_heater+52,y_heater+12))
       surf_text = fnt12.render("OFF", True, red)
       GPIO.output(35, GPIO.HIGH)
   screen.blit(surf_text, (x_heater+55, y_heater+37))
#******************     PH      ************************
   pygame.draw.rect(screen, back_txt_color, (x_ph+47,y_ph+12,30,30),0)
   surf_text = fnt16.render(str(ph), True, white)
   screen.blit(surf_text, (x_ph+50, y_ph+18))
#******************     DAYLight      ************************
   pygame.draw.rect(screen, back_txt_color, (160,y_day+10,80,35),0)
   if auto1==True:
       surf_text1 = fnt16.render("ON:", True, green)
       surf_text2 = fnt16.render("OFF:", True, red)
       surf_text11 = fnt16.render(stringa5(hh_day_on,min_day_on), True, white)
       surf_text12 = fnt16.render(stringa5(hh_day_off,min_day_off), True, white)
       screen.blit(auto_1, (70,y_day+12))
       #screen.blit(onoff, (175,y_day+8))
       d.ChangeDutyCycle(pwm1_a/2.55)
       #barra
       pygame.draw.rect(screen, back_txt_color, (71,y_day_bar+1,246,hbar-2),0)
       pygame.draw.rect(screen, yellow, (71,y_day_bar+1,max(1,int(pwm1_a/255*246)),hbar-2),0)
       surf_text3 = fnt16.render(str(int(pwm1_a/2.55))+"%", True, moon)
   else:
       surf_text1 = fnt16.render("", True, black)
       surf_text2 = fnt16.render("", True, black)
       surf_text11 = fnt16.render("", True, black)
       surf_text12 = fnt16.render("", True, black)
       screen.blit(auto_2, (70,y_day+12))
       #barra
       pygame.draw.rect(screen, back_txt_color, (71,y_day_bar+1,246,hbar-2),0)
       if on1==True:
           pygame.draw.rect(screen, yellow, (71,y_day_bar+1,max(1,int(pwm1_m/255*247)),hbar-2),0)
           screen.blit(on, (x_switch_onoff,y_day+8))
           d.ChangeDutyCycle(pwm1_m/2.55)
       else:
           pygame.draw.rect(screen, yellow, (71,y_day_bar+1,max(1,int(pwm1_m/255*247)),hbar-3),2)
           screen.blit(off, (x_switch_onoff,y_day+8))
           d.ChangeDutyCycle(0)
       surf_text3 = fnt16.render(str(int(pwm1_m/2.54))+"%", True, moon)
       
   if on1==True:
       screen.blit(led_on, (x_led_onoff, y_day+18))
       surf_text= fnt12.render("ON",  True, green)
   else:
       screen.blit(led_off, (x_led_onoff, y_day+18))
       surf_text= fnt12.render("OFF",  True, red)
   
   screen.blit(surf_text1, (165, y_day+10))
   screen.blit(surf_text2, (165, y_day+28))
   screen.blit(surf_text11, (205, y_day+10))
   screen.blit(surf_text12, (205, y_day+28))
   screen.blit(surf_text3, (185, y_day_bar+3))
   pygame.draw.rect(screen, back_txt_color, (295,y_day+20,24,18),0)
   screen.blit(surf_text, (295, y_day+22))

#******************     MOONLight      ************************
   pygame.draw.rect(screen, back_txt_color, (160,y_moon+10,80,35),0)
   if auto2==True:
       surf_text1 = fnt16.render("ON:", True, green)
       surf_text2 = fnt16.render("OFF:", True, red)
       surf_text11 = fnt16.render(stringa5(hh_moon_on,min_moon_on), True, white)
       surf_text12 = fnt16.render(stringa5(hh_moon_off,min_moon_off), True, white)

       screen.blit(auto_1, (70,y_moon+12))
       #screen.blit(onoff, (175,y_moon+8))
       m.ChangeDutyCycle(pwm2_a/2.55)
       #barra
       pygame.draw.rect(screen, back_txt_color, (71,y_moon_bar+1,246,hbar-2),0)
       pygame.draw.rect(screen, moon, (71,y_moon_bar+1,max(1,int(pwm2_a/255*246)),hbar-2),0)
       surf_text3 = fnt16.render(str(int(pwm2_a/2.55))+"%", True, yellow)  
   else:
       surf_text1 = fnt16.render("", True, black)
       surf_text2 = fnt16.render("", True, black)
       surf_text11 = fnt16.render("", True, black)
       surf_text12 = fnt16.render("", True, black)
       screen.blit(auto_2, (70,y_moon+12))
       #barra
       pygame.draw.rect(screen, back_txt_color, (71,y_moon_bar+1,246,hbar-2),0)
       if on2==True:
           pygame.draw.rect(screen, moon, (71,y_moon_bar+1,max(1,int(pwm2_m/255*247)),hbar-2),0)
           screen.blit(on, (x_switch_onoff,y_moon+8))
           m.ChangeDutyCycle(pwm2_m/2.55)
       else:
           pygame.draw.rect(screen, moon, (71,y_moon_bar+1,max(1,int(pwm2_m/255*247)),hbar-3),2)
           screen.blit(off, (x_switch_onoff,y_moon+8))
           m.ChangeDutyCycle(0)
       surf_text3 = fnt16.render(str(int(pwm2_m/2.53))+"%", True, yellow)
       
   if on2==True:
       screen.blit(led_on, (x_led_onoff, y_moon+18))
       surf_text= fnt12.render("ON",  True, green)
   else:
       screen.blit(led_off, (x_led_onoff, y_moon+18))
       surf_text= fnt12.render("OFF",  True, red)
 
   screen.blit(surf_text1, (165, y_moon+10))
   screen.blit(surf_text2, (165, y_moon+28))
   screen.blit(surf_text11, (205, y_moon+10))
   screen.blit(surf_text12, (205, y_moon+28))
   screen.blit(surf_text3, (185, y_moon_bar+3))
   pygame.draw.rect(screen, back_txt_color, (295,y_moon+20,24,18),0)
   screen.blit(surf_text, (295, y_moon+22))
   
#******************    CO2      ************************
   pygame.draw.rect(screen, back_txt_color, (160,y_co2+10,80,40),0)
   if auto3==True:
       surf_text1 = fnt16.render("ON:", True, green)
       surf_text2 = fnt16.render("OFF:", True, red)
       surf_text11 = fnt16.render(stringa5(hh_co2_on,min_co2_on), True, white)
       surf_text12 = fnt16.render(stringa5(hh_co2_off,min_co2_off), True, white)
       screen.blit(auto_1, (70,y_co2+15))
       #screen.blit(onoff, (175,y_co2+8))
   else:
       surf_text1 = fnt16.render("", True, black)
       surf_text2 = fnt16.render("", True, black)
       surf_text11 = fnt16.render("", True, black)
       surf_text12 = fnt16.render("", True, black)
       screen.blit(auto_2, (70,y_co2+15))
       if on3==True:
           screen.blit(on, (x_switch_onoff,y_co2+15))
       else:
           screen.blit(off, (x_switch_onoff,y_co2+15))
       
   if on3==True:
       screen.blit(led_on, (x_led_onoff, y_co2+23))
       surf_text= fnt12.render("ON",  True, green)

       GPIO.output(36, GPIO.LOW)
   else:
       screen.blit(led_off, (x_led_onoff, y_co2+23))
       surf_text= fnt12.render("OFF",  True, red)
       GPIO.output(36, GPIO.HIGH)
       
   screen.blit(surf_text1, (165, y_co2+13))
   screen.blit(surf_text2, (165, y_co2+33))
   screen.blit(surf_text11, (205, y_co2+13))
   screen.blit(surf_text12, (205, y_co2+33))
   pygame.draw.rect(screen, back_txt_color, (295,y_co2+26,24,18),0)
   screen.blit(surf_text, (295, y_co2+28))
   

   
#******************    O2      ************************
   pygame.draw.rect(screen, back_txt_color, (160,y_o2+10,80,40),0)
   if auto4==True:
       surf_text1 = fnt16.render("ON:", True, green)
       surf_text2 = fnt16.render("OFF:", True, red)
       surf_text11 = fnt16.render(stringa5(hh_o2_on,min_o2_on), True, white)
       surf_text12 = fnt16.render(stringa5(hh_o2_off,min_o2_off), True, white)
       screen.blit(auto_1, (70,y_o2+15))
       #screen.blit(onoff, (175,y_o2+8))
   else:
       surf_text1 = fnt16.render("", True, black)
       surf_text2 = fnt16.render("", True, black)
       surf_text11 = fnt16.render("", True, black)
       surf_text12 = fnt16.render("", True, black)
       screen.blit(auto_2, (70,y_o2+15))
       if on4==True:
           screen.blit(on, (x_switch_onoff,y_o2+15))
       else:
           screen.blit(off, (x_switch_onoff,y_o2+15))
   if on4==True:
       screen.blit(led_on, (x_led_onoff, y_o2+23))
       surf_text= fnt12.render("ON",  True, green)
       GPIO.output(37, GPIO.LOW)
   else:
       screen.blit(led_off, (x_led_onoff, y_o2+23))
       surf_text= fnt12.render("OFF",  True, red)
       GPIO.output(37,GPIO.HIGH)
 
   screen.blit(surf_text1, (165, y_o2+13))
   screen.blit(surf_text2, (165, y_o2+33))
   screen.blit(surf_text11, (205, y_o2+13))
   screen.blit(surf_text12, (205, y_o2+33))
   pygame.draw.rect(screen, back_txt_color, (295,y_o2+26,24,18),0)
   screen.blit(surf_text, (295, y_o2+28))
   

   
#****************** FOOD      ************************
   pygame.draw.rect(screen, back_txt_color, (160,y_food+4,104,63),0)
   if auto5==True:
       surf_text1 = fnt16.render("ON:", True, green)
       surf_text11 = fnt16.render(stringa5(hh_food1_on,min_food1_on), True, white)
       surf_text2 = fnt16.render(stringa5(hh_food2_on,min_food2_on), True, white)
       surf_text3 = fnt16.render(stringa5(hh_food3_on,min_food3_on), True, white)
       surf_text4 = fnt16.render(stringa5(hh_food4_on,min_food4_on), True, white)
       screen.blit(auto_1, (70,y_food+18))
       #screen.blit(onoff, (175,y_food+8))
   else:
       surf_text1 = fnt16.render("", True, black)
       surf_text11 = fnt16.render("", True, black)
       surf_text2 = fnt16.render("", True, black)
       surf_text3 = fnt16.render("", True, black)
       surf_text4 = fnt16.render("", True, black)
       screen.blit(auto_2, (70,y_food+18))
       if on5==True:
           screen.blit(on, (x_switch_onoff,y_food+20))
           GPIO.output(38, GPIO.LOW)
       else:
           screen.blit(off, (x_switch_onoff,y_food+20))
           GPIO.output(38, GPIO.HIGH)
   if on5==True:
       screen.blit(led_on, (x_led_onoff, y_food+26))
       surf_text= fnt12.render("ON",  True, green)
   else:
       screen.blit(led_off, (x_led_onoff, y_food+26))
       surf_text= fnt12.render("OFF",  True, red)
       
   screen.blit(surf_text1, (165, y_food+4))
   screen.blit(surf_text11, (194, y_food+4))
   screen.blit(surf_text2, (194, y_food+19))
   screen.blit(surf_text3, (194, y_food+34))
   screen.blit(surf_text4, (194, y_food+49))
   pygame.draw.rect(screen, back_txt_color, (295,y_food+26,24,18),0)
   screen.blit(surf_text, (295, y_food+30))
   
   if auto5==True:
       if food1_pass>0:
           screen.blit(led_on8, (235, y_food+9))
           if food1_pass>1: screen.blit(led_on8, (245, y_food+9))
           if food1_pass>2:screen.blit(led_on8, (255, y_food+9))
       else:screen.blit(led_off8, (235, y_food+9))

       if food2_pass>0:
           screen.blit(led_on8, (235, y_food+24))
           if food2_pass>1:screen.blit(led_on8, (245, y_food+24))
           if food2_pass>2:screen.blit(led_on8, (255, y_food+24))
       else:screen.blit(led_off8, (235, y_food+24))

       if food3_pass>0:
           screen.blit(led_on8, (235, y_food+39))
           if food3_pass>1:screen.blit(led_on8, (245, y_food+39))
           if food3_pass>2:screen.blit(led_on8, (255, y_food+39))
       else:screen.blit(led_off8, (235, y_food+39))

       if food4_pass>0:
           screen.blit(led_on8, (235, y_food+54))
           if food4_pass>1:screen.blit(led_on8, (245, y_food+54))
           if food4_pass>2:screen.blit(led_on8, (255, y_food+54))
       else:screen.blit(led_off8, (235, y_food+54))
       
  #****************** ON_OFF Lights ************************
   if auto1==True:
       if (((ora*60+minuti)>= (hh_day_on*60+ min_day_on)) and ((ora*60+minuti)< (hh_day_off*60+ min_day_off))):
           on1=True
           
       else:
           on1=False
   if auto2==True:
       if (((ora*60+minuti)>= (hh_moon_on*60+ min_moon_on)) and ((ora*60+minuti)< (hh_moon_off*60+ min_moon_off))):
           on2=True
       else:
           on2=False  
   #****************** CALCOLA PWM_A ************************
   def Dsec(hh,min):
       n_sec=hh*3600+min*60
       n_sec_now=ora*3600+minuti*60+secondi
       delta=n_sec_now-n_sec
       return delta
   
   if Dsec(hh_day_on,min_day_on)<0:
       pwm1_a=0
   if Dsec(hh_day_on,min_day_on)>=0:
       pwm1_a=min(Dsec(hh_day_on,min_day_on)/7,255)
   if Dsec(hh_day_off,min_day_off)>=0:
       pwm1_a=max(255-Dsec(hh_day_off,min_day_off)/7,0)

   if Dsec(hh_moon_on,min_moon_on)<0:
       pwm1_a=0
   if Dsec(hh_moon_on,min_moon_on)>=0:
       pwm2_a=min(Dsec(hh_moon_on,min_moon_on)/7,255)
   if Dsec(hh_moon_off,min_moon_off)>=0:
       pwm2_a=max(255-Dsec(hh_moon_off,min_moon_off)/7,0)
       
#****************** ON_OFF CO2-O2-FOOD ************************
   if auto3==True:
       if (((ora*60+minuti)>= (hh_co2_on*60+ min_co2_on)) and ((ora*60+minuti)< (hh_co2_off*60+ min_co2_off))):
           on3=True
       else:
          on3=False

   if  auto4==True:
       if (((ora*60+minuti)>= (hh_o2_on*60+ min_o2_on)) and ((ora*60+minuti)< (hh_o2_off*60+ min_o2_off))):
           on4=True
       else:
           on4=False
           
   if auto5==True:
       if (((ora*60+minuti)== (hh_food1_on*60+ min_food1_on))):
           on5=True
       else:
           on5=False

 

 
 

   
   pygame.display.update()

   time.sleep(0.1)

#**************************************** Scan touchscreen events ***********
   for event in pygame.event.get():
       if(event.type is MOUSEBUTTONDOWN):
           pos = pygame.mouse.get_pos()
           print (pos)
          # elif(event.type is MOUSEBUTTONUP):
          #     pos = pygame.mouse.get_pos()
          #     print (pos)    
           x,y = pos
           pos=y,x
           
       
           print (pos)

#****************************************** setup **********************
           if y>y_time and y<y_time+50:
               setup_1(1)
           if y>y_temp and y<y_temp+50:
               setup_1(2)

           if x>0 and x<68:
               if y>y_day and y<y_day+80:
                   print("SETUP1")
                   hh_on=hh_day_on
                   hh_off=hh_day_off
                   min_on=min_day_on
                   min_off=min_day_off
                   setup_2(1)
                   hh_day_on=hh_on
                   hh_day_off=hh_off
                   min_day_on=min_on
                   min_day_off=min_off

               if y>y_moon and y<y_moon+80:
                   hh_on=hh_moon_on
                   hh_off=hh_moon_off
                   min_on=min_moon_on
                   min_off=min_moon_off
                   setup_2(2)
                   hh_moon_on=hh_on
                   hh_moon_off=hh_off
                   min_moon_on=min_on
                   min_moon_off=min_off

               if y>y_co2 and y<y_co2+65:
                   hh_on=hh_co2_on
                   hh_off=hh_co2_off
                   min_on=min_co2_on
                   min_off=min_co2_off
                   setup_2(3)
                   hh_co2_on=hh_on
                   hh_co2_off=hh_off
                   min_co2_on=min_on
                   min_co2_off=min_off

               if y>y_o2 and y<y_o2+65:
                   hh_on=hh_o2_on
                   hh_off=hh_o2_off
                   min_on=min_o2_on
                   min_off=min_o2_off
                   setup_2(4)
                   hh_o2_on=hh_on
                   hh_o2_off=hh_off
                   min_o2_on=min_on
                   min_o2_off=min_off

               if y>y_food and y<y_food+70:
                   setup_3()
           fix()
           #header(0)
           print("sono tornato in loop")
               
               
                   
#****************************************** switch A/M **********************
           if x>70 and x<150:  
               if y>y_day+10 and y<y_day+50:
                   auto1=not(auto1)
               if y>y_moon+10 and y<y_moon+50:
                   auto2=not(auto2)
               if y>y_co2+15 and y<y_co2+50:
                   auto3=not(auto3)
               if y>y_o2+15 and y<y_o2+50:
                   auto4=not(auto4)
               if y>y_food+20 and y<y_food+55:
                   auto5=not(auto5)
#****************************************** switch on/off **********************
           if x>200 and x<235:
               if y>y_day+15 and y<y_day+50 and auto1==False:
                   on1=not(on1)
               if y>y_moon+15 and y<y_moon+50 and auto2==False:
                   on2=not(on2)
               if y>y_co2+15 and y<y_co2+50 and auto3==False:
                   on3=not(on3)
               if y>y_o2+15 and y<y_o2+50 and auto4==False:
                   on4=not(on4)
               if y>y_food+20 and y<y_food+55 and auto5==False:
                   on5=not(on5)
#************************************************ pwm **************************
           if y>y_day+55  and y<y_day+75  and auto1==False:
               pwm1_m=min(int((x-70)/250*257),255)
               print(pwm1_m)
           if y>y_moon+55  and y<y_moon+75 and auto2==False:
               pwm2_m=min(int((x-70)/250*257),255)
               print(pwm2_m)




#************************************************ eeprom *************************
   def hm2(hm):
       hm2=""
       if hm<10:
           hm2=hm2+"0"
       hm2=hm2+str(hm)
       return(hm2)
       
   if cont>100:        
       eeprom="ORA:"+hm2(ora)+hm2(minuti)+'\n'
       eeprom=eeprom+"TEMP:"+str(T)+"-EC:"+str(ec)+"-PH:"+str(ph)+'\n'
       eeprom=eeprom+"AUTO:"+str(int(auto1))+str(int(auto2))+str(int(auto3))+str(int(auto4))+str(int(auto5))+'\n'
       eeprom=eeprom+"ON:"+str(int(on1))+str(int(on2))+str(int(on3))+str(int(on4))+str(int(on5))+'\n'
       eeprom=eeprom+"DAYON:"+hm2(hh_day_on)+hm2(min_day_on)+"-DAYOFF:"+hm2(hh_day_off)+hm2(min_day_off)+'\n'
       eeprom=eeprom+"PWM1_M:"
       if pwm1_m<100:
           eeprom=eeprom+"0"
       if pwm1_m<10:
           eeprom=eeprom+"0"
       eeprom=eeprom+str(pwm1_m)+'\n'
       eeprom=eeprom+"MOONON:"+hm2(hh_moon_on)+hm2(min_moon_on)+"-MOONOFF:"+hm2(hh_moon_off)+hm2(min_moon_off)+'\n'
       eeprom=eeprom+"PWM2_M:"
       if pwm2_m<100:
           eeprom=eeprom+"0"
       if pwm2_m<10:
           eeprom=eeprom+"0"
       eeprom=eeprom+str(pwm2_m)+'\n'
       eeprom=eeprom+"CO2ON:"+hm2(hh_co2_on)+hm2(min_co2_on)+"-CO2OFF:"+hm2(hh_co2_off)+hm2(min_co2_off)+'\n'
       eeprom=eeprom+"O2ON:"+hm2(hh_o2_on)+hm2(min_o2_on)+"-O2OFF:"+hm2(hh_o2_off)+hm2(min_o2_off)+'\n'
       eeprom=eeprom+"FOOD1:"+hm2(hh_food1_on)+hm2(min_food1_on)+'\n'
       eeprom=eeprom+"FOOD2:"+hm2(hh_food2_on)+hm2(min_food2_on)+'\n'
       eeprom=eeprom+"FOOD3:"+hm2(hh_food3_on)+hm2(min_food3_on)+'\n'
       eeprom=eeprom+"FOOD4:"+hm2(hh_food4_on)+hm2(min_food4_on)+'\n'
       eeprom=eeprom+"FOOD_pass:"+str(food1_pass)+str(food2_pass)+str(food3_pass)+str(food4_pass)+'\n'
       
       eeprom=eeprom+str(Tx)+'\n'
       
       out_file = open("data.txt","w")
       out_file.write(eeprom)
       out_file.close()
       print("scritto file ",ora,minuti,secondi)
       cont=0
           
 
Risposta
#13
(28/08/2018, 20:56)Zzed Ha scritto: Per capire dove sbagli, mettici gli script.. (a proposito, sono nella stessa directory?)
Per quanto riguarda lo scrivere in Python in modo che abbia un funzionamento asincrono, significa imparare a programmare a livello avanzato, non intendere lo script solo come "una lista di cose da fare in sequenza", ma scoprire come far fare più cose contemporaneamente all'interno del programma.. qui ad esempio viene introdotto l'argomento.
Chiaramente, in questo caso specifico, fai prima con uno degli altri metodi proposti, ma puoi usarlo come scusa per approfondire le tue conoscenze, se ne hai voglia. Credo RaspberryPi serva anche a questo.

scusatemi, mi spiego meglio, il problema che ho descritto (non trova w1thermsensor) si verifica solo se lancio lo script da terminale con python "nomefile".
Se lo eseguo da Thonny funziona tutto (ma nn mi pare sia possibile eseguirlo da Thonny in background)
grazie ancora
Risposta
#14
Ragazzi, ho risolto copiando la libreria w1thermsensor anche nella cartella dove risiedono gli script.
Resta per me un mistero perchè la libreria venga trovata lanciando lo script da idle mentre non venga trovata lanciandolo da terminale.
Se qualcuno conoscesse il motivo, ve ne sarei grato.
Grazie a tutti per l'aiuto
paolo
Risposta
#15
(31/08/2018, 01:43)paolo attanasio Ha scritto: Ragazzi, ho risolto copiando la libreria w1thermsensor anche nella cartella dove risiedono gli script.
Resta per me un mistero perchè la libreria venga trovata lanciando lo script da idle mentre non venga trovata lanciandolo da terminale.
Se qualcuno conoscesse il motivo, ve ne sarei grato.
Grazie a tutti per l'aiuto
paolo
Le librerie devono essere in una cartella compresa nella variabile d'ambiente PYTHONPATH, con la stessa sintassi della variabile di shell PATH, cioè una lista di nomi di directory. Quando PYTHONPATH non è configurata, o quando il file non si trova nelle directory lì elencate, la ricerca continua su un percorso predefinito :/usr/local/lib/python.
L'idle comunque si vede che ti dà una mano, ma pare che la tua libreria sia nel posto sbagliato
Risposta
#16
grazie Zzed
Risposta
  


Vai al forum:


Navigazione: 1 Ospite(i)
Forum con nuovi Post
Forum senza nuovi post
Forum bloccato
Forum Redirect