mirror of
				https://github.com/0xFEEDC0DE64/arduino-esp32.git
				synced 2025-11-04 08:01:38 +01:00 
			
		
		
		
	
		
			
	
	
		
			52 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Arduino
		
	
	
	
	
	
		
		
			
		
	
	
			52 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Arduino
		
	
	
	
	
	
| 
								 | 
							
								#include "WiFi.h"
							 | 
						||
| 
								 | 
							
								#include "AsyncUDP.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								const char * ssid = "***********";
							 | 
						||
| 
								 | 
							
								const char * password = "***********";
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								AsyncUDP udp;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								void setup()
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    Serial.begin(115200);
							 | 
						||
| 
								 | 
							
								    WiFi.mode(WIFI_STA);
							 | 
						||
| 
								 | 
							
								    WiFi.begin(ssid, password);
							 | 
						||
| 
								 | 
							
								    if (WiFi.waitForConnectResult() != WL_CONNECTED) {
							 | 
						||
| 
								 | 
							
								        Serial.println("WiFi Failed");
							 | 
						||
| 
								 | 
							
								        while(1) {
							 | 
						||
| 
								 | 
							
								            delay(1000);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    if(udp.connect(IPAddress(192,168,1,100), 1234)) {
							 | 
						||
| 
								 | 
							
								        Serial.println("UDP connected");
							 | 
						||
| 
								 | 
							
								        udp.onPacket([](AsyncUDPPacket packet) {
							 | 
						||
| 
								 | 
							
								            Serial.print("UDP Packet Type: ");
							 | 
						||
| 
								 | 
							
								            Serial.print(packet.isBroadcast()?"Broadcast":packet.isMulticast()?"Multicast":"Unicast");
							 | 
						||
| 
								 | 
							
								            Serial.print(", From: ");
							 | 
						||
| 
								 | 
							
								            Serial.print(packet.remoteIP());
							 | 
						||
| 
								 | 
							
								            Serial.print(":");
							 | 
						||
| 
								 | 
							
								            Serial.print(packet.remotePort());
							 | 
						||
| 
								 | 
							
								            Serial.print(", To: ");
							 | 
						||
| 
								 | 
							
								            Serial.print(packet.localIP());
							 | 
						||
| 
								 | 
							
								            Serial.print(":");
							 | 
						||
| 
								 | 
							
								            Serial.print(packet.localPort());
							 | 
						||
| 
								 | 
							
								            Serial.print(", Length: ");
							 | 
						||
| 
								 | 
							
								            Serial.print(packet.length());
							 | 
						||
| 
								 | 
							
								            Serial.print(", Data: ");
							 | 
						||
| 
								 | 
							
								            Serial.write(packet.data(), packet.length());
							 | 
						||
| 
								 | 
							
								            Serial.println();
							 | 
						||
| 
								 | 
							
								            //reply to the client
							 | 
						||
| 
								 | 
							
								            packet.printf("Got %u bytes of data", packet.length());
							 | 
						||
| 
								 | 
							
								        });
							 | 
						||
| 
								 | 
							
								        //Send unicast
							 | 
						||
| 
								 | 
							
								        udp.print("Hello Server!");
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								void loop()
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    delay(1000);
							 | 
						||
| 
								 | 
							
								    //Send broadcast on port 1234
							 | 
						||
| 
								 | 
							
								    udp.broadcastTo("Anyone here?", 1234);
							 | 
						||
| 
								 | 
							
								}
							 |