【问题标题】:how do i send timestamp in correct format from RTC to website in a Get request? using arduino如何在 Get 请求中以正确格式将时间戳从 RTC 发送到网站?使用arduino
【发布时间】:2014-03-30 00:28:18
【问题描述】:

您好,我一直在尝试将数据从我的 arduino 发送到我的 ASP.Net 网站,并且一直成功,直到我尝试在 GET 请求中将时间戳作为变量发送。

这是因为 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
  } 

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

【问题讨论】:

    标签: c# asp.net timestamp arduino


    【解决方案1】:

    试试这个:

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

    这将返回一个 dd/mm/yyyy hh:mm:ss 用编码字符替换空格。

    至少在 Asp Net 中你有 HttpUtility,如果你可以访问它,你可以使用: System.Web.HttpUtility

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

    Day 和 Month 将返回一个 Int 值,因此不包括左零。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-04
      • 2011-08-09
      • 1970-01-01
      相关资源
      最近更新 更多