Last active
April 20, 2023 18:48
-
-
Save Tarik2142/f4d92f34041e8e1a0249185c11333dbf to your computer and use it in GitHub Desktop.
discover esp8266 in local network
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // discover esp8266 in local network | |
| // 29.01.2020 | |
| #include "WiFiUdp.h" | |
| #include "ESP8266WiFi.h" | |
| #define DISCOVERY_MESSAGE "8266_DISCOVERY" | |
| #define DEBUG | |
| const uint16_t echoPort = 8266; | |
| char inString[255]; | |
| //char replyString[] = "{name:\"led time\", \"version:1.8\", \"description\":\"часи\", \"ip\":\"192.168.1.1\", \"mac\":\"11.11.11.11.11\"}"; | |
| char replyString[255]; | |
| WiFiUDP responser; | |
| void esp_discovery_config(const char* projectName, const char* version, const char* description){//set reply | |
| snprintf(replyString, 255, "{\"name\":\"%s\",\"version\":\"%s\",\"description\":\"%s\",\"ip\":\"%s\",\"mac\":\"%s\"}", projectName, version, description, WiFi.localIP().toString().c_str(), WiFi.macAddress().c_str()); | |
| responser.begin(echoPort); | |
| #ifdef DEBUG | |
| Serial.println("FREE RAM: " + ESP.getFreeHeap()); | |
| #endif | |
| } | |
| void esp_discovery_handle(){//in loop() | |
| int packetSize = responser.parsePacket(); | |
| if (packetSize) { | |
| #ifdef DEBUG | |
| Serial.print("Received packet of size "); | |
| Serial.println(packetSize); | |
| Serial.print("From "); | |
| #endif | |
| IPAddress remoteIp = responser.remoteIP(); | |
| #ifdef DEBUG | |
| Serial.print(remoteIp); | |
| Serial.print(", port "); | |
| Serial.println(responser.remotePort()); | |
| #endif | |
| int len = responser.read(inString, 255); | |
| if (len > 0) { | |
| inString[len] = 0; | |
| } | |
| #ifdef DEBUG | |
| Serial.println("Contents:"); | |
| Serial.println(inString); | |
| #endif | |
| if (strcmp(inString, DISCOVERY_MESSAGE) == 0){ | |
| responser.beginPacket(responser.remoteIP(), responser.remotePort()); | |
| responser.write(replyString); | |
| responser.endPacket(); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
void setup(){
esp_discovery_config("project name", "1.0", "description");
}
void loop(){
esp_discovery_handle();
}