Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teensy 4.1 crashing when I have poor ethernet connection #37

Open
Erikvvats opened this issue Sep 2, 2024 · 0 comments
Open

Teensy 4.1 crashing when I have poor ethernet connection #37

Erikvvats opened this issue Sep 2, 2024 · 0 comments

Comments

@Erikvvats
Copy link

Hello,

Please note, I am not sure if I should post this at FNET library or here.

I am using this library to send API Posts to and API endpoint from a Teensy through a drone.

When the drone is far away and there is poor connection, the Teensy 4.1 will crash after a while.

Here is a code snippet for sending API POST:


void NetworkHandler::handle(const Sensor& sensorRef) {

  if (measurementCount < MAX_BUFFER_SIZE) {
    measurementBuffer[measurementCount++] = sensorRef;
  } else {
    // If the buffer is full, roll over and overwrite the oldest measurement
    for (int i = 0; i < MAX_BUFFER_SIZE - 1; i++) {
      measurementBuffer[i] = measurementBuffer[i + 1];
    }
    measurementBuffer[MAX_BUFFER_SIZE - 1] = sensorRef;
  }

  buildJSONDocument();

  for (int attempt = 0; attempt < MAX_RETRIES; attempt++) {
    if (client.connect(server, port)) {
      greenLight();
      Serial.println("Client connected");
      bool success = sendAPIPost();
      Serial.print("API post sent, free ram: "); Serial.println(freeram());
      
      if(client.connected()){
        client.flush();
      }  
      client.stop();  // Close connection after reading the response

      if (success) {
        // Successful response, reset measurementCount  , and exit loop
        measurementCount = 0; 
        break;  
      }
    }
    else {
      Serial.println("Client not connected");
      redLight();
    }

  }

jsonDocument.clear();

  jsonStr[0] = '\0';  // Clear the JSON string
}


bool NetworkHandler::sendAPIPost() {
  
  //Write API POST
  char httpRequest[2048];
  int length = snprintf(httpRequest, sizeof(httpRequest),
    "POST /api/Measurement HTTP/1.1\r\n"
    "User-Agent: python-requests/2.22.0\r\n"
    "Accept-Encoding: gzip, deflate\r\n"
    "Accept: */*\r\n"
    "Connection: keep-alive\r\n"
    "Host: %s:%d\r\n"
    "Content-Type: application/json\r\n"
    "Content-Length: %d\r\n\r\n"
    "%s",
    server, port, strlen(jsonStr), jsonStr);
  

  // Check if snprintf succeeded
  if (length < 0 || length >= sizeof(httpRequest)) {
    Serial.println("Error: HTTP request string is too large.");
    return false;
  }

  //return false;
  // Send the HTTP request headers
  
  // Split data into chunks of 64 bytes
  const int chunkSize = 64;  
  if (client.connected()){
    for (int i = 0; i < length; i += chunkSize) {
      int sizeToWrite = min(chunkSize, length - i);
      client.write((const uint8_t *)&httpRequest[i], sizeToWrite);
      delay(5);
    }
  }else {
    Serial.println("Client not connected before write.");
    return false;
  }
  
  
  //client.write((const uint8_t *)httpRequest, length);


  httpRequest[0] = '\0';  // Clear htttpRequest 

  
  // Wait for SERVER RESPONSE TIMEOUT
  unsigned long startTime = millis();
  while (!client.available()) {
    //Serial.println("Client not available");
    if (millis() - startTime > SERVER_RESPONSE_TIMEOUT) {
      client.flush();
      client.stop();
      return false; // Timeout
    }
  }

  char response[RESPONSE_BUFFER_SIZE];
  int responseIndex = 0;

  while (client.available()) {
    char c = client.read();
    // Ensure we don't exceed buffer size
    if(responseIndex < RESPONSE_BUFFER_SIZE - 1) { 
      response[responseIndex++] = c;
    }
  }
  // Null-terminate the char array
  response[responseIndex] = '\0'; 
  Serial.println(response);

  return (strstr(response, "HTTP/1.1 200 OK") != NULL);  
}

  // JSON document
  StaticJsonDocument<2048> jsonDocument;
  char jsonStr[2048];

I am not using any dynamic memory variables.

Here is a crash report:
CrashReport:
A problem occurred at (system time) 13:32:9
Code was executing from address 0x42D4
CFSR: 82
(DACCVIOL) Data Access Violation
(MMARVALID) Accessed Address: 0x4 (nullptr)
Check code at 0x42D4 - very likely a bug!
Run "addr2line -e mysketch.ino.elf 0x42D4" for filename & line number.
Temperature inside the chip was 65.78 °C
Startup CPU clock speed is 600MHz

And when I am using addr2line I get the following:
fnet_tcp.c:2367

/* Delete the repeated segment.*/
buf = cb->tcpcb_rcvchain;
cb->tcpcb_rcvchain = cb->tcpcb_rcvchain->next_chain;

/* Set the new size of the temporary buffer.*/
cb->tcpcb_count -= cb->tcpcb_rcvchain->total_length;
_fnet_netbuf_free_chain(buf);

Everything works perfect for long periods of time, <2hours when the connection is okay, however only a few minutes when the connection is poor.

I hope to hear from anybody and all help is very much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant