問題描述
我從 arduino 獲取數據并通過 ethienet 存儲在數據庫中.但是當我從 arduino 獲得價值環境時.當我在串行監視器上測試時,它顯示了正確的值.但是當我使用 sprintf() 時,我得到了 -3275 或 0 值,這不是正確的值.這是我在草圖中的部分代碼,請幫助...這是做他的項目的人.Sketch serial montior 的結果是:ambientTemp 23.55 然后 GET/getData/temp.php?t=-3278 我復制了他的一些:獲取數據并存入mysql
I am getting data from arduino and stored in database through ethienet. But when i got value ambientemp from arduino. it showed correct value when i tested on serial monitor. but when I use sprintf() I got -3275 or 0 value, which it is not correct value. Here is my partial code in sketch, please help...Here is guy doing his project. The result on Sketch serial montior is: ambientTemp 23.55 and then GET /getData/temp.php?t=-3278 I copied some of him: 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();
}
}
}
推薦答案
您需要閱讀 C 格式規范.%d" 表示采用相應的變量(在您的情況下為 ambientTemp)并將其解釋為整數.所以運行時代碼正在做的是查看組成雙精度的字節,并解釋其中的前 2 個整數.不是你想要的......使用%f"作為格式說明符......
You need to read up on C format specifications. "%d" means take the corresponsding variable (ambientTemp in your case) and interpret it as an integer. So what the runtime code is doing is looking at the bytes which make up your double, and interpreting the first 2 of those an in integer. Not what you want.... use "%f" as a format specifier...
這篇關于從arduino獲取數據并通過ethienet存儲在數據庫中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!