2016-08-21 4 views
0

UDP를 사용하여 두 개의 노드 모듈을 연결하고 있습니다. 하나의 노드는 무선 액세스 포인트이고 또 다른 노드는 클라이언트로서 액세스 포인트에 연결됩니다.ESP8266 AP에 UDP 문자열 보내기

Udp.beginPacket("192.168.4.1", UDPPort);//send ip to server 
    char ipBuffer[20]; 
    WiFi.localIP().toString().toCharArray(ipBuffer, 20); 
    Udp.write(ipBuffer); 
    Udp.endPacket(); 
    Serial.println("Sent ip adress to server"); 

을하지만 서버 측에서 나는이 패킷을받을하지 않습니다 클라이언트가 연결되면

이 코드는 AP에 클라이언트의 IP 주소를 전송합니다.

클라이언트 :

#include <ESP8266WiFi.h> 
#include <WiFiUDP.h> 

unsigned int UDPPort = 2390;  // local port to listen on 

char packetBuffer[255]; //buffer to hold incoming packet 
char replyBuffer[] = "acknowledged";  // a string to send back 

WiFiUDP Udp; 

void setup() { 
    Serial.begin(115200); 
    WiFi.begin("Wi-Fi"); 
    Serial.println(); 
    Serial.print("Wait for WiFi"); 

    while (WiFi.status() != WL_CONNECTED) { 
     delay(500); 
     Serial.print("."); 
    } 
    Serial.println(""); 
    Serial.println("WiFi connected"); 
    Serial.println("IP address: " + WiFi.localIP().toString()); 

    Udp.begin(UDPPort); 

    Udp.beginPacket("192.168.4.1", UDPPort);//send ip to server 
    char ipBuffer[255]; 
    WiFi.localIP().toString().toCharArray(ipBuffer, 255); 
    Udp.write(ipBuffer); 
    Udp.endPacket(); 
    Serial.println("Sent ip adress to server"); 
    } 

void loop() { 

    // if there's data available, read a packet 
    int packetSize = Udp.parsePacket(); 
    if (packetSize) { 
    Serial.print("Received packet of size "); 
    Serial.println(packetSize); 
    Serial.print("From "); 
    IPAddress remoteIp = Udp.remoteIP(); 
    Serial.print(remoteIp); 
    Serial.print(", port "); 
    Serial.println(Udp.remotePort()); 

    // read the packet into packetBufffer 
    int len = Udp.read(packetBuffer, 255); 
    if (len > 0) { 
     packetBuffer[len] = 0; 
    } 
    Serial.println("Contents:"); 
    Serial.println(packetBuffer); 

    // send a reply, to the IP address and port that sent us the packet we received 
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); 
    Udp.write(replyBuffer); 
    Udp.endPacket(); 
    } 

} 

서버 :

#include <ESP8266WiFi.h> 
#include <WiFiUDP.h> 

unsigned int UDPPort = 2390;  // local port to listen on 

char packetBuffer[255]; //buffer to hold incoming packet 
char ReplyBuffer[] = "acknowledged";  // a string to send back 
WiFiUDP Udp; 
void setup() { 
    Serial.begin(115200); 

    WiFi.softAP("Wi-Fi"); 
    Udp.begin(UDPPort); 
    Serial.println(); 
    Serial.println("Started ap. Local ip: " + WiFi.localIP().toString()); 
} 

void loop() { 
    // if there's data available, read a packet 
    int packetSize = Udp.parsePacket(); 
    if (packetSize) { 
    Serial.print("Received packet of size "); 
    Serial.println(packetSize); 
    Serial.print("From "); 
    IPAddress remoteIp = Udp.remoteIP(); 
    Serial.print(remoteIp); 
    Serial.print(", port "); 
    Serial.println(Udp.remotePort()); 

    // read the packet into packetBufffer 
    int len = Udp.read(packetBuffer, 255); 
    if (len > 0) { 
     packetBuffer[len] = 0; 
    } 
    Serial.println("Contents:"); 
    Serial.println(packetBuffer); 
    // send a reply, to the IP address and port that sent us the packet we received 
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); 
    Udp.write(ReplyBuffer); 
    Udp.endPacket(); 
    } 

} 

또 다른 것은 작동하지 않는 : 나는 (또한 AP에 연결) 클라이언트 nodemcu에 AP의 nodemcu에 연결된 다른 장치에서 패킷을 전송하는 경우 , 패킷이 수신되었지만 기기에 수신 확인 응답을 보내지 않습니다.

다른 모든 작동 - 다른 장치에서 AP nodemcu로 패킷을 보내면 패킷이 수신되고 수신 확인을받습니다. 또한 클라이언트 nodemcu가있는 집에 Wi-Fi 라우터를 연결하고 PC에서 패킷을 수신하는 경우 연결될 때 클라이언트의 IP 주소가 표시됩니다.

+0

는 서버가 예상되는 IP 주소를 표시합니까 .. 어떤 패키지 손실 .... 지금 때마다 작동? 클라이언트가 동일한 서브넷에 IP를 가지고 있습니까? – leetibbett

답변

1

각 conected esp8266에 대한 포트 번호를 변경해야했습니다. ESP의 IP가 192.168.4.2, 내가 2302에 포트를 설정 한 경우, 192.168.4.3, 나는 내가 고심 ... 비슷한 문제를 쏠 문제에 노력 해왔다

0

... 2303로 설정

저도 ESP8266의 패키지를받지 못했습니다

라인이 변경되었습니다.

WiFi.softAP ("Wi-Fi");
~ ....
WiFi.mode (WIFI_STA); 이 액세스 포인트 역할을하는 경우

관련 문제