From 0fe20b2b27cce9a0588c78e59e18019419f75cee Mon Sep 17 00:00:00 2001 From: Zoe Pfister <zoe.pfister@uibk.ac.at> Date: Tue, 24 Jan 2023 15:33:46 +0100 Subject: [PATCH] WIP: move tags to Definitions.h, move more functions from main.cpp to Utilities.cpp and other files --- host/host_central_mast/include/MessageType.h | 15 ++ .../ConnectionManager.cpp | 16 +- .../ConnectionManager.h | 5 +- .../lib/TimeManager/TimeManager.cpp | 13 +- .../lib/TimeManager/TimeManager.h | 3 +- .../lib/Utilities/Definitions.h | 13 + .../lib/Utilities/Utilities.cpp | 33 ++- .../lib/Utilities/Utilities.h | 2 + host/host_central_mast/src/main.cpp | 242 +++++------------- 9 files changed, 138 insertions(+), 204 deletions(-) create mode 100644 host/host_central_mast/include/MessageType.h diff --git a/host/host_central_mast/include/MessageType.h b/host/host_central_mast/include/MessageType.h new file mode 100644 index 0000000..2d99e33 --- /dev/null +++ b/host/host_central_mast/include/MessageType.h @@ -0,0 +1,15 @@ +// +// Created by zoe on 1/24/23. +// + +#ifndef HOST_CENTRAL_MAST_MESSAGETYPE_H +#define HOST_CENTRAL_MAST_MESSAGETYPE_H + +enum MessageType { dataAck, hostChange }; + +typedef struct response { + MessageType type; + uint8_t mac[6]; + long time; +} response; +#endif // HOST_CENTRAL_MAST_MESSAGETYPE_H diff --git a/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.cpp b/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.cpp index 29feb8c..a8424ed 100644 --- a/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.cpp +++ b/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.cpp @@ -3,10 +3,7 @@ // #include "ConnectionManager.h" -#include "LTEConnectionException.hpp" -#include "ModemFunctionalityLevel.h" -#include <utility> ConnectionManager::ConnectionManager(TinyGsm &modem, GPRSCredentials credentials, ModemPins modemPins) : modem(modem), credentials(std::move(credentials)), modemPins(modemPins) {} @@ -14,17 +11,16 @@ ConnectionManager::ConnectionManager(TinyGsm &modem, GPRSCredentials credentials void ConnectionManager::restartModem() { // Restart takes quite some time // To skip it, call init() instead of restart() - esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "Restarting modem...\n"); + esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Restarting modem...\n"); if (!modem.restart()) { - esp_log_write(ESP_LOG_WARN, TAG_GSM.c_str(), - "Failed to restart modem, attempting to continue without restarting\n"); + esp_log_write(ESP_LOG_WARN, TAG_GSM, "Failed to restart modem, attempting to continue without restarting\n"); } } void ConnectionManager::initModem() { - esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "Initializing modem...\n"); + esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Initializing modem...\n"); if (!modem.init()) { - esp_log_write(ESP_LOG_WARN, TAG_GSM.c_str(), "Failed to initialize modem, attempting to continue...\n"); + esp_log_write(ESP_LOG_WARN, TAG_GSM, "Failed to initialize modem, attempting to continue...\n"); } setModemFunctionalityLevel(MINIMAL); } @@ -115,7 +111,7 @@ std::string ConnectionManager::connect(int port, RequestInformation requestInfor while (client.connected()) { line += client.readStringUntil('\n'); if (line == "\r") { - esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "headers received\n"); + esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "headers received\n"); break; } } @@ -133,7 +129,7 @@ void ConnectionManager::modemPowerOn() { void ConnectionManager::modemPowerOff() { modem.sendAT("+CPOWD=1"); if (modem.waitResponse(10000L) != 1) { - esp_log_write(ESP_LOG_WARN, TAG_GSM.c_str(), "Failed to power off modem\n"); + esp_log_write(ESP_LOG_WARN, TAG_GSM, "Failed to power off modem\n"); } modem.poweroff(); pinMode(modemPins.pwr, OUTPUT); diff --git a/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.h b/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.h index 4f774d0..9ed10f3 100644 --- a/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.h +++ b/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.h @@ -7,15 +7,16 @@ #define TINY_GSM_MODEM_SIM7000 +#include "Definitions.h" #include "GPRSCredentials.h" +#include "LTEConnectionException.hpp" #include "Mode.h" #include "ModemFunctionalityLevel.h" #include "NetworkMode.h" #include "RequestInformation.h" #include "StreamDebugger.h" #include <TinyGsmClient.h> - -static const std::string TAG_GSM = "GSM"; +#include <utility> class ConnectionManager { diff --git a/host/host_central_mast/lib/TimeManager/TimeManager.cpp b/host/host_central_mast/lib/TimeManager/TimeManager.cpp index 4d5c5b5..3509b80 100644 --- a/host/host_central_mast/lib/TimeManager/TimeManager.cpp +++ b/host/host_central_mast/lib/TimeManager/TimeManager.cpp @@ -21,15 +21,16 @@ void TimeManager::tryNtpServerSync(const std::string &backupNtpServer, return; } catch (NTPException &e) { if (server == backupNtpServer) { - esp_log_write(ESP_LOG_ERROR, TAG, "Failed to sync NTP with %s (%s)\n", server.c_str(), e.what()); + esp_log_write(ESP_LOG_ERROR, TAG_TIMEMANAGER, "Failed to sync NTP with %s (%s)\n", server.c_str(), + e.what()); } - esp_log_write(ESP_LOG_ERROR, TAG, "Failed to sync NTP with %s (%s). Retrying with backup...\n", + esp_log_write(ESP_LOG_ERROR, TAG_TIMEMANAGER, "Failed to sync NTP with %s (%s). Retrying with backup...\n", server.c_str(), e.what()); } } } void TimeManager::synchronizeNTPWithModem(const std::string &ntpServer) { - esp_log_write(ESP_LOG_DEBUG, TAG, "NTP Server Syncing...\n"); + esp_log_write(ESP_LOG_DEBUG, TAG_TIMEMANAGER, "NTP Server Syncing...\n"); long error = modem.NTPServerSync(ntpServer.c_str(), timeZone); /** @@ -46,7 +47,7 @@ void TimeManager::synchronizeNTPWithModem(const std::string &ntpServer) { if (error != 1 && error != 255) { throw NTPException(modem.ShowNTPError(error).c_str()); } else { - esp_log_write(ESP_LOG_DEBUG, TAG, "NTP: Network time synchronization is successful\n"); + esp_log_write(ESP_LOG_DEBUG, TAG_TIMEMANAGER, "NTP: Network time synchronization is successful\n"); } } @@ -79,9 +80,9 @@ time_t TimeManager::timeToUnixEpochSeconds(const std::string &time, const std::s void TimeManager::writeModemTimeToRTC() { auto gsmDateTimeString = modem.getGSMDateTime(DATE_FULL); - esp_log_write(ESP_LOG_DEBUG, TAG, "GSM DateTime: %s\n", gsmDateTimeString.c_str()); + esp_log_write(ESP_LOG_DEBUG, TAG_TIMEMANAGER, "GSM DateTime: %s\n", gsmDateTimeString.c_str()); time_t time = timeToUnixEpochSeconds(gsmDateTimeString.c_str()); rtc.setTime(time); - esp_log_write(ESP_LOG_INFO, TAG, "Time set to EPOCH: %s\n", String(rtc.getEpoch()).c_str()); + esp_log_write(ESP_LOG_INFO, TAG_TIMEMANAGER, "Time set to EPOCH: %s\n", String(rtc.getEpoch()).c_str()); } diff --git a/host/host_central_mast/lib/TimeManager/TimeManager.h b/host/host_central_mast/lib/TimeManager/TimeManager.h index d970a29..a5e5cca 100644 --- a/host/host_central_mast/lib/TimeManager/TimeManager.h +++ b/host/host_central_mast/lib/TimeManager/TimeManager.h @@ -7,6 +7,7 @@ #define TINY_GSM_MODEM_SIM7000 +#include "Definitions.h" #include "ESP32Time.h" #include "NTPException.hpp" #include "StringToTimeConversionException.hpp" @@ -19,8 +20,6 @@ class TimeManager { explicit TimeManager(TinyGsmSim7000 &modem); private: - constexpr static char *TAG = "TimeManager"; - ESP32Time rtc; int timeZone = 0; // I would like this to be a const reference but the functions used by the modem are not const diff --git a/host/host_central_mast/lib/Utilities/Definitions.h b/host/host_central_mast/lib/Utilities/Definitions.h index a5e7f2d..bef8797 100644 --- a/host/host_central_mast/lib/Utilities/Definitions.h +++ b/host/host_central_mast/lib/Utilities/Definitions.h @@ -23,6 +23,19 @@ #define SD_CS 13 #define LED_PIN 12 +// See all AT commands, if wanted +#define DUMP_AT_COMMANDS + +#define TAG_ESPNOW "ESPNOW" +#define TAG_MAIN "MAIN" +#define TAG_SD "SD" +#define TAG_GSM "GSM" +#define TAG_TIMEMANAGER "TIMEMANAGER" + +// global definition of RTC. Initialised in main extern ESP32Time rtc; +const String INFLUXDB_TOKEN = + "dUh2gbVLv7e3egqocxriDsJQNUacA9qZ5YXsYtdnVAglnHgy4nx-jDVO7nGlSF34BosfnuwnUDaviC7dQeC5RQ=="; + #endif // HOST_CENTRAL_MAST_DEFINITIONS_H diff --git a/host/host_central_mast/lib/Utilities/Utilities.cpp b/host/host_central_mast/lib/Utilities/Utilities.cpp index 21f3aca..9439cc4 100644 --- a/host/host_central_mast/lib/Utilities/Utilities.cpp +++ b/host/host_central_mast/lib/Utilities/Utilities.cpp @@ -4,7 +4,18 @@ #include "Utilities.h" -void setupSDCard(); +void setupSDCard() { + SPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS); + if (!SD.begin(SD_CS)) { + esp_log_write(ESP_LOG_ERROR, TAG_SD, "Card MOUNT FAIL\n"); + // TODO: Error handling + } else { + uint32_t cardSize = SD.cardSize() / (1024 * 1024); + String sdcardSizeString = "SDCard Size: " + String(cardSize) + "MB"; + esp_log_write(ESP_LOG_DEBUG, TAG_SD, "%s\n", sdcardSizeString.c_str()); + } +} + void saveStringToSDCard(const std::string &dataString); std::list<String> getFilesInDirectory(const String &dirname) { std::list<String> files; @@ -148,4 +159,24 @@ void saveStringToSDCard(const std::string &dataString) { esp_log_write(ESP_LOG_ERROR, "Utilities", "error opening datalog.txt\n"); throw SDCardException("error opening datalog.txt"); } +} + +// I don't think this does anything. Copied from the example +void turnOffLEDs() { // Set LED OFF + pinMode(LED_PIN, OUTPUT); + digitalWrite(LED_PIN, HIGH); + + pinMode(PWR_PIN, OUTPUT); + digitalWrite(PWR_PIN, HIGH); + delay(300); + digitalWrite(PWR_PIN, LOW); +} + +String getMacAddressAsString(const uint8_t *mac) { + String macAddress; + for (int i = 0; i < 6; i++) { + macAddress += String(mac[i], HEX); + } + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "MAC: %s\n", macAddress.c_str()); + return macAddress; } \ No newline at end of file diff --git a/host/host_central_mast/lib/Utilities/Utilities.h b/host/host_central_mast/lib/Utilities/Utilities.h index 1156c02..b78843c 100644 --- a/host/host_central_mast/lib/Utilities/Utilities.h +++ b/host/host_central_mast/lib/Utilities/Utilities.h @@ -22,4 +22,6 @@ String readFile(const String &filePath); void createDirectory(const String &dirname); void setupSDCard(int MISO, int MOSI, int SCLK, int CS); void saveStringToSDCard(const std::string &dataString); +void turnOffLEDs(); +String getMacAddressAsString(const uint8_t *mac); #endif // HOST_CENTRAL_MAST_UTILITIES_H diff --git a/host/host_central_mast/src/main.cpp b/host/host_central_mast/src/main.cpp index 5394036..0eea7a5 100644 --- a/host/host_central_mast/src/main.cpp +++ b/host/host_central_mast/src/main.cpp @@ -1,71 +1,42 @@ +/*** + * code is modified from the original code found at + * https://github.com/Xinyuan-LilyGO/LilyGO-T-SIM7000G/blob/master/examples/Arduino_TinyGSM/AllFunctions/AllFunctions.ino + */ + #define TINY_GSM_MODEM_SIM7000 #include "ConnectionManager.h" -#include "FS.h" -#include "SD.h" +#include "MessageType.h" #include "SDCardLogger.h" #include "SPI.h" -#include "time.h" -#include <Arduino.h> #include <ArduinoJson.h> #include <Definitions.h> #include <ESP32Time.h> +#include <TimeManager.h> +#include <TinyGsmClient.h> #include <WiFi.h> #include <esp_log.h> #include <esp_now.h> #include <queue> #include <sys/unistd.h> -static const std::string TAG = "MAIN"; -static const std::string TAG_ESPNOW = "ESPNOW"; -// static const std::string TAG_GSM = "GSM"; - -/* - FILE: AllFunctions.ino - AUTHOR: Koby Hale - PURPOSE: Test functionality -*/ +ESP32Time rtc; #define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb #define SerialAT Serial1 -// See all AT commands, if wanted -#define DUMP_AT_COMMANDS - -// set GSM PIN, if any -#define GSM_PIN "" - -// Your GPRS credentials, if any -const char apn[] = "m2m.public.at"; // SET TO YOUR APN -const char gprsUser[] = ""; -const char gprsPass[] = ""; - - -const GPRSCredentials gprsCredentials = GPRSCredentials("m2m.public.at", "", ""); - -#include <TinyGsmClient.h> - -#ifdef DUMP_AT_COMMANDS // if enabled it requires the streamDebugger lib - +#ifdef DUMP_AT_COMMANDS // if enabled it requires the streamDebugger library #include <StreamDebugger.h> - StreamDebugger debugger(SerialAT, Serial); TinyGsm modem(debugger); -ConnectionManager connectionManager{modem, gprsCredentials, - ConnectionManager::ModemPins{PIN_DTR, PIN_TX, PIN_RX, PWR_PIN}}; - #else TinyGsm modem(SerialAT); #endif -enum MessageType { dataAck, hostChange }; -typedef struct response { - MessageType type; - uint8_t mac[6]; - long time; -} response; +const GPRSCredentials gprsCredentials = GPRSCredentials("m2m.public.at", "", ""); -#include <TimeManager.h> +ConnectionManager connectionManager{modem, gprsCredentials, + ConnectionManager::ModemPins{PIN_DTR, PIN_TX, PIN_RX, PWR_PIN}}; TimeManager timeManager{modem}; @@ -73,8 +44,6 @@ uint8_t BROADCAST_MAC[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; esp_now_peer_info_t broadcast = {}; response announce = {}; -ESP32Time rtc; - SemaphoreHandle_t xMutex; TaskHandle_t ESPNOWTask; @@ -97,8 +66,6 @@ void turnOffLEDs(); void setupSDCard(); -void syncUTCTimeToRTC(); - String documentToServerReadableString(const DynamicJsonDocument &doc) { StaticJsonDocument<1024> serverDoc; String hostMacAddressString = WiFi.macAddress(); @@ -119,16 +86,16 @@ String documentToServerReadableString(const DynamicJsonDocument &doc) { String serverString; serializeJson(serverDoc, serverString); - esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "Server readable data: %s\n", serverString.c_str()); + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "Server readable data: %s\n", serverString.c_str()); return serverString; } void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) { - esp_log_write(ESP_LOG_INFO, TAG_ESPNOW.c_str(), "Message recieved\n"); + esp_log_write(ESP_LOG_INFO, TAG_ESPNOW, "Message recieved\n"); // copy received data to a char array char data[len]; memcpy(data, incomingData, len); - esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "Raw received Data: %s\n", data); + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "Raw received Data: %s\n", data); if (!esp_now_is_peer_exist(mac)) { esp_now_peer_info_t client = {}; @@ -138,7 +105,7 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) { esp_err_t status = esp_now_add_peer(&client); if (status != ESP_OK) { - esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "Failed to add new Peer: %d", status); + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "Failed to add new Peer: %d", status); } } @@ -160,7 +127,7 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) { String lineData = documentToLineProtocolString(doc); - esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "Line protocol data: %s\n", lineData.c_str()); + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "Line protocol data: %s\n", lineData.c_str()); xSemaphoreTake(xMutex, portMAX_DELAY); queue.push(lineData); @@ -171,7 +138,7 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) { esp_read_mac(response.mac, ESP_MAC_WIFI_STA); response.time = rtc.getEpoch(); esp_err_t success = esp_now_send(mac, (uint8_t *)&response, sizeof(response)); - esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), (success == ESP_OK) ? "Response sent\n" : "Failed to respond\n"); + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, (success == ESP_OK) ? "Response sent\n" : "Failed to respond\n"); } String documentToLineProtocolString(const DynamicJsonDocument &doc) { @@ -188,56 +155,30 @@ String documentToLineProtocolString(const DynamicJsonDocument &doc) { return lineData; } -void saveStringToSDCard(const std::string &dataString) { - File dataFile = SD.open("/datalog.txt", FILE_APPEND); - - // if the file is available, write to it: - if (dataFile) { - if (dataString.length() > 0) { - dataFile.println(dataString.c_str()); - } - dataFile.close(); - } - // if the file isn't open, pop up an error: - else { - esp_log_write(ESP_LOG_ERROR, TAG.c_str(), "error opening datalog.txt\n"); - // TODO: Error handling - } -} - DynamicJsonDocument parseReceivedJsonData(char *data) { DynamicJsonDocument doc(250); auto error = deserializeJson(doc, data); if (error) { - esp_log_write(ESP_LOG_ERROR, TAG_ESPNOW.c_str(), "Error while parsing json: %s\n", error.f_str()); + esp_log_write(ESP_LOG_ERROR, TAG_ESPNOW, "Error while parsing json: %s\n", error.f_str()); // TODO error handling } return doc; } -String getMacAddressAsString(const uint8_t *mac) { - String macAddress; - for (int i = 0; i < 6; i++) { - macAddress += String(mac[i], HEX); - } - esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "MAC: %s\n", macAddress.c_str()); - return macAddress; -} - [[noreturn]] void ESPNOWReceiveTask(void *parameter) { - esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "ESPNOWReceiveTask started on core %d\n", xPortGetCoreID()); + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "ESPNOWReceiveTask started on core %d\n", xPortGetCoreID()); WiFi.mode(WIFI_STA); - esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "Initialising ESPNow...\n"); + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "Initialising ESPNow...\n"); if (esp_now_init() != ESP_OK) { // initialization failed - esp_log_write(ESP_LOG_ERROR, TAG_ESPNOW.c_str(), "Initialising ESPNow FAILED\n"); + esp_log_write(ESP_LOG_ERROR, TAG_ESPNOW, "Initialising ESPNow FAILED\n"); exit(ESP_FAIL); } - esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "Initialising ESPNow SUCCESS\n"); + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "Initialising ESPNow SUCCESS\n"); esp_now_register_recv_cb(on_data_recv); @@ -245,46 +186,22 @@ String getMacAddressAsString(const uint8_t *mac) { broadcast.encrypt = false; memcpy(&broadcast.peer_addr, &BROADCAST_MAC, sizeof(BROADCAST_MAC)); if (esp_now_add_peer(&broadcast) != ESP_OK) { - esp_log_write(ESP_LOG_WARN, TAG_ESPNOW.c_str(), "Failed to add Broadcast Host"); + esp_log_write(ESP_LOG_WARN, TAG_ESPNOW, "Failed to add Broadcast Host"); } announce.type = hostChange; esp_read_mac(announce.mac, ESP_MAC_WIFI_STA); announce.time = rtc.getEpoch(); if (esp_now_send(BROADCAST_MAC, (uint8_t *)&announce, sizeof(announce)) != ESP_OK) { - esp_log_write(ESP_LOG_WARN, TAG_ESPNOW.c_str(), "Failed to announce mac"); + esp_log_write(ESP_LOG_WARN, TAG_ESPNOW, "Failed to announce mac"); } else { - esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "Mac announced!"); + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "Mac announced!"); } while (true) { } } -time_t timeToUnixEpochSeconds(const std::string &time) { - // 22/10/27,10:16:20+00 - struct tm tm {}; - time_t dateInEpoch = 0; - - if (strptime(time.c_str(), "%y/%m/%d,%T+00", &tm)) { - time_t curTime; - struct tm *timeinfo; - - timeinfo = localtime(&curTime); - - timeinfo->tm_year = tm.tm_year; - timeinfo->tm_mon = tm.tm_mon; - timeinfo->tm_mday = tm.tm_mday; - timeinfo->tm_hour = tm.tm_hour; - timeinfo->tm_min = tm.tm_min; - timeinfo->tm_sec = tm.tm_sec; - timeinfo->tm_isdst = -1; - - dateInEpoch = mktime(timeinfo); - } - return dateInEpoch; -} - void setup() { // Set console baud rate Serial.begin(115200); @@ -296,7 +213,7 @@ void setup() { // https://stackoverflow.com/questions/60442350/arduinos-esp-log-set-vprintf-does-not-work-on-esp32 esp_log_set_vprintf(&SDCardLogger::vprintf_into_sd); esp_log_level_set("*", ESP_LOG_VERBOSE); - esp_log_write(ESP_LOG_DEBUG, TAG.c_str(), "%s", WiFi.macAddress().c_str()); + esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "%s", WiFi.macAddress().c_str()); turnOffLEDs(); @@ -320,47 +237,6 @@ void setup() { // To skip it, call init() instead of restart() } -// waitResponse default is 1000ms - -void setupSDCard() { - SPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS); - if (!SD.begin(SD_CS)) { - esp_log_write(ESP_LOG_ERROR, TAG.c_str(), "Card MOUNT FAIL\n"); - // TODO: Error handling - } else { - uint32_t cardSize = SD.cardSize() / (1024 * 1024); - String sdcardSizeString = "SDCard Size: " + String(cardSize) + "MB"; - esp_log_write(ESP_LOG_DEBUG, TAG.c_str(), "%s\n", sdcardSizeString.c_str()); - } -} - -// I don't think this does anything. Copied from the example -void turnOffLEDs() { // Set LED OFF - pinMode(LED_PIN, OUTPUT); - digitalWrite(LED_PIN, HIGH); - - pinMode(PWR_PIN, OUTPUT); - digitalWrite(PWR_PIN, HIGH); - delay(300); - digitalWrite(PWR_PIN, LOW); -} - -const String INFLUXDB_TOKEN = - "dUh2gbVLv7e3egqocxriDsJQNUacA9qZ5YXsYtdnVAglnHgy4nx-jDVO7nGlSF34BosfnuwnUDaviC7dQeC5RQ=="; - -String buildRequest(const RequestInformation &requestInformation) { - String request = ""; - request += requestInformation.method + " " + requestInformation.path + " HTTP/1.1\r\n"; - request += "Host: " + requestInformation.host + "\r\n"; - request += "Authorization: Token " + INFLUXDB_TOKEN + "\r\n"; - request += "User-Agent: ESP32\r\n"; - request += "Content-Type: text/plain\r\n"; - request += "Content-Length: " + String(requestInformation.body.length()) + "\r\n"; - request += "\r\n"; - request += requestInformation.body; - return request; -} - void loop() { connectionManager.modemPowerOn(); @@ -372,9 +248,9 @@ void loop() { announce.time = rtc.getEpoch(); if (esp_now_send(BROADCAST_MAC, (uint8_t *)&announce, sizeof(announce)) != ESP_OK) { - esp_log_write(ESP_LOG_WARN, TAG_ESPNOW.c_str(), "Failed to announce mac\n"); + esp_log_write(ESP_LOG_WARN, TAG_ESPNOW, "Failed to announce mac\n"); } else { - esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "Mac announced!\n"); + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "Mac announced!\n"); } connectionManager.disableGPS(); @@ -383,74 +259,74 @@ void loop() { connectionManager.setNetworkMode(LTE_ONLY); connectionManager.setPreferredMode(CAT_M); } catch (const std::exception &e) { - esp_log_write(ESP_LOG_ERROR, TAG_GSM.c_str(), "Error setting network mode: %s\n", e.what()); + esp_log_write(ESP_LOG_ERROR, TAG_GSM, "Error setting network mode: %s\n", e.what()); } String name = modem.getModemName(); - esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "Modem Name: %s\n", name.c_str()); + esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Modem Name: %s\n", name.c_str()); String modemInfo = modem.getModemInfo(); - esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "Modem Info: %s\n", modemInfo.c_str()); + esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Modem Info: %s\n", modemInfo.c_str()); connectionManager.unlockSimCard(); if (!connectionManager.waitForNetwork()) { - esp_log_write(ESP_LOG_ERROR, TAG_GSM.c_str(), "Network not available\n"); + esp_log_write(ESP_LOG_ERROR, TAG_GSM, "Network not available\n"); return; } if (connectionManager.isNetworkConnected()) { - esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "Network connected\n"); + esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Network connected\n"); } delay(200); - esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "Connecting to %s\n", gprsCredentials.apn.c_str()); + esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Connecting to %s\n", gprsCredentials.apn.c_str()); if (!connectionManager.gprsConnect()) { - esp_log_write(ESP_LOG_ERROR, TAG_GSM.c_str(), "GPRS not connected\n"); + esp_log_write(ESP_LOG_ERROR, TAG_GSM, "GPRS not connected\n"); return; } bool res = modem.isGprsConnected(); - esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "GPRS connected: %s\n", res ? "true" : "false"); + esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "GPRS connected: %s\n", res ? "true" : "false"); String ccid = modem.getSimCCID(); - esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "CCID: %s\n", ccid.c_str()); + esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "CCID: %s\n", ccid.c_str()); String imei = modem.getIMEI(); - esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "IMEI: %s\n", imei.c_str()); + esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "IMEI: %s\n", imei.c_str()); String imsi = modem.getIMSI(); - esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "IMSI: %s\n", imsi.c_str()); + esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "IMSI: %s\n", imsi.c_str()); String cop = modem.getOperator(); - esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "Operator: %s\n", cop.c_str()); + esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Operator: %s\n", cop.c_str()); IPAddress local = modem.localIP(); - esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "Local IP: %s\n", local.toString().c_str()); + esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Local IP: %s\n", local.toString().c_str()); int csq = modem.getSignalQuality(); - esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "Signal quality: %d\n", csq); + esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Signal quality: %d\n", csq); timeManager.syncNTP("pool.ntp.org", "at.pool.ntp.org"); try { timeManager.writeModemTimeToRTC(); } catch (const std::exception &e) { - esp_log_write(ESP_LOG_ERROR, TAG_GSM.c_str(), "Error writing time to rtc: %s\n", e.what()); + esp_log_write(ESP_LOG_ERROR, TAG_GSM, "Error writing time to rtc: %s\n", e.what()); } // RequestInformation requestInformation("GET", "vsh.pp.ua", "/TinyGSM/logo.txt", ""); // auto s = connectionManager.connect(80, requestInformation); - // esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "Response: %s\n", s.c_str()); + // esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Response: %s\n", s.c_str()); connectionManager.gprsDisconnect(); delay(5000L); if (!modem.isGprsConnected()) { - esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "GPRS disconnected\n"); + esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "GPRS disconnected\n"); } else { - esp_log_write(ESP_LOG_ERROR, TAG_GSM.c_str(), "GPRS not disconnected\n"); + esp_log_write(ESP_LOG_ERROR, TAG_GSM, "GPRS not disconnected\n"); } connectionManager.modemPowerOff(); @@ -469,9 +345,9 @@ void loop() { // modem.disableGPS(); // delay(200); - // esp_log_write(ESP_LOG_DEBUG, TAG.c_str(), "%s\n", String(modem.getSignalQuality()).c_str()); + // esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "%s\n", String(modem.getSignalQuality()).c_str()); // - // esp_log_write(ESP_LOG_DEBUG, TAG.c_str(), "Trying to connect to network\n"); + // esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "Trying to connect to network\n"); // // Serial.println("\n\n\nWaiting for network..."); // if (!modem.waitForNetwork()) { @@ -479,12 +355,12 @@ void loop() { // return; // } - // esp_log_write(ESP_LOG_DEBUG, TAG.c_str(), "Waiting for network...\n"); + // esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "Waiting for network...\n"); // if (!connectionManager.isNetworkConnected()) { - // esp_log_write(ESP_LOG_DEBUG, TAG.c_str(), "Network not connected\n"); + // esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "Network not connected\n"); // return; // } else { - // esp_log_write(ESP_LOG_DEBUG, TAG.c_str(), "Network connected\n"); + // esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "Network connected\n"); // delay(200); // connectionManager.gprsConnect(); // delay(200); @@ -492,12 +368,12 @@ void loop() { // syncUTCTimeToRTC(); // // // quality - // esp_log_write(ESP_LOG_DEBUG, TAG.c_str(), "%s\n", String(modem.getSignalQuality()).c_str()); + // esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "%s\n", String(modem.getSignalQuality()).c_str()); // // make a http post request // String url = "influxdb.qe-forte.uibk.ac.at"; // String path = "/api/v2/write?org=QE&bucket=esp32test&precision=s"; // Serial.print("Connecting to "); - // esp_log_write(ESP_LOG_DEBUG, TAG.c_str(), "%s\n", url.c_str()); + // esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "%s\n", url.c_str()); // // Use WiFiClient class to create TCP connections // // while (!queue.empty()) { @@ -513,13 +389,13 @@ void loop() { // //"sensorName":"DRS26","timestamp":1666872216,"protocol":"I2C","value":0,"channel":0,"measurementType":"CIRCUMFERENCE_INCREMENT" // // String request = buildRequest(requestInformation); - // esp_log_write(ESP_LOG_VERBOSE, TAG.c_str(), "request: %s\n", request.c_str()); + // esp_log_write(ESP_LOG_VERBOSE, TAG_MAIN, "request: %s\n", request.c_str()); // // connectionManager.connect(80, requestInformation); // // TinyGsmClient client{modem}; // // const int httpPort = 80; // // if (!client.connect(url.c_str(), httpPort)) { - // // esp_log_write(ESP_LOG_DEBUG, TAG.c_str(), "connection failed\n"); + // // esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "connection failed\n"); // // return; // // } // // @@ -529,7 +405,7 @@ void loop() { // // while (client.connected()) { // // String line = client.readStringUntil('\n'); // // if (line == "\r") { - // // esp_log_write(ESP_LOG_DEBUG, TAG.c_str(), "headers received\n"); + // // esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "headers received\n"); // // break; // // } // // } @@ -547,7 +423,7 @@ void loop() { // DBG("+CPOWD=1"); // } // modem.poweroff(); - esp_log_write(ESP_LOG_DEBUG, TAG.c_str(), "Poweroff."); + esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "Poweroff."); #endif delay(5000); -- GitLab