From b8aac04ef26b1edf3e645ffc140ebe1c823a1cef Mon Sep 17 00:00:00 2001 From: Alex Alvarado <931857-alexb737@users.noreply.gitlab.com> Date: Tue, 17 May 2022 12:06:04 +0200 Subject: [PATCH] Created helper functions --- include/Helpers.hpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 include/Helpers.hpp diff --git a/include/Helpers.hpp b/include/Helpers.hpp new file mode 100644 index 0000000..35e9876 --- /dev/null +++ b/include/Helpers.hpp @@ -0,0 +1,44 @@ +/* Funciones Personalizadas YHP */ + +/**********************************************/ +/* Genera un log en el puerto Serial */ +void log(String s){ + Serial.println(s); +} +/**********************************************/ +/* Retorna IPAddress en formato "n.n.n.n" de IP a String */ +String ipStr(const IPAddress &ip){ + String sFn = ""; + for (byte bFn = 0; bFn < 3; bFn++) + { + sFn += String((ip >> (8 * bFn)) & 0xFF) + "."; + } + sFn += String(((ip >> 8 * 3)) & 0xFF); + return sFn; +} + +// De HEX->String +String hexStr(const unsigned long &h, const byte &l = 8){ + String s; + s = String(h, HEX); + s.toUpperCase(); + s = ("00000000" + s).substring(s.length() + 8 - l); + return s; +} + +// Create a Unique ID from MAC address */ +String idUnique(){ + // Retorna los ultimos 4 Bytes del MAC rotados + char idunique[15]; + uint64_t chipid = ESP.getEfuseMac(); + uint16_t chip = (uint16_t)(chipid >> 32); + snprintf(idunique, 15, "%04X", chip); + return idunique; +} +/**********************************************/ +// ID del Dispositivo para La Base de Datos +const String device_id = hexStr(ESP.getEfuseMac()) + "CE" + String(idUnique()); +/* ESP32 utiliza funcion getEfuseMac() */ +String deviceID(){ + return "ESP32" + hexStr(ESP.getEfuseMac()) + String(idUnique()); +} \ No newline at end of file