From 6db1fa77494795fe38ab31730684e945ed23324d Mon Sep 17 00:00:00 2001 From: Zoe Pfister <zoe.pfister@uibk.ac.at> Date: Thu, 26 Jan 2023 14:22:09 +0100 Subject: [PATCH] Minor changes in Requestinformation and ConnectionManager --- .../ConnectionManager.cpp | 7 ++-- .../ConnectionManager.h | 3 +- .../RequestInformation.h | 31 ++++++++++------ host/host_central_mast/src/main.cpp | 36 ++++++++++++++++--- 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.cpp b/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.cpp index 4ad6b4f..bdadaf9 100644 --- a/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.cpp +++ b/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.cpp @@ -95,9 +95,9 @@ bool ConnectionManager::gprsConnect() { bool ConnectionManager::isNetworkConnected() { return modem.isNetworkConnected(); } -std::string ConnectionManager::connect(int port, RequestInformation requestInformation) { +std::string ConnectionManager::connect(RequestInformation requestInformation) { TinyGsmClient client{modem, 0}; - if (!client.connect(requestInformation.host.c_str(), port)) { + if (!client.connect(requestInformation.host.c_str(), requestInformation.port)) { throw LTEConnectionException("Failed to connect to host"); } @@ -176,3 +176,6 @@ void ConnectionManager::logModemInformation() { int csq = modem.getSignalQuality(); esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Signal quality: %d\n", csq); } +bool ConnectionManager::isGprsConnected() { + return modem.isGprsConnected(); +} diff --git a/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.h b/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.h index 6b0d364..70b4668 100644 --- a/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.h +++ b/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.h @@ -36,7 +36,7 @@ class ConnectionManager { public: ConnectionManager(TinyGsm &modem, GPRSCredentials credentials, ModemPins modemPins); - std::string connect(int port, RequestInformation requestInformation); + std::string connect(RequestInformation requestInformation); void enableGPS(); @@ -69,6 +69,7 @@ class ConnectionManager { bool waitForNetwork(); void logModemInformation(); + bool isGprsConnected(); }; #endif // HOST_CENTRAL_MAST_CONNECTIONMANAGER_H diff --git a/host/host_central_mast/lib/NetworkConnectionManager/RequestInformation.h b/host/host_central_mast/lib/NetworkConnectionManager/RequestInformation.h index 84843cf..5d41611 100644 --- a/host/host_central_mast/lib/NetworkConnectionManager/RequestInformation.h +++ b/host/host_central_mast/lib/NetworkConnectionManager/RequestInformation.h @@ -6,31 +6,42 @@ #define HOST_CENTRAL_MAST_REQUESTINFORMATION_H #include <Arduino.h> +#include <Definitions.h> +#include <map> + +enum RequestMethod { + GET, + POST, +}; struct RequestInformation { - String method; + RequestMethod method; + std::map<String, String> headers; + int port; String host; String path; String body; - RequestInformation(String method, String host, String path, String body) { + RequestInformation(const RequestMethod &method, const String &host, const int &port, const String &path, + const String &body, const std::map<String, String> &headers) { this->method = method; + this->port = port; + this->headers = headers; this->host = host; this->path = path; this->body = body; } - // TODO: Move to configuration file - const String INFLUXDB_TOKEN = - "dUh2gbVLv7e3egqocxriDsJQNUacA9qZ5YXsYtdnVAglnHgy4nx-jDVO7nGlSF34BosfnuwnUDaviC7dQeC5RQ=="; - String buildRequest() { + String methodString = method == RequestMethod::GET ? "GET" : "POST"; String request = ""; - request += method + " " + path + " HTTP/1.1\r\n"; + request += methodString + " " + path + " HTTP/1.1\r\n"; request += "Host: " + host + "\r\n"; - request += "Authorization: Token " + INFLUXDB_TOKEN + "\r\n"; - request += "User-Agent: ESP32\r\n"; - request += "Content-Type: text/plain\r\n"; + request += "User-Agent: ESP32-Host\r\n"; + for (auto &header : headers) { + request += header.first + ": " + header.second + "\r\n"; + } + // request += "Authorization: Token " + INFLUXDB_TOKEN + "\r\n"; request += "Content-Length: " + String(body.length()) + "\r\n"; if (body.length() > 0) { request += "\r\n"; diff --git a/host/host_central_mast/src/main.cpp b/host/host_central_mast/src/main.cpp index f58fa1c..ee3b061 100644 --- a/host/host_central_mast/src/main.cpp +++ b/host/host_central_mast/src/main.cpp @@ -14,10 +14,12 @@ #include <Definitions.h> #include <ESP32Time.h> #include <NTPManager.h> +#include <ResendManager.h> #include <TinyGsmClient.h> #include <WiFi.h> #include <esp_log.h> #include <esp_now.h> +#include <map> #include <queue> #include <sys/unistd.h> @@ -51,6 +53,8 @@ TaskHandle_t ESPNOWTask; static std::queue<String> queue; +ResendManager resendManager; + void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status) { // go to sleep } @@ -90,8 +94,6 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) { // doc["timestamp"] = rtc.getEpoch(); doc["clientMac"] = macAddress; - documentToServerReadableString(doc); - // serialize json document again std::string dataString{}; serializeJson(doc, dataString); @@ -102,12 +104,12 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) { esp_log_write(ESP_LOG_ERROR, TAG_ESPNOW, "Failed to save data to SD card: %s", e.what()); } - String lineData = documentToLineProtocolString(doc); + String serverData = documentToServerReadableString(doc); - esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "Line protocol data: %s\n", lineData.c_str()); + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "Data to be sent: %s\n", serverData.c_str()); xSemaphoreTake(xMutex, portMAX_DELAY); - queue.push(lineData); + queue.push(serverData); xSemaphoreGive(xMutex); } @@ -170,6 +172,7 @@ void setup() { esp_log_level_set("*", ESP_LOG_VERBOSE); esp_log_write(ESP_LOG_DEBUG, TAG_MAIN, "%s", WiFi.macAddress().c_str()); + resendManager.init(); turnOffLEDs(); xMutex = xSemaphoreCreateMutex(); @@ -251,6 +254,29 @@ void loop() { esp_log_write(ESP_LOG_ERROR, TAG_GSM, "Error writing time to rtc: %s\n", e.what()); } + if (connectionManager.isGprsConnected()) { + esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "GPRS connected\n"); + // make list of map of headers + std::map<String, String> headers; + headers["Content-Type"] = "application/json"; + headers["Accept"] = "application/json"; + + xSemaphoreTake(xMutex, portMAX_DELAY); + String data = queue.front(); + queue.pop(); + xSemaphoreGive(xMutex); + + try { + RequestInformation requestInformation{POST, "influxdb.qe-forte.uibk.ac.at", + 80, "/api/v2/write?org=QE&bucket=esp32test&precision=s", + data, headers}; + connectionManager.connect(requestInformation); + } catch (const std::exception &e) { + esp_log_write(ESP_LOG_ERROR, TAG_GSM, "Error sending data: %s\n", e.what()); + } + esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Data sent: %s\n", data.c_str()); + } + // RequestInformation requestInformation("GET", "vsh.pp.ua", "/TinyGSM/logo.txt", ""); // auto s = connectionManager.connect(80, requestInformation); // esp_log_write(ESP_LOG_DEBUG, TAG_GSM, "Response: %s\n", s.c_str()); -- GitLab