From 23202683db913494f34d9cf28367d262a7e2cfa8 Mon Sep 17 00:00:00 2001 From: Joren Six Date: Tue, 3 Jan 2017 14:10:53 +0100 Subject: [PATCH 1/2] Added a basic WiFi UDP client example: a sketch that sends random bytes over udp. --- .../examples/WiFiUDPClient/WiFiUDPClient.ino | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 libraries/WiFi/examples/WiFiUDPClient/WiFiUDPClient.ino diff --git a/libraries/WiFi/examples/WiFiUDPClient/WiFiUDPClient.ino b/libraries/WiFi/examples/WiFiUDPClient/WiFiUDPClient.ino new file mode 100644 index 00000000000..85eb482131e --- /dev/null +++ b/libraries/WiFi/examples/WiFiUDPClient/WiFiUDPClient.ino @@ -0,0 +1,77 @@ +/* + * This sketch sends random data over UDP on a ESP32 device + * + */ +#include +#include + +// WiFi network name and password: +const char * networkName = "network_name"; +const char * networkPswd = "password"; + +//IP address to send UDP data to: +const char * udpAddress = "192.168.1.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 one random byte + byte randomData = (byte) random(255); + + //Send a packet + udp.beginPacket(udpAddress,udpPort); + udp.write(randomData); + udp.endPacket(); + } + //Wait for 1.5 seconds + delay(1500); +} + +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 + port.begin(WiFi.localIP(),udpPort); + connected = true; + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + Serial.println("WiFi lost connection"); + connected = false; + break; + } +} From 7f68d5b9c12a640f242438483a5f40f71f9e4286 Mon Sep 17 00:00:00 2001 From: Joren Six Date: Fri, 3 Feb 2017 14:00:00 +0100 Subject: [PATCH 2/2] Updated udp example and included Python and Ruby UDP servers. --- .../examples/WiFiUDPClient/WiFiUDPClient.ino | 20 ++++++------- .../WiFi/examples/WiFiUDPClient/udp_server.py | 30 +++++++++++++++++++ .../WiFi/examples/WiFiUDPClient/udp_server.rb | 16 ++++++++++ 3 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 libraries/WiFi/examples/WiFiUDPClient/udp_server.py create mode 100644 libraries/WiFi/examples/WiFiUDPClient/udp_server.rb diff --git a/libraries/WiFi/examples/WiFiUDPClient/WiFiUDPClient.ino b/libraries/WiFi/examples/WiFiUDPClient/WiFiUDPClient.ino index 85eb482131e..04d4effd0a8 100644 --- a/libraries/WiFi/examples/WiFiUDPClient/WiFiUDPClient.ino +++ b/libraries/WiFi/examples/WiFiUDPClient/WiFiUDPClient.ino @@ -6,11 +6,13 @@ #include // WiFi network name and password: -const char * networkName = "network_name"; -const char * networkPswd = "password"; +const char * networkName = "your-ssid"; +const char * networkPswd = "your-password"; //IP address to send UDP data to: -const char * udpAddress = "192.168.1.255"; +// 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? @@ -30,17 +32,13 @@ void setup(){ void loop(){ //only send data when connected if(connected){ - - //send one random byte - byte randomData = (byte) random(255); - //Send a packet udp.beginPacket(udpAddress,udpPort); - udp.write(randomData); + udp.printf("Seconds since boot: %u", millis()/1000); udp.endPacket(); } - //Wait for 1.5 seconds - delay(1500); + //Wait for 1 second + delay(1000); } void connectToWiFi(const char * ssid, const char * pwd){ @@ -66,7 +64,7 @@ void WiFiEvent(WiFiEvent_t event){ Serial.println(WiFi.localIP()); //initializes the UDP state //This initializes the transfer buffer - port.begin(WiFi.localIP(),udpPort); + udp.begin(WiFi.localIP(),udpPort); connected = true; break; case SYSTEM_EVENT_STA_DISCONNECTED: diff --git a/libraries/WiFi/examples/WiFiUDPClient/udp_server.py b/libraries/WiFi/examples/WiFiUDPClient/udp_server.py new file mode 100644 index 00000000000..90272353ac1 --- /dev/null +++ b/libraries/WiFi/examples/WiFiUDPClient/udp_server.py @@ -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() \ No newline at end of file diff --git a/libraries/WiFi/examples/WiFiUDPClient/udp_server.rb b/libraries/WiFi/examples/WiFiUDPClient/udp_server.rb new file mode 100644 index 00000000000..50e241d4bcf --- /dev/null +++ b/libraries/WiFi/examples/WiFiUDPClient/udp_server.rb @@ -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 \ No newline at end of file