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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions libraries/WiFi/examples/WiFiUDPClient/WiFiUDPClient.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* This sketch sends random data over UDP on a ESP32 device
*
*/
#include <WiFi.h>
#include <WiFiUdp.h>

// WiFi network name and password:
const char * networkName = "your-ssid";
const char * networkPswd = "your-password";

//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.0.255";
const int udpPort = 3333;

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

//The udp library class
WiFiUDP udp;

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){
//Send a packet
udp.beginPacket(udpAddress,udpPort);
udp.printf("Seconds since boot: %u", millis()/1000);
udp.endPacket();
}
//Wait for 1 second
delay(1000);
}

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

//Initiate connection
WiFi.begin(ssid, pwd);

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

//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;
}
}
30 changes: 30 additions & 0 deletions libraries/WiFi/examples/WiFiUDPClient/udp_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This python script listens on UDP port 3333
# for messages from the ESP32 board and prints them
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()
16 changes: 16 additions & 0 deletions libraries/WiFi/examples/WiFiUDPClient/udp_server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This ruby script listens on UDP port 3333
# for messages from the ESP32 board and prints them

require 'socket'
include Socket::Constants

udp_socket = UDPSocket.new(AF_INET)

#bind
udp_socket.bind("", 3333)
puts 'Server listening'

while true do
message, sender = udp_socket.recvfrom(1024)
puts message
end