arduino crash due to Serial and String...

Robin2:
Not as far as I can see.

Nope... the attachment has the entire code (yes, not the libraries... but do we need them??)

Plus what about the "#define issue base on if clause via debug enable port??

aq_mishu:
Nope... the attachment has the entire code

Reply #5 (which I quoted from) has no attachment.

Please post the latest version of your complete program in your next Reply.

...R

The entire source code given... no hidings... :-p

Student_Attendance_fw1-6-0-TDS.ino (28.7 KB)

I have lost track of your question now.

Is it this, from Reply #5

I have given the entire code here. The motivation is: I wish to keep an enable jumper, pin11 will be source and pin 12 will be a input_pullup. This way all I want to is manipulate the jumper use such a way, a if pin11 sends 0 then pin 12 will read LOW and thus will disable the debug. If we remove the jumper (as jumper removal will be ONLY for debug purpose), pin12 will see HI and will enable the debug process.

If so that seems a sensible thinng but I don't understand what problem you are having. I don't see any attempt to read the status of enablePin in setup(). For example

debugEnabled == digitalRead(enablePin);

(I think debugPin would be a more obvious name but YMMV).

And you can't use the state of a pin at runtime to set a #define at compile-time. You can use a variable, for example debugEnabled, to determine whether things are printed - like this for example

if (debugEnabled == true) {
  Serial.println("my error message");
}

Separately, you still have String in lots of places and you also have lots of fixed text that is not using the F macro.

...R

Robin2:
Separately, you still have String in lots of places and you also have lots of fixed text that is not using the F macro.

...R

first of all, sorry for causing the loose of track!!

OK now, I got it after a lot of reading and a hecktik day of loosing my 20 years of data through a HDD crash of my cloud drive. Blah!!

Anyway... So, yep, i understood... though i tried to keep it a bit short, but discovered, i have to write so many of the "IF"s to achieve that. No sir, so far I can see in my codes, I have used a lot of F( as after learning that "F" thing it's now a defacto in my codes...

But still there are strings... (Strings). And I have tried to convert few, like:

//String str_rx = "";
char str_rx[30];

//String data = "";
int data;
if (str_rx == "conf t") {
    str_rx[0] = 0; 
    configMode();
  }
static void smartDelayPrompt(unsigned long ms) {
  unsigned long start = millis();
  do {
    while (Serial.available() > 0){  
      Serial.readBytesUntil(10, str_rx, 30);
    }
  } 
  while (millis() - start < ms);
  //do something
}

etc, now the next error is at:

    if ((str_rx.startsWith("?"))||(str_rx.startsWith("help"))) { //show the help

and dont know how to solve it... Plus,

void postData() {
  if (!inetOK) {
    inetInit();
  }
  wdt_reset();
  unsigned int strSize = EEPROM.read(SERVER_ADDR_SIZE);
  char server[strSize];
  eeprom_read_string(SERVER_ADDR_OFFSET, server, strSize);
  String myURL = "GET /attd_posting.php?tag=";
  myURL += data;
  myURL += " HTTP/1.1";
  digitalWrite(readerLED, HIGH);
  digitalWrite(systemLED, HIGH);
  client.stop();
  DEBUG_PRINTLN(F("Connecting to server..."));
  // if you get a connection, report back via serial:
  
.........
  
}

Here is also I think I need that String...

[God!!Why String is so giving pain... heap is why growing??]

From Reply #12

etc, now the next error is at:

if ((str_rx.startsWith("?"))||(str_rx.startsWith("help"))) { //show the help

When you use cstrings you must use different functions to manipulate them. I gave you a link in Reply #2.

I agree that cstrings are not as easy to use as the String class - but that convenience is a luxury that comes with a much more powerful processor - such as PC or RaspberryPi.

...R

ummm... just it become hang again... RESET also did not worked... I was wondering, why watchdog is not working...

looking into the link you gave...

One more thing... WDT Reset... can I use that?? Then I need a real describing of a practical step by step... like

if(condition matches) {
WDT Reset;
}

I am not familiar with using the watchdog so I won't offer any advice about it.

Do you really need it?

...R

#include <MemoryFree.h>
#include <avr/wdt.h>

// On Arduino Leonardo with ATmega32U4:
//
// Reported free memory with str commented out:
// 2373 bytes
//
// Reported free memory with str and Serial.println(str) uncommented:
// 2359 bytes
//
// Difference: 14 bytes (13 ASCII chars + null terminator)

// 14-bytes string
//char str[] = "Hello, world!";

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  wdt_enable(WDTO_8S);
}

void loop() {
//  Serial.println(str);
  wdt_reset();
  for (int i=0; i<30; i++){
    wdt_reset();
    Serial.print("Iteration: ");
    Serial.println(i);
    Serial.print("freeMemory()=");
    Serial.println(freeMemory());
  
    delay(1000);
    if (i==25){
      delay(9000);
    }
  }
}

This is a code that i used for testing the watchdog. Strangely, i found, it freezes the arduino and only a power cycle is the lat savior. Basically i was playing with the WDT and planning was WDT @ 8s, and it will go until a time, say 25 sec. on 25th sec, i added a delay of 9s so before that WDT will enabled and it will make a reset. But instead, it freezes...

Robin2:
Do you really need it?

...R

Just in case system is unstable, and needs a fresh restart (basically the kaboom heap growth issue), then i was planning on WDT. [apart from char[] :wink: ]

aq_mishu:
Just in case system is unstable, and needs a fresh restart (basically the kaboom heap growth issue),

The watchdog timer is the wrong solution or that problem.

The correct solution is to write the code properly so the heap does not grow out of bounds. Apart from anything else you are going to lose data if the WDT needs to be triggered.

...R

Robin2:
you are going to lose data if the WDT needs to be triggered.

...R

This will just read a RFID reader and will pass it to another machine. No storage at all... so I'm not worried about data loose for this project. But yes, this is not the right solution. I'm desperately trying to solve that char[] topic.

But I ws just hoping for a auto recovery, as the machine will run for a long time, and no gurantee of what may happen... thus was thinking of a reset (reboot) when it faces kaboom...

how can i convert str_rx.startsWith("show run") to char part?? I need same functionality, but as i have to avoid Strings...

each Strings except Serial.println(F("ABC)) is a heap increasing right??

aq_mishu:
how can i convert str_rx.startsWith("show run") to char part?? I need same functionality, but as i have to avoid Strings...

I gave you a link to a web page with all the cstring functions. What have you tried?

...R

http://www.cplusplus.com/reference/cstring/

Here I tried to understood comparison and searching... but it all seems too much complex.

Also So, how does Serial.readBytesUntil() work? | pwillard.com link seems a bit promising... what do you say??

aq_mishu:
but it all seems too much complex.

Stick with it. I found that I began to understand it after the 14th reading. :slight_smile:

...R

Sketch uses 29596 bytes (11%) of program storage space. Maximum is 253952 bytes.
Global variables use 1866 bytes (22%) of dynamic memory, leaving 6326 bytes for local variables. Maximum is 8192 bytes.

Does this actually means DRAM or it's the SRAM also???

There is no DRAM, it's all SRAM.

Hi,

void postData() {
  if (!inetOK) {
    inetInit();
  }
  unsigned int strSize = EEPROM.read(SERVER_ADDR_SIZE);
  char server[strSize];
  eeprom_read_string(SERVER_ADDR_OFFSET, server, strSize);
  //String myURL = "GET /attd_posting.php?tag=";
  char myURL[] = "GET /attd_posting.php?tag=";
  strcat(myURL, data);
  strcat(myURL, " HTTP/1.1");
  digitalWrite(readerLED, HIGH);
  digitalWrite(systemLED, HIGH);
  client.stop();
  DEBUG_PRINTLN(F("Connecting to server..."));
  // if you get a connection, report back via serial:
  
  if (client.connect(server, 80)) {
    DEBUG_PRINTLN(F("Connected!"));
    // Make a HTTP request:
    DEBUG_PRINTLN(F("Posting..."));
    client.println(myURL);
    DEBUG_PRINTLN(myURL);
    client.print(F("Host: "));
    client.println(server);
    DEBUG_PRINT(F("Host: "));
    DEBUG_PRINTLN(server);
    client.println(F("Connection: close"));
    client.println();
    DEBUG_PRINTLN(F("Posted. Disconnecting from Server."));
    DEBUG_PRINTLN();
    client.flush();
    client.stop();
    DEBUG_PRINTLN(F("Connection closed"));
    digitalWrite(systemLED, LOW);
    digitalWrite(readerLED, LOW);
  }
  // if the server's disconnected, stop the client: A fail safe
  else if (!client.connected()) {
    client.stop();
    DEBUG_PRINTLN(F("Posting failed."));
  }
  else {
    // if you didn't get a connection to the server:
    DEBUG_PRINTLN(F("Posting failed."));
  }
  Serial.flush();
  keepAliveTimeElapsed = 0;
  
}

Here the

unsigned int strSize = EEPROM.read(SERVER_ADDR_SIZE);
  char server[strSize];
  eeprom_read_string(SERVER_ADDR_OFFSET, server, strSize);

is used in another place too. And as it is actually a data that is all enough if loaded during setup, I tried to keep this at Setup location. But then after compiling, i got his part: " if (client.connect(server, 80)) " gave an error... it was not declred it is saying...

ideas??????

guys!! I'm still stuck in above message.. any ideas???