Problem in Posting Float Data to MySQL Database

Hello all.

I am working on a project with Arduino Uno & Arduino Ethernet Shield. I want to post some float data to a MySQL database, which is eventually be shown on a website.

By the mercy of God I have been able to do that, but not completely.

You see, all my data contains 4 digits after the decimal. But, the MySQL server is receiving only two digits after the decimal, and the rest two are always zero.

Most probably the added screenshot will be helpful for better understanding. The data shown at the serial monitor are the data I want to show on the website.

I am also adding the Arduino code.

// Including libraries
#include <SPI.h>
#include <Ethernet.h>

// MAC address for controller
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xFE, 0x40 };

// Initializing the Ethernet client
EthernetClient client;

float vr, ir, v, a, b, i, p = 0;  
String data;


void setup() {
  
  // Opening serial communications
  Serial.begin(9600);

  // Starting the Ethernet connection
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
  }

  // Giving the Ethernet shield 5 seconds to initialize
  delay(5000);
 
}

void loop(){

  // Measuring voltage
  vr = (float) analogRead(A1);
  v = (vr*5)/1024;
  
  // Measuring current
  ir = (float) analogRead(A2);
  a = (ir*5)/1024;
  b = a/10;
  i = b/1000;

  // Measuring power
  p = v * i;

  // Printing on Serial monitor
  Serial.println("Readings:");
  Serial.print("Votage = ");
  Serial.println(v, 4);
  Serial.print("Current = ");
  Serial.println(i, 4);
  Serial.print("Power = ");
  Serial.println(p, 4);

  // Transforming to String
  data = "volt=";
  data.concat(v);
  data.concat("&curt=");
  data.concat(i);
  data.concat("&powr=");
  data.concat(p);
  
  // Connecting to server 
  Serial.println("Connecting to the server...");
  if (client.connect("livedata.secondaryfunding.net", 80)) { 
    if (client.connected()) { 
      Serial.println("Connected! Uploading to the server....");
    } 
    // Making a HTTP request 
    client.println("POST /add.php HTTP/1.1"); 
    client.println("Host: livedata.secondaryfunding.net"); 
    client.println("Content-Type: application/x-www-form-urlencoded"); 
    client.print("Content-Length: "); 
    client.println(data.length()); 
    client.println(); 
    client.print(data); 
  }

   else {
      // If there is no connection to the server
      Serial.println("Connection failed!");
    }
  
    // Disconnecting from server after uploading data
    if (client.connected()) {
      Serial.println("Reading uploaded! Disconnecting from the server.....");
      Serial.println();
      client.stop();
    }
    
    // Repeating the whole process every 2 minutes
    delay(120000);
}

So, could someone please explain what wrong have I done here?

Many many thanks in advance. :smiley:

Do you know what

  data = "volt=";
  data.concat(v);
  data.concat("&curt=");
  data.concat(i);
  data.concat("&powr=");
  data.concat(p);

is doing?

Printing to Serial Monitor is not the same as this.

It is transforming the data into strings. I used the serial monitor to check whether the acquired data & the data showing on the website are the same.

Send the data variable to the serial monitor after you concat all the variables. Does it show all digits you want in the request string?

@SurferTim,

Thanks a lot for the suggestion. After concatenating the variables, they still show 4 digits after the decimal on the serial monitor. But the website can only show 2 digits after the decimal. Please check the attached screenshot.

Any other suggestions please?

Sami584: It is transforming the data into strings. I used the serial monitor to check whether the acquired data & the data showing on the website are the same.

What you are doing here is not the same as what you are doing with the Serial Monitor.

If you can't understand that, you won't fix your problem.

If the request has 4 digits, and the webpage shows 2, then it appears you have a problem on the server/PHP side of this.

SurferTim: If the request has 4 digits, and the webpage shows 2, then it appears you have a problem on the server/PHP side of this.

He doesn't know that the request has 4 digits. He is forcing it to be 4 digits in the Serial Monitor part of his sketch.

  Serial.println("Readings:");
  Serial.print("Votage = ");
  Serial.println(v, 4);
  Serial.print("Current = ");
  Serial.println(i, 4);
  Serial.print("Power = ");
  Serial.println(p, 4);

He doesn't know that the request has 4 digits. He is forcing it to be 4 digits in the Serial Monitor part of his sketch.

Correct. The automatic conversion of a float to a String results in two decimal places. Directly invoking the String constructor can be done, and can allow controlling the number of decimal places in the resulting String. But, at the cost of wasting even more resources.

PaulS: Correct. The automatic conversion of a float to a String results in two decimal places. Directly invoking the String constructor can be done, and can allow controlling the number of decimal places in the resulting String. But, at the cost of wasting even more resources.

He doesn't care at the moment about wasting resources.

he doesn't understand that his two snippets of code are not the same.