Instantly share code, notes, and snippets.
Last activeApril 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" | |
#defineDISCOVERY_MESSAGE "8266_DISCOVERY" | |
#defineDEBUG | |
constuint16_techoPort=8266; | |
charinString[255]; | |
//char replyString[] = "{name:\"led time\", \"version:1.8\", \"description\":\"часи\", \"ip\":\"192.168.1.1\", \"mac\":\"11.11.11.11.11\"}"; | |
charreplyString[255]; | |
WiFiUDPresponser; | |
voidesp_discovery_config(constchar*projectName,constchar*version,constchar*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); | |
#ifdefDEBUG | |
Serial.println("FREE RAM: "+ESP.getFreeHeap()); | |
#endif | |
} | |
voidesp_discovery_handle(){//in loop() | |
intpacketSize=responser.parsePacket(); | |
if (packetSize) { | |
#ifdefDEBUG | |
Serial.print("Received packet of size "); | |
Serial.println(packetSize); | |
Serial.print("From "); | |
#endif | |
IPAddressremoteIp=responser.remoteIP(); | |
#ifdefDEBUG | |
Serial.print(remoteIp); | |
Serial.print(", port "); | |
Serial.println(responser.remotePort()); | |
#endif | |
intlen=responser.read(inString,255); | |
if (len>0) { | |
inString[len]=0; | |
} | |
#ifdefDEBUG | |
Serial.println("Contents:"); | |
Serial.println(inString); | |
#endif | |
if (strcmp(inString,DISCOVERY_MESSAGE)==0){ | |
responser.beginPacket(responser.remoteIP(),responser.remotePort()); | |
responser.write(replyString); | |
responser.endPacket(); | |
} | |
} | |
} |
void setup(){
esp_discovery_config("project name", "1.0", "description");
}
void loop(){
esp_discovery_handle();
}
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment