Coding Review Help

Hello all,

I have mix matched some coding and even hired a freelancer to help with the final program but have lost contact with the freelancer and am way outside my level of knowledge now. I am hoping to trouble shot the code I have.

A little background, I have a Nano 33 IOT, it is attached to an HC-SRO4 ultrasonic sensor. I am trying to send the accelerometer and ultrasonic data wirelessly to my computer. The code also has a few formulas in it along the way. Essentially this device will attach to a barbell, when the barbell is lifted it will measure distance traveled, velocity, time, power and a couple other simple math calculations.

I asked the freelancer to have the code start when I put a number into the serial input, this will be the weight being lifted. Which will also be used in some of the calculations down the line.

As of now I believe I am able to connect to my wifi, and get an IP address, I am supposed to put this in a web browser to see the data. The web page never loads. When I type something into the serial input I get a few random Weights spit out and nothing else more.

The code is below, along with some picture references of the serial monitor.

Any input is appreciated.

#include <SPI.h>
#include <WiFiNINA.h>
#include <Arduino_LSM6DS3.h>

 
#define trigPin 10
#define echoPin 12
#define SECRET_SSID "****"
#define SECRET_PASS "****"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;                 // your network key index number (needed only for WEP)
float x, y, z;
int input = 0;
String input1 = "";
int status = WL_IDLE_STATUS;
boolean isInput1 = true;
  boolean isInput2 = false; 
    boolean isInputDone = false;
      float G = 0;
       float state_G = 0;
        float max_G = 0;
         float state_dist = 0;
          float max_dist = 0;
           boolean time1 = false;
            boolean get_data = true;
             boolean get_data_done = false;
             boolean cal_data = false;
              int weight = 0;
               float F = 0;
                float previousDist = 0;
                 float main_dist = 0;
                  int main_time = 0;
                   float V = 0;
                    float W = 0;
                     float P = 0;
                      float N = 0;
                   
                 
                       float distance = 0;
                       long duration = 0;
                
       
      static const unsigned long REFRESH_INTERVAL = 1000; // ms
      static unsigned long lastRefreshTime = 0;
WiFiServer server(80);

void setup() {
  Serial.begin(9600);      // initialize serial communication
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);       // set the LED pin mode
   get_dist();
    previousDist = distance;
  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status
}






void loop() {
    if(isInput1){ 
     while (Serial.available()) {
      input += (char)Serial.read();
       delay(5);
        Serial.print("Weight = ");
         Serial.print(input);
          Serial.println(); 
           weight = input;
           if(input >= 1){
              
             isInput2 = true;
             isInput1 = false;
           }
    } 
    } 

     if(isInput2){ 
     while (Serial.available()) {
      input1 += (char)Serial.read();
       delay(5);
         if(input1 == "ok"){
           isInputDone = true;
           isInput2 = false;
           
         }
    } 
    }

     if(isInputDone){
         if(get_data){
         get_accel();
          if(x >= 1){
            time1 = true;
            get_accel();
             get_dist();
              state_G = x*9.80;
               state_dist = distance;
               
               if(state_dist > max_dist){
                  max_dist = state_dist;                 
               }

               if(state_G > max_G){
                  max_G = state_G;
               }


               

               

                if(time1 == true){
                   if(millis() - lastRefreshTime >= REFRESH_INTERVAL){
                          lastRefreshTime += REFRESH_INTERVAL;
                 }
                }
              
              if(x > 1){
                 time1 = false;
                 get_data_done = true;
                 get_data = false;
                  
              }

              
             
               
              
          }
         }

         if(get_data_done){
            F = weight*state_G;
             main_dist = max_dist - previousDist;
              main_time = lastRefreshTime/1000;
               N = weight*9.806;
                
               V = main_dist/main_time;
               
               W = F*main_dist;

               P = W*main_dist;

               upload_data();
             
         }

        
     }

    
     
   

    
  
}



void upload_data(){ 
    WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

             client.println("<p>Weight </p>");
              client.println(weight);
               client.println();
                client.println("<p>Newtons </p>");
                 client.println(N);
                  client.println();
                   client.println("<p>Velocity </p>");
                    client.println(V);
                     client.println();
                      client.println("<p>Work </p>");
                       client.println(W);
                        client.println();
                         client.println("<p>Power </p>");
                          client.println(P);
                           client.println();
                         

             
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(9, HIGH);               // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(9, LOW);                // GET /L turns the LED off
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}



void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

void get_accel(){
    
    if (IMU.accelerationAvailable()) {
    IMU.readAcceleration(x, y, z);
    }
    
}

void get_dist(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * .000343; 
  
}

What’s weight type? What’s input type?

if(input >= 1){
Same here, what’s input’s type? Does that work?

Are you compiling with all warnings activated?

(Please make sure you indent the code in the IDE before copying, that’s done by pressing ctrlT on a PC or cmdT on a Mac)

Hello J-M-L,

Sorry if this is not the right answer to the questions you are looking for but the weight and input are both int I believe.

I am unsure about the warnings as I have not heard of those those before, I will look at those now.

I was able to address the wifi portion and am able to load the website for the display of where the calculations for weight power etc are supposed to be. So that's a big step. It seems to me like there are some things out of order though. I am unable to get any of the actual calculations to show up on the http://

When I put in a weight it prints 4 weights in the serial monitor, not the website, and none of them are 100, and none of the other calculations ever show up.

I think I need to break it down and just get the sensors working properly at this point then re work it back into displaying on the Client Print portion of the code

I more than likely cannot help you with your problems, but if a code change was involved, please post the updated code.

Great, thank you sterretje.

The updated code is below.
As well as a picture of the website where data will be sent, and what I am getting after I input 100, followed by ok

#include <SPI.h>
#include <WiFiNINA.h>
#include <Arduino_LSM6DS3
![Screen Shot 2021-07-30 at 3.05.14 PM|153x500](upload://eSvNzhYyyhNwT7IJkDciPgjJcAI.png)
![Screen Shot 2021-07-30 at 3.08.16 PM|690x187](upload://mNAnpoUDDBuVjGXzb4J5CAPD4JR.png)
.h>
 
#define trigPin 10
#define echoPin 12
#define SECRET_SSID "****"
#define SECRET_PASS "****"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;                 // your network key index number (needed only for WEP)
float x, y, z;
int input = 0;
String input1 = "";
int status = WL_IDLE_STATUS;
boolean isInput1 = true;
  boolean isInput2 = false; 
    boolean isInputDone = false;
      float G = 0;
       float state_G = 0;
        float max_G = 0;
         float state_dist = 0;
          float max_dist = 0;
           boolean time1 = false;
            boolean get_data = true;
             boolean get_data_done = false;
             boolean cal_data = false;
              int weight = 0;
               float F = 0;
                float previousDist = 0;
                 float main_dist = 0;
                  int main_time = 0;
                   float V = 0;
                    float W = 0;
                     float P = 0;
                      float N = 0;
                   
                 
                       float distance = 0;
                       long duration = 0;
                
       
      static const unsigned long REFRESH_INTERVAL = 1000; // ms
      static unsigned long lastRefreshTime = 0;
WiFiServer server(80);

void setup() {
  Serial.begin(9600);      // initialize serial communication
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);       // set the LED pin mode
   get_dist();
    previousDist = distance;
  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status
}






void loop() {

    upload_data();
   
    if(isInput1){ 
     while (Serial.available()) {
      input += (char)Serial.read();
       delay(5);
        
           
           if(input >= 1){
            weight = input;
            
              Serial.print("Weight = ");
         Serial.print(weight);
          Serial.println(); 
             isInput2 = true;
             isInput1 = false;
             
           }
    } 
    } 

     if(isInput2){ 
     while (Serial.available()) {
      input1 += (char)Serial.read();
       delay(5);
         if(input1 == "ok"){
          Serial.println("inputDone");
           isInputDone = true;
           isInput2 = false;
           
         }
    } 
    }

     if(isInputDone){
         if(get_data){
         get_accel();
          if(x >= 1){
            time1 = true;
            get_accel();
             get_dist();
              state_G = x*9.80;
               state_dist = distance;
               
               if(state_dist > max_dist){
                  max_dist = state_dist;                 
               }

               if(state_G > max_G){
                  max_G = state_G;
               }


               

               

                if(time1 == true){
                   if(millis() - lastRefreshTime >= REFRESH_INTERVAL){
                          lastRefreshTime += REFRESH_INTERVAL;
                 }
                }
              
              if(x > 1){
                 time1 = false;
                 get_data_done = true;
                 get_data = false;
                  
              }

              
             
               
              
          }
         }

         if(get_data_done){
            F = weight*state_G;
             main_dist = max_dist - previousDist;
              main_time = lastRefreshTime/1000;
               N = weight*9.806;
                
               V = main_dist/main_time;
               
               W = F*main_dist;

               P = W*main_dist;

               
             
         }

        
     }

    
    
   

    
  
}



void upload_data(){ 
    WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

             client.println("<p>Weight </p>");
              client.println(weight);
               client.println();
                client.println("<p>Newtons </p>");
                 client.println(N);
                  client.println();
                   client.println("<p>Velocity </p>");
                    client.println(V);
                     client.println();
                      client.println("<p>Work </p>");
                       client.println(W);
                        client.println();
                         client.println("<p>Power </p>");
                          client.println(P);
                           client.println();
                         

             
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}



void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

void get_accel(){
    
    if (IMU.accelerationAvailable()) {
    IMU.readAcceleration(x, y, z);
    }
    
}

void get_dist(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * .000343; 
  
}


Check again

Is the weight a string?

I think input is… isn’t it ?

input is listed as an int
input1 is listed as a string and

weight = input so I thought it would be an int

Right - I need my glasses :slight_smile: it was input1

The way you build input reading Serial is weird

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