Skip to content

Wifi UDP client does not work #714

@echoGee

Description

@echoGee

Hardware:

Board: ESP32 Dev Module
Core Installation/update date: 0.10.0
IDE name: Platform.io
Flash Frequency: unsure , assuming 80mhz
Upload Speed: 115200

Description:

I ran this code on the esp32. It connects to the wifi network. I was able to ping the esp32 from the pc. However, the python program https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/examples/WiFiUDPClient/udp_server.py does not get any messages from esp. It asked for a permission to access the network, which I agreed
The pc ip is "192.168.137.1"

Am I missing something?

Sketch:

//Change the code below by your sketch
/*
 *  This sketch sends random data over UDP on a ESP32 device
 *
 */
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiUdp.h>

// WiFi network name and password:
const char * networkName = "baloon";
const char * networkPswd = "598468588";

//IP address to send UDP data to:
// either use the ip address of the server or
// a network broadcast address
const char * udpAddress = "192.168.137.1";
const int udpPort = 3333;
IPAddress myIP(192,168,137,12);
IPAddress gateway(192,168,137,1);
IPAddress subnet(255,255,255,0);

//Are we currently connected?
boolean connected = false;

//The udp library class
WiFiUDP udp;

//wifi event handler
void WiFiEvent(WiFiEvent_t event){
    switch(event) {
      case SYSTEM_EVENT_STA_GOT_IP:
          //When connected set
          Serial.print("WiFi connected! IP address: ");
          Serial.println(WiFi.localIP());
          //initializes the UDP state
          //This initializes the transfer buffer
          udp.begin(WiFi.localIP(),udpPort);
          connected = true;
          break;
      case SYSTEM_EVENT_STA_DISCONNECTED:
          Serial.println("WiFi lost connection");
          connected = false;
          break;
    }
}

void connectToWiFi(const char * ssid, const char * pwd){
  Serial.println("Connecting to WiFi network: " + String(ssid));

  // delete old config
  WiFi.disconnect(true);
  //register event handler
  WiFi.onEvent(WiFiEvent);
  WiFi.config(myIP,gateway,subnet);
  //Initiate connection
  WiFi.begin(ssid, pwd);

  Serial.println("Waiting for WIFI connection...");
}

void setup(){
  // Initilize hardware serial:
  Serial.begin(115200);

  //Connect to the WiFi network
  connectToWiFi(networkName, networkPswd);
}

void loop(){
  //only send data when connected
  if(connected){
    Serial.println("printing");
    //Send a packet
    udp.beginPacket(udpAddress,udpPort);
    udp.printf("Seconds since boot: %u", millis()/1000);
    udp.endPacket();
  }
  //Wait for 1 second
  delay(1000);
}

Debug Messages:

Using Platform.io. Unable to set debug flags

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions