我实现了一个 coap 服务器:
代码:
全选#include
coapServer coap;
void getListOfWiFi(coapPacket *packet, IPAddress ip, int port, int obs) //id = 0 GET
{
Serial.println("hello");
int numberOfNetworks = WiFi.scanNetworks();
String WifiList = "";
for (int i = 0; i < numberOfNetworks - 1; i++)
{
WifiList += WiFi.SSID(i) + "####";
}
WifiList += WiFi.SSID(numberOfNetworks - 1);
char *temp = new char[WifiList.length() + 1];
WifiList.toCharArray(temp, WifiList.length() + 1);
coap.sendResponse(ip, port, temp);
Serial.println("goodbye");
}
void setup()
{
coap.server(getListOfWiFi, "wifilist");
coap.start(5683);
}
void loop()
{
coap.loop();
}
当启动 esp 时,串口监视器的输出是:
代码:
全选hello
goodbye
hello
goodbye
hello
goodbye
hello
goodbye
hello
goodbye
hello
goodbye
并且它在没有任何接收数据包或请求的情况下一直打印它......
为什么会这样?
谢谢。