Skip to content
Snippets Groups Projects
Verified Commit 6db1fa77 authored by Zoe Michaela Dietmar Pfister's avatar Zoe Michaela Dietmar Pfister :gay_pride_flag:
Browse files

Minor changes in Requestinformation and ConnectionManager

parent ce2210f3
No related branches found
No related tags found
3 merge requests!39Merge Develop into Main,!25Minor additions to host,!24Refactor of Host ESP32
......@@ -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();
}
......@@ -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
......@@ -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";
......
......@@ -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());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment