board_android-IoT/ESP32
2024. 9. 10. 00:24
- ESP32 - Arduino source
/* * This sketch sends random data over UDP on a ESP32 device * */ #include <WiFi.h> #include <NetworkUdp.h> /* 8:28:24.965 -> entry 0x403c98ac 18:28:25.120 -> Connecting to WiFi network: Csk6457 18:28:25.214 -> Waiting for WIFI connection... 18:28:25.297 -> WiFi connected! IP address: 192.168.43.217 18:28:27.178 -> UDP recv=0 18:28:29.218 -> UDP recv=217 */ // WiFi network name and password: const char *networkName = "Csk6457"; const char *networkPswd = "1q2w3e4r@"; //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.43.1"; //"192.168.0.255"; const int udpPort = 56680; const int localPort = udpPort ; //Are we currently connected? boolean connected = false; //The udp library class NetworkUDP udp; char packetBuffer[255]; void setup() { // Initialize hardware serial: Serial.begin(115200); //Connect to the WiFi network connectToWiFi(networkName, networkPswd); } int packetSize ; unsigned long testIDPrev ; unsigned long interval = 2000 ; //2000= 2 seconds void loop() { //only send data when connected if (connected) { unsigned long testID = millis(); if( testID - testIDPrev >= interval ){ testIDPrev = testID ; //Send a packet udp.beginPacket(udpAddress, udpPort); udp.printf("Since boot: %lu(ms)\n", millis() ); // millis() / 1000); udp.endPacket(); // receive packets packetSize = udp_readPackets() ; } } //Wait for 2 second //delay(2000); } int packetSizePrev ; int udp_readPackets() { int packetSize ; packetSize = udp.parsePacket(); if (packetSize> 0 ) { Serial.print(" Received packet from : "); Serial.println(udp.remoteIP()); Serial.print(" Size : "); Serial.println(packetSize); int len = udp.read(packetBuffer, 255); if (len > 0) packetBuffer[len - 1] = 0; Serial.printf("[client<-server]Recv Data: %s\n", packetBuffer); } if( packetSize != packetSizePrev ) { packetSizePrev = packetSize ; Serial.printf("UDP recv=%d\n", packetSize) ; } return packetSize ; } 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); // Will call WiFiEvent() from another thread. //Initiate connection WiFi.begin(ssid, pwd); Serial.println("Waiting for WIFI connection..."); } // WARNING: WiFiEvent is called from a separate FreeRTOS task (thread)! void WiFiEvent(WiFiEvent_t event) { switch (event) { case ARDUINO_EVENT_WIFI_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; udp.begin(localPort); //20240906-1 break; case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: Serial.println("WiFi lost connection"); connected = false; break; default: break; } } |
- 스마트폰에서 ESP32 로 UDP통신을 하는 화면
'board_android-IoT > ESP32' 카테고리의 다른 글
ESP32-S3-A7670E-4G , Developement environment (0) | 2024.11.28 |
---|---|
Compile on both ESP32 and ESP8266 (0) | 2024.10.29 |
ESP32 UDP SEND_RECV example (0) | 2024.09.06 |
ESP8266 , DB18B20 connection (0) | 2024.07.29 |
ESP32 Dev Board DTR/RTS (0) | 2024.06.07 |