2014-02-26 41 views
0

嗨,我一直試圖從我的arduino發送數據到我的ASP.Net網站,並已成功,直到我嘗試發送一個時間戳作爲GET請求中的變量。如何以正確格式從RTC發送時間戳到獲取請求中的網站?使用arduino

這是因爲asp.net預計:01/01/01 01:01:01併發送1/1/1 1:1:1。所以我需要弄清楚如果需要

我的Arduino代碼到目前爲止(發送部分)與零前面怎麼把它

void sendLightData() { 
    DateTime now = rtc.now(); 
    if (Ethernet.begin(mac) == 0) { 
    Serial.println("Failed to configure Ethernet using DHCP"); 
    // no point in carrying on, so do nothing forevermore: 
    // try to congifure using IP address instead of DHCP: 
    Ethernet.begin(mac, ip); 
    } 
    // give the Ethernet shield a second to initialize: 
    delay(1000); 
    Serial.println("connecting..."); 

    // if you get a connection, report back via serial: 
    if (client.connect(server, 80)) { 
    Serial.println("connected"); 
    // Make a HTTP request: 
    client.print("GET /LightData.aspx?uname="); 
    client.print(userName); 
    client.print("&pword="); 
    client.print(password); 
    client.print("&LStatus="); 
    client.print(lightStatus); 
    client.print("&LHeight=9&"); 
    client.print("timestamp="); 
    client.print(now.day(), DEC); 
    client.print("/"); 
    client.print(now.month(), DEC); 
    client.print("/"); 
    client.print(now.year(), DEC); 
    client.print("%20"); 
    client.print(now.hour(), DEC); 
    client.print(":"); 
    client.print(now.minute(), DEC); 
    client.print(":"); 
    client.print(now.second(), DEC); 
    client.println(" HTTP/1.1"); 
    client.println("Host: www.auntieagie.eu"); 
    client.println("Connection: close"); 
    client.println(); 
    // this works if entered into a browser (trying to replicate in arduino) http://auntieagie.eu/LightData.aspx?uname=test&pword=t&LStatus=1&LHeight=2&timestamp=21/02/2014%2001:01:01 
    } 

任何幫助或在正確的方向某點,將是巨大的

回答

0

嘗試這一個:

client.print(now.Date.ToString().Replace(" ", "%20")); 

這將返回一個DD/MM/YYYY HH:MM:SS經過編碼的字符替換的空間。

至少在ASP網你有HttpUtility,如果你有機會到這一點,你可以使用: System.Web.HttpUtility

client.print(HttpUtility.UrlEncode(now.Date.ToString())); 

日期和月份會返回一個int值,所以,包括零。

相關問題