Issues pertaining to the WiFi "Server" for the Nano RP2040

So I have some issues pertaining to the WiFiNINA_Generic Server example code, at least, a modified version of it. The main issues is that the Nano "crashes" when it gets to the point highlighted in the included code. I kind of need that line, so if anyone knows how to fix this, please tell me. I wo

#include <WiFiNINA_Generic.h>
#include <Arduino_LSM6DS3.h>
#include "defines.h"
#include <Servo.h>
#include <Wire.h>
#include <SPI.h>

static const double AVERAGE_GRAVITY_ERROR = 0.05;
static const byte BUZZER_PIN = 4;
static const byte SERVO_PIN = 5;
static char ssid[] = ""; // your network SSID (name)
static char pass[] = ""; // your network password (use for WPA, or use as key for WEP), length must be 8+

double directionVector[3];//x,y,z
double averageAcceleration[3];//x,y,z
double averageAngle[3];//x,y,z
double accelerationMagnitude;
double verticalDisplacement;
double velocityAfterInitalAcceleration;
double netVelocity;
float gyro[3];
float accel[3];
//0: while timer
//1: total time from the start to last average
//2: total time from the start to this average
int timer[3];
int keyIndex = 0;                 // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
//The times when the rocket starts(ignition), apogee, ends(impact), and Acceleration Ends
int times[4];
bool isLanded = false;
bool chuteDeployed = false;
boolean currentLineIsBlank;
char c;

WiFiServer server(80);

WiFiClient client;

Servo releaseServo;

void setup() {
  Serial.begin(115200);
  pinMode(BUZZER_PIN, OUTPUT);
  releaseServo.attach(SERVO_PIN);
  releaseServo.write(0);//"Zeros" the servo at the closed position
  
  //IMU.begin();//Starts the IMU
  //Wait until connected
  while(status != WL_CONNECTED){
    Serial.print(F("Attempting to connect to SSID: "));
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
  }
  
  // print the SSID of the network you're attached to:
  Serial.print(F("SSID: "));
  Serial.println(WiFi.SSID());
  Serial.print(F("IP Address: "));
  Serial.println(WiFi.localIP());

  server.begin();
  client = server.available();
  //Buzzes that the Nano is powered
  analogWrite(BUZZER_PIN, 150);
  delay(100);
  analogWrite(BUZZER_PIN,  0 );
  while(!client){
    client = server.available();
    Serial.println("Waiting for a client!");
    delay(100);
  }
  Serial.println("Client Connected!");
  client = server.available();
  if(client){
    // an http request ends with a blank line
    currentLineIsBlank = true;
    while(client.connected()){
      startClient();
      client.println("Getting Data");
      endClient();
    }
  }
  delay(1000);
  
  int waitTime = 2000;
  timer[0] = millis();
  
  while(timer[0] - millis() <= waitTime){
    IMU.readGyroscope(gyro[0], gyro[1], gyro[2]);
    IMU.readAcceleration(accel[0], accel[1], accel[2]);
    Serial.println(gyro[0]);
    Serial.println(gyro[1]);
    Serial.println(gyro[2]);
    Serial.println(accel[0]);
    Serial.println(accel[1]);
    Serial.println(accel[2]);
    
    timer[2] = millis() - timer[0];//Total time since start
    
    for(byte i = 0; i < 3; i++){
      //Gravity is fluctuating if it swings above 1.05 and bellow 0.95. It adds 50 milliseconds to the timer when this occurs
      if((averageAcceleration[i] + AVERAGE_GRAVITY_ERROR >= 1.05 || averageAcceleration[i] - AVERAGE_GRAVITY_ERROR <= 0.95) && timer[0] - millis() >= 1750) waitTime += 50;
      averageAcceleration[i] = (averageAcceleration[i] * timer[1] + accel[i]) / timer[2];
      directionVector[i] = (directionVector[i] * timer[1] + cos(gyro[i]) * accel[i]) / timer[2];
      averageAngle[i] = (averageAngle[i] * timer[1] + gyro[i]) / timer[2];
    }
    //After 20 seconds, yells and terminates
    if(waitTime >= 20000){
      digitalWrite(BUZZER_PIN, HIGH);
      delay(500);
      digitalWrite(BUZZER_PIN, LOW);
      client.println("Unable to get good vectors!/nRestart Please");
      while(true);//Does not proceed with anything else
    }
    timer[1] = millis() - timer[0];
    client = server.available();
    if(client){
      // an http request ends with a blank line
      currentLineIsBlank = true;
      while(client.connected()){
        startClient();
        updateClient();
        endClient();
      }
    }
  }
  //Signify that the data collection is done
  digitalWrite(BUZZER_PIN, HIGH);
  delay(50);
  digitalWrite(BUZZER_PIN, LOW);
}

bool isInFlight = false;

void loop() {
  client = server.available();
  if(client){
    // an http request ends with a blank line
    currentLineIsBlank = true;
    while(client.connected()){
      Serial.println("In Loop, with a client");
      startClient();
      updateClient();
      endClient();
    }
  }
  //Gravity should be positive, so when ANY of them go negative
  IMU.readAcceleration(accel[0], accel[1], accel[2]);
  if(isInFlight == false){
    for(byte i = 0; i < 3; i++){
      //In Flight if any go negative beyond a certain value
      if(accel[i] <= -0.1){
        isInFlight = true;
        //Ignition
        if(times[0] == 0) times[0] = millis();
      }
    }
  }

  if(isInFlight == true){
    timer[0] = millis();
    IMU.readAcceleration(accel[0], accel[1], accel[2]);
    //c = square root (x^2 + y^2)
    double c = pow(0.5, (pow(2, accel[0]) + pow(2, accel[1])));
    //v = square root (z^2 + c^2)
    accelerationMagnitude = pow(0.5, (pow(2, accel[2]) + pow(2, c) ) );

    if(!chuteDeployed){
      timer[1] = millis();
      //Takes the magnitude of acceleration, times the difference in time 
      netVelocity = accelerationMagnitude * (timer[1] - timer[0]);
      if(!(accelerationMagnitude + AVERAGE_GRAVITY_ERROR >= 1.0) || !(accelerationMagnitude - AVERAGE_GRAVITY_ERROR <= 0.95)){
        velocityAfterInitalAcceleration += accelerationMagnitude * (timer[1] - timer[0]);
      }
      verticalDisplacement += netVelocity * (timer[1] - timer[0]);
    }
    //And now we have the current magnitude of acceleration
    //If it's within an error, then we are now falling only under gravity
    if((netVelocity + AVERAGE_GRAVITY_ERROR >= -0.05 || netVelocity - AVERAGE_GRAVITY_ERROR <= 0.05) && !chuteDeployed){
      times[1] = millis();//Updates the apogee time
      chuteDeployed = true;
    }
  }
  //Deploy the chute 1 second after apogee
  if(chuteDeployed){
    status = WiFi.begin(ssid, pass);
    //Tries it again
    if(status != WL_CONNECTED){
      status = WiFi.begin(ssid, pass);
      Serial.println("Can't connect!");
    }
    if(times[1] + 1000 >= millis()){
      releaseServo.write(180);
      client = server.available();
      if(client){
        // an http request ends with a blank line
        currentLineIsBlank = true;
        while(client.connected()){
          startClient();
          updateClient();
          client.println(times[0]);
          client.println(times[1]);
          client.println(times[2]);
          endClient();
        }
      }
    }
  }
}

void startClient(){
  if(client.available()){

    
    //It hangs here!
    c = client.read();
    Serial.write(c);

    
    // if you've gotten to the end of the line (received a newline
    // character) and the line is blank, the http request has ended,
    // so you can send a reply
    if(c == '\n' && currentLineIsBlank){
      //Basic setup info
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("Connection: close");  // the connection will be closed after completion of the response
      client.println("Refresh: 1");  // refresh the page automatically every 1 sec
      client.println();
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
    }
  }
}

void endClient(){
  if(client){
    client.println("</html>");
    if(c == '\n'){
      //Starting a new line
      currentLineIsBlank = true;
    }else if(c != '\r'){
      //Character is on the current line
      currentLineIsBlank = false;
    }
  }
  // give the web browser time to receive the data
  delay(1);
  // close the connection:
  client.stop();
}
void updateClient(){  
  //Putting the data on the site
//This stuff isn't nesscasary, just putting things. No real stuff going on here
.
.
.
}

I don't see any "highlighted" line in your code.

So it is in the 'startClient' method, near the bottom.

I think your issue might be the last two lines. I think they should be part of the if clause. Think about what happens when if(client) returns false?

1 Like

Yeah, that was the issue. Looking at the example, the client.end() is within the if(client){}

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.