mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 22:22:16 +03:00
udp support
This commit is contained in:
7
include/RemoteOrdersUdp.h
Normal file
7
include/RemoteOrdersUdp.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
#include "ESPAsyncUDP.h"
|
||||
extern AsyncUDP asyncUdp;
|
||||
extern void asyncUdpInit();
|
||||
extern String uint8tToString(uint8_t* data, size_t len);
|
||||
extern bool udpPacketValidation(String& data);
|
||||
extern void udpPacketParse(String& data);
|
||||
@@ -20,6 +20,7 @@ lib_deps_external =
|
||||
Adafruit BMP280 Library
|
||||
Adafruit BME280 Library
|
||||
DallasTemperature
|
||||
ESP Async UDP
|
||||
lib_deps_internal =
|
||||
ESP Async WebServer
|
||||
GyverFilters
|
||||
|
||||
71
src/RemoteOrdersUdp.cpp
Normal file
71
src/RemoteOrdersUdp.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "RemoteOrdersUdp.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "Global.h"
|
||||
|
||||
AsyncUDP asyncUdp;
|
||||
|
||||
void asyncUdpInit() {
|
||||
//if (asyncUdp.listen(1234)) {
|
||||
if (asyncUdp.listenMulticast(IPAddress(239, 255, 255, 255), 1234)) {
|
||||
asyncUdp.onPacket([](AsyncUDPPacket packet) {
|
||||
|
||||
//Serial.print("UDP Packet Type: ");
|
||||
//Serial.print(packet.isBroadcast() ? "Broadcast" : packet.isMulticast() ? "Multicast" : "Unicast");
|
||||
//
|
||||
//Serial.print(", From: ");
|
||||
//Serial.print(packet.remoteIP());
|
||||
//Serial.print(":");
|
||||
//Serial.print(packet.remotePort());
|
||||
//
|
||||
//Serial.print(", To: ");
|
||||
//Serial.print(packet.localIP());
|
||||
//Serial.print(":");
|
||||
//Serial.print(packet.localPort());
|
||||
//
|
||||
//Serial.print(", Length: ");
|
||||
//Serial.print(packet.length());
|
||||
//
|
||||
//Serial.print(", Data: ");
|
||||
//Serial.write(packet.data(), packet.length());
|
||||
|
||||
String data = uint8tToString(packet.data(), packet.length());
|
||||
Serial.print("[i] [udp] Packet received: '");
|
||||
Serial.print(data);
|
||||
if (udpPacketValidation(data)) {
|
||||
udpPacketParse(data);
|
||||
Serial.println("', Packet valid");
|
||||
} else {
|
||||
Serial.println("', Packet invalid");
|
||||
}
|
||||
|
||||
//reply to the client
|
||||
|
||||
packet.printf("Got %u bytes of data", packet.length());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
String uint8tToString(uint8_t* data, size_t len) {
|
||||
String ret;
|
||||
while (len--) {
|
||||
ret += (char)*data++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool udpPacketValidation(String& data) {
|
||||
if (data.indexOf("iotm;") != -1 && data.indexOf(getChipId()) != -1) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//iotm;chipid;button-out-1_1
|
||||
void udpPacketParse(String& data) {
|
||||
data = selectFromMarkerToMarker(data, ";", 2);
|
||||
data.replace("_", " ");
|
||||
orderBuf += data + ",";
|
||||
}
|
||||
@@ -149,7 +149,7 @@ String getUptimeTotal() {
|
||||
static int hrs;
|
||||
EEPROM.begin(512);
|
||||
hrs = eeGetInt(0);
|
||||
String hrsStr = prettySeconds(hrs * 60);
|
||||
String hrsStr = prettySeconds(hrs * 60 * 60);
|
||||
//Serial.println(hrsStr);
|
||||
return hrsStr;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "Utils/Timings.h"
|
||||
#include "Utils\WebUtils.h"
|
||||
#include "items/ButtonInClass.h"
|
||||
#include "RemoteOrdersUdp.h"
|
||||
|
||||
void not_async_actions();
|
||||
|
||||
@@ -68,8 +69,12 @@ void setup() {
|
||||
pm.info("WebAdmin");
|
||||
web_init();
|
||||
|
||||
pm.info("InitSt");
|
||||
initSt();
|
||||
|
||||
pm.info("asyncUdpInit");
|
||||
asyncUdpInit();
|
||||
|
||||
#ifdef UDP_ENABLED
|
||||
pm.info("Broadcast UDP");
|
||||
udpInit();
|
||||
|
||||
Reference in New Issue
Block a user