如何在Get请求中将RTC的时间戳发送到网站?使用arduino

时间:2014-02-26 21:02:49

标签: asp.net timestamp arduino

您好我一直在尝试将数据从我的arduino发送到我的ASP.Net网站并且已经成功,直到我尝试在GET请求中发送时间戳作为变量。我认为它与分隔值的正斜杠有关,但是当我发送一个像“ - ”这样的不同符号时,我会得到相同的结果(没有保存数据)

编辑:对不起它不是正斜线!这是因为asp.net期望:01/01/01 01:01:01并且发送1/1/1 1:1:1。所以我需要弄清楚如果需要,如何将它与前面的零一起发送

到目前为止我的代码(发送部分)

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
  } 

任何帮助或正确方向上的一点都会很棒

2 个答案:

答案 0 :(得分:0)

您需要对/

等内容进行网址编码

%2F

因此27/2/2014将为27%2F2%2F2014

答案 1 :(得分:0)

只需对整个日期进行URL编码:

...
client.print("timestamp=");
client.print(URLEncode(String(now)));
...

帮助方法:

/**
 * URL Encode a string.
 * 
 * Based on http://www.icosaedro.it/apache/urlencode.c
 *
 */
String URLEncode(const char* msg)
{
    const char *hex = "0123456789abcdef";
    String encodedMsg = "";

    while (*msg!='\0'){
        if( ('a' <= *msg && *msg <= 'z')
                || ('A' <= *msg && *msg <= 'Z')
                || ('0' <= *msg && *msg <= '9') ) {
            encodedMsg += *msg;
        } else {
            encodedMsg += '%';
            encodedMsg += hex[*msg >> 4];
            encodedMsg += hex[*msg & 15];
        }
        msg++;
    }
    return encodedMsg;
}