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.