我已通过wifi将三台设备(esp8266)连接到本地网络。此后,我在PC上启动一个客户端,然后客户端开始在本地网络中搜索这三个设备。
我可以实现所有搜索方法,但是我不知道该使用哪种方法。
我已经检查过arp(命令:arp -an
),但它仅显示有线设备,并且在ping后可以看到esp8266。
我的问题是,进行此搜索的最佳方法是什么?扫描网络? arp -a
?也许是另一种方法?
答案 0 :(得分:0)
我已经找到了解决方案。 客户端(esp8266)全部发送10秒。一条多播消息到IP:端口239.255.255.250:4000。而且我在PC上的程序会收到此消息,然后检查设备。
esp8266的C代码
#include "ets_sys.h"
#include "user_interface.h"
#include "osapi.h"
#include "gpio.h"
#include "os_type.h"
#include <espconn.h>
/* Change to desired SSID name */
const char ssid[32] = "wifi";
/* Enter the Password of the AP */
const char password[32] = "wifipass";
/* Will hold the SSID and Password Information */
struct station_config stationConf;
struct espconn sendResponse; //udp
esp_udp udp;
// timer
os_timer_t send_udp_device_info;
/******************************************************************************
* FunctionName : user_init
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
void send_dev_info(void *pArg)
{
int err;
sendResponse.type = ESPCONN_UDP;
sendResponse.state = ESPCONN_NONE;
sendResponse.proto.udp = &udp;
IP4_ADDR((ip_addr_t *)sendResponse.proto.udp->remote_ip, 239, 255, 255, 250);
sendResponse.proto.udp->remote_port = 4000; // Remote port
err = espconn_create(&sendResponse);
err = espconn_send(&sendResponse, "hi123", 5);
err = espconn_delete(&sendResponse);
}
void user_init(void)
{
/* Select UART 0 and configure the baud rate to 9600 */
uart_div_modify(0, UART_CLK_FREQ / 9600);
os_printf("Demo Example - ESP8266 as Station\r\n");
/* Configure the ESP8266 to Station Mode */
wifi_set_opmode( STATION_MODE );
/* Copy the SSID and Password Info to the structure */
os_memcpy(&stationConf.ssid, ssid, 32);
os_memcpy(&stationConf.password, password, 32);
/* Configure the station to connect to the following AP */
wifi_station_set_config(&stationConf);
/* Connects to the AP */
wifi_station_connect();
os_timer_setfn(&send_udp_device_info, send_dev_info, NULL);
os_timer_arm(&send_udp_device_info, 10*1000, 1);
}
还有PC上的程序
var PORT = 4000;
var MULTICAST_ADDR = '239.255.255.250';
var dgram = require('dgram');
var client = dgram.createSocket('udp4');
client.on('listening', function () {
var address = client.address();
console.log('UDP Client listening on ' + address.address + ":" + address.port);
});
client.on('message', function (message, rinfo) {
console.log('Message from: ' + rinfo.address + ':' + rinfo.port + ' - ' + message);
});
client.bind(PORT, function () {
client.addMembership(MULTICAST_ADDR);
});