#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT #define ETH_PHY_POWER 12 #include #include #include #include #include #include #include #include #include #include #include WiFiUDP udp; WebServer server(80); const char* serverIndex = "
"; //Anwesenheit const char* host = "AW_TEST";//Hostname für Update Webside String module_name = "aw"; //Kürzel vor Mac-Adresse String module_ident = "AW_TEST"; //Kürzel für ALIVE, scan_start und scan_done IPAddress local_IP(192, 168, 1, 2); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 255, 0); IPAddress primaryDNS(192, 168, 1, 1); IPAddress udpAddress(192, 168, 1, 3); // IP Adresse-Loxone-MiniServer unsigned int udpPort = 7001; // UDP Port Loxone-MiniServer unsigned int localUdpPort = 7000; // Empfangsport der ESP32 #define SCAN_TIME 30 // Zeit wie lange gescannt werden soll in Sekunden (Std. 30 / min. 10 Sekunden) #define ALIVE 60000 // Alle xxxxx Milliskunden wird ein Alive-Signal gesendet //Ab hier nichts ändern! char incomingPacket[255]; String device_mac; String device_rssi; BLEScan* pBLEScan; class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { void onResult(BLEAdvertisedDevice advertisedDevice) { Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str()); String device_str = (module_name + "_mac=" + advertisedDevice.getAddress().toString().c_str() + " " + module_name + "_rssi_" + advertisedDevice.getAddress().toString().c_str() + "=" + advertisedDevice.getRSSI()); udp.beginPacket(udpAddress, udpPort); udp.print(device_str); udp.endPacket(); } }; static bool eth_connected = false; void WiFiEvent(WiFiEvent_t event) { switch (event) { case SYSTEM_EVENT_ETH_START: Serial.println("ETH Started"); //set eth hostname here ETH.setHostname("esp32-ethernet"); break; case SYSTEM_EVENT_ETH_CONNECTED: Serial.println("ETH Connected"); break; case SYSTEM_EVENT_ETH_GOT_IP: Serial.print("ETH MAC: "); Serial.print(ETH.macAddress()); Serial.print(", IPv4: "); Serial.print(ETH.localIP()); if (ETH.fullDuplex()) { Serial.print(", FULL_DUPLEX"); } Serial.print(", "); Serial.print(ETH.linkSpeed()); Serial.println("Mbps"); eth_connected = true; break; case SYSTEM_EVENT_ETH_DISCONNECTED: Serial.println("ETH Disconnected"); eth_connected = false; break; case SYSTEM_EVENT_ETH_STOP: Serial.println("ETH Stopped"); eth_connected = false; break; default: break; } } void setup() { Serial.begin(115200); WiFi.onEvent(WiFiEvent); ETH.begin(); ETH.config(local_IP, gateway, subnet, primaryDNS); MDNS.begin(host); server.on("/", HTTP_GET, []() { server.sendHeader("Connection", "close"); server.send(200, "text/html", serverIndex); }); server.on("/update", HTTP_POST, []() { server.sendHeader("Connection", "close"); server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); ESP.restart(); }, []() { HTTPUpload& upload = server.upload(); if (upload.status == UPLOAD_FILE_START) { Serial.setDebugOutput(true); Serial.printf("Update: %s\n", upload.filename.c_str()); if (!Update.begin()) { //start with max available size Update.printError(Serial); } } else if (upload.status == UPLOAD_FILE_WRITE) { if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { Update.printError(Serial); } } else if (upload.status == UPLOAD_FILE_END) { if (Update.end(true)) { //true to set the size to the current progress Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); } else { Update.printError(Serial); } Serial.setDebugOutput(false); } else { Serial.printf("Update Failed Unexpectedly (likely broken connection): status=%d\n", upload.status); } }); server.begin(); MDNS.addService("http", "tcp", 80); Serial.printf("Ready! Open http://%s.local in your browser\n", host); Wire.begin(); BLEDevice::init(""); udp.begin(localUdpPort); } void scan_ble() { BLEDevice::init(""); pBLEScan = BLEDevice::getScan(); //create new scan pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster pBLEScan->setInterval(100); pBLEScan->setWindow(99); // less or equal setInterval value Serial.printf("Start BLE scan for %d seconds...\n", SCAN_TIME); String scan_start = (module_ident + "=scan_start"); udp.beginPacket(udpAddress, udpPort); udp.print(scan_start); udp.endPacket(); BLEScanResults foundDevices = pBLEScan->start(SCAN_TIME, false); Serial.print("Devices found: "); Serial.println(foundDevices.getCount()); Serial.println("Scan done!"); String scan_stop = (module_ident + "=scan_done"); udp.beginPacket(udpAddress, udpPort); udp.print(scan_stop); udp.endPacket(); pBLEScan->clearResults(); } static void alive(unsigned long now) { static unsigned long next; if (next > now) return; next = now + ALIVE; String device_str = (module_ident + "_alive=1"); udp.beginPacket(udpAddress, udpPort); udp.print(device_str); udp.endPacket(); } void loop() { server.handleClient(); unsigned long now = millis(); alive(now); if (!strcmp(incomingPacket, "ble") || !strcmp(incomingPacket, "ble\n")) { scan_ble(); for (int i = 0; i < 255; i++) incomingPacket[i] = 0; } if (!strcmp(incomingPacket, "reboot") || !strcmp(incomingPacket, "reboot\n")) { ESP.restart(); for (int i = 0; i < 255; i++) incomingPacket[i] = 0; } int packetSize = udp.parsePacket(); if (packetSize) { int len = udp.read(incomingPacket, 255); if (len > 0) { incomingPacket[len] = 0; } Serial.print("UDP packet: "); Serial.println(incomingPacket); } }