52 lines
1.2 KiB
Arduino
52 lines
1.2 KiB
Arduino
#include <Arduino.h>
|
|
#include <WiFi.h>
|
|
#include <lwip/sockets.h>
|
|
|
|
#define _SSID "ESP32-AP1"
|
|
#define _PASSWD "PASS0000"
|
|
#define UDP_PORT 6666
|
|
|
|
#define MTU 100000
|
|
|
|
IPAddress apIP(192,168,178,1);
|
|
IPAddress subnetMask(255,255,255,0);
|
|
|
|
static int udp_sock;
|
|
|
|
void udp_task(void* /*args*/) {
|
|
struct sockaddr_in sourceAddr;
|
|
socklen_t addrLen = sizeof(sourceAddr);
|
|
static uint8_t udp_buf[MTU];
|
|
|
|
while (true) {
|
|
int len = recvfrom(udp_sock, udp_buf, MTU - 1, 0, (struct sockaddr*)&sourceAddr, &addrLen);
|
|
if (len > 0) {
|
|
udp_buf[len] = '\n';
|
|
Serial.write(udp_buf, len + 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
while (!Serial) { delay(10); }
|
|
|
|
WiFi.mode(WIFI_AP);
|
|
WiFi.softAPConfig(apIP, apIP, subnetMask);
|
|
WiFi.softAP(_SSID, _PASSWD, /*channel*/1, /*hidden*/false, /*maxConn*/4);
|
|
|
|
udp_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
struct sockaddr_in localAddr;
|
|
memset(&localAddr, 0, sizeof(localAddr));
|
|
localAddr.sin_family = AF_INET;
|
|
localAddr.sin_port = htons(UDP_PORT);
|
|
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
bind(udp_sock, (struct sockaddr*)&localAddr, sizeof(localAddr));
|
|
|
|
xTaskCreatePinnedToCore(udp_task, "UDPReceiver", 4096, nullptr, 5, nullptr, 1);
|
|
}
|
|
|
|
void loop() {
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
}
|