Быстрый поиск устройства в локальной сети (tcp) - PullRequest
0 голосов
/ 28 июня 2018

Я подключил три устройства (esp8266) к локальной сети через Wi-Fi. После этого я запускаю клиент на моем компьютере, и клиент начинает поиск этих трех устройств в моей локальной сети.

Я могу реализовать каждый метод для поиска, но я не знаю, какой.

Я проверял с помощью arp (команда: arp -an), но он показывает только проводные устройства и esp8266 виден после ping.

Мой вопрос: каков наилучший метод для этого поиска? Сканирование сети? arp -a? Может быть, другой метод?

1 Ответ

0 голосов
/ 01 июля 2018

Я нашел решение для себя. Клиент (esp8266) отправляет все 10 сек. многоадресное сообщение на IP: порт 239.255.255.250:4000. И моя программа на ПК получает это сообщение и затем проверяет устройство.

C код для esp8266

#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);
}

И программа на ПК

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);
});
...