Dear,
I tried all the tips posted above, but none of them worked for me. I put my code below to see if anyone has a suggestion. The problem just seems damn 'float'. Since I can not turn it into string, it does not work my insertion into the database using the library mysql.h.
#include "SPI.h"
#include "Ethernet.h"
#include "sha1.h"
#include "mysql.h"
// Network conf.
byte mac_addr[] = { 0x54, 0x55, 0x4F, 0x11, 0x0F, 0x2C };
EthernetClient client;
//Server address
IPAddress server_addr(1, 1, 1, 1);
//Data variables
int pinosensor = 0;
int valorlido = 0;
float temperatura = 0;
int porta = 8;
//Configuracao da conexao com o arduino
Connector my_conn; // The Connector/Arduino reference
char user[] = "arduino";
char password[] = "arduino";
void read_dados(){
pinMode(porta, INPUT);
//Medidor de Temperatura
valorlido = analogRead(pinosensor);
temperatura = (valorlido * 0.00488);
temperatura = temperatura * 100;
Serial.print("Temperatura atual: ");
Serial.println(temperatura);
Serial.print("\n");
}
char *dtostrf (double val, signed char width, unsigned char prec, char *sout) {
char fmt[20];
sprintf(fmt, "%%%d.%df", width, prec);
sprintf(sout, fmt, val);
return sout;
}
void insert_data(){
//Don't work - this is a test from toppic 103935.0
String strLab = "Temperatura = ";
char tmp[10];
dtostrf(temperatura,1,2,tmp);
String strOut = strLab + tmp;
Serial.println(strOut);
//the result of this serial.println is Temperatura = ?
//Don't work - this is a test from toppic 103935.0
char voltageMsg[25];
sprintf(voltageMsg, "Temperatura : %f", temperatura);
Serial.println(voltageMsg);
//the result of this serial.println is Temperatura : ?
//Just function 'now()' is inputed correctly in Database
char buf[256];
sprintf(buf, "INSERT INTO datacenter.tb_teste (data, temp) VALUES (now(), %d)", temperatura);
my_conn.cmd_query(buf);
}
void setup() {
// start the serial library:
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac_addr) == 0) {
Serial.println("DHCP Failed");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// print your local IP address:
Serial.println(Ethernet.localIP());
delay(1000);
//database connection
Serial.println("Connecting...");
if (my_conn.mysql_connect(server_addr, 3306, user, password)){
delay(500);
Serial.println("Connected!");
}else{
Serial.println("Connection failed!");
}
}
void loop() {
read_dados();
insert_data();
delay(10000);
}//end