Arduino stops running

Hi All

I have a project, where my Arduino Uno stops sending data to my SQL database, it seems like it is falling a sleep, so I will like to know it anyone knows what I'm doing wrong. If I connect to it with a PuTTY connection it waiks up, and start sending data, so it seems that the progran doesn't fails.

I have tried with another board, a Arduino Mega, that does the same.

I have another Arduino Uno which is sending data to the database, it has been running for many month without any problems. I have update some og the libraies, between the two projects.

I have attached my code below:

#include <TimeLib.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <MySQL_Cursor.h>
#include <MySQL_Connection.h>

// Ethernet info
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEC };
byte ip[] = { 192, 168, 1, 31 }; // arduino IP in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
unsigned int LocalPort = 8888;
EthernetClient client;

//SQL info
const IPAddress server_addr(192, 168, 1, 10); // IP of the MySQL *server* here
const char user[] = "Test";              // MySQL user login username
const char password[] = "";        // MySQL user login password
const unsigned int port = 3306;           // MySQL connection Port
                            
char INSERT_DATA_5min[] = "INSERT INTO maalerdata.sensor (TimeStamp, nr, value) VALUES ('%s',%d,%s)";
char query[100];
MySQL_Connection conn_5min((Client *)&client);

// NTP Info
EthernetUDP ntpUDP; // To send & receive packets using UDP
IPAddress timeServer(216, 239, 35, 0); // time.google.com
const int timeZone = 1;     // Central European Time

// Måler info
const unsigned char interruptPinRegn = 3;
const unsigned char interruptPinVind = 2;
volatile float Regn;
volatile float Puls;
volatile unsigned char Vind[300];
volatile int OldPos;
volatile int Pos;
int Direction = A0;
float DirValue;
float VindRetning;
float VindGns;
float VindMax;

char TimeStamp[20];
char Temp[8];

static int NtpSync;
static int NtpSyncOld;
static int NtpCnt;
int OldMin;
int OldMinut;
int OldSecond;
String test;

void(* resetFunc) (void) = 0;//declare reset function at address 0


void setup() {
  Serial.begin(38400);
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  ntpUDP.begin(LocalPort);
  setSyncProvider(getNtpTime);
  NtpSync = 10;
  setSyncInterval(NtpSync);
  attachInterrupt(digitalPinToInterrupt(interruptPinRegn), countregn, RISING);
  attachInterrupt(digitalPinToInterrupt(interruptPinVind), countvind, RISING);
}

time_t prevDisplay = 0; // when the digital clock was displayed


void loop()
{
  if (year() == 1970)
  {
    Serial.print("\e[19;1H");
    Serial.println("Tids data ej ok!!");
    NtpSync = 10;
  }
  else
  {
    NtpSync = 3600;
  }
  if (second() != OldSecond) 
  {
    Serial.print("\e[1;80H");
    test = String(year()) + "-" + digits(month()) + "-" + digits(day()) + " " +  digits(hour()) + ":" + digits(minute()) +  ":" + digits(second())  + "\0";
    test.toCharArray(TimeStamp, 20);
    Serial.print(TimeStamp);
    OldSecond = second();
    DirValue = analogRead(Direction) * 0.0049;  
  }
  if (NtpSync != NtpSyncOld)
  {
    setSyncInterval(NtpSync);
    NtpSyncOld = NtpSync;   
  }
    
  if ((minute() % 5 == 0) and (second() == 3) and (minute() != OldMinut))
  {
    Serial.print("\e[4;1H");
    OldMinut = minute();
    if (DirValue >= 3.80 and DirValue < 3.88) {VindRetning =   0.0;}
    if (DirValue >= 1.94 and DirValue < 2.02) {VindRetning =  22.5;}
    if (DirValue >= 2.21 and DirValue < 2.29) {VindRetning =  45.0;}
    if (DirValue >= 0.37 and DirValue < 0.43) {VindRetning =  67.5;}
    if (DirValue >= 0.43 and DirValue < 0.49) {VindRetning =  90.0;}
    if (DirValue >= 0.28 and DirValue < 0.36) {VindRetning = 112.5;}
    if (DirValue >= 0.86 and DirValue < 0.94) {VindRetning = 135.0;}
    if (DirValue >= 0.58 and DirValue < 0.66) {VindRetning = 157.5;}
    if (DirValue >= 1.36 and DirValue < 1.44) {VindRetning = 180.0;}
    if (DirValue >= 1.15 and DirValue < 1.23) {VindRetning = 202.5;}
    if (DirValue >= 3.04 and DirValue < 3.12) {VindRetning = 225.0;}
    if (DirValue >= 2.89 and DirValue < 2.97) {VindRetning = 247.5;}
    if (DirValue >= 4.58 and DirValue < 4.66) {VindRetning = 270.0;}
    if (DirValue >= 4.00 and DirValue < 4.08) {VindRetning = 292.5;}
    if (DirValue >= 4.29 and DirValue < 4.37) {VindRetning = 315.0;}
    if (DirValue >= 3.39 and DirValue < 3.47) {VindRetning = 337.5;}

    test = String(year()) + "-" + digits(month()) + "-" + digits(day()) + " " +  digits(hour()) + ":" + digits(minute())  + ":00\0";
    test.toCharArray(TimeStamp, 20);
    SendSQLData5min();
    Serial.println("       ");
    if ((hour() == 04) and (minute() == 0))
    {
      Serial.print("\e[23;1H");
      Serial.println("Doing reset!!!");
      Serial.println("       ");
      Serial.println(TimeStamp);
      delay(100);
      resetFunc();
    }
  }
}

String digits(int value) {
  if (value <= 9)
    return "0" + String(value) + "\0";
  else
    return String(value) + "\0";
}

void countregn()
{
  Regn = Regn + 0.2794;
}

void countvind()
{
  Pos = ((minute() % 5) * 60) + second();
  if (Pos != OldPos) 
  {
    OldPos = Pos;
    Vind[Pos] = 0;
  }
  Vind[Pos] = Vind[Pos] + 1; 
}

void SendSQLData5min()
{
Serial.println(second());
  if (conn_5min.connect(server_addr, port, user, password)) 
    {

      float sum1 = 0;
      float sum2 = 0;
      for(int x = 0; x < 300; x++)
      {
          sum1 = sum1 + Vind[x];
          if(Vind[x] > sum2)
          {
           sum2 = Vind[x];
          }
      }
      VindGns = (sum1 / 300) * 0.6669837;
      VindMax = sum2 * 0.6669837;
      
      MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn_5min);
      dtostrf(Regn, 6, 1, Temp);
      Regn = 0;
      sprintf(query, INSERT_DATA_5min, TimeStamp, 1022, Temp);
      cur_mem->execute(query);
      Serial.println(query);
      delay(100);
      dtostrf(VindGns, 6, 1, Temp);
      sprintf(query, INSERT_DATA_5min, TimeStamp, 1023, Temp);
      cur_mem->execute(query);
      Serial.println(query);
      delay(100);
      dtostrf(VindMax, 6, 1, Temp);
      sprintf(query, INSERT_DATA_5min, TimeStamp, 1024, Temp);
      cur_mem->execute(query);
      Serial.println(query);
      delay(100);
      dtostrf(VindRetning, 6, 1, Temp);
      sprintf(query, INSERT_DATA_5min, TimeStamp, 1025, Temp);
      cur_mem->execute(query);
      Serial.println(query);
      delete cur_mem;
  }
  conn_5min.close();
Serial.println(second());
}

/*-------- NTP code ----------*/
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

time_t getNtpTime()
{
  Serial.print("\e[1;1HNTP  ");
  Serial.print(++NtpCnt);
  Serial.println("       ");
  while (ntpUDP.parsePacket() > 0) ; // discard any previously received packets
  sendNTPpacket(timeServer);
  uint32_t beginWait = millis();
  while (millis() - beginWait < 1500) {
    int size = ntpUDP.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
      ntpUDP.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
      unsigned long secsSince1900;
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    }
  }
  return 0; 
}

void sendNTPpacket(IPAddress &address)
{
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;
  ntpUDP.beginPacket(address, 123); //NTP requests are to port 123
  ntpUDP.write(packetBuffer, NTP_PACKET_SIZE);
  ntpUDP.endPacket();
}

You are using the "String" object which is known to cause problems on Arduino's. My guess is that your Arduino is crashing because the "String" usage is corrupting the memory and whenever you PuTTY into it, the Arduino is reset and starts to work again.

The usual recommendation for Arduinos is to avoid String and learn to use c strings (char arrays that have their strings ended by a null character).

Christian1969:
I have another Arduino Uno which is sending data to the database, it has been running for many month without any problems.

Then it seems to me your first step is to examine very very closely the differences between the two programs and isolate the change that is causing the problem.

And I agree with others who advise against using the String class in an Arduino.

...R