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

Minor cleanup

parent 8f675f68
No related branches found
No related tags found
2 merge requests!39Merge Develop into Main,!27Move from json-string based transfer between client and host to C struct / class based transfer
//
// Created by zoe on 2/7/23.
//
#ifndef HOST_CENTRAL_MAST_QUEUESTRUCT_H
#define HOST_CENTRAL_MAST_QUEUESTRUCT_H
struct QueueStruct {
std::array<ClientDataPackage, 6> data;
uint8_t mac[6]{};
};
#endif // HOST_CENTRAL_MAST_QUEUESTRUCT_H
...@@ -237,6 +237,40 @@ namespace SDUtilities { ...@@ -237,6 +237,40 @@ namespace SDUtilities {
} }
} // namespace SDUtilities } // namespace SDUtilities
namespace ESPUtilities {
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;
}
void checkPeerExistence(const uint8_t *mac) {
if (!esp_now_is_peer_exist(mac)) {
esp_now_peer_info_t client = {};
memcpy(client.peer_addr, mac, sizeof(uint8_t) * 6);
client.encrypt = false;
client.channel = 0;
esp_err_t status = esp_now_add_peer(&client);
if (status != ESP_OK) {
esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "Failed to add new Peer: %d", status);
}
}
}
void sendResponse(const uint8_t *mac) {
response response = {};
response.type = dataAck;
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));
}
} // namespace ESPUtilities
// I don't think this does anything. Copied from the example // I don't think this does anything. Copied from the example
void turnOffLEDs() { // Set LED OFF void turnOffLEDs() { // Set LED OFF
pinMode(LED_PIN, OUTPUT); pinMode(LED_PIN, OUTPUT);
...@@ -248,15 +282,6 @@ void turnOffLEDs() { // Set LED OFF ...@@ -248,15 +282,6 @@ void turnOffLEDs() { // Set LED OFF
digitalWrite(PWR_PIN, LOW); 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;
}
String documentToLineProtocolString(const DynamicJsonDocument &doc) { String documentToLineProtocolString(const DynamicJsonDocument &doc) {
String measurementType = doc["measurementType"].as<String>(); String measurementType = doc["measurementType"].as<String>();
String sensorName = doc["sensorName"].as<String>(); String sensorName = doc["sensorName"].as<String>();
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define HOST_CENTRAL_MAST_UTILITIES_H #define HOST_CENTRAL_MAST_UTILITIES_H
#include "ArduinoJson.h" #include "ArduinoJson.h"
#include "MessageType.h"
#include "SD.h" #include "SD.h"
#include "SDCardException.h" #include "SDCardException.h"
#include "SDSetupException.h" #include "SDSetupException.h"
...@@ -14,6 +15,7 @@ ...@@ -14,6 +15,7 @@
#include <ClientDataPackage.hpp> #include <ClientDataPackage.hpp>
#include <Definitions.h> #include <Definitions.h>
#include <WString.h> #include <WString.h>
#include <esp_now.h>
#include <list> #include <list>
namespace SDUtilities { namespace SDUtilities {
...@@ -30,6 +32,13 @@ namespace SDUtilities { ...@@ -30,6 +32,13 @@ namespace SDUtilities {
bool isSDAvailable(); bool isSDAvailable();
} // namespace SDUtilities } // namespace SDUtilities
namespace ESPUtilities {
String getMacAddressAsString(const uint8_t *mac);
void checkPeerExistence(const uint8_t *mac);
void sendResponse(const uint8_t *mac);
} // namespace ESPUtilities
void turnOffLEDs(); void turnOffLEDs();
String getMacAddressAsString(const uint8_t *mac); String getMacAddressAsString(const uint8_t *mac);
String documentToLineProtocolString(const DynamicJsonDocument &doc); String documentToLineProtocolString(const DynamicJsonDocument &doc);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "ClientDataPackage.hpp" #include "ClientDataPackage.hpp"
#include "ConnectionManager.h" #include "ConnectionManager.h"
#include "MessageType.h" #include "MessageType.h"
#include "QueueStruct.h"
#include "SDCardLogger.h" #include "SDCardLogger.h"
#include "SPI.h" #include "SPI.h"
#include "Utilities.h" #include "Utilities.h"
...@@ -51,31 +52,6 @@ SemaphoreHandle_t xMutex; ...@@ -51,31 +52,6 @@ SemaphoreHandle_t xMutex;
TaskHandle_t ESPNOWTask; TaskHandle_t ESPNOWTask;
struct QueueStruct {
std::array<ClientDataPackage, 6> data;
uint8_t mac[6]{};
};
void sendResponse(const uint8_t *mac) {
response response = {};
response.type = dataAck;
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));
}
void checkPeerExistence(const uint8_t *mac) {
if (!esp_now_is_peer_exist(mac)) {
esp_now_peer_info_t client = {};
memcpy(client.peer_addr, mac, sizeof(uint8_t) * 6);
client.encrypt = false;
client.channel = 0;
esp_err_t status = esp_now_add_peer(&client);
if (status != ESP_OK) {
esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, "Failed to add new Peer: %d", status);
}
}
}
void handleDataReceive(const uint8_t *mac, const uint8_t *incomingData); void handleDataReceive(const uint8_t *mac, const uint8_t *incomingData);
static std::queue<QueueStruct> queue; static std::queue<QueueStruct> queue;
...@@ -90,10 +66,11 @@ void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status) { ...@@ -90,10 +66,11 @@ void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status) {
* @param len length of the incoming data in bytes * @param len length of the incoming data in bytes
*/ */
void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) { void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) {
checkPeerExistence(mac); ESPUtilities::checkPeerExistence(mac);
sendResponse(mac); ESPUtilities::sendResponse(mac);
// requires queue and I am too lazy to extern declare the queue
handleDataReceive(mac, incomingData); handleDataReceive(mac, incomingData);
} }
...@@ -179,27 +156,28 @@ void setup() { ...@@ -179,27 +156,28 @@ void setup() {
// To skip it, call init() instead of restart() // To skip it, call init() instead of restart()
} }
void loop() { bool isQueueEmpty() {
bool emptyQueue; bool emptyQueue;
// check if queue is empty
xSemaphoreTake(xMutex, portMAX_DELAY); xSemaphoreTake(xMutex, portMAX_DELAY);
emptyQueue = queue.empty(); emptyQueue = queue.empty();
xSemaphoreGive(xMutex); xSemaphoreGive(xMutex);
return emptyQueue;
}
void loop() {
// exit after 10 retries // exit after 10 retries
int earlyExitCounter = 0; int earlyExitCounter = 0;
// do while loop that takes one element of the queue each until the queue is empty // do while loop that takes one element of the queue each until the queue is empty
while (!emptyQueue) { while (!isQueueEmpty()) {
xSemaphoreTake(xMutex, portMAX_DELAY); xSemaphoreTake(xMutex, portMAX_DELAY);
auto data = queue.front(); auto data = queue.front();
queue.pop(); queue.pop();
xSemaphoreGive(xMutex); xSemaphoreGive(xMutex);
// get mac address as string // get mac address as string
String macAddress = getMacAddressAsString(data.mac); String macAddress = ESPUtilities::getMacAddressAsString(data.mac);
for (const auto &dataPackage : data.data) { for (const auto &dataPackage : data.data) {
// ignore padding messages // ignore padding messages
...@@ -222,11 +200,6 @@ void loop() { ...@@ -222,11 +200,6 @@ void loop() {
break; break;
} }
earlyExitCounter++; earlyExitCounter++;
// check if queue is empty
xSemaphoreTake(xMutex, portMAX_DELAY);
emptyQueue = queue.empty();
xSemaphoreGive(xMutex);
} }
connectionManager.modemPowerOn(); connectionManager.modemPowerOn();
......
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