mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-30 11:59:12 +03:00
servo support added
This commit is contained in:
122
udp.ino
122
udp.ino
@@ -4,16 +4,24 @@ void UDP_init() {
|
||||
if (request->hasArg("arg")) {
|
||||
value = request->getParam("arg")->value();
|
||||
}
|
||||
if (value == "0") {
|
||||
jsonWriteStr(configSetup, "udponoff", value);
|
||||
request->send(200, "text/text", "ok");
|
||||
}
|
||||
if (value == "1") {
|
||||
mqtt_send_settings_to_udp = true;
|
||||
jsonWriteStr(configSetup, "udponoff", value);
|
||||
request->send(200, "text/text", "ok");
|
||||
}
|
||||
if (value == "2") {
|
||||
mqtt_send_settings_to_udp = true;
|
||||
request->send(200, "text/text", "ok");
|
||||
}
|
||||
if (value == "3") {
|
||||
SPIFFS.remove("/dev.csv");
|
||||
addFile("dev.csv", "device id;device name;ip address");
|
||||
request->redirect("/?dev");
|
||||
}
|
||||
if (value == "3") {
|
||||
if (value == "4") {
|
||||
request->redirect("/?dev");
|
||||
}
|
||||
});
|
||||
@@ -34,23 +42,25 @@ void UDP_init() {
|
||||
#endif
|
||||
|
||||
handleUdp_esp32();
|
||||
|
||||
|
||||
randomSeed(micros());
|
||||
udp_period = random(20000, 40000);
|
||||
|
||||
|
||||
ts.add(UDP, udp_period, [&](void*) {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
if (!udp_busy) {
|
||||
String line_to_send = "iotm;" + chipID + ";" + jsonRead(configSetup, "name");
|
||||
if (jsonRead(configSetup, "udponoff") == "1") {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
if (!udp_busy) {
|
||||
String line_to_send = "iotm;" + chipID + ";" + jsonRead(configSetup, "name");
|
||||
#ifdef ESP8266
|
||||
Udp.beginPacketMulticast(udp_multicastIP, udp_port, WiFi.localIP());
|
||||
Udp.write(line_to_send.c_str());
|
||||
Udp.endPacket();
|
||||
Udp.beginPacketMulticast(udp_multicastIP, udp_port, WiFi.localIP());
|
||||
Udp.write(line_to_send.c_str());
|
||||
Udp.endPacket();
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
udp.broadcast(line_to_send.c_str());
|
||||
udp.broadcast(line_to_send.c_str());
|
||||
#endif
|
||||
Serial.println("[UDP<=] dev info send");
|
||||
Serial.println("[UDP<=] dev info send");
|
||||
}
|
||||
}
|
||||
}
|
||||
}, nullptr, false);
|
||||
@@ -58,25 +68,27 @@ void UDP_init() {
|
||||
|
||||
void handleUdp() {
|
||||
#ifdef ESP8266
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
int packetSize = Udp.parsePacket();
|
||||
if (packetSize) {
|
||||
char udp_incomingPacket[255];
|
||||
Serial.printf("[UDP=>] Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
|
||||
received_ip = Udp.remoteIP().toString();
|
||||
int len = Udp.read(udp_incomingPacket, 255);
|
||||
if (len > 0) {
|
||||
udp_incomingPacket[len] = 0;
|
||||
if (jsonRead(configSetup, "udponoff") == "1") {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
int packetSize = Udp.parsePacket();
|
||||
if (packetSize) {
|
||||
char udp_incomingPacket[255];
|
||||
Serial.printf("[UDP=>] Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
|
||||
received_ip = Udp.remoteIP().toString();
|
||||
int len = Udp.read(udp_incomingPacket, 255);
|
||||
if (len > 0) {
|
||||
udp_incomingPacket[len] = 0;
|
||||
}
|
||||
received_udp_line = String(udp_incomingPacket);
|
||||
|
||||
if (received_udp_line.indexOf("iotm;") >= 0) {
|
||||
udp_data_parse = true;
|
||||
}
|
||||
if (received_udp_line.indexOf("mqttServer") >= 0) {
|
||||
udp_data_parse = true;
|
||||
}
|
||||
|
||||
}
|
||||
received_udp_line = String(udp_incomingPacket);
|
||||
|
||||
if (received_udp_line.indexOf("iotm;") >= 0) {
|
||||
udp_data_parse = true;
|
||||
}
|
||||
if (received_udp_line.indexOf("mqttServer") >= 0) {
|
||||
udp_data_parse = true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -88,14 +100,16 @@ void handleUdp_esp32() {
|
||||
udp.onPacket([](AsyncUDPPacket packet) {
|
||||
received_udp_line = (char*)packet.data();
|
||||
received_ip = packet.remoteIP().toString();
|
||||
|
||||
if (received_udp_line.indexOf("iotm;") >= 0) {
|
||||
udp_data_parse = true;
|
||||
if (jsonRead(configSetup, "udponoff") == "1") {
|
||||
|
||||
if (received_udp_line.indexOf("iotm;") >= 0) {
|
||||
udp_data_parse = true;
|
||||
}
|
||||
if (received_udp_line.indexOf("mqttServer") >= 0) {
|
||||
udp_data_parse = true;
|
||||
}
|
||||
|
||||
}
|
||||
if (received_udp_line.indexOf("mqttServer") >= 0) {
|
||||
udp_data_parse = true;
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
#endif
|
||||
@@ -117,7 +131,7 @@ void do_udp_data_parse() {
|
||||
Serial.println("[V] new mqtt setting received from udp and saved");
|
||||
mqtt_connection = true;
|
||||
}
|
||||
if (received_udp_line.indexOf("iotm;") >= 0) {
|
||||
if (received_udp_line.indexOf("iotm;") >= 0) {
|
||||
add_dev_in_list("dev.csv", selectFromMarkerToMarker(received_udp_line, ";", 1), selectFromMarkerToMarker(received_udp_line, ";", 2), received_ip);
|
||||
}
|
||||
}
|
||||
@@ -131,24 +145,26 @@ void add_dev_in_list(String fileName, String id, String dev_name, String ip) {
|
||||
}
|
||||
|
||||
void send_mqtt_to_udp() {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
udp_busy = true;
|
||||
String mqtt_data = "{}";
|
||||
jsonWriteStr(mqtt_data, "mqttServer", jsonRead(configSetup, "mqttServer"));
|
||||
jsonWriteInt(mqtt_data, "mqttPort", jsonReadtoInt(configSetup, "mqttPort"));
|
||||
jsonWriteStr(mqtt_data, "mqttPrefix", jsonRead(configSetup, "mqttPrefix"));
|
||||
jsonWriteStr(mqtt_data, "mqttUser", jsonRead(configSetup, "mqttUser"));
|
||||
jsonWriteStr(mqtt_data, "mqttPass", jsonRead(configSetup, "mqttPass"));
|
||||
Serial.println(mqtt_data);
|
||||
if (jsonRead(configSetup, "udponoff") == "1") {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
udp_busy = true;
|
||||
String mqtt_data = "{}";
|
||||
jsonWriteStr(mqtt_data, "mqttServer", jsonRead(configSetup, "mqttServer"));
|
||||
jsonWriteInt(mqtt_data, "mqttPort", jsonReadtoInt(configSetup, "mqttPort"));
|
||||
jsonWriteStr(mqtt_data, "mqttPrefix", jsonRead(configSetup, "mqttPrefix"));
|
||||
jsonWriteStr(mqtt_data, "mqttUser", jsonRead(configSetup, "mqttUser"));
|
||||
jsonWriteStr(mqtt_data, "mqttPass", jsonRead(configSetup, "mqttPass"));
|
||||
Serial.println(mqtt_data);
|
||||
#ifdef ESP8266
|
||||
Udp.beginPacketMulticast(udp_multicastIP, udp_port, WiFi.localIP());
|
||||
Udp.write(mqtt_data.c_str());
|
||||
Udp.endPacket();
|
||||
Udp.beginPacketMulticast(udp_multicastIP, udp_port, WiFi.localIP());
|
||||
Udp.write(mqtt_data.c_str());
|
||||
Udp.endPacket();
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
udp.broadcast(mqtt_data.c_str());
|
||||
udp.broadcast(mqtt_data.c_str());
|
||||
#endif
|
||||
Serial.println("[UDP<=] mqtt info send");
|
||||
udp_busy = false;
|
||||
Serial.println("[UDP<=] mqtt info send");
|
||||
udp_busy = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user