我正在尝试在arduino mkr1000上创建REST服务器。在搜索google时,我遇到了aRest库,该库可以处理我需要的大多数内容。
因此,我根据指南创建了一个示例草图。这是代码:
#include < SPI.h >
#include < WiFi101.h >
#include < aREST.h >
aREST rest = aREST();
int status = WL_IDLE_STATUS;
WiFiServer restServer(80);
char ssid[] = "user"; // not actual username
char pass[] = "pass"; // not actual password
int clapMode(String data){
Serial.println("Request Recieved: " + data);
}
void setup() {
Serial.begin(115200);
rest.set_id("000");
rest.set_name("MKR1000");
rest.function("test",clapMode);
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP
network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println();
// you're connected now, so print out the status:
printWifiStatus();
// Start server
restServer.begin();
Serial.println(F("Listening for connections..."));
}
void loop() {
WiFiClient client = restServer.available();
if (!client) {
return;
}
while(!client.available()){
delay(1);
}
rest.handle(client);
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
IPAddress subnet = WiFi.subnetMask();
Serial.print("Netmask: ");
Serial.println(subnet);
IPAddress gateway = WiFi.gatewayIP();
Serial.print("Gateway: ");
Serial.println(gateway);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
该代码大部分可用。使用POSTMAN执行GET时,arduino能够给出适当的响应。
现在对于不起作用的部分,是我刚刚使用此代码创建的端点
rest.function("test",clapMode);
在邮递员中执行GET时,arduino可以做出响应,但是它应该执行此代码
int clapMode(String data){
Serial.println("Request Recieved: " + data);
}
但是在我的串行监视器上,我什么也没得到。
我也找不到如何根据请求调整arduino的响应。我怎么办?
非常感谢您
答案 0 :(得分:1)
您必须将请求发送到<host>/test?params=0
,而不是<host>/clapMode
。
此外,您可以使用修改ID和名称
rest.set_id("device ID");
rest.set_name("device name");
并使用
添加变量rest.variable("variable name", &variable);
响应被硬编码到库中,因此,如果要添加/删除其他内容,则必须自己编辑库。