How to Send Float/Double Data upto 4 Decimal Places Using Ethernet Shield?

n my project, I’m trying to send some float data to a MYSQL server using Arduino Uno & Arduino Ethernet Shield

According to the nature of Arduino, it is sending the data with upto 2 digits after the decimal. But I need to send data with upto 4 digits after the decimal.

Here is my full 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;

  // Transforming to String
  data = "volt=";
  data.concat(v);
  data.concat("&curt=");
  data.concat(i);
  data.concat("&powr=");
  data.concat(p);

  // 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);

  // 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);
}

Could anyone please check the code, and inform me what do I need to correct?

Thanks in advance.

This is what I use: dtostrf

Could anyone please check the code, and inform me what do I need to correct?

You need to quit using Strings. The automatic conversion of a float/double to a String is limited to 2 decimal places.

Of course, you could piss away even more resources creating more Strings, where you could control the number of decimal places. But, why would you. It is so simple to do everything you are doing with using any Strings.

Sami584:
n my project, I’m trying to send some float data to a MYSQL server using Arduino Uno & Arduino Ethernet Shield

According to the nature of Arduino, it is sending the data with upto 2 digits after the decimal. But I need to send data with upto 4 digits after the decimal.

Here is my full 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;

// Transforming to String
 data = “volt=”;
 data.concat(v);
 data.concat("&curt=");
 data.concat(i);
 data.concat("&powr=");
 data.concat(p);

// 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);

// 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);
}




Could anyone please check the code, and inform me what do I need to correct?

Thanks in advance.

You should have continued the discussion on your original thread. http://forum.arduino.cc/index.php?topic=395093.0 because it is relevant to the problem you are having.

There is absolutely NO reason to start yet another thread.

:frowning:

Thanks a lot SurferTim. :slight_smile:

I have made a few changes in my code. Could you kindly check them?

// 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, fv, ir, fi, a, b, fp = 0;  
String data; 
char v[15], i[15], p[15]; ;


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);
  fv = (vr*5)/1024;
  dtostrf(fv, 7, 4, v);
  
  // Measuring current
  ir = (float) analogRead(A2);
  a = (ir*5)/1024;
  b = a/10;
  fi = b/1000;
  dtostrf(fi, 7, 4, i);

  // Measuring power
  fp = fv * fi;
  dtostrf(fp, 7, 4, p);

  // Transforming to String
  data = "volt=";
  data.concat(v);
  data.concat("&curt=");
  data.concat(i);
  data.concat("&powr=");
  data.concat(p);

  // Printing on Serial monitor
  Serial.println("Readings:");
  Serial.print("Votage = ");
  Serial.println(v);
  Serial.print("Current = ");
  Serial.println(i);
  Serial.print("Power = ");
  Serial.println(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);
}

That dtostrf should work good for you. It looks correct. How does it run? That is the true test.

SurferTim: That dtostrf should work good for you. It looks correct. How does it run? That is the true test.

Thanks a lot. By the mercy of God, I'm able to do what I was wishing for.

My sincere prayer that may God bless you with welfare both in this life & the hereafter.