Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Arduino可以使用RESTAPI方法将数据发送到Rails服务器吗?_Ruby On Rails_Rest_Api_Server_Arduino - Fatal编程技术网

Ruby on rails Arduino可以使用RESTAPI方法将数据发送到Rails服务器吗?

Ruby on rails Arduino可以使用RESTAPI方法将数据发送到Rails服务器吗?,ruby-on-rails,rest,api,server,arduino,Ruby On Rails,Rest,Api,Server,Arduino,我们是否可以使用API将使用以太网或wifi屏蔽的arduino数据发送到RAILS服务器?如果可能的话,您能告诉我哪个库以及如何使用它吗?是的,您可以通过以太网或wifi屏蔽从Arduino调用RESTful API。我更喜欢使用标准库,例如 以下是一个示例(以太网)实现,供您参考: #include <Ethernet2.h> #include <EthernetClient.h> #include <EthernetUdp2.h> #include &l

我们是否可以使用API将使用以太网或wifi屏蔽的arduino数据发送到RAILS服务器?如果可能的话,您能告诉我哪个库以及如何使用它吗?

是的,您可以通过以太网或wifi屏蔽从Arduino调用RESTful API。我更喜欢使用标准库,例如

以下是一个示例(以太网)实现,供您参考:

#include <Ethernet2.h>
#include <EthernetClient.h>
#include <EthernetUdp2.h>
#include <util.h>

IPAddress _ip(192, 168, 1, 12); // Client (Arduino) IP address
byte _mac[] = {0x90, 0xA2, 0xDA, 0x11, 0x3C, 0x69}; // Arduino mac address
char _server[] = "192.168.1.10"; // Server IP address
int _port = 9200; // Server port number
EthernetClient _client;

void setup() {    
  Ethernet.begin(_mac, _ip);
  delay(1000);
  Serial.print("Local IP: ");
  Serial.println(Ethernet.localIP());

  if (_client.connect(_server, _port)) {
    Serial.println("SUCCESS: Connected to the server!");
  } else {
    Serial.println("ERROR: Connection failed to the server!");
    return;
  }
  delay(1000);
}

void loop() {       
  // JSON formatted data package including sample 
  // 'temperature', 'humidity', and 'timestamp' values
  String data = "{\"temperature\": " + String(temperature) + ", " +
    "\"humidity\": " + String(humidity) + ", " +
    "\"timestamp\": " + String(timestamp) + "}";

  String url = "/my-api/savedata"; // API url hosted on the server

  // Finally, make an API call: POST request
  _client.print("POST " + url + " HTTP/1.1 \r\n" +
    "Content-Type: application/json \r\n" +
    "Content-Length: " + data.length() + " \r\n" +
    "\r\n" + data);

  delay(500); // Give the network some time

  // Read all the lines of the reply from server and 
  // print them to Serial to validate your API call
  while (_client.available()) {
    String reply = _client.readStringUntil('\r');
    Serial.print(reply);
  }
  Serial.println();
}
#包括
#包括
#包括
#包括
ip地址_ip(192、168、1、12);//客户端(Arduino)IP地址
字节_mac[]={0x90,0xA2,0xDA,0x11,0x3C,0x69};//Arduino mac地址
char _server[]=“192.168.1.10”;//服务器IP地址
int _port=9200;//服务器端口号
以太网络客户端;
无效设置(){
以太网。开始(_mac,_ip);
延迟(1000);
串行打印(“本地IP:”);
Serial.println(Ethernet.localIP());
if(_client.connect(_server,_port)){
Serial.println(“成功:连接到服务器!”);
}否则{
Serial.println(“错误:连接到服务器失败!”);
返回;
}
延迟(1000);
}
void loop(){
//JSON格式的数据包,包括示例
//“温度”、“湿度”和“时间戳”值
字符串数据=“{\”温度\:“+字符串(温度)+”+
“湿度”:“+字符串(湿度)+”+
“\”时间戳\“:“+字符串(时间戳)+”}”;
String url=“/my api/savedata”;//服务器上托管的api url
//最后,进行一个API调用:POST请求
_client.print(“POST”+url+“HTTP/1.1\r\n”+
“内容类型:application/json\r\n”+
内容长度:“+data.Length()+”\r\n+
“\r\n”+数据);
延迟(500);//给网络一些时间
//阅读服务器和服务器的所有回复行
//将它们打印到串行以验证API调用
而(_client.available()){
字符串回复=_client.readStringUntil('\r');
连续打印(回复);
}
Serial.println();
}