2016-08-21 10 views
0

UDPを使用して2つのnodemcuモジュールを接続しています。 1つのノードは無線アクセスポイントであり、別のノードはクライアントとしてアクセスポイントに接続します。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

各接続されたesp8266のポート番号を変更する必要がありました。 ESPのIPが192.168.4.2、私は2302年にポートを設定した場合は、192.168.4.3のために、私は苦労し...同様の問題を撃つトラブルにしようとしている

0

... 2303年に設定し

私もESP8266のパッケージを受け取れませんでした

私はあなたのラインを変更しました。

WiFi.softAP( "Wi-Fi");
〜....
WiFi.mode(WIFI_STA);それがアクセスポイントとして動作しているときがない、パッケージ損失....今たびに動作します

...

関連する問題