我从arduino获取数据并通过ethienet存储在数据库中。但是当我从arduino获得价值ambientemp时。当我在串行监视器上测试时,它显示正确的值。但是当我使用sprintf()时,我得到-3275或0值,这是不正确的值。这是我在草图中的部分代码,请帮助......这是做他的项目的人。 Sketch serial montior的结果是:ambientTemp 23.55然后GET /getData/temp.php?t=-3278我复制了他的一些人:getting data and stored it into mysql
void getData() {
double ambientTemp=23.55; //to make it easy I assign ambientTemp a value.
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 10000;
char strURL[70];
EthernetClient client;
// If there's a successful connection, send the HTTP POST request
currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (client.connect(server, 80)) {
Serial.println("get data connecting...");
//client.println("GET /getData/temp.php?t=%d,temp HTTP/1.1");
// delay(10000);
Serial.println("ambientTemp");
Serial.println(ambientTemp);
sprintf(strURL,"GET /getData/temp.php?t=%d",ambientTemp);
delay(50000);
client.print(strURL);
client.println();
// client.print(strURL);
Serial.print(strURL);
}
else {
// If you couldn't make a connection:
Serial.println("Connection failed");
Serial.println("Disconnecting.");
client.stop();
}
}
}
答案 0 :(得分:0)
您需要阅读C格式规范。 “%d”表示获取相应的变量(在您的情况下为ambientTemp)并将其解释为整数。那么运行时代码正在做的是查看组成double的字节,并解释整数中的前2个。不是你想要的......使用“%f”作为格式说明符......