如何在“获取"请求中将时间戳从RTC发送到网站?使用arduino [英] how do i send timestamp from RTC to website in a Get request? using arduino

查看:240
本文介绍了如何在“获取"请求中将时间戳从RTC发送到网站?使用arduino的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将数据从arduino发送到我的ASP.Net网站,并且一直成功,直到我尝试将时间戳记作为GET请求中的变量发送出去为止.我认为它与分隔值的正斜杠有关,但是当我发送诸如-"之类的不同符号时,我得到的结果相同(未保存任何数据)

Hi i have been trying to send data from my arduino to my ASP.Net website and have been successful until i try to send a timestamp as a variable in the GET request. I have think it has somthing to do with the forward slash that separates the values but when i send a diffrent symbol like a "-" i get the same result (no data saved)

对不起,它不是正斜杠!这是因为asp.net预期:01/01/01 01:01:01并且正在发送1/1/1 1:1:1.所以我需要弄清楚如果需要的话如何以零开头发送它

sorry its not the forward slash! it is because asp.net expects: 01/01/01 01:01:01 and am sending 1/1/1 1:1:1. so i need to figure out how send it with the zero in front if needed

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

my code so far (the sending part)

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
  } 

任何帮助或指向正确方向的观点都将很好

any help or a point in the right direction would be great

推荐答案

只需URL即可编码整个日期:

Just URL encode the whole date:

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

Helper方法:

/**
 * 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;
}

这篇关于如何在“获取"请求中将时间戳从RTC发送到网站?使用arduino的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆