Skip to content

Added a basic WiFi UDP client example #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 3, 2017
Merged

Conversation

JorenSix
Copy link
Contributor

@JorenSix JorenSix commented Jan 3, 2017

Added a basic WiFi UDP client example: a sketch that sends random bytes over UDP. It uses the recently implemented UDP functionality.

Serial.println(WiFi.localIP());
//initializes the UDP state
//This initializes the transfer buffer
port.begin(WiFi.localIP(),udpPort);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be udp.begin(WiFi.localIP(),udpPort);


//Send a packet
udp.beginPacket(udpAddress,udpPort);
udp.write(randomData);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this to:

if(connected){
    //Send a packet
    udp.beginPacket(udpAddress,udpPort);
    udp.printf("seconds since boot: %u", millis()/1000);
    udp.endPacket();
}

and then used this simple python server to listen:

import socket
import sys

try :
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except socket.error, msg :
    print 'Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()

try:
    s.bind(('', 3333))
except socket.error , msg:
    print 'Bind failed. Error: ' + str(msg[0]) + ': ' + msg[1]
    sys.exit()
     
print 'Server listening'

while 1:
    d = s.recvfrom(1024)
    data = d[0]
     
    if not data: 
        break
    
    print data.strip()
    
s.close()

You might want to add those to further enhance your example :)

@me-no-dev
Copy link
Member

@JorenSix hey :) you mind fixing this commit so I can merge it?

@JorenSix
Copy link
Contributor Author

JorenSix commented Feb 3, 2017

Ok :) check the changes

@me-no-dev me-no-dev merged commit 97e9a12 into espressif:master Feb 3, 2017
brentru pushed a commit to adafruit/arduino-esp32 that referenced this pull request Oct 22, 2024
Allow .generate for rp2040 targets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants