2016-06-29 19 views
-3

我想發送值並將值插入到數據庫中,但即時獲取空值。如何發送URL中的浮點值,即時通訊發送浮動值,但獲得空值

float PHValue = Value/10; 
    float DOValue= 12.22; 
    gprsSerial.println("AT+HTTPPARA=\"URL\",\"http://itempurl.com/smartpond/AddTemprature?WaterTemperature=""+celsius+""&PHValue=""+PHValue+""&DOValue=""+DOValue+""&currentTime=06-30-2016\""); 
+0

問:當你添加一個浮點數和一個字符串時,你會得到什麼? A.編譯器錯誤! – stark

+0

即時通訊沒有收到編譯錯誤。即時通訊在數據庫中獲得空值,因爲它將變量名稱而不是數據。問題在雙引號中。我需要帶動態參數的API。 –

+0

使用snprintf將值打印到字符串中(或者如果您想用C++方式執行,請使用stringstream)。 – stark

回答

0

可以單獨使用print()像派:

int n_decimals = 3; 
float PHValue = Value/10; 
float DOValue= 12.22; 

gprsSerial.print("AT+HTTPPARA=\"URL\",\"http://itempurl.com/smartpond/AddTemprature?WaterTemperature="); 
gprsSerial.print(celsius, n_decimals); 
gprsSerial.print("&PHValue="); 
gprsSerial.print(PHValue, n_decimals); 
gprsSerial.print("&DOValue="); 
gprsSerial.print(DOValue, n_decimals); 
gprsSerial.println("&currentTime=06-30-2016\""); 

隨着n_decimals要打印的小數位的數量。

或者像@stark在評論中說,你可以使用snprintf生成您的AT命令:

char command[256]; 
float PHValue = Value/10; 
float DOValue= 12.22; 

snprintf(command, sizeof(command), "AT+HTTPPARA=\"URL\",\"" 
      "http://itempurl.com/smartpond/AddTemprature?" 
      "WaterTemperature=%f&PHValue=%f&DOValue=%f" 
      "&currentTime=06-30-2016\"", celsius, PHValue, DOValue); 
gprsSerial.println(command); 

注:我不知道,如果你想在URL中值引號之間去的,所以我沒有離開他們。