0

我正在使用 Arduino Galileo 使用 REST API 在 plot.ly 上绘制图表。使用此代码

  client.println("POST /clientresp HTTP/1.1");
  //client.println("Host: 107.21.214.199");
  client.println("Host: plot.ly");
  client.println("User-Agent: Galileo/0.0.1");
  client.print("Content-Length: ");

  int content_length = 276 + username.length() + api_key.length() + temperaturesY.length() + timesX.length();
  client.println(content_length);
  client.println();
  client.print("version=2.3&origin=plot&platform=Galileo&un=");
  client.print(username);
  client.print("&key=");
  client.print(api_key);
  client.print("&args={\"x\":");
  client.print(timesX);
  client.print(",\"y\":");
  client.print(temperaturesY);
  client.print(",\"type\":\"scatter\",\"mode\":\"lines+markers\",\"visible\":true}&kwargs={\"filename\":\"galileo_temperature\",\"fileopt\":\"overwrite\",\"style\":{\"type\":\"line\"},\"layout\":{\"title\":\"Galileo CPU Temperature\"},\"world_readable\":true}");
  client.println();

我收到此错误:

{"url": "", "message": "", "warning": "", "filename": "", "error": "Missing required POST parameters: platform un key origin args kwargs"}

如何适应这个?

4

2 回答 2

1

您需要执行 POST 请求,例如:

POST /clientresp HTTP/1.1
Host: plotly.ly
User-Agent: Galileo/0.0.1
Content-Length: (needs to be calculated)

version=2.3&origin=plot&platform=Galileo&un=my_username&key=my_api_key&args={"x":[my_collected_x_values],"y":[my_collected_y_values],"type":"scatter","mode":"lines+markers","visible":true}&kwargs={"filename":"galileo_temperature","fileopt":"overwrite","style":{"type":"line"},"layout":{"title":"Galileo CPUTemperature"},"world_readable": true}

现在在你的草图中:

#include <Ethernet.h>

byte mac[] = { 0xDA, 0xAD, 0xBE, 0xEF, 0xFA, 0xEA };
EthernetClient client;

String username = "MY_USERNAME";
String api_key = "MY_API_KEY";

String temperaturesY = "[10,20,30,40,50]";
String timesX = "[0,1,2,3,4]";


void plotChart(){
  if (!client.connect("plot.ly", 80)) {
    Serial.println("... Couldn\'t connect to plotly's REST servers... ");
    return;
  }
  Serial.println("Connected to plotly's REST servers");
  Serial.println("Sending HTTP Post to plotly");

  client.println("POST /clientresp HTTP/1.1");
  client.println("Host: plot.ly");
  client.println("User-Agent: Galileo/0.0.1");
  client.print("Content-Length: ");

  int content_length = 276 + username.length() + api_key.length() + temperaturesY.length() + timesX.length();
  client.println(content_length);
  client.println();
  client.print("version=2.3&origin=plot&platform=Galileo&un=");
  client.print(username);
  client.print("&key=");
  client.print(api_key);
  client.print("&args={\"x\":");
  client.print(timesX);
  client.print(",\"y\":");
  client.print(temperaturesY);
  client.print(",\"type\":\"scatter\",\"mode\":\"lines+markers\",\"visible\":true}&kwargs={\"filename\":\"galileo_temperature\",\"fileopt\":\"overwrite\",\"style\":{\"type\":\"line\"},\"layout\":{\"title\":\"Galileo CPU Temperature\"},\"world_readable\":true}");
  client.println();
  Serial.println("Request sent. Waiting response..");
}

void setup() { 
  Serial.begin(9600);
  Ethernet.begin(mac);
  delay(5000);
  plotChart();
}

void loop() {
  //If Plotly response
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  delay(1000);
}

响应应该是这样的:

{"url": "http://plot.ly/~username/1", "message": "", "warning": "", "filename": ""}

如果您打开检索到的 url,您应该在那里找到您的图表

资料来源:Intel Galileo 物联网,第 3 章,“绘制 Galileo CPU 温度”

于 2015-12-22T13:14:08.333 回答
1

代码后:

client.println("POST /clientresp HTTP/1.1");
client.println("Host: plot.ly");
client.println("User-Agent: Galileo/0.0.1");

添加内容类型标头:

client.println("Content-Type: application/x-www-form-urlencoded");

有了它,我能够成功地绘制图表

于 2016-02-26T17:25:34.100 回答