Please note: Friday June 18 is a holiday celebrating Juneteenth, please allow extra time for your order to arrive and plan accordingly.
0

Externat Reset - Adafruit Feather 32u4
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Externat Reset - Adafruit Feather 32u4

by JoanCS on Mon May 03, 2021 10:01 am

Good afternoon. What system could I do to generate an external reset every x hours on a 32u4 feather board.
The board carries a PIN to reset. Attached is the image of the plaque.
Thanks
Attachments
Captura001.PNG
Captura001.PNG (214.4 KiB) Viewed 145 times

JoanCS
 
Posts: 46
Joined: Thu May 07, 2020 12:43 pm

Re: Externat Reset - Adafruit Feather 32u4

by adafruit_support_bill on Mon May 03, 2021 10:14 am

You can check the time in your code and use the watchdog.enable function in the SleepyDog library to cause the reset.

https://github.com/adafruit/Adafruit_SleepyDog

adafruit_support_bill
 
Posts: 81308
Joined: Sat Feb 07, 2009 10:11 am

Re: Externat Reset - Adafruit Feather 32u4

by JoanCS on Tue May 04, 2021 3:28 am

Thanks for the reply. I will try it.

JoanCS
 
Posts: 46
Joined: Thu May 07, 2020 12:43 pm

Re: Externat Reset - Adafruit Feather 32u4

by JoanCS on Wed May 05, 2021 2:03 am

Good morning. The "Sleepy Dog", I understand that the program falls asleep to resuscitate consumption. Correct?
What I'm looking for is a system that every x minutes, if the program does not respond, can schedule a reset of the program. Why do I need this? The program I am testing, reads from a MySQL database and based on the data read captures some data that I register again in the MySQL database. If for some reason, the connection to the server where I have the MySQL database fails, sometimes it does not restore communication. This error happens little, but it makes my system unreliable enough. Nessito give reliability to the system.

JoanCS
 
Posts: 46
Joined: Thu May 07, 2020 12:43 pm

Re: Externat Reset - Adafruit Feather 32u4

by JoanCS on Wed May 05, 2021 2:22 am

Adjunto el meu programa amb la llibreria posada i el programa queda parat al inicialitzar la ethernet !!!

****
Iniciant SETUP...
Auto reset programat a 1 minut
15 milliseconds!
15


****

Code: Select all | TOGGLE FULL SIZE
#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>

#include <Adafruit_SleepyDog.h>   // Control de prenjades.

//byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE };
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x00 };
int idSonda  = 1002;                       // Id de la Sonda, cada sona ha de portar un Id Unica

float Temperatura ;
float Calibratge = 0.020; //
int TempsEntreLectures = 10;
int OnOff = 0;     //0= no gravem 1=si gravem

// MySQL
IPAddress serverMySQL_addr(10.10.10.10);  // IP of the MySQL *server* here
char userMySQL[] = "fffff";              // MySQL user login username
char passwordMySQL[] = "rrrrrrrrrr";     // MySQL user login password

//char query_1[] = "SELECT Calibratge, Periode, idTipusSondes FROM gesregistres.sondes WHERE idSonda = '1002'";
char SELECT_SQL[] = "SELECT Calibratge, Periode, idTipusSondes, OnOff FROM gesregistres.sondes WHERE (%s)";

// Query insert de test
//char INSERT_SQL[] = "INSERT INTO gesRegistres.Registres (idSonda, idTipusSonda, Valor) VALUES (1001, 't', 25)";
char INSERT_SQL[] = "INSERT INTO gesRegistres.Registres (idSonda, idTipusSonda, Valor) VALUES (%s, '%s', %s)";
char query[128];
   

 
EthernetClient client1;
MySQL_Connection conn((Client *)&client1);
MySQL_Cursor cur = MySQL_Cursor(&conn);

void setup()
  {
  Serial.begin(9600);
  while (!Serial); // wait for serial port to connect
  Serial.println();
  Serial.println("Iniciant SETUP...");
  //Autoreset
  Serial.println("Auto reset programat a 1 minut ");
  int countdownMS = Watchdog.enable(60000);          //autoreset
  Serial.print(countdownMS, DEC);
  Serial.println(" milliseconds!"); 
  //Fi autorreset
  Serial.print(countdownMS, DEC);
  Ethernet.begin(mac_addr);
  Serial.println(Ethernet.localIP());
  LlegirMySQL();
  CapturaDeDades();
  Serial.println("Fi SETUP");
  Serial.println("*****************");
  delay (2000);
  }


void loop()
  {
  Watchdog.reset();
  delay(1000);
  //Serial.println(" -> Recording data.");
  LlegirMySQL();
  CapturaDeDades();
  if (OnOff == 1) Serial.println(" -> OnOff = ON");
  if (OnOff == 0) Serial.println(" -> OnOff = OFF");
  if (OnOff == 1) RegistreMySQL(idSonda, "t", Temperatura);
  // Temps que esperem entre captura i captura, en minuts--------------------   
  Esperem (TempsEntreLectures); //esperem 1, 10 minut,...
  // Fi espera entre dades--------------------------------------------------- 
  }

void RegistreMySQL(int idSondaSQL, char IdTipusSondaSQL[], float ValorSQL)
  {
    char idSonaInsertat[6];
    char ValorInsertar[6];

    dtostrf( idSondaSQL , 1, 0, idSonaInsertat);   // zero decimals
    dtostrf( ValorSQL , 1, 2, ValorInsertar);   // dos decimals
   
    //MySQL
    Serial.println(" -> Registrant al MySQL...");
    if (conn.connect(serverMySQL_addr, 3306, userMySQL, passwordMySQL))
     {
     delay(1000);
     MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
     sprintf(query, INSERT_SQL, idSonaInsertat, IdTipusSondaSQL, ValorInsertar);
     cur_mem->execute(query);
     delete cur_mem;
     conn.close();
     Serial.println(" -> Dada gravada al MySQL");
     }
     else  Serial.println(" -> Connection MySQL failed.");
   
  }

//Funció capturar dades------------------------------------------------------
void CapturaDeDades()
  {
  int reading; 
  float Valor, voltage;
    //TMP36 Pin Variables
    int sensorPin = 0;      //Capturarem el valro per el pin 0 analogic, A0
                            //the analog pin the TMP36's Vout (sense) pin is connected to
                            //the resolution is 10 mV / degree centigrade with a
                            //500 mV offset to allow for negative temperatures 
    Serial.println(" -> Capturant dades de la sonda ...");
    reading = analogRead(sensorPin);  //llegim el valor de A0, analogic
    // Convertim la lectrua a voltatge
    voltage = reading * 5.0;
    voltage = voltage/1024.0; //voltage /= 1024.0;
    // Printem
    Serial.print(" -> Voltatge analogic     = "); Serial.print(voltage); Serial.println(" volts");
    //if (voltage > Calibratge) voltage = voltage - Calibratge;  //calibratge
    //Serial.print(" -> Voltatge analogic cal.= "); Serial.print(voltage); Serial.println(" volts");
    Temperatura = (voltage - 0.5) * 100 ;                      //convertim 10 mv per graus amb un offser 500 mV offset
                                                               //podem fer negatius
                                                               //a graus centigrats ((volatge - 500mV) times 100)
    Serial.print(" -> Temperatura           = "); Serial.print(Temperatura); Serial.println(" graus C");                                                               
    Temperatura = Temperatura + Calibratge;                    //calibratge                                                     
    Serial.print(" -> Temperatura Calibrada = "); Serial.print(Temperatura); Serial.println(" graus C"); 
  }
// Fi capturar dades---------------------------------------------------------

void LlegirMySQL()
  {
//Calibratge:
      //MySQL
  Serial.println(" -> Llegint al MySQL...");
  if (conn.connect(serverMySQL_addr, 3306, userMySQL, passwordMySQL))
    {
    delay(1000);
    }
  else  Serial.println("Connection failed."); 
  row_values *row = NULL;
  char idSonaInsertat[6];
  dtostrf( idSonda, 1, 0, idSonaInsertat);   // zero decimals   
  MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
  // Execute the query
  sprintf(query, SELECT_SQL, idSonaInsertat);
  cur_mem->execute(query);
  // Fetch the columns (required) but we don't use them.
  column_names *columns = cur_mem->get_columns();

  // Read the row (we are only expecting the one)
  do
    {
    row = cur_mem->get_next_row();
    if (row != NULL)
      {
      Calibratge = atol(row->values[0]);
      Calibratge = Calibratge/100;  //per tenir decimals
      TempsEntreLectures = atol(row->values[1]);
      //head_count2 = (row->values[2]);
      OnOff = atol(row->values[3]);
      }
     }
  while (row != NULL);
  // Deleting the cursor also frees up memory used
  delete cur_mem;
  conn.close();
 
  // Show the result
  Serial.print(" ->           Calibratge = ");
  Serial.println(Calibratge);   
  Serial.print(" -> Temps entre lectures = ");
  Serial.println(TempsEntreLectures);
  //Serial.print("idTipusSondes = ");
  //Serial.println(head_count2); 
  //Serial.println(); 
  Serial.print(" ->                OnOff = ");
  Serial.println(OnOff);     
  //Fi Calibratge   
  }

// Funció temps d'espera entre captura de dades, en minuts-------------------
void Esperem (int minuts)
  {
  Serial.print(" -> Esperant (minuts) :");
  Serial.print( minuts);
  Serial.print(" ");
  for (int a=0; a<minuts; a++)
    { 
    delay(60000);  //60000 es un minut
    Serial.print(".");
    }
  Serial.println (" "); 
  }
// Fi funció temps d'espera--------------------------------------------------

JoanCS
 
Posts: 46
Joined: Thu May 07, 2020 12:43 pm

Re: Externat Reset - Adafruit Feather 32u4

by JoanCS on Wed May 05, 2021 6:26 am

Sorry, I translate !!!
****
I attach my program with the library on and the program stops when initializing the ethernet !!!

JoanCS
 
Posts: 46
Joined: Thu May 07, 2020 12:43 pm

Re: Externat Reset - Adafruit Feather 32u4

by adafruit_support_bill on Wed May 05, 2021 6:33 am

Good morning. The "Sleepy Dog", I understand that the program falls asleep to resuscitate consumption. Correct?

That is one use for it. You can also use it to set the watchdog timer to automatically reset the system after a certain time.

I attach my program with the library on and the program stops when initializing the ethernet !!!

That looks like a rather complex program for a 32U4. I suspect that you may have a memory problem - in particular a shortage of RAM causing a stack crash.

This guide has some tips on optimizing memory usage.
https://learn.adafruit.com/memories-of- ... izing-sram

Other than that, you may need to move to a processor with more memory.

adafruit_support_bill
 
Posts: 81308
Joined: Sat Feb 07, 2009 10:11 am

Re: Externat Reset - Adafruit Feather 32u4

by JoanCS on Wed May 05, 2021 10:06 am

Now it’s left to do a reset every 2 seconds and it doesn’t give me time to load a new program. Can I do a reset to leave the board as a "default factory"

JoanCS
 
Posts: 46
Joined: Thu May 07, 2020 12:43 pm

Re: Externat Reset - Adafruit Feather 32u4

by adafruit_support_bill on Wed May 05, 2021 10:25 am

If you press the reset button as soon as the compile completes, it should enter bootloader mode and allow you to upload a program.

adafruit_support_bill
 
Posts: 81308
Joined: Sat Feb 07, 2009 10:11 am

Re: Externat Reset - Adafruit Feather 32u4

by JoanCS on Tue May 11, 2021 2:33 am

Which board model can I use that has the most memory?

JoanCS
 
Posts: 46
Joined: Thu May 07, 2020 12:43 pm

Re: Externat Reset - Adafruit Feather 32u4

by adafruit_support_bill on Tue May 11, 2021 5:31 am

The Feather M4 Express has about 75 times more RAM than the 32U4. That should give you plenty of room.
https://www.adafruit.com/product/3857

The RP2040 has more memory. But it is currently out of stock and there is no Arduino IDE support for it yet:
https://www.adafruit.com/product/4884

adafruit_support_bill
 
Posts: 81308
Joined: Sat Feb 07, 2009 10:11 am

Re: Externat Reset - Adafruit Feather 32u4

by JoanCS on Fri May 14, 2021 10:00 am

Good afternoon, I attach a code that already works and in case of problems in the connection resets and returns to psoar to work.

Code: Select all | TOGGLE FULL SIZE
#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <avr/wdt.h>   // Control de prenjades.

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x00 };
int idSonda  = 1002;                       // Id de la Sonda, cada sona ha de portar un Id Unica

float Temperatura ;
float Calibratge = 0.020; //
int TempsEntreLectures = 10;
int OnOff = 0;     //0= no gravem 1=si gravem

// MySQL
IPAddress serverMySQL_addr(172,20,105,7);  // IP of the MySQL *server* here
char userMySQL[] = "xxxxx";                            // MySQL user login username
char passwordMySQL[] = "yyyyyy";                // MySQL user login password

char SELECT_SQL[] = "SELECT Calibratge, Periode, idTipusSondes, OnOff FROM gesregistres.sondes WHERE (%s)";

char INSERT_SQL[] = "INSERT INTO gesRegistres.Registres (idSonda, idTipusSonda, Valor) VALUES (%s, '%s', %s)";
char query[128];
   

 
EthernetClient client1;
MySQL_Connection conn((Client *)&client1);
MySQL_Cursor cur = MySQL_Cursor(&conn);

void setup()
  {

  //Autoreset
  wdt_enable(WDTO_8S);
  //Fi autorreset
 
  Serial.begin(9600);
  while (!Serial); // wait for serial port to connect
  Serial.println();
  Serial.println("Iniciant SETUP...");
  Serial.print("Idsonda:"); Serial.println(idSonda);

  wdt_reset();
  Ethernet.begin(mac_addr);
  Serial.println(Ethernet.localIP());

  wdt_enable(WDTO_8S);
  LlegirMySQL();

  CapturaDeDades();
  wdt_reset();
 
  Serial.println("Fi SETUP");
  Serial.println("*****************");
  delay (2000);
  }


void loop()
  {
  wdt_reset();
  LlegirMySQL();
  wdt_reset();
  CapturaDeDades();
  wdt_reset();
  if (OnOff == 1) Serial.println(" -> OnOff = ON");
  if (OnOff == 0) Serial.println(" -> OnOff = OFF");
  if (OnOff == 1) RegistreMySQL(idSonda, "t", Temperatura);
  wdt_disable();
  // Temps que esperem entre captura i captura, en minuts--------------------   
  Esperem (TempsEntreLectures); //esperem 1, 10 minut,...
  // Fi espera entre dades--------------------------------------------------- 
  wdt_enable(WDTO_8S);
  }

void RegistreMySQL(int idSondaSQL, char IdTipusSondaSQL[], float ValorSQL)
  {
    char idSonaInsertat[6];
    char ValorInsertar[6];

    dtostrf( idSondaSQL , 1, 0, idSonaInsertat);   // zero decimals
    dtostrf( ValorSQL , 1, 2, ValorInsertar);   // dos decimals
   
    //MySQL
    Serial.println(" -> Registrant al MySQL...");
    if (conn.connect(serverMySQL_addr, 3306, userMySQL, passwordMySQL))
     {
     delay(1000);
     MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
     sprintf(query, INSERT_SQL, idSonaInsertat, IdTipusSondaSQL, ValorInsertar);
     cur_mem->execute(query);
     delete cur_mem;
     conn.close();
     Serial.println(" -> Dada gravada al MySQL");
     }
     else  Serial.println(" -> Connection MySQL failed.");
   
  }

//Funció capturar dades------------------------------------------------------
void CapturaDeDades()
  {
  int reading; 
  float Valor, voltage;
    //TMP36 Pin Variables
    int sensorPin = 0;      //Capturarem el valro per el pin 0 analogic, A0
                            //the analog pin the TMP36's Vout (sense) pin is connected to
                            //the resolution is 10 mV / degree centigrade with a
                            //500 mV offset to allow for negative temperatures 
    Serial.println(" -> Capturant dades de la sonda ...");
    reading = analogRead(sensorPin);  //llegim el valor de A0, analogic
    // Convertim la lectrua a voltatge
    voltage = reading * 5.0;
    voltage = voltage/1024.0; //voltage /= 1024.0;
    // Printem
    Serial.print(" -> Voltatge analogic     = "); Serial.print(voltage); Serial.println(" volts");
    //if (voltage > Calibratge) voltage = voltage - Calibratge;  //calibratge
    //Serial.print(" -> Voltatge analogic cal.= "); Serial.print(voltage); Serial.println(" volts");
    Temperatura = (voltage - 0.5) * 100 ;                      //convertim 10 mv per graus amb un offser 500 mV offset
                                                               //podem fer negatius
                                                               //a graus centigrats ((volatge - 500mV) times 100)
    Serial.print(" -> Temperatura           = "); Serial.print(Temperatura); Serial.println(" graus C");                                                               
    Temperatura = Temperatura + Calibratge;                    //calibratge                                                     
    Serial.print(" -> Temperatura Calibrada = "); Serial.print(Temperatura); Serial.println(" graus C"); 
  }
// Fi capturar dades---------------------------------------------------------

void LlegirMySQL()
  {
//Calibratge:
      //MySQL
  Serial.println(" -> Llegint al MySQL...");
  if (conn.connect(serverMySQL_addr, 3306, userMySQL, passwordMySQL))
    {
    delay(1000);
    }
  else  Serial.println("Connection failed."); 
  row_values *row = NULL;
  char idSonaInsertat[6];
  dtostrf( idSonda, 1, 0, idSonaInsertat);   // zero decimals   
  MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
  // Execute the query
  sprintf(query, SELECT_SQL, idSonaInsertat);
  cur_mem->execute(query);
  // Fetch the columns (required) but we don't use them.
  column_names *columns = cur_mem->get_columns();

  // Read the row (we are only expecting the one)
  do
    {
    row = cur_mem->get_next_row();
    if (row != NULL)
      {
      Calibratge = atol(row->values[0]);
      Calibratge = Calibratge/100;  //per tenir decimals
      TempsEntreLectures = atol(row->values[1]);
      //head_count2 = (row->values[2]);
      OnOff = atol(row->values[3]);
      }
     }
  while (row != NULL);
  // Deleting the cursor also frees up memory used
  delete cur_mem;
  conn.close();
 
  // Show the result
  Serial.print(" ->           Calibratge = ");
  Serial.println(Calibratge);   
  Serial.print(" -> Temps entre lectures = ");
  Serial.println(TempsEntreLectures);
  //Serial.print("idTipusSondes = ");
  //Serial.println(head_count2); 
  //Serial.println(); 
  Serial.print(" ->                OnOff = ");
  Serial.println(OnOff);     
  //Fi Calibratge   
  }

// Funció temps d'espera entre captura de dades, en minuts-------------------
void Esperem (int minuts)
  {
  Serial.print(" -> Esperant (minuts) :");
  Serial.print( minuts);
  Serial.print(" ");
  for (int a=0; a<minuts; a++)
    {     
    delay(60000);  //60000 es un minut
    Serial.print(".");
    }
  Serial.println (" "); 
  }
// Fi funció temps d'espera--------------------------------------------------

JoanCS
 
Posts: 46
Joined: Thu May 07, 2020 12:43 pm

Re: Externat Reset - Adafruit Feather 32u4

by JoanCS on Fri May 14, 2021 10:03 am

With this program, I still have a problem. If I am monitoring the serial port (TeraTerm or Hypertermial or similar), the program works. If I put the plate alone, without reading the serial port, it doesn't work !!!
How can I solve this problem?

JoanCS
 
Posts: 46
Joined: Thu May 07, 2020 12:43 pm

Re: Externat Reset - Adafruit Feather 32u4

by adafruit_support_bill on Fri May 14, 2021 10:38 am

Remove this line from your code. It will loop indefinitely, waiting for a serial connection:

Code: Select all | TOGGLE FULL SIZE
  while (!Serial); // wait for serial port to connect

adafruit_support_bill
 
Posts: 81308
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.