Ds3231 collegato in cascata con sht21 dopo qualche ora va in blocco

Salve a tutti, ho un problema che non riesco a risolvere da giorni, ho collegato in cascata al DS3231 con ponticello su A1 un SHT21 collegato ad un cavo lungo 4 metri (CAVO LAN CAT6), il tutto gira bene all'accensione ma dopo qualche ora si blocca, prima che si blocca completamente sul display dove c'è il label della data visualizzo una scritta "fail"per qualche secondo dopo torna di nuovo la data e successivamente dopo qualche minuto arduino non risponde più.

Come potrei risolvere questo problema che mi sta facendo andare di matto?

Grazie in anticipo per le risposte.

Suppongo che il problema sia nell'esaurimento della memoria e come ulteriore supposizione posso ipotizzare che tu abbia usato l'oggetto String per trattare le stringhe invece delle stringhe classiche del C. Se cerchi sul forum ci sono moltissimi topic al riguardo.
Ma finché non darai più indicazioni o, ancora meglio, posterai il codice completo che ha il problema non è possibile dare ulteriori indicazioni.

... inoltre che significa "in cascata" ? ? ?

Sono moduli I2C e quindi sono connessi in parallelo sul bus I2C non in "cascata" uno dopo l'altro.

Guglielmo

Senza vedere il codice è difficile dare consigli azzeccati...

Comunque 4 metri con un dispositivo I2C è proprio andarseli a cercare i malfunzionamenti.

Concordo al 100% ... anche se lui dice di usare un cavo CAT6 e dice che per un certo tempo il tutto funziona.

Il problema, nel I2C ed ancor di più nel SPI, NON sono tanto i disturbi e l'utilizzo di cavi schermati, quanto la "capacità" in pF del cavo che va a creare una rete che rovina i fronti di salita/discesa del segnale, quindi creando problemi di collegamento. Del resto I2C è l'acronimo di Inter Integrated Circuit ovvero di un bus per il collegamento di IC sulla stessa scheda e NON è certo stato pensato per essere portato in giro.

Non per nulla, per portarlo in giro, esistono appositi IC che sono degli externder del bus I2C come il P82B715.

Guglielmo

In cascata intendo in questo modo poiché il datasheet dichiara che è possibile collegare altre periferiche in questo modo

Comunque attualmente sono fuori casa appena rientro posterò il codice

Ok, quello è solo un disegno ed un fatto di praticità per collegare tra loro varie schedine, nella realtà TUTTI i devices I2C sono collegati in parallelo, così come quei pin sui due lati ...

image

Guglielmo

Ok, grazie, appena rientro a casa farò questa modifica a livello hardware, ci sono quelle 2 resistenze di pullup nella foto che hai allegato, di che valore dovrei metterle? Oppure come si calcolano?

Non ci siamo capiti, quel modulino è già fatto in quel modo, i pin sono in paralello e non in serie come potrebbe sembrare dalla serigrafia, quindi ... NON devi modificare nulla :slight_smile:

... che sicuramente sono già presenti sui modulini che stai usanso o il bus I2C non funzionerebbe, dato che sono obbligatorie. Cerca lo schema delle schedine che stai usando e verifica la loro presenza.

Guglielmo

Allora come schema ho trovato il seguente gogoolando, le uniche resistenze che vedo tra vcc sda e scl, non credo che il modulo ne porti qualcuna già installata sopra.
Per l'SHT21 non riesco a trovare lo schema dei collegamenti dello schedino

DS3231-RTC-Module-internal-circuit-diagram

Quelle cerchiate in blu sono esattamente le resistenze di pull-up di cui si parlava, quindi ... sono già integrate nel modulo.

Hai un link al modulo che stai usando ?

Guglielmo

ti posso mostrare la foto il modulo sht21 l'ho comprato su ebay invece il ds3231 su amazon

3788-SHT21-Umidita-and-Temp-Sensore

Allego il codice è un po disordinato ma è ancora in fase di sviluppo

#include "EasyNextionLibrary.h" 
#include "NexButton.h"
#include "NexText.h"
#include <string.h>
#include "Arduino.h"
#include "Wire.h"
#include "RTCLib.h"
#include "NexPage.h"
#include "NexPicture.h"
#include "NexGauge.h"
#include <OneWire.h>
//#include "DHT.h"
#include <Nextion.h>
#include "SHT21.h"

/*#define DHTPIN 13 // sensore collegato al pin 5
#define DHTTYPE DHT22 // tipo sensore
DHT dht(DHTPIN, DHTTYPE); // creo oggetto dht*/
SHT21 sht;


float TTTT;
float HHHH;

#define PIN_SERVIZIO 6 //LED SISTEMA IN ESECUZIONE 
#define PIN_PUMP  3
#define PIN_FEED  8
#define PIN_LIGHT 4
#define PIN_UV    2 //ventilatore


unsigned long t1, t2, t3, t4;

//OneWire  ds(5); 

#define ACCESO LOW
#define SPENTO HIGH

#define at_serial Serial

/*
 *******************************************************************
 * AT Instruction Set
 * 
 *******************************************************************
 */

#define AT_AT           "AT"
#define AT_TIME         "AT+TIME"
#define AT_PUMP         "AT+PUMP"
#define AT_FEED         "AT+FEED"
#define AT_LIGHT        "AT+LIGHT"
#define AT_UV           "AT+UV"
#define AT_UMIDO         "AT+UMIDO"
#define AT_OK           "OK"
#define AT_FAILED       "FAILED"
#define AT_UNKNOWN_CMD  "Unknown command"
#define AT_UNSUPPORTED  "Not supported"
#define AT_ERR_FORMAT   "Bad format"
#define AT_ERR_PARAM   "Bad param"

/*
 *******************************************************************
 * Nextion component for page:home 
 * 
 *******************************************************************
 */

 
NexText hour = NexText(0, 6, "hour");
NexText minute = NexText(0, 8, "min");
NexText second = NexText(0, 7, "sec");
NexText month = NexText(0, 10, "mon");
NexText year = NexText(0, 12, "year2");
NexText week = NexText(0, 13, "week");
NexText day = NexText(0, 9, "day");
NexText temperature = NexText(0, 3, "tem");
NexText umidita = NexText(0, 5, "umid");
NexPicture home_pumppic = NexPicture(0, 23, "aspiratore");
NexPicture home_feedpic = NexPicture(0, 24, "irrigazione");
NexPicture home_lightpic = NexPicture(0, 21, "luce_off");
NexPicture home_uvpic = NexPicture(0, 22, "ventilatore");
NexPicture home_umidpic = NexPicture(0, 25, "ico_umid");
NexButton goset = NexButton(0, 15, "goset");
/*
 *******************************************************************
 * Nextion component for page:set
 * 
 *******************************************************************
 */
NexButton backhome = NexButton(1, 4, "backhome");
NexButton settime = NexButton(1, 2, "settime");
NexButton setpump = NexButton(1, 8, "setpump");
NexButton setfeed = NexButton(1, 9, "setfeed");
NexButton setlight = NexButton(1, 3, "setlight");
NexButton setuv = NexButton(1, 5, "setvent");
NexButton setumid = NexButton(1, 10, "setumid");
/*
 *******************************************************************
 * Nextion component for page:time
 * 
 *******************************************************************
 */
NexText sethour = NexText(2, 9, "sethour1");
NexText setmin = NexText(2, 12, "setmin1");
NexText setweek = NexText(2, 14, "setweek1");
NexText setday = NexText(2, 2, "setday1");
NexText setmon = NexText(2, 4, "setmon1");
NexText setyear = NexText(2, 7, "setyear2");
NexButton settime_add = NexButton(2, 17, "up0");
NexButton settime_dec = NexButton(2, 18, "down0");
NexButton settime_ok = NexButton(2, 16, "settimeok");
NexButton time_back_to_set = NexButton(2, 19, "backset1");
/*
 *******************************************************************
 * Nextion component for page:pump
 * 
 *******************************************************************
 */
NexPicture setpump_manual = NexPicture(6, 3, "on1");
NexPicture setpump_timer = NexPicture(6, 5, "timer1");
NexButton setpump_add = NexButton(6, 10, "up1");
NexButton setpump_dec = NexButton(6, 11, "down1");
NexText setpump_hour = NexText(6, 7, "hour1");
NexText setpump_min = NexText(6, 9, "min1");
NexButton pump_back_to_set = NexButton(6, 12, "backset2");
/*
 *******************************************************************
 * Nextion component for page:feed
 * 
 *******************************************************************
 */
NexPicture setfeed_manual = NexPicture(7, 3, "on2");
NexPicture setfeed_timer = NexPicture(7, 5, "timer2");
NexButton setfeed_add = NexButton(7, 10, "up2");
NexButton setfeed_dec = NexButton(7, 11, "down2");
NexText setfeed_hour = NexText(7, 7, "hour2");
NexText setfeed_min = NexText(7, 9, "min2");
NexButton feed_back_to_set = NexButton(7, 12, "backset3");
/*
 *******************************************************************
 * Nextion component for page:light
 * 
 *******************************************************************
 */
NexPicture setlight_manual = NexPicture(3, 16, "on3");
NexPicture setlight_timer = NexPicture(3, 15, "timer3");
NexButton setlight_add = NexButton(3, 6, "up3");
NexButton setlight_dec = NexButton(3, 7, "down3");
NexText setlight_hour_on = NexText(3, 11, "hour31");
NexText setlight_hour_off = NexText(3, 9, "hour32");
NexText setlight_min_on = NexText(3, 12, "min31");
NexText setlight_min_off = NexText(3, 10, "min32");
NexButton light_back_to_set = NexButton(3, 14, "backset4");
/*
 *******************************************************************
 * Nextion component for page:uv
 * 
 *******************************************************************
 */
NexPicture setuv_manual = NexPicture(4, 3, "on4");
NexPicture setuv_timer = NexPicture(4, 5, "timer4");
NexButton setuv_add = NexButton(4, 10, "up4");
NexButton setuv_dec = NexButton(4, 11, "down4");
NexText setuv_hour = NexText(4, 7, "hour4");
NexText setuv_min = NexText(4, 9, "min4");
NexButton uv_back_to_set = NexButton(4, 12, "backset5");
/*
 *******************************************************************
 * Nextion component for page: umidificatore
 * 
 *******************************************************************
 */
NexPicture setum_manual = NexPicture(8, 3, "on8");
NexPicture setum_timer = NexPicture(8, 5, "prog");
NexButton setum_add = NexButton(8, 8, "piu");
NexButton setum_dec = NexButton(8, 9, "meno");
NexText setum_hour = NexText(8, 7, "r_umido");
NexButton um_back_to_set = NexButton(8, 10, "backset11");
/*
 *******************************************************************
 * Nextion component all page
 * 
 *******************************************************************
 */
NexPage home = NexPage(0, 0, "home");
NexPage set = NexPage(1, 0, "set");
NexPage time = NexPage(2, 0, "time");
NexPage pump = NexPage(6, 0, "aspiratore");
NexPage feed = NexPage(7, 0, "irrigazione");
NexPage light = NexPage(3, 0, "light");
NexPage uv = NexPage(4, 0, "ventilatore");
NexPage umidificatore = NexPage(8, 0, "umidificatore");

NexTouch *nex_Listen_List[] = 
{
    &settime, &setpump, &setfeed, &setlight, &setuv, &backhome, 
    &goset, &setumid,

    &setum_manual, &setum_timer, &setum_add, &setum_dec,
    &setum_hour, &um_back_to_set,

    &sethour, &setmin, &setweek, &setday, &setmon, &setyear, 
    &settime_add, &settime_dec, &settime_ok, &time_back_to_set,
    
    &setpump_manual, &setpump_timer, &setpump_add, &setpump_dec,
    &setpump_hour, &setpump_min, &pump_back_to_set,

    &setfeed_manual, &setfeed_timer, &setfeed_add, &setfeed_dec,
    &setfeed_hour, &setfeed_min, &feed_back_to_set,

    &setlight_manual, &setlight_timer, &setlight_add, &setlight_dec,
    &setlight_hour_on, &setlight_min_off, &setlight_hour_off, &setlight_min_on,
    &light_back_to_set,
    
    &setuv_manual, &setuv_timer, &setuv_add, &setuv_dec,
    &setuv_hour, &setuv_min, &uv_back_to_set,

    NULL
};

RTCLib rtc;

/*
 *******************************************************************
 * Temperature partial variable
 * 
 *******************************************************************
*/
uint32_t number = 0;
//uint16_t sensor_pin = 5;    /* select the input pin for the Temperature */
//uint16_t sensor_value = 0;   /* variable to store the value coming from the Temperature*/
unsigned long update = 1;
int32_t timer = -1; /* timer for second */
uint16_t umidita_grow = 0;
/*
 *******************************************************************
 * Buffer zone of various variables
 * 
 *******************************************************************
 */
char buffer[10] = {0};
char buffer_temp[10] = {0};
char buffer_year[10] = {0};
char buffer_month[10] = {0};
char buffer_day[10] = {0};
char buffer_hour[10] = {0};
char buffer_minute[10] = {0};
char buffer_week[10] = {0};
uint8_t buffer_second =0;
char buffer_temperature[10] = {0};
char buffer_umidita[10] = {0};

/*
 *******************************************************************
 * TIME partial variable
 * 
 *******************************************************************
 */
int8_t settime_type = -1;
int8_t settime_up = -1;
int8_t number_settime_year = -1;
int8_t number_settime_month = -1;
int8_t number_settime_day = -1;
int8_t number_settime_hour = -1;
int8_t number_settime_minute = -1;
int32_t number_settime_week = -1;

/*
 *******************************************************************
 * Umidità partial variable
 * 
 *******************************************************************
*/ 
int8_t setumido_type = -1;
int8_t setumid_add = -1;
uint8_t number_setumido_hour = 50;
uint8_t number_setum_min = 0;
uint32_t umido_manual = 3;
uint32_t umido_timer = 3;
uint32_t umido_respond = 0;
uint32_t umido_stop = 0;
/*
 *******************************************************************
 * PUMP partial variable
 * 
 *******************************************************************
 */
int8_t setpump_type = -1;
int8_t setpump_up = -1;
uint8_t number_setpump_hour = 1;
uint8_t number_setpump_min = 30;
uint32_t pump_timer = 3;//15
uint32_t pump_manual = 3;//16
uint32_t pump_respond = 0;
uint32_t pump_stop = 0;

/*
 *******************************************************************
 * FEED partial variable
 * 
 *******************************************************************
 */
int8_t setfeed_type = -1;
int8_t setfeed_up = -1;
uint8_t number_setfeed_hour = 4;
uint8_t number_setfeed_sec = 30;
uint32_t feed_timer = 3;//15
uint32_t feed_manual = 3;//16
uint32_t feed_respond = 0;
uint32_t feed_stop = 0;

/*
 *******************************************************************
 * UV partial variable
 * 
 *******************************************************************
*/ 
int8_t setuv_type = -1;
int8_t setuv_up = -1;
uint8_t number_setuv_hour = 1;
uint8_t number_setuv_min = 30;
uint32_t uv_timer = 3;//15
uint32_t uv_manual = 3;//16
uint32_t uv_respond = 0;
uint32_t uv_stop = 0;

/*
 *******************************************************************
 * LIGHT partial variable
 * 
 *******************************************************************
 */
int8_t setlight_type = -1;
int8_t setlight_up = -1;
uint8_t number_setlight_hour_on = 00;
uint8_t number_setlight_min_on = 01;
uint8_t number_setlight_hour_off = 20;
uint8_t number_setlight_min_off = 00;
uint32_t light_timer = 1;//michele 40
uint32_t light_manual = 1;//michele 38

/*
 *******************************************************************
 * AT partial start
 * 
 *******************************************************************
 */
void atInit(void)
{
    at_serial.begin(9600);
    at_serial.println("Dispositivo Pronto");
}

void atRespond(void)
{
    char temp[40];
    int16_t num;
    int16_t index_at = -1;
    int16_t index_crlf = -1;
    int16_t index_question_mark = -1;
    int16_t index_equal_mark = -1;
    String cmd;
    String param;
    while (at_serial.available() > 0)
    {
        String str = "";
        at_serial.setTimeout(500);
        num = at_serial.readBytesUntil('\n', temp, sizeof(temp) - 2);
        temp[num] = '\n';
        temp[num + 1] = '\0';
        str += temp;
        index_at = str.indexOf(AT_AT);
        index_equal_mark = str.indexOf("=");
        index_question_mark = str.indexOf("?");
        index_crlf = str.indexOf("\r\n");
        
        if (index_crlf > index_at 
            && index_at >= 0
            && index_equal_mark < 0
            && index_question_mark < 0
            )
        {
            /* Execute */
            cmd = str.substring(index_at, index_crlf);
            if (cmd == AT_AT)
            {
                at_serial.println(AT_OK);
            }
            else 
            {
                at_serial.println(AT_UNKNOWN_CMD);
            }
        }
        else if (index_crlf > index_equal_mark 
            && (index_equal_mark > index_at) 
            && index_at >= 0
            && index_question_mark < 0
            )
        {
            /* Set */
            cmd = str.substring(index_at, index_equal_mark);
            param = str.substring(index_equal_mark + 1, index_crlf);
            if (cmd == AT_TIME)
            {
                if (param.length() == 14)//14
                {
                    timeATRespond(param);
                }
                else 
                {
                    at_serial.println(AT_ERR_PARAM);
                }
            }
            else if (cmd == AT_PUMP)
            {
                if (atSet(param, &pump_manual, &pump_timer, &number_setpump_hour, &number_setpump_min))
                {
                   pumpATRespond();
                }
             
            }
            else if (cmd == AT_FEED)
            {
                if (atSet(param, &feed_manual, &feed_timer, &number_setfeed_hour, &number_setfeed_sec))
                {
                    feedATRespond();
                }
            }
            else if (cmd == AT_UV)
            {
                if(atSet(param, &uv_manual, &uv_timer, &number_setuv_hour, &number_setuv_min))
                {
                    uvATRespond();
                }
            }

            else if(cmd == AT_UMIDO)
            {
              if(atSet(param, &umido_manual, &umido_timer, &number_setumido_hour, &number_setum_min))
              {
                UMIDO_AT_Respond();
              }
              
              }
            else if (cmd == AT_LIGHT)
            {

                lightATRespond(param);
            }
           
            else
            {
                at_serial.println(AT_UNKNOWN_CMD);
            }
            
        }
        else if (index_crlf > index_question_mark
            && index_question_mark > index_at
            && index_at >= 0
            && index_equal_mark < 0
            )
        {
/* Query */
            cmd = str.substring(index_at, index_question_mark);
            if(cmd == AT_PUMP)
            {
                atQuery(pump_manual, pump_timer, number_setpump_hour, number_setpump_min, NULL, NULL);
            }
            else if (cmd == AT_FEED)
            {
                atQuery(feed_manual, feed_timer, number_setfeed_hour, number_setfeed_sec, NULL, NULL);
            }
            else if (cmd == AT_UV)
            {
                atQuery(uv_manual, uv_timer, number_setuv_hour, number_setuv_min, NULL, NULL);    
            }
            else if (cmd == AT_UMIDO)
            {
              atQuery( umido_manual, umido_timer, number_setumido_hour, number_setum_min , NULL, NULL);
            }
            else if (cmd == AT_LIGHT)
            {
                atQuery(light_manual, light_timer, number_setlight_hour_on, number_setlight_min_on, 
                        number_setlight_hour_off, number_setlight_min_off);    
            }
            else
            {
                at_serial.println(AT_UNKNOWN_CMD);
            }
        }          
        else if (index_crlf > index_question_mark
            && index_question_mark > index_equal_mark
            && index_equal_mark > index_at
            && index_at >= 0
            )
        {
            /* Test */
           at_serial.println(AT_UNSUPPORTED); 
            
        }
        else
        { 
            at_serial.println(AT_ERR_FORMAT);
        } 
        if (index_at >= 0 && index_crlf >= 0)
        {
            refreshHome();
        }
 
    }
       
}

void atQuery(uint8_t query_manaul, uint8_t query_timer, uint8_t query_hour, uint8_t query_min,
                uint8_t query_hour2, uint8_t query_min2)
{
    String query;
    if (query_manaul == 1 || query_manaul == 1)//michele timer
    {
        query += "OFF";
    }
    else if (query_manaul == 2 || query_manaul == 2)
    {
        query += "ON";
    }
    query += ",";
    if (query_timer == 1 || query_timer == 1)
    {
        query += "OFF";
    }
    else if (query_timer == 2 || query_timer == 2)
    {
        query += "ON";
    }
    query += ",";
    if (query_hour <10)
    {
        query += "0";
    }
    itoa(query_hour,buffer,10);
    query += buffer;
    query += ",";
    if (query_min <10)
    {
        query += "0";
    }
    itoa(query_min, buffer, 10);
    query += buffer;
    if (query_manaul == 1 || query_manaul == 2)
    {
        at_serial.println(query);    
    }
    else
    {
        query += ",";
        if (query_hour2 <10)
        {
            query += "0";
        }
        itoa(query_hour2, buffer, 10);
        query += buffer;
        query += ",";
        if (query_min2 <10)
        {
            query += "0";
        }
        itoa(query_min2, buffer, 10);
        query += buffer;
        at_serial.println(query);    
    }
    at_serial.println(AT_OK);    
}

bool atSet(String param, uint32_t *set_manual, uint32_t *set_timer, uint8_t *set_hour, uint8_t *set_min)
{
    String newparam;
    String manual;
    String timer;
    String next_time;
    String respond_timer;
    uint8_t comma;
    uint8_t temp_hour;
    uint8_t temp_min;
    comma = param.indexOf(",");
    manual = param.substring(0 , comma);
    param = param.substring(comma + 1);
    comma = param.indexOf(",");
    timer = param.substring(0 , comma);
    param = param.substring(comma + 1);

    comma = param.indexOf(",");
    next_time = param.substring(0, comma);
    newparam = param.substring(comma + 1);
    respond_timer = newparam.substring(0);
    if(param.length() == 5)
    {
        temp_hour = atoi(next_time.c_str());
        temp_min = atoi(respond_timer.c_str());
        if (temp_hour >= 1 && temp_hour <= 12
            && temp_min >= 1 && temp_min <= 59
            && manual.length() > 0 
            && timer.length() > 0
            )
        {
            *set_hour = temp_hour;
            *set_min = temp_min;
            if (!strcmp(manual.c_str(), "OFF"))
            {
                *set_manual = 1;//16
            }
            else if (!strcmp(manual.c_str(), "ON")) 
            {
                *set_manual = 2; //14
            }
            else
            {
                at_serial.println(AT_ERR_PARAM);
                return false;      
            }
            if (!strcmp(timer.c_str(), "OFF"))
            {
                *set_timer = 1;//15
            }
            else if (!strcmp(timer.c_str(), "ON"))
            {
                *set_timer = 2; //17
            }
            else
            {
                at_serial.println(AT_ERR_PARAM);
                return false;      
            }
            at_serial.println(AT_OK);
            return true;
        }
        else
        {
            at_serial.println(AT_ERR_PARAM);
            return false;  
        }    
    }
    else 
    {
        at_serial.println(AT_ERR_PARAM);
        return false;    
    }
}
void pumpATRespond()
{
    if (pump_manual == 4)//14
    {
        pump_timer = 3;//15
        digitalWrite(PIN_PUMP, ACCESO);
    }
    else
    {
        digitalWrite(PIN_PUMP, SPENTO);
    }
    if (pump_timer == 4)
    {
        pump_respond = number_setpump_hour*60*60 + timer;
        pump_stop = number_setpump_hour*1*60*60 + timer + number_setpump_min*1*60;
    }
    setpump_manual.setPic(pump_manual);
    setpump_timer.setPic(pump_timer);
}
void feedATRespond()
{
    if (feed_manual == 4)//14
    {
        feed_timer = 3;//15
        digitalWrite(PIN_FEED, ACCESO);
    }
    else
    {
        digitalWrite(PIN_FEED, SPENTO);
    }
    if (feed_timer == 4)//17
    {
        feed_respond = number_setfeed_hour*1*60*60 + timer;
        feed_stop = number_setfeed_hour*1*60*60 + timer + number_setfeed_sec*1;
    }
    setfeed_manual.setPic(feed_manual);
    setfeed_timer.setPic(feed_timer);
}
void uvATRespond()
{
    if (uv_manual == 4)//14
    {
        uv_timer = 3;
        digitalWrite(PIN_UV, ACCESO);
    }
    else
    {
        digitalWrite(PIN_UV, SPENTO);
    }
    if (uv_timer == 4)
    {
        uv_respond = number_setuv_hour*1*60*60 + timer;
        uv_stop = number_setuv_hour*1*60*60 + timer + number_setuv_min*1*60;
    }
    setuv_manual.setPic(uv_manual);
    setuv_timer.setPic(uv_timer);
}

void UMIDO_AT_Respond()
{
  if(umido_manual == 4)
  {
    umido_timer =3;
    digitalWrite(PIN_FEED, ACCESO);        
  }
  else
  {
    digitalWrite(PIN_FEED, SPENTO);
  }
  if(umido_timer == 4)
{
                umido_respond = number_setumido_hour<=HHHH; //number_setuv_hour*1*60*60 + timer;
                umido_stop = number_setumido_hour>=HHHH; //number_setuv_hour*1*60*60 + timer +number_setuv_min*1*60;
}
    setum_manual.setPic(umido_manual);
    setum_timer.setPic(umido_timer);
  }




  


void lightATRespond(String param)
{
    String newparam;
    String manual;
    String timer;
    uint8_t comma;
    comma = param.indexOf(",");
    manual = param.substring(0 , comma);
    param = param.substring(comma + 1);
    comma = param.indexOf(",");
    timer = param.substring(0 , comma);
    
    param = param.substring(comma + 1);
    comma = param.indexOf(",");
    String light_hour_on = param.substring(0, comma);

    newparam = param.substring(comma + 1);
    comma = newparam.indexOf(",");
    String light_min_on = newparam.substring(0, comma);

    newparam = newparam.substring(comma + 1);
    comma = newparam.indexOf(",");
    String light_hour_off = newparam.substring(0, comma);

    newparam = newparam.substring(comma + 1);
    String light_min_off = newparam.substring(0);
    if(param.length() == 11)
    {
        number_setlight_hour_on = atoi(light_hour_on.c_str());
        number_setlight_min_on = atoi(light_min_on.c_str());
        number_setlight_hour_off = atoi(light_hour_off.c_str());
        number_setlight_min_off = atoi(light_min_off.c_str());
        if(number_setlight_hour_on >= 0 && number_setlight_hour_on <= 23
            &&number_setlight_hour_off >= 0 && number_setlight_hour_off <= 23
            &&number_setlight_min_on >= 0 && number_setlight_min_on <=59
            &&number_setlight_min_off >= 0 && number_setlight_min_off <=59
         )
         {
            if (!strcmp(timer.c_str(),"OFF"))
            {
                light_timer = 1;//40
            }
            else if (!strcmp(timer.c_str(),"ON")) 
            {
                light_timer = 2;//41
            } 
            else
            {
                at_serial.println(AT_ERR_PARAM);
            }
            if (!strcmp(manual.c_str(),"OFF"))
            {
                light_manual = 1;
                digitalWrite(PIN_LIGHT, ACCESO);
                setlight_manual.setPic(light_manual);
                setlight_timer.setPic(light_timer);
                at_serial.println(AT_OK); 
            }
            else if (!strcmp(manual.c_str(),"ON"))
            {
                
                light_manual = 2;
                light_timer = 1; 
                digitalWrite(PIN_LIGHT, SPENTO);
                setlight_manual.setPic(light_manual);
                setlight_timer.setPic(light_timer);
                at_serial.println(AT_OK);
            }
            else 
            {
                at_serial.println(AT_ERR_PARAM);    
            }
         }
         else
         {
            at_serial.println(AT_ERR_PARAM);
         }
     }
     else
     {
            at_serial.println(AT_ERR_PARAM);
     }

         
     
}
void timeATRespond(String param)
{
    
    uint8_t comma;
    
    comma = param.indexOf(",");
    String year = param.substring(0 , comma);
    param = param.substring(comma+1);
    number_settime_year = atoi(year.c_str());

    comma = param.indexOf(",");
    String mon = param.substring(0 , comma);
    param = param.substring(comma+1);
    number_settime_month = atoi(mon.c_str());

    comma = param.indexOf(",");
    String day = param.substring(0 , comma);
    param = param.substring(comma+1);
    number_settime_day = atoi(day.c_str());

    comma = param.indexOf(",");
    String hour = param.substring(0 , comma);
    param = param.substring(comma+1);
    number_settime_hour = atoi(hour.c_str());

    comma = param.indexOf(",");
    String min = param.substring(0 , comma);
    param = param.substring(comma+1);
    number_settime_minute = atoi(min.c_str()); 

    setWeek();
    if (number_settime_minute >= 0 && number_settime_minute <= 59
        && number_settime_hour >= 0 && number_settime_hour <= 23
        && number_settime_month >= 1 && number_settime_month <= 12
        && number_settime_year >= 10 && number_settime_year <= 99
        )
    {
        if (number_settime_month == 1 || number_settime_month == 3
            || number_settime_month == 5 || number_settime_month == 7
            || number_settime_month == 8 || number_settime_month == 10
            || number_settime_month == 12
        )
        {
            if (number_settime_day >=  1 && number_settime_day <= 31)
            {
                rtc.set(0, number_settime_minute, number_settime_hour, 
                    number_settime_week, number_settime_day, 
                    number_settime_month, number_settime_year);
                at_serial.println(AT_OK); 
            }
            else
            {
                at_serial.println(AT_ERR_PARAM);
            }
      
        }
        else if (number_settime_month == 4 || number_settime_month == 6
            || number_settime_month == 9 || number_settime_month == 10
            || number_settime_month == 11
        )
            if (number_settime_day >=  1 && number_settime_day <= 30)
            {
                rtc.set(0, number_settime_minute, number_settime_hour, 
                    number_settime_week, number_settime_day, 
                    number_settime_month, number_settime_year);
                at_serial.println(AT_OK); 
            }
            else
            {
                at_serial.println(AT_ERR_PARAM);
            }
        else if (number_settime_month == 2)
        {
            if ((number_settime_year%4 == 0&&number_settime_year%100 != 0)||number_settime_year%400 == 0)
            {
                if (number_settime_day >= 1 && number_settime_day <=29)
                {
                    rtc.set(0, number_settime_minute, number_settime_hour, 
                        number_settime_week, number_settime_day, 
                        number_settime_month, number_settime_year);
                    at_serial.println(AT_OK); 
                }
                else
                {
                    at_serial.println(AT_ERR_PARAM);
                }
            }
            else
            {
                if (number_settime_day >= 1 && number_settime_day <=28)
                {
                    rtc.set(0, number_settime_minute, number_settime_hour, 
                        number_settime_week, number_settime_day, 
                        number_settime_month, number_settime_year);
                    at_serial.println(AT_OK); 
                }    
                else
                {
                    at_serial.println(AT_ERR_PARAM);
                }
            }
        }
          
    }
    else
    {
        at_serial.println(AT_ERR_PARAM);
    }
     
}
/*Local Synchronization Funcation */
void localSync(String cmd, uint32_t *manual, uint32_t *timer, uint8_t *time_hour, 
                      uint8_t *time_min, uint8_t *time_hour2, uint8_t *time_min2)
{
    cmd += "=";
    if (*manual == 2 || *manual == 2)//michele if (*manual == 14 || *manual == 39)
    {
        cmd += "ON";
    }
    else if (*manual == 1 || *manual == 1)//    else if (*manual == 16 || *manual == 38)
    {
        cmd += "OFF";
    }
    cmd += ",";
    if (*timer == 2 || *timer == 2)//if (*timer == 17 || *timer == 41)
    {
        cmd += "ON";
    }
    else if (*timer == 1 || *timer == 1)//else if (*timer == 15 || *timer == 40)
    {
        cmd += "OFF";
    }
    cmd += ",";
    if (*time_hour < 10)
    {
        cmd += "0";    
    }
    itoa(*time_hour, buffer, 10);
    cmd += buffer;
    cmd += ",";
    if (*time_min < 10)
    {
        cmd += "0";    
    }
    itoa(*time_min, buffer, 10);
    cmd += buffer;
    String cmd_new = "+IPD,";
    uint8_t len;
    if (*manual == 2 || *manual == 1)//if (*manual == 14 || *manual == 16)
    {
        len = cmd.length();
        itoa(len, buffer, 10);
        cmd_new += buffer;
        cmd_new += ":";
        cmd_new += cmd;
        at_serial.println(cmd_new);
    }
    else
    {
        cmd += ",";
        if (*time_hour2 < 10)
        {
            cmd += "0";    
        }
        itoa(*time_hour2, buffer, 10);
        cmd += buffer;
        cmd += ",";
        if (*time_min2 < 10)
        {
            cmd += "0";    
        }
        itoa(*time_min2, buffer, 10);
        cmd += buffer;
        len = cmd.length();
        itoa(len, buffer, 10);
        cmd_new += buffer;
        cmd_new += ":";
        cmd_new += cmd;
        at_serial.println(cmd_new);
    }
    
}
/*
 *******************************************************************
 * UMIDITA partial end
 * 
 *******************************************************************
 */
void UMIDO_PopCallback(void *ptr)
{
    umidificatore.show();
    memset(buffer, 0, sizeof(buffer));  
    itoa(number_setumido_hour, buffer, 10);
    setum_hour.setText(buffer);

/*    memset(buffer, 0, sizeof(buffer));  
    itoa(number_setuv_min, buffer, 10);
    setuv_min.setText(buffer);*/

    setum_timer.setPic(umido_timer);
    setum_manual.setPic(umido_manual);
}

void UMIDO_HourPopCallback(void *ptr)
{
    setumido_type = 1;
}

void umMinPopCallback(void *ptr)
{
    setumido_type = 2;
}

void UMIDO_AddPopCallback(void *ptr)
{
    setumid_add = 1;
    setUm();
}

void UmDECPopCallback(void *ptr)
{
    setumid_add = 0;
    setUm();
}
void setUm(void)
{
    switch (setumido_type)
    {
        case 1: 
            if (setumid_add == 1)
            {
               number_setumido_hour++;
               if (number_setumido_hour > 99)
               {
                    number_setumido_hour = 1;
               }
               memset(buffer, 0, sizeof(buffer));  
               itoa(number_setumido_hour, buffer, 10);
               setum_hour.setText(buffer);
            }   
            else if (setumid_add == 0)
            {
                number_setumido_hour--;
                if (number_setumido_hour < 1)
                {
                    number_setumido_hour = 99;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_setumido_hour, buffer, 10);
                setum_hour.setText(buffer);
            }
            break;
        case 2:
            if (setumid_add == 1)
            {
                number_setuv_min++;
                if (number_setum_min > 60)
                {
                    number_setum_min = 1;
                }
                //memset(buffer, 0, sizeof(buffer));  
                //itoa(number_setuv_min, buffer, 10);
                //setuv_min.setText(buffer);
            }
            else if (setumid_add == 0)
            {
                number_setum_min--;
                if (number_setum_min < 1)
                {
                    number_setum_min= 60;
                }
                //memset(buffer, 0, sizeof(buffer));  
                //itoa(number_setuv_min, buffer, 10);
                //setuv_min.setText(buffer);
            }
            break;
    }

}


void UmManualPopCallback(void *ptr)
{
    setum_manual.getPic(&umido_manual);
    if (umido_manual == 4)//14
    {
        umido_manual  = 3;//16
        digitalWrite(PIN_FEED, SPENTO);
        
    }
    else
    {
        umido_manual = 4;//14
        digitalWrite(PIN_FEED, ACCESO);
    }
    setum_manual.setPic(umido_manual);

    setum_timer.getPic(&umido_timer);
    if (umido_timer == 4)//17
    {
        umido_timer = 3;//15
        setum_timer.setPic(umido_timer);
    }
    localSync("UM", &umido_manual, &umido_timer, &number_setumido_hour,&number_setum_min, NULL, NULL);
}

void umTimerPopCallback(void *ptr)
{    
    setum_timer.getPic(&umido_timer);
   if (umido_manual == 3)
    {
         if (umido_timer == 3)
        {
            umido_timer = 4;
                umido_respond = number_setumido_hour<=HHHH; //number_setuv_hour*1*60*60 + timer;
                umido_stop = number_setumido_hour>=HHHH; //number_setuv_hour*1*60*60 + timer +number_setuv_min*1*60;           
            
                    }
        else
        {
            umido_timer = 3;
            digitalWrite(PIN_FEED, SPENTO);
        }           
    localSync("UM", &umido_manual, &umido_timer, &number_setumido_hour, &number_setum_min, NULL, NULL);
    }
   setum_timer.setPic(umido_timer);
}



void umTimerRespond()//DA VERIFICARE
{
  if(umido_timer == 4)
  {
    if(number_setumido_hour>=HHHH)
    {
      digitalWrite(PIN_FEED, ACCESO);
    }
    else
    {
      digitalWrite(PIN_FEED, SPENTO);
    }
  }
/*    if (umido_timer == 4)
    {
        if (umidita_grow >= umido_respond)
        {
            if (umidita_grow != umido_stop)
            {
                digitalWrite(PIN_FEED, LOW);
            }
            else
            {
                digitalWrite(PIN_FEED, HIGH);
                umido_respond = number_setumido_hour<=HHHH; //number_setuv_hour*1*60*60 + timer;
                umido_stop = number_setumido_hour>=HHHH; //number_setuv_hour*1*60*60 + timer +number_setuv_min*1*60;
            }
        }
    }*/
}

void umToSetPopCallback(void *ptr)
{
    set.show();        
}

/*
 *******************************************************************
 * UMIDITA partial end
 * 
 *******************************************************************
 */
/*
 *******************************************************************
 * UV partial start
 * 
 *******************************************************************
 */
void uvPopCallback(void *ptr)
{
    uv.show();
    memset(buffer, 0, sizeof(buffer));  
    itoa(number_setuv_hour, buffer, 10);
    setuv_hour.setText(buffer);

    memset(buffer, 0, sizeof(buffer));  
    itoa(number_setuv_min, buffer, 10);
    setuv_min.setText(buffer);

    setuv_timer.setPic(uv_timer);
    setuv_manual.setPic(uv_manual);
}

void uvHourPopCallback(void *ptr)
{
    setuv_type = 1;
}

void uvMinPopCallback(void *ptr)
{
    setuv_type = 2;
}

void uvAddPopCallback(void *ptr)
{
    setuv_up = 1;
    setUv();
}

void uvDECPopCallback(void *ptr)
{
    setuv_up = 0;
    setUv();
}
void setUv(void)
{
    switch (setuv_type)
    {
        case 1: 
            if (setuv_up == 1)
            {
               number_setuv_hour++;
               if (number_setuv_hour > 12)
               {
                    number_setuv_hour = 1;
               }
               memset(buffer, 0, sizeof(buffer));  
               itoa(number_setuv_hour, buffer, 10);
               setuv_hour.setText(buffer);
            }   
            else if (setuv_up == 0)
            {
                number_setuv_hour--;
                if (number_setuv_hour < 1)
                {
                    number_setuv_hour = 12;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_setuv_hour, buffer, 10);
                setuv_hour.setText(buffer);
            }
            break;
        case 2:
            if (setuv_up == 1)
            {
                number_setuv_min++;
                if (number_setuv_min > 60)
                {
                    number_setuv_min = 1;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_setuv_min, buffer, 10);
                setuv_min.setText(buffer);
            }
            else if (setuv_up == 0)
            {
                number_setuv_min--;
                if (number_setuv_min < 1)
                {
                    number_setuv_min= 60;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_setuv_min, buffer, 10);
                setuv_min.setText(buffer);
            }
            break;
    }

}

void uvManualPopCallback(void *ptr)
{
    setuv_manual.getPic(&uv_manual);
    if (uv_manual == 4)//14
    {
        uv_manual = 3;//16
        digitalWrite(PIN_UV, SPENTO);
        
    }
    else
    {
        uv_manual = 4;//14
        digitalWrite(PIN_UV, ACCESO);
    }
    setuv_manual.setPic(uv_manual);

    setuv_timer.getPic(&uv_timer);
    if (uv_timer == 4)//17
    {
        uv_timer = 3;//15
        setuv_timer.setPic(uv_timer);
    }
    localSync("UV", &uv_manual, &uv_timer, &number_setuv_hour, &number_setuv_min, NULL, NULL);
}

void uvTimerPopCallback(void *ptr)
{
    
    setuv_timer.getPic(&uv_timer);
   if (uv_manual == 3)
    {
         if (uv_timer == 3)
        {
            uv_timer = 4;
            uv_respond = number_setuv_hour*1*60*60 + timer;
            uv_stop = number_setuv_hour*1*60*60 + timer +number_setuv_min*1*60;
        }
        else
        {
            uv_timer = 3;
            digitalWrite(PIN_UV, SPENTO);
        }           
    localSync("UV", &uv_manual, &uv_timer, &number_setuv_hour, &number_setuv_min, NULL, NULL);
    }
    setuv_timer.setPic(uv_timer);
}


void uvTimerRespond()
{
    if (uv_timer == 4)
    {
        if (timer >= uv_respond)
        {
            if (timer != uv_stop)
            {
                digitalWrite(PIN_UV, ACCESO);
            }
            else
            {
                digitalWrite(PIN_UV, SPENTO);
                uv_respond = number_setuv_hour*1*60*60 + timer;
                uv_stop = number_setuv_hour*1*60*60 + timer +number_setuv_min*1*60;
            }
        }
    }
}

void uvToSetPopCallback(void *ptr)
{
    set.show();        
}

/*
 *******************************************************************
 * UV partial end
 * 
 *******************************************************************
 */

/*
 *******************************************************************
 * LIGHT partial start
 * 
 *******************************************************************
 */
void lightPopCallback(void *ptr)
{
    light.show();
    memset(buffer, 0, sizeof(buffer));  
    itoa(number_setlight_hour_on, buffer, 10);
    setlight_hour_on.setText(buffer);

    memset(buffer, 0, sizeof(buffer));  
    itoa(number_setlight_min_on, buffer, 10);
    setlight_min_on.setText(buffer);

    memset(buffer, 0, sizeof(buffer));  
    itoa(number_setlight_hour_off, buffer, 10);
    setlight_hour_off.setText(buffer);

    memset(buffer, 0, sizeof(buffer));  
    itoa(number_setlight_min_off, buffer, 10);
    setlight_min_off.setText(buffer);

    setlight_timer.setPic(light_timer);
    setlight_manual.setPic(light_manual);
}

void lightHourOnPopCallback(void *ptr)
{
    setlight_type = 1;
}

void lightHourOffPopCallback(void *ptr)
{
    setlight_type = 2;
}


void lightMinOnPopCallback(void *ptr)
{
    setlight_type = 3;
}

void lightMinOffPopCallback(void *ptr)
{
    setlight_type = 4;
}


void lightAddPopCallback(void *ptr)
{
    setlight_up = 1;
    setLight();
}

void lightDECPopCallback(void *ptr)
{
    setlight_up = 0;
    setLight();
}

void setLight(void)
{
    switch (setlight_type)
    {
        case 1: 
            if (setlight_up == 1)
            {
               number_setlight_hour_on++;
               if (number_setlight_hour_on > 23)//23
               {
                    number_setlight_hour_on = 0;
               }
               memset(buffer, 0, sizeof(buffer));  
               itoa(number_setlight_hour_on, buffer, 10);
               setlight_hour_on.setText(buffer);
            }   
            else if (setlight_up == 0)
            {
                number_setlight_hour_on--;
                if (number_setlight_hour_on < 0)
                {
                    number_setlight_hour_on = 23;//23
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_setlight_hour_on, buffer, 10);
                setlight_hour_on.setText(buffer);
            }
            break;
        case 2: 
            if (setlight_up == 1)
            {
               number_setlight_hour_off++;
               if (number_setlight_hour_off > 23)//23
               {
                    number_setlight_hour_off = 0;
               }
               memset(buffer, 0, sizeof(buffer));  
               itoa(number_setlight_hour_off, buffer, 10);
               setlight_hour_off.setText(buffer);
            }   
            else if (setlight_up == 0)
            {
                number_setlight_hour_off--;
                if (number_setlight_hour_off < 0)
                {
                    number_setlight_hour_off = 23;//23
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_setlight_hour_off, buffer, 10);
                setlight_hour_off.setText(buffer);
            }
            break;    
        case 3:
            if (setlight_up == 1)
            {
                number_setlight_min_on++;
                if (number_setlight_min_on > 59)//60
                {
                    number_setlight_min_on = 1;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_setlight_min_on, buffer, 10);
                setlight_min_on.setText(buffer);
            }
            else if (setlight_up == 0)
            {
                number_setlight_min_on--;
                if (number_setlight_min_on < 1)
                {
                    number_setlight_min_on = 59;//60
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_setlight_min_on, buffer, 10);
                setlight_min_on.setText(buffer);
            }
            break;
        case 4:
            if (setlight_up == 1)
            {
                number_setlight_min_off++;
                if (number_setlight_min_off > 59)//60
                {
                    number_setlight_min_off = 1;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_setlight_min_off, buffer, 10);
                setlight_min_off.setText(buffer);
            }
            else if (setlight_up == 0)
            {
                number_setlight_min_off--;
                if (number_setlight_min_off < 1)
                {
                    number_setlight_min_off = 59;//60
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_setlight_min_off, buffer, 10);
                setlight_min_off.setText(buffer);
            }
            break;            
    }

}

void lightManualPopCallback(void *ptr)
{   
    setlight_manual.getPic(&light_manual);
    if (light_manual == 2)//if (light_manual == 39)
    {
        light_manual = 1;//light_manual = 38;
        digitalWrite(PIN_LIGHT, ACCESO);
    }
    else
    {
        light_manual = 2;//light_manual = 39;
        digitalWrite(PIN_LIGHT, SPENTO);
    }
    setlight_manual.setPic(light_manual);
    
    setlight_timer.getPic(&light_timer);
    if (light_timer == 2)// if (light_timer == 41)
    {
        light_timer = 1;//light_timer = 40;
        setlight_timer.setPic(light_timer);
    }
    localSync("LIGHT", &light_manual, &light_timer, &number_setlight_hour_on, 
        &number_setlight_min_on, &number_setlight_hour_off, &number_setlight_min_off);
}

void lightTimerPopCallback(void *ptr)
{
    setlight_timer.getPic(&light_timer);
    if(light_manual == 1)//if(light_manual == 38)
    { 
        if (light_timer == 1)//if (light_timer == 40)
        {
            light_timer = 2;// light_timer = 41;
        }
        else
        {
            light_timer = 1;//light_timer = 40;
            digitalWrite(PIN_LIGHT, ACCESO);
        }
    localSync("LIGHT", &light_manual, &light_timer, &number_setlight_hour_on, 
        &number_setlight_min_on, &number_setlight_hour_off, &number_setlight_min_off);
    }
    setlight_timer.setPic(light_timer);
}


void lightTimerRespond()
{
    if (light_timer == 2)//if (light_timer == 41)
    {
        if (rtc.hour() > number_setlight_hour_on && rtc.hour() < number_setlight_hour_off)
        {
            digitalWrite(PIN_LIGHT, SPENTO);
        }
        if (rtc.hour() < number_setlight_hour_on && rtc.hour() > number_setlight_hour_off)
        {
            digitalWrite(PIN_LIGHT, ACCESO);
            
        }
        if (number_setlight_hour_on != number_setlight_hour_off)
        {
            if (rtc.hour() == number_setlight_hour_on)
            {
                if (rtc.minute() >= number_setlight_min_on)
                {
                    digitalWrite(PIN_LIGHT, SPENTO);
                }
                else
                {
                    digitalWrite(PIN_LIGHT, ACCESO);
                }    
            }
            if (rtc.hour() == number_setlight_hour_off)
            {
                if (rtc.minute() < number_setlight_min_off)
                {
                    digitalWrite(PIN_LIGHT, SPENTO);
                }
                else
                {
                    digitalWrite(PIN_LIGHT, ACCESO);
                }
            }
        }
        else
        {
            if (rtc.minute() >= number_setlight_min_on && rtc.minute() < number_setlight_min_off)
            {
                digitalWrite(PIN_LIGHT, SPENTO);
            }
            else
            {
                digitalWrite(PIN_LIGHT, ACCESO);
            }
        }
    }
    
}

void lightToSetPopCallback(void *ptr)
{
    set.show();        
}
/*
 *******************************************************************
 * LIGHT partial end
 * 
 *******************************************************************
 */

/*
 *******************************************************************
 * FEED partial start
 * 
 *******************************************************************
 */
void feedPopCallback(void *ptr)
{
    feed.show();
    memset(buffer, 0, sizeof(buffer));  
    itoa(number_setfeed_hour, buffer, 10);
    setfeed_hour.setText(buffer);

    memset(buffer, 0, sizeof(buffer));  
    itoa(number_setfeed_sec, buffer, 10);
    setfeed_min.setText(buffer);

    setfeed_timer.setPic(feed_timer);
    setfeed_manual.setPic(feed_manual);
}

void feedHourPopCallback(void *ptr)
{
    setfeed_type = 1;
}

void feedMinPopCallback(void *ptr)
{
    setfeed_type = 2;
}

void feedAddPopCallback(void *ptr)
{
    setfeed_up = 1;
    setFeed();
}

void feedDECPopCallback(void *ptr)
{
    setfeed_up = 0;
    setFeed();
}

void setFeed(void)
{
    switch (setfeed_type)
    {
        case 1: 
            if (setfeed_up == 1)
            {
               number_setfeed_hour++;
               if (number_setfeed_hour > 12)
               {
                    number_setfeed_hour = 1;
               }
               memset(buffer, 0, sizeof(buffer));  
               itoa(number_setfeed_hour, buffer, 10);
               setfeed_hour.setText(buffer);
            }   
            else if (setfeed_up == 0)
            {
                number_setfeed_hour--;
                if (number_setfeed_hour < 1)
                {
                    number_setfeed_hour = 12;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_setfeed_hour, buffer, 10);
                setfeed_hour.setText(buffer);
            }
            break;
        case 2:
            if (setfeed_up == 1)
            {
                number_setfeed_sec++;
                if (number_setfeed_sec > 60)
                {
                    number_setfeed_sec = 1;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_setfeed_sec, buffer, 10);
                setfeed_min.setText(buffer);
            }
            else if (setfeed_up == 0)
            {
                number_setfeed_sec--;
                if (number_setfeed_sec < 1)
                {
                    number_setfeed_sec= 60;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_setfeed_sec, buffer, 10);
                setfeed_min.setText(buffer);
            }
            break;
    }

}

void feedManualPopCallback(void *ptr)
{
    setfeed_manual.getPic(&feed_manual);
    if (feed_manual == 4)//14
    {
        feed_manual = 3;//16
        digitalWrite(PIN_FEED, SPENTO);
        delay(10);
    }
    else
    {
        feed_manual = 4;//14
        digitalWrite(PIN_FEED, ACCESO);
        delay(10);
    }
    setfeed_manual.setPic(feed_manual);

    setfeed_timer.getPic(&feed_timer);
    if (feed_timer == 4)//17
    {
        feed_timer = 3;//15
        setfeed_timer.setPic(feed_timer);
    }
    localSync("FEED", &feed_manual, &feed_timer, &number_setfeed_hour, &number_setfeed_sec, NULL, NULL);
}

void feedTimerPopCallback(void *ptr)
{
    setfeed_timer.getPic(&feed_timer);
    if (feed_manual == 3)//16
    {
        if (feed_timer == 3)//15
        {
            feed_timer = 4;//17
            feed_respond = number_setfeed_hour*1*60*60 + timer;
            feed_stop = number_setfeed_hour*1*60*60 + timer +number_setfeed_sec*1;
        }
        else
        {
            feed_timer = 3;//15
            digitalWrite(PIN_FEED, SPENTO);
        }
    localSync("FEED", &feed_manual, &feed_timer, &number_setfeed_hour, &number_setfeed_sec, NULL, NULL);
    }
    setfeed_timer.setPic(feed_timer);
}


void feedTimerRespond()
{
    if (feed_timer == 4)//17
    {
        if (timer >= feed_respond)
        {
            if (timer != feed_stop)
            {
                digitalWrite(PIN_FEED, ACCESO);
            }
            else
            {
                digitalWrite(PIN_FEED, SPENTO);
                feed_respond = number_setfeed_hour*1*60*60 + timer;
                feed_stop = number_setfeed_hour*1*60*60 + timer +number_setfeed_sec*1;
            }
        }
    }
}

void feedToSetPopCallback(void *ptr)
{
    set.show();        
}
/*
 *******************************************************************
 * FEED partial end
 * 
 *******************************************************************
 */

 /*
 *******************************************************************
 * PUMP partial start
 * 
 *******************************************************************
 */
void pumpPopCallback(void *ptr)
{
    pump.show();
    memset(buffer, 0, sizeof(buffer));  
    itoa(number_setpump_hour, buffer, 10);
    setpump_hour.setText(buffer);

    memset(buffer, 0, sizeof(buffer));  
    itoa(number_setpump_min, buffer, 10);
    setpump_min.setText(buffer);

    setpump_timer.setPic(pump_timer);
    setpump_manual.setPic(pump_manual);
}

void pumpHourPopCallback(void *ptr)
{
    setpump_type = 1;
}

void pumpMinPopCallback(void *ptr)
{
    setpump_type = 2;
}

void pumpAddPopCallback(void *ptr)
{
    setpump_up = 1;
    setPump();
}

void pumpDECPopCallback(void *ptr)
{
    setpump_up = 0;
    setPump();
}

void setPump(void)
{
    switch (setpump_type)
    {
        case 1: 
            if (setpump_up == 1)
            {
               number_setpump_hour++;
               if (number_setpump_hour > 12)
               {
                    number_setpump_hour = 1;
               }
               memset(buffer, 0, sizeof(buffer));  
               itoa(number_setpump_hour, buffer, 10);
               setpump_hour.setText(buffer);
            }   
            else if (setpump_up == 0)
            {
                number_setpump_hour--;
                if (number_setpump_hour < 1)
                {
                    number_setpump_hour = 12;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_setpump_hour, buffer, 10);
                setpump_hour.setText(buffer);
            }
            break;
        case 2:
            if (setpump_up == 1)
            {
                number_setpump_min++;
                if (number_setpump_min > 60)
                {
                    number_setpump_min = 1;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_setpump_min, buffer, 10);
                setpump_min.setText(buffer);
            }
            else if (setpump_up == 0)
            {
                number_setpump_min--;
                if (number_setpump_min < 1)
                {
                    number_setpump_min= 60;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_setpump_min, buffer, 10);
                setpump_min.setText(buffer);
            }
            break;
    }

}
void pumpManualPopCallback(void *ptr)
{
    setpump_manual.getPic(&pump_manual);
    if (pump_manual == 4)//14
    {
        pump_manual = 3;//16
        digitalWrite(PIN_PUMP, SPENTO);
        delay(10);
    }
    else
    {
        pump_manual = 4;//14
        digitalWrite(PIN_PUMP, ACCESO);
        delay(10);
    }
    setpump_manual.setPic(pump_manual);

    if (pump_timer == 4)//17
    {
        pump_timer = 3;//15
        setpump_timer.setPic(pump_timer);
    }
    localSync("PUMP", &pump_manual, &pump_timer, &number_setpump_hour, &number_setpump_min, NULL, NULL);
}

void pumpTimerPopCallback(void *ptr)
{
    setpump_timer.getPic(&pump_timer);
    if(pump_manual == 3)//16
    {
        if (pump_timer == 3)//15
        {
            pump_timer = 4;//17
            pump_respond = number_setpump_hour*1*60*60 + timer;
            pump_stop = number_setpump_hour*1*60*60 + timer + number_setpump_min*1*60;
        }
        else if (pump_timer == 4)//17
        {
            pump_timer = 3;//15
            digitalWrite(PIN_PUMP, SPENTO);//
        }
    localSync("PUMP", &pump_manual, &pump_timer, &number_setpump_hour, &number_setpump_min, NULL, NULL);
    }
    setpump_timer.setPic(pump_timer);
    
}



void pumpTimerRespond()
{
    if (pump_timer == 4)//17
    {
        if (timer >= pump_respond)
        {
            if (timer != pump_stop)
            {
                digitalWrite(PIN_PUMP, ACCESO);
            }
            else
            {
                digitalWrite(PIN_PUMP, SPENTO);
                pump_respond = number_setpump_hour*1*60*60 + timer;
                pump_stop = number_setpump_hour*1*60*60 + timer + number_setpump_min*1*60;
            }
        }
    }
}
void pumpToSetPopCallback(void *ptr)
{
    set.show();        
}
/*
*******************************************************************
* PUMP partial end
* 
*******************************************************************
*/

/*
*******************************************************************
* TIME partial start
* 
*******************************************************************
*/
void timePopCallback(void *ptr)
{
    time.show();
    setyear.setText(buffer_year);

    memset(buffer, 0, sizeof(buffer)); 
    number_settime_month = rtc.month();
    itoa(number_settime_month, buffer, 10);
    setmon.setText(buffer);
    
    setday.setText(buffer_day);
    sethour.setText(buffer_hour);
    setmin.setText(buffer_minute);
    setweek.setText(buffer_week);
    number_settime_year = atoi(buffer_year);
    number_settime_day = atoi(buffer_day);
    number_settime_hour = atoi(buffer_hour);
    number_settime_minute = atoi(buffer_minute);
    number_settime_week = rtc.dayOfWeek();
    
    
}

void timeOkPopCallback(void *ptr)
{
    rtc.set(0, number_settime_minute, number_settime_hour, 
            number_settime_week, number_settime_day, 
            number_settime_month, number_settime_year);
}

void timeAddPopCallback(void *ptr)
{
    settime_up = 1;
    setTime();
    setWeek();
}

void timeDECPopCallback(void *ptr)
{
    settime_up = 0;
    setTime();
    setWeek();
}

void setYearPopCallback(void *ptr)
{
    settime_type = 1;   
}

void setMonPopCallback(void *ptr)
{
    settime_type = 2;
}

void setDayPopCallback(void *ptr)
{
    settime_type = 3;
}

void setHourPopCallback(void *ptr)
{
    settime_type = 4;
}

void setMinPopCallback(void *ptr)
{
    settime_type = 5;
}

void setWeekPopCallback(void *ptr)
{
    settime_type = 6;

}

void setTime(void)
{
    switch (settime_type)
    {
        case 1: 
            if (settime_up == 1)
            {
               number_settime_year++;
               if (number_settime_year > 99)
               {
                    number_settime_year = 10;
               }
               memset(buffer, 0, sizeof(buffer));  
               itoa(number_settime_year, buffer, 10);
               setyear.setText(buffer);
            }   
            else if (settime_up == 0)
            {
                number_settime_year--;
                if (number_settime_year < 10)
                {
                    number_settime_year = 99;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_year, buffer, 10);
                setyear.setText(buffer);
            }
            break;
        case 2:
            if (settime_up == 1)
            {
                number_settime_month++;
                if (number_settime_month > 12)
                {
                    number_settime_month = 1;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_month, buffer, 10);
                setmon.setText(buffer);
            }
            else if (settime_up == 0)
            {
                number_settime_month--;
                if (number_settime_month < 1)
                {
                    number_settime_month= 12;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_month, buffer, 10);
                setmon.setText(buffer);
            }
            break;
        case 3:
        {
            
            int8_t dayofmonth = number_settime_month;
            if (settime_up == 1)
            {
                number_settime_day++;
                if (dayofmonth == 1 || dayofmonth == 3 || dayofmonth == 5 
                    || dayofmonth == 7 || dayofmonth == 8 || dayofmonth == 10
                    || dayofmonth == 12 )
                {
                    if (number_settime_day > 31)
                    {
                        number_settime_day = 1;
                    }
                }
                if (dayofmonth == 4 || dayofmonth == 6 || dayofmonth == 9 
                    || dayofmonth == 11)
                {
                    if (number_settime_day > 30)
                    {
                        number_settime_day = 1;
                    }
                }
                if (dayofmonth == 2)
                {
                   if ((number_settime_year%4 == 0&&number_settime_year%100 != 0)||number_settime_year%400 == 0)
                    {
                        if (number_settime_day > 29)
                        {
                            number_settime_day = 1;
                        }
                    }
                    else
                    {
                        if (number_settime_day > 28)
                        {
                            number_settime_day = 1;
                        }
                    }
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_day, buffer, 10);
                setday.setText(buffer);
            }
            else if (settime_up == 0)
            {
                number_settime_day--;
                if (dayofmonth == 1 || dayofmonth == 3 || dayofmonth == 5 
                    || dayofmonth == 7 || dayofmonth == 8 || dayofmonth == 10
                    || dayofmonth == 12 )
                {
                    if (number_settime_day < 1)
                    {
                        number_settime_day = 31;
                    }
                }
                if (dayofmonth == 4 || dayofmonth == 6 || dayofmonth == 9 
                    || dayofmonth == 11)
                {
                    if (number_settime_day < 1)
                    {
                        number_settime_day = 30;
                    }
                }
                if (dayofmonth == 2)
                {
                    if ((number_settime_year%4 == 0&&number_settime_year%100 != 0)||number_settime_year%400 == 0)
                    {
                        if (number_settime_day < 1)
                        {
                            number_settime_day = 29;
                        }
                    }
                    else
                    {
                        if (number_settime_day < 1)
                        {
                            number_settime_day = 28;
                        }
                    }
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_day, buffer, 10);
                setday.setText(buffer);
            }
            break;
        }
        case 4:
            if (settime_up == 1)
            {
                number_settime_hour++;
                if (number_settime_hour > 23)
                {
                    number_settime_hour = 0;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_hour, buffer, 10);
                sethour.setText(buffer);
            }
            else if (settime_up == 0)
            {
                number_settime_hour--;
                if (number_settime_hour < 0)
                {
                    number_settime_hour = 23;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_hour, buffer, 10);
                sethour.setText(buffer);
            }
            break;
        case 5:
            if (settime_up == 1)
            {
                number_settime_minute++;
                if (number_settime_minute > 59)
                {
                    number_settime_minute = 0;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_minute, buffer, 10);
                setmin.setText(buffer);
            }
            else if (settime_up == 0)
            {
                number_settime_minute--;
                if (number_settime_minute < 0)
                {
                    number_settime_minute = 59;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_minute, buffer, 10);
                setmin.setText(buffer);
            }
            break;                
    }
}

void setWeek(void)
{
    int32_t year;
    int32_t month;
    if(number_settime_month < 3)
    {
        month = number_settime_month + 12;
        year = number_settime_year-1;
    }
    else
    {
        month = number_settime_month;
        year = number_settime_year;
    }
    
    number_settime_week =year + year/4 + 20/4 - 2*20 + ((26*(month+1))/10 + number_settime_day - 1);
    number_settime_week = (number_settime_week %7 + 7)%7 + 1; 
    switch (number_settime_week)
    {
        case 1:
                strcpy(buffer, "Domenica");
                break;
        case 2: 
                strcpy(buffer, "Lunedi");
                break;
        case 3:
                strcpy(buffer, "Martedi");
                break;
        case 4:
                strcpy(buffer, "Mercoledi");
                break;
        case 5:
                strcpy(buffer, "Giovedi");
                break;
        case 6:
                strcpy(buffer, "Venerdi");
                break;
        case 7:
                strcpy(buffer, "Sabato");
                break;
        default:
                strcpy(buffer, "fail");
                break;
    }
    setweek.setText(buffer);
}

void timeToSetPopCallback(void *ptr)
{
    set.show();        
}


/*
*******************************************************************
* TIME partial end
* 
*******************************************************************
*/

void backHomePopCallback(void *ptr)
{
    home.show();
    refreshHome();
    
}

void  refreshHome(void)
{
    year.setText(buffer_year);
    month.setText(buffer_month);
    day.setText(buffer_day);
    hour.setText(buffer_hour);
    minute.setText(buffer_minute);
    week.setText(buffer_week);
    temperature.setText(buffer_temperature);
    umidita.setText(buffer_umidita);
    

    
     if (light_timer == 2 )
    {
        home_lightpic.setPic(5);
    }
    else
    {
        home_lightpic.setPic(6);   
    }
    if (uv_timer == 4 )
    {
        home_uvpic.setPic(7);
    }
    else
    {
        home_uvpic.setPic(8);    
    }

    if (pump_timer == 4 )
    {
        home_pumppic.setPic(9);
    }
    else
    {
        home_pumppic.setPic(10);    
    } 
    if (feed_timer == 4 )
    {
        home_feedpic.setPic(12);
    }
    else
    {
        home_feedpic.setPic(11);    
    }
    if(umido_timer == 4)
    {
      home_umidpic.setPic(14);
    }
    else
    {
      home_umidpic.setPic(13);
    }
}

void goSetPopCallback(void *ptr)
{
    set.show();     
}

void gosetumidPopCallback(void *ptr)//umidificatore
{
  umidificatore.show();
}

void timeDisplay(void)
{
    memset(buffer, 0, sizeof(buffer)); 
    number = rtc.hour();
    itoa(number, buffer, 10);
    if (strcmp(buffer_hour, buffer))
    {
        hour.setText(buffer);
        strcpy(buffer_hour, buffer); 
    }

    memset(buffer, 0, sizeof(buffer));
    memset(buffer_temp, 0, sizeof(buffer_temp));
    number = rtc.minute();
    itoa(number, buffer_temp, 10);
    if (rtc.minute() < 10)
    {
        strcat(buffer, "0");
    }
    strcat(buffer, buffer_temp);
    if (strcmp(buffer_minute, buffer))
    {
        minute.setText(buffer);
        strcpy(buffer_minute, buffer); 
    }
    
    memset(buffer, 0, sizeof(buffer)); 
    memset(buffer_temp, 0, sizeof(buffer_temp)); 
    number = rtc.second();
    if(number != buffer_second)
    {
        timer++;
        if (number%2)
        {
            strcpy(buffer,":");
            second.setText(buffer);
            buffer_second = number;
        }
        else
        {
            strcpy(buffer," ");
            second.setText(buffer);
            buffer_second = number;
        }
    }
    switch (rtc.dayOfWeek())
    {
        case 1:
                strcpy(buffer, "Domenica");
                break;
        case 2: 
                strcpy(buffer, "Lunedi");
                break;
        case 3:
                strcpy(buffer, "Martedi");
                break;
        case 4:
                strcpy(buffer, "Mercoledi");
                break;
        case 5:
                strcpy(buffer, "Giovedi");
                break;
        case 6:
                strcpy(buffer, "Venerdi");
                break;
        case 7:
                strcpy(buffer, "Sabato");
                break;
        default:
                strcpy(buffer, "fail");
                break;
    }

    if (strcmp(buffer_week, buffer))
    {
        week.setText(buffer);
        strcpy(buffer_week, buffer);
    }

    memset(buffer, 0, sizeof(buffer));
    memset(buffer_temp, 0, sizeof(buffer_temp));
    number = rtc.year();
    itoa(number, buffer, 10);
    if (strcmp(buffer_year, buffer))
    {
        year.setText(buffer);
        strcpy(buffer_year, buffer);
    }
    
    switch (rtc.month())
    {
        case 1:
                strcpy(buffer, "01");
                break;
        case 2: 
                strcpy(buffer, "02");
                break;
        case 3:
                strcpy(buffer, "03");
                break;
        case 4:
                strcpy(buffer, "04");
                break;
        case 5:
                strcpy(buffer, "05");
                break;
        case 6:
                strcpy(buffer, "06");
                break;
        case 7:
                strcpy(buffer, "07");
                break;
        case 8: 
                strcpy(buffer, "08");
                break;
        case 9:
                strcpy(buffer, "09");
                break;
        case 10:
                strcpy(buffer, "10");
                break;
        case 11:
                strcpy(buffer, "11");
                break;
        case 12:
                strcpy(buffer, "12");
                break;
        default:
                strcpy(buffer, "fail");
                break;
    }
    if (strcmp(buffer_month, buffer))
    {
        month.setText(buffer);
        strcpy(buffer_month, buffer);
    }
    
    memset(buffer, 0, sizeof(buffer));
    number = rtc.day();
    itoa(number, buffer, 10);
    if (strcmp(buffer_day, buffer))
    {
        day.setText(buffer);
        strcpy(buffer_day, buffer);
    }
}

void temperatureDisplay(void)//michele
{
    TTTT = sht.getTemperature(); // rilevo temperatura
    HHHH = sht.getHumidity(); // rilevo umidità
    


  //dht22.read2(&temperature, &humidity, NULL);


    //memset(buffer, 0, sizeof(buffer));
    dtostrf(TTTT, 6, 2, buffer_temperature);
    //memset(buffer_temperature, 0, sizeof(buffer_temperature))   ;
    //itoa(TTTT, buffer_temperature, 10);

//    memset(buffer, 0, sizeof(buffer));
    dtostrf(HHHH, 6, 2, buffer_umidita);
//    memset(buffer_umidita, 0, sizeof(buffer_umidita));
//    itoa(HHHH, buffer_umidita, 10);

    
    temperature.setText(buffer_temperature);
    umidita.setText(buffer_umidita);


//    memset(buffer, 0, sizeof(buffer));
//    itoa(celsius, buffer, 10);*/
//    temperature.setText(buffer);

/*String command = "tem.txt=\""+String(TTTT,1)+"\"";
Serial.print(command);
endNextionCommand();

//temperature.setText(buffer_temperature);
*/
    
}


void setup(void)
{   
//    dht.begin();
    nexInit();
    atInit();
    goset.attachPop(goSetPopCallback);
    backhome.attachPop(backHomePopCallback);
    settime.attachPop(timePopCallback);
    settime_ok.attachPop(timeOkPopCallback);
    settime_add.attachPop(timeAddPopCallback);
    settime_dec.attachPop(timeDECPopCallback);
    setyear.attachPop(setYearPopCallback);
    setmon.attachPop(setMonPopCallback);
    setday.attachPop(setDayPopCallback);
    sethour.attachPop(setHourPopCallback);
    setmin.attachPop(setMinPopCallback);
    setweek.attachPop(setWeekPopCallback);
    time_back_to_set.attachPop(timeToSetPopCallback);

    setpump.attachPop(pumpPopCallback);
    setpump_manual.attachPop(pumpManualPopCallback);
    setpump_timer.attachPop(pumpTimerPopCallback);
    setpump_add.attachPop(pumpAddPopCallback);
    setpump_dec.attachPop(pumpDECPopCallback);
    setpump_hour.attachPop(pumpHourPopCallback);
    setpump_min.attachPop(pumpMinPopCallback);
    pump_back_to_set.attachPop(pumpToSetPopCallback);

setumid.attachPop(UMIDO_PopCallback);
setum_manual.attachPop(UmManualPopCallback);
setum_timer.attachPop(umTimerPopCallback);
setum_add.attachPop(UMIDO_AddPopCallback);
setum_dec.attachPop(UmDECPopCallback);
setum_hour.attachPop(UMIDO_HourPopCallback);
//setum_min.attachPop(umMinPopCallback);
um_back_to_set.attachPop(umToSetPopCallback);

    

    setfeed.attachPop(feedPopCallback);
    setfeed_manual.attachPop(feedManualPopCallback);
    setfeed_timer.attachPop(feedTimerPopCallback);
    setfeed_add.attachPop(feedAddPopCallback);
    setfeed_dec.attachPop(feedDECPopCallback);
    setfeed_hour.attachPop(feedHourPopCallback);
    setfeed_min.attachPop(feedMinPopCallback);
    feed_back_to_set.attachPop(feedToSetPopCallback);

    setuv.attachPop(uvPopCallback);
    setuv_manual.attachPop(uvManualPopCallback);
    setuv_timer.attachPop(uvTimerPopCallback);
    setuv_add.attachPop(uvAddPopCallback);
    setuv_dec.attachPop(uvDECPopCallback);
    setuv_hour.attachPop(uvHourPopCallback);
    setuv_min.attachPop(uvMinPopCallback);
    uv_back_to_set.attachPop(uvToSetPopCallback);

    setlight.attachPop(lightPopCallback);
    setlight_manual.attachPop(lightManualPopCallback);
    setlight_timer.attachPop(lightTimerPopCallback);
    setlight_add.attachPop(lightAddPopCallback);
    setlight_dec.attachPop(lightDECPopCallback);
    setlight_hour_on.attachPop(lightHourOnPopCallback);
    setlight_hour_off.attachPop(lightHourOffPopCallback);
    setlight_min_on.attachPop(lightMinOnPopCallback);
    setlight_min_off.attachPop(lightMinOffPopCallback);
    light_back_to_set.attachPop(lightToSetPopCallback);
    
    pinMode(PIN_LIGHT, OUTPUT);
    pinMode(PIN_UV, OUTPUT);
    pinMode(PIN_FEED, OUTPUT);
    pinMode(PIN_PUMP, OUTPUT); 
    digitalWrite(PIN_LIGHT, ACCESO);
    digitalWrite(PIN_UV, SPENTO);
    digitalWrite(PIN_FEED, SPENTO);
    digitalWrite(PIN_PUMP, SPENTO);
   /* RTCLib::set(byte second, byte minute, byte hour, 
    * byte dayOfWeek, byte dayOfMonth, byte month, byte year);
    */
   /*rtc.set(0, 5, 16, 3, 15, 2, 16);*/ 
   

   
}


void loop(void)
{    
    if (timer > update)
    {
        temperatureDisplay();
        update += 30;
    }
    nexLoop(nex_Listen_List);
    rtc.refresh();
    timeDisplay();
    pumpTimerRespond();
    feedTimerRespond();
    uvTimerRespond();
    lightTimerRespond();
    umTimerRespond();
    
    if (timer > 3)
    {
        atRespond();
    }
    else
    {
        while (at_serial.available() > 0)
        {
            at_serial.read();
            Serial.print((uint8_t)at_serial.read(),HEX);
        }
    }

    if (millis() - t2 >= 3600) {
    t2 = millis();
    Flash();
    
  }

}

void Flash() //Sistema in esecuzione
{
  digitalWrite(6, HIGH);
  delay(50);
  digitalWrite(6, LOW);
  delay(50);
}

Metti anche il risultato della compilazione ... la fine, quando da l'occupazione di memoria.

Guglielmo

Ecco quando richiesto Guglielmo.

Grazie, altra domanda, su che modello di Arduino stai sviluppando la cosa ? ? ? Perché quella NON è la memoria di un Arduino UNO o simili AVR ... :roll_eyes:

Guglielmo

Arduino Mega2560

Ok, grazie, ricordavo male la quantità di SRAM sulla MEGA ... :grin:

Mmm ... flash ne occupi pochissima ed anche come SRAM allocata staticamente siamo al 38% ... occorrerebbe monitorare cose accade durante l'uso, ma mi sembra strano che venga riempita la SRAM.

Per sicurezza, potresti magari provare a stampare, in vari punti del programma, il valore che ti ritorna la funzione freeRam() così definita (mettila in testa al tuo programma):

int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}

Baste che poi, in vari punti del programma, metti:

Serial.print( "Memoria libera: " );
Serial.println( freeRam() );

ed avrai la stampa, in quel punto, di quanta SRAM ti rimane libera. Se vedi che cala di parecchio mano mano che il programma lavora, allora c'è un problema ... :roll_eyes:

Guglielmo

Va bene, questa sera dopo implementerò questo comando e vi aggiorno

Riporto la mia esperienza con un cavo lungo 3,5 m tra Arduino Uno e l'esp8266-01. Ho avuto problemi come la scomparsa dell'AP, una volta connesso al mio smartphone non comunicavo con l'esp dopo pochi giorni di funzionamento.
Ho risolto riducendo la lunghezza ad un metro e riavviando l'esp ogni 24h.
Ovvio che non è lo stesso tuo caso, ma potrebbe fornirti uno spunto utile :wink:.