-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* 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 = "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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be |
||
connected = true; | ||
break; | ||
case SYSTEM_EVENT_STA_DISCONNECTED: | ||
Serial.println("WiFi lost connection"); | ||
connected = false; | ||
break; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this to:
and then used this simple python server to listen:
You might want to add those to further enhance your example :)