2020-08-25 19:25:54 +03:00
|
|
|
#include "Class/NotAsinc.h"
|
|
|
|
|
#include "udp.h"
|
|
|
|
|
#include "udp_.h"
|
2020-07-26 23:48:19 +02:00
|
|
|
#include "Global.h"
|
|
|
|
|
|
|
|
|
|
static const char* MODULE = "Udp";
|
|
|
|
|
|
|
|
|
|
#ifdef ESP8266
|
|
|
|
|
IPAddress udp_multicastIP(255, 255, 255, 255);
|
|
|
|
|
WiFiUDP udp;
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef ESP32
|
|
|
|
|
IPAddress udp_multicastIP(239, 255, 255, 255);
|
|
|
|
|
AsyncUDP udp;
|
|
|
|
|
#endif
|
|
|
|
|
String remote_ip;
|
|
|
|
|
String received;
|
|
|
|
|
int udp_period;
|
|
|
|
|
boolean udp_busy = false;
|
|
|
|
|
unsigned int udp_port = 4210;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef UDP_ENABLED
|
2020-08-25 19:25:54 +03:00
|
|
|
void udpInit() {
|
|
|
|
|
myNotAsincActions->add(
|
|
|
|
|
do_UDPDATAPARSE, [&](void*) {
|
|
|
|
|
do_udp_data_parse();
|
|
|
|
|
},
|
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
|
|
myNotAsincActions->add(
|
|
|
|
|
do_MQTTUDP, [&](void*) {
|
|
|
|
|
send_mqtt_to_udp();
|
|
|
|
|
},
|
|
|
|
|
nullptr);
|
|
|
|
|
|
2020-07-26 23:48:19 +02:00
|
|
|
removeFile("dev.csv");
|
2020-08-04 02:21:20 +02:00
|
|
|
addFileLn("dev.csv", "device id;device name;ip address");
|
2020-07-26 23:48:19 +02:00
|
|
|
|
|
|
|
|
#ifdef ESP8266
|
|
|
|
|
udp.begin(udp_port);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
handleUdp_esp32();
|
|
|
|
|
|
|
|
|
|
randomSeed(micros());
|
|
|
|
|
udp_period = random(50000, 60000);
|
|
|
|
|
|
|
|
|
|
ts.add(
|
|
|
|
|
UDP, udp_period, [&](void*) {
|
|
|
|
|
if (jsonReadBool(configSetupJson, "udponoff") && isNetworkActive() && !udp_busy) {
|
|
|
|
|
pm.info("send info");
|
|
|
|
|
String payload = "iotm;";
|
|
|
|
|
payload += chipId;
|
|
|
|
|
payload += ";";
|
|
|
|
|
payload += jsonReadStr(configSetupJson, "name");
|
|
|
|
|
#ifdef ESP8266
|
|
|
|
|
udp.beginPacketMulticast(udp_multicastIP, udp_port, WiFi.localIP());
|
|
|
|
|
udp.write(payload.c_str());
|
|
|
|
|
udp.endPacket();
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef ESP32
|
|
|
|
|
udp.broadcast(line_to_send.c_str());
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
nullptr, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isUdpEnabled() {
|
|
|
|
|
return jsonReadBool(configSetupJson, "udponoff") && isNetworkActive();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loopUdp() {
|
|
|
|
|
#ifdef ESP8266
|
|
|
|
|
if (!isUdpEnabled()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int packetSize = udp.parsePacket();
|
|
|
|
|
if (!packetSize) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char udp_packet[255];
|
|
|
|
|
remote_ip = udp.remoteIP().toString();
|
|
|
|
|
|
|
|
|
|
pm.info(prettyBytes(packetSize) + " from " + remote_ip + ":" + udp.remotePort());
|
|
|
|
|
|
|
|
|
|
int len = udp.read(udp_packet, 255);
|
|
|
|
|
if (len) {
|
|
|
|
|
udp_packet[len] = '\x00';
|
|
|
|
|
}
|
|
|
|
|
received = String(udp_packet);
|
|
|
|
|
if (received.indexOf("iotm;") >= 0 || received.indexOf("mqttServer") >= 0) {
|
2020-08-25 19:25:54 +03:00
|
|
|
myNotAsincActions->make(do_UDPDATAPARSE);
|
2020-07-26 23:48:19 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void handleUdp_esp32() {
|
|
|
|
|
#ifdef ESP32
|
|
|
|
|
if (udp.listenMulticast(udp_multicastIP, udp_port)) {
|
|
|
|
|
udp.onPacket([](AsyncUDPPacket packet) {
|
|
|
|
|
received = (char*)packet.data();
|
|
|
|
|
remote_ip = packet.remoteIP().toString();
|
|
|
|
|
if (jsonReadStr(configSetupJson, "udponoff") == "1") {
|
|
|
|
|
if (received.indexOf("iotm;") >= 0) {
|
2020-08-25 19:25:54 +03:00
|
|
|
myNotAsincActions->make(do_UDPDATAPARSE);
|
2020-07-26 23:48:19 +02:00
|
|
|
}
|
|
|
|
|
if (received.indexOf("mqttServer") >= 0) {
|
2020-08-25 19:25:54 +03:00
|
|
|
myNotAsincActions->make(do_UDPDATAPARSE);
|
2020-07-26 23:48:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void do_udp_data_parse() {
|
|
|
|
|
if (received.indexOf("mqttServer") >= 0) {
|
|
|
|
|
pm.info("received setting");
|
|
|
|
|
jsonWriteStr(configSetupJson, "mqttServer", jsonReadStr(received, "mqttServer"));
|
|
|
|
|
jsonWriteInt(configSetupJson, "mqttPort", jsonReadInt(received, "mqttPort"));
|
|
|
|
|
jsonWriteStr(configSetupJson, "mqttPrefix", jsonReadStr(received, "mqttPrefix"));
|
|
|
|
|
jsonWriteStr(configSetupJson, "mqttUser", jsonReadStr(received, "mqttUser"));
|
|
|
|
|
jsonWriteStr(configSetupJson, "mqttPass", jsonReadStr(received, "mqttPass"));
|
|
|
|
|
saveConfig();
|
2020-08-25 19:25:54 +03:00
|
|
|
myNotAsincActions->make(do_MQTTPARAMSCHANGED);
|
2020-07-26 23:48:19 +02:00
|
|
|
}
|
|
|
|
|
if (received.indexOf("iotm;") >= 0) {
|
|
|
|
|
add_dev_in_list("dev.csv", selectFromMarkerToMarker(received, ";", 1), selectFromMarkerToMarker(received, ";", 2), received);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void add_dev_in_list(String filename, String id, String dev_name, String ip) {
|
|
|
|
|
auto file = seekFile("/" + filename);
|
|
|
|
|
if (!file.find(id.c_str())) {
|
2020-08-04 02:21:20 +02:00
|
|
|
addFileLn(filename, id + ";" + dev_name + "; <a href=\"http://" + ip + "\" target=\"_blank\"\">" + ip + "</a>");
|
2020-07-26 23:48:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void send_mqtt_to_udp() {
|
|
|
|
|
if (!isUdpEnabled()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
udp_busy = true;
|
|
|
|
|
String data = "{}";
|
|
|
|
|
jsonWriteStr(data, "mqttServer", jsonReadStr(configSetupJson, "mqttServer"));
|
|
|
|
|
jsonWriteInt(data, "mqttPort", jsonReadInt(configSetupJson, "mqttPort"));
|
|
|
|
|
jsonWriteStr(data, "mqttPrefix", jsonReadStr(configSetupJson, "mqttPrefix"));
|
|
|
|
|
jsonWriteStr(data, "mqttUser", jsonReadStr(configSetupJson, "mqttUser"));
|
|
|
|
|
jsonWriteStr(data, "mqttPass", jsonReadStr(configSetupJson, "mqttPass"));
|
|
|
|
|
#ifdef ESP8266
|
|
|
|
|
udp.beginPacketMulticast(udp_multicastIP, udp_port, WiFi.localIP());
|
|
|
|
|
udp.write(data.c_str());
|
|
|
|
|
udp.endPacket();
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef ESP32
|
|
|
|
|
udp.broadcast(mqtt_data.c_str());
|
|
|
|
|
#endif
|
|
|
|
|
pm.info("sent info");
|
|
|
|
|
udp_busy = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|