2017-02-03 15:31:41 +01:00
|
|
|
/*
|
|
|
|
* 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);
|
2019-09-29 22:47:02 +02:00
|
|
|
udp.printf("Seconds since boot: %lu", millis()/1000);
|
2017-02-03 15:31:41 +01:00
|
|
|
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) {
|
2021-04-05 13:23:58 +02:00
|
|
|
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
2017-02-03 15:31:41 +01:00
|
|
|
//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;
|
2021-04-05 13:23:58 +02:00
|
|
|
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
|
2017-02-03 15:31:41 +01:00
|
|
|
Serial.println("WiFi lost connection");
|
|
|
|
connected = false;
|
|
|
|
break;
|
2019-09-29 22:47:02 +02:00
|
|
|
default: break;
|
2017-02-03 15:31:41 +01:00
|
|
|
}
|
|
|
|
}
|