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

- add additional constructors to TEROS10, DR26

- move get_host_mac to RtcMemory implementation
- allow hardcoding of mac address of host on client
parent fa1905ee
No related branches found
No related tags found
2 merge requests!39Merge Develop into Main,!36Make the host send to the actual API of our server
Pipeline #117171 failed
#include "DeepSleep.hpp"
#include "ESPNow.hpp"
#include "MockSensor.hpp"
#include "NoDataAvailableException.hpp"
#include "DeepSleep.hpp"
#include <Arduino.h>
#include <ClientDataPackage.hpp>
#include <soc/rtc_cntl_reg.h>
#include <LC709203F.hpp>
#include <soc/rtc_cntl_reg.h>
static const char *TAG = "MAIN";
......@@ -16,7 +15,6 @@ MockSensor mock_channel3{3};
LC709203F battery{};
// one loop takes ~2200 ms
void setup() {
// disable brownout
......@@ -27,6 +25,7 @@ void setup() {
DeepSleep::printWakeupReason();
DeepSleep::bootCount++;
ESP_LOGD(TAG, "Boot number: %d", DeepSleep::bootCount);
ESP_LOGD(TAG, "Mac address: %s", WiFi.macAddress().c_str());
// delay(100);
mock_channel0.setup();
......@@ -35,80 +34,41 @@ void setup() {
mock_channel3.setup();
battery.setup();
// disable led
gpio_set_direction(GPIO_NUM_32, GPIO_MODE_OUTPUT);
ESP_LOGD(TAG, "Setup took %ld ms", millis() - ts);
try {
// FIXME: put me into seperate trys? No data will be sent when 1 exception occurs
ts = millis();
auto messages0 = mock_channel0.buildMessages();
auto messages1 = mock_channel1.buildMessages();
auto messages2 = mock_channel2.buildMessages();
auto messages3 = mock_channel3.buildMessages();
auto batteryMessages = battery.buildMessages();
// roughly takes 500ms, ~120ms for each adc channel, barely anything for battery monitor
ESP_LOGD(TAG, "Reading data and building messages took %ld ms", millis() - ts);
gpio_set_level(GPIO_NUM_32, 0);
ESP_LOGD(TAG, "Size of message to be sent: %d", sizeof(messages0.front()));
ESP_LOGD(TAG, "Size of Message class: %d", sizeof(Message));
ESP_LOGD(TAG, "Size of ClientDataPackage class: %d", sizeof(ClientDataPackage));
// sizeof string
ESP_LOGD(TAG, "Size of string: %d", sizeof(std::string));
// sizeof string with 5 char
ESP_LOGD(TAG, "Size of string with 5 char: %d", sizeof(char[5]));
// sizeof optional int
ESP_LOGD(TAG, "Size of optional int: %d", sizeof(std::optional<int>));
// sizeof int
ESP_LOGD(TAG, "Size of int: %d", sizeof(int));
// sizeof short
ESP_LOGD(TAG, "Size of short: %d", sizeof(short));
// sizeof double
ESP_LOGD(TAG, "Size of double: %d", sizeof(double));
// sizeof float
ESP_LOGD(TAG, "Size of float: %d", sizeof(float));
// list of 5 compresseddatapackage
ClientDataPackage compresseddatapackage{};
ESP_LOGD(TAG, "Size of list of 4 ClientDataPackage: %d",
sizeof(std::list<ClientDataPackage>) + (sizeof(ClientDataPackage) * 4));
// sizeof compresseddatapackage
ESP_LOGD(TAG, "Size of ClientDataPackage: %d", sizeof(ClientDataPackage));
// sizeof list
ESP_LOGD(TAG, "Size of list: %d", sizeof(std::list<ClientDataPackage>));
// sizeof vector
ESP_LOGD(TAG, "Size of vector: %d", sizeof(std::vector<ClientDataPackage>));
// FIXME: put this outside the try loop?
ts = millis();
espnow_setup();
ESP_LOGD(TAG, "EPSNow setup took %ld ms", millis() - ts);
// make a list of messages
std::array<Message, 15> messages = {
messages0.front(), messages1.front(), messages2.front(), batteryMessages.front(),
Message::nullMessage(), Message::nullMessage(), Message::nullMessage(), Message::nullMessage(),
Message::nullMessage(), Message::nullMessage(), Message::nullMessage(), Message::nullMessage(),
Message::nullMessage(), Message::nullMessage(), Message::nullMessage(),
};
// print messages
for (const Message &msg : messages) {
ESP_LOGD(TAG, "Message: %s", msg.getMessageAsMinifiedJsonString().c_str());
}
ts = millis();
Message::sendMessages(messages);
ESP_LOGD(TAG, "Sending messages took %ld ms", millis() - ts);
} catch (const NoDataAvailableException &e) {
std::cerr << e.what() << '\n';
// FIXME: put me into seperate trys? No data will be sent when 1 exception occurs
ts = millis();
auto messages0 = mock_channel0.buildMessages();
auto messages1 = mock_channel1.buildMessages();
auto messages2 = mock_channel2.buildMessages();
auto messages3 = mock_channel3.buildMessages();
auto batteryMessages = battery.buildMessages();
ts = millis();
espnow_setup();
ESP_LOGD(TAG, "EPSNow setup took %ld ms", millis() - ts);
// make a list of messages
std::array<Message, 15> messages = {
messages0.front(), messages1.front(), messages2.front(), messages3.front(),
Message::nullMessage(), Message::nullMessage(), Message::nullMessage(), Message::nullMessage(),
Message::nullMessage(), Message::nullMessage(), Message::nullMessage(), Message::nullMessage(),
Message::nullMessage(), Message::nullMessage(), Message::nullMessage(),
};
// print messages
for (const Message &msg : messages) {
ESP_LOGD(TAG, "Message: %s", msg.getMessageAsMinifiedJsonString().c_str());
}
ts = millis();
Message::sendMessages(messages);
ESP_LOGD(TAG, "Sending messages took %ld ms", millis() - ts);
// battery protection: go to deep sleep for unlimited time when voltage less than 3.2V
DeepSleep::deepSleepSeconds(5);
DeepSleep::deepSleepSeconds(10);
}
void loop() {}
\ No newline at end of file
......@@ -50,7 +50,6 @@ Measurement DR26::readData() {
ErrorType::DATA_OK};
}
void DR26::setVoltageRange(ADS1115_RANGE newVoltageRange) {
ads.setVoltageRange_mV(newVoltageRange);
}
......@@ -71,4 +70,10 @@ SensorInformation DR26::getSensorInformation() const {
return sensorInformation;
}
DR26::DR26(ADS1115_MUX channel, ADS1115_RANGE voltageRange) : channel(channel), voltageRange(voltageRange) {}
DR26::DR26(ADS1115_MUX channel, ADS1115_RANGE voltageRange) : channel(channel), voltageRange(voltageRange) {
ads = ADS1115_WE(0x48);
}
DR26::DR26(ADS1115_MUX channel, ADS1115_RANGE voltageRange, uint8_t address)
: channel(channel), voltageRange(voltageRange) {
ads = ADS1115_WE(address);
}
......@@ -55,6 +55,8 @@ public:
*/
explicit DR26(ADS1115_MUX channel, ADS1115_RANGE voltageRange);
explicit DR26(ADS1115_MUX channel, ADS1115_RANGE voltageRange, uint8_t address);
private:
float readChannel(ADS1115_MUX compareChannel);
......
......@@ -13,7 +13,7 @@ Measurement MockSensor::readData() {
// generate a random float value between 0 and 100
float randomValue = DeepSleep::bootCount * 10 + channel;
ESP_LOGD(TAG, "MOCK Sensor read value: %f", randomValue);
return {randomValue, channel, NO_I2C_ADDRESS, MeasurementType::MOCK,
return {randomValue, channel, NO_I2C_ADDRESS, MeasurementType::SOIL_TEMPERATURE,
ErrorType::DATA_OK};
}
......
......@@ -9,7 +9,6 @@
#include "../time/Time.hpp"
#include "ForteSensor.hpp"
#include "Measurement.hpp"
#include "Pinout.hpp"
#include "RTClib.h" // adafruit/RTClib @^2.1.1
#include "SHTSensor.h" // sensirion/arduino-sht@^1.2.2
#include "SPI.h"
......
#include "TEROS10.hpp"
//TODO: Refactor
// TODO: Refactor
static const char *TAG = "TEROS10";
......@@ -78,4 +78,11 @@ SensorInformation TEROS10::getSensorInformation() const {
return sensorInformation;
}
TEROS10::TEROS10(ADS1115_MUX channel, ADS1115_RANGE voltageRange) : channel(channel), voltageRange(voltageRange) {}
TEROS10::TEROS10(ADS1115_MUX channel, ADS1115_RANGE voltageRange) : channel(channel), voltageRange(voltageRange) {
ads = ADS1115_WE(0x48);
}
TEROS10::TEROS10(ADS1115_MUX channel, ADS1115_RANGE voltageRange, uint8_t address)
: channel(channel), voltageRange(voltageRange) {
ads = ADS1115_WE(address);
}
\ No newline at end of file
......@@ -5,8 +5,6 @@
#include "ADS1115_WE.h"
#include "ForteSensor.hpp"
#include "Definitions.h"
#include "NoDataAvailableException.hpp"
#include "Pinout.hpp"
#include "esp_log.h"
#include <Wire.h>
......@@ -26,6 +24,8 @@ public:
TEROS10(ADS1115_MUX channel, ADS1115_RANGE voltageRange);
TEROS10(ADS1115_MUX channel, ADS1115_RANGE voltageRange, uint8_t address);
private:
float readChannel(ADS1115_MUX compareChannel);
......
......@@ -35,7 +35,7 @@ void RtcMemory::store_data(std::string message) {
// apparently I am not allowed to copy to rtc mem
for (int i = 0; i < maxLength_; i++) {
storage[i][headElement] = jsonString[i];
if(jsonString[i] == '\0'){
if (jsonString[i] == '\0') {
break;
}
}
......@@ -43,14 +43,14 @@ void RtcMemory::store_data(std::string message) {
ESP_LOGE(TAG, "Moved message to storage.");
}
String RtcMemory::get_data_from_storage(){
String RtcMemory::get_data_from_storage() {
// remove element pointed at by tail
String buf = "";
char current = '\0';
for(int i = 0; i<maxLength_; i++){
for (int i = 0; i < maxLength_; i++) {
current = storage[i][tailElement];
buf += current;
if(current == '\0'){
if (current == '\0') {
break;
}
}
......@@ -88,33 +88,37 @@ int RtcMemory::amount_stored_data() {
void RtcMemory::get_host_mac(uint8_t *destination, bool open) {
if(!open){preferences.begin("config", true);}
if (USE_HARDCODED_HOST_MAC) {
memcpy(destination, HARDCODED_HOST_MAC, sizeof(HARDCODED_HOST_MAC));
return;
}
if (!open) { preferences.begin("config", true); }
if (preferences.isKey("host")) {
if (preferences.isKey("host")) {
preferences.getBytes("host", destination, sizeof(uint8_t) * 6);
ESP_LOGI(TAG, "Host Mac retrieved from flash");
} else {
memcpy(destination, BROADCAST_MAC, sizeof(BROADCAST_MAC));
ESP_LOGI(TAG, "Backup MAC address used");
}
memcpy(destination, BROADCAST_MAC, sizeof(BROADCAST_MAC));
ESP_LOGI(TAG, "Backup MAC address used");
}
if(!open){preferences.end();}
if (!open) { preferences.end(); }
}
void RtcMemory::store_mac_address(uint8_t* mac){
void RtcMemory::store_mac_address(uint8_t *mac) {
preferences.begin("config", false);
if(preferences.putBytes("host", mac, sizeof(uint8_t) * 6) > 0){
if (preferences.putBytes("host", mac, sizeof(uint8_t) * 6) > 0) {
ESP_LOGI(TAG, "Host MAC address saved to flash %02X:%02X:%02X:%02X:%02X:%02X", mac[0],
mac[1],mac[2],mac[3],mac[4],mac[5]);
}
else{
ESP_LOGI(TAG, "Couldn't save Host Mac to flash");
}
mac[1], mac[2], mac[3], mac[4], mac[5]);
} else {
ESP_LOGI(TAG, "Couldn't save Host Mac to flash");
}
preferences.end();
}
bool RtcMemory::does_host_exist(){
bool RtcMemory::does_host_exist() {
preferences.begin("config", true);
bool answer = preferences.isKey("host");
preferences.end();
......
......@@ -5,6 +5,7 @@
#include "ClientDataPackage.hpp"
#include "ESP32Time.h"
#include "esp_log.h"
#include <Definitions.h>
#include <Preferences.h>
#include <Time.hpp>
......@@ -18,30 +19,30 @@ class RtcMemory {
static RtcMemory &getInstance() {
static RtcMemory instance;
return instance;
}
RtcMemory(RtcMemory const&) = delete;
void operator=(RtcMemory const&) = delete;
static void store_data(std::string message);
ClientDataPackage get_message_from_storage();
bool is_data_storage_full();
int amount_stored_data();
void store_mac_address(uint8_t *mac);
void get_host_mac(uint8_t *destination, bool open = false);
bool does_host_exist();
void track_failed_attempt();
void reset_fail_counter();
int get_fail_counter();
private:
RtcMemory() {}
// used for MAC storage
Preferences preferences;
uint8_t BROADCAST_MAC[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
String get_data_from_storage();
}
RtcMemory(RtcMemory const &) = delete;
void operator=(RtcMemory const &) = delete;
static void store_data(std::string message);
ClientDataPackage get_message_from_storage();
bool is_data_storage_full();
int amount_stored_data();
void store_mac_address(uint8_t *mac);
void get_host_mac(uint8_t *destination, bool open = false);
bool does_host_exist();
void track_failed_attempt();
void reset_fail_counter();
int get_fail_counter();
private:
RtcMemory() {}
// used for MAC storage
Preferences preferences;
uint8_t BROADCAST_MAC[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
String get_data_from_storage();
};
#endif
\ No newline at end of file
......@@ -16,14 +16,7 @@ bool was_msg_received() {
}
void get_host_mac(uint8_t *destination) {
preferences.begin("config", true);
if (preferences.isKey("host")) {
preferences.getBytes("host", destination, sizeof(uint8_t) * 6);
} else {
memcpy(destination, BROADCAST_MAC, sizeof(BROADCAST_MAC));
ESP_LOGI(TAG, "Backup MAC address used");
}
preferences.end();
RtcMemory::getInstance().get_host_mac(destination);
}
esp_err_t add_host_to_peers(response received) {
......@@ -55,55 +48,55 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) {
// all Espressif registered MAC starting bytes: https://maclookup.app/vendors/espressif-inc
// you can also set your own MAC https://randomnerdtutorials.com/get-change-esp32-esp8266-mac-address-arduino/
switch (received_msg.type) {
case hostChange: {
ESP_LOGI(TAG, "hostChange received");
Time::getInstance().setTime(received_msg.time);
// delete old host
preferences.begin("config", false);
if (preferences.isKey("host")) {
ESP_LOGI(TAG, "removing old host");
uint8_t old[6];
case hostChange: {
ESP_LOGI(TAG, "hostChange received");
Time::getInstance().setTime(received_msg.time);
// delete old host
preferences.begin("config", false);
if (preferences.isKey("host")) {
ESP_LOGI(TAG, "removing old host");
uint8_t old[6];
preferences.end();
get_host_mac(old); // maybe problem here, re-opening preferences
esp_now_del_peer(old);
}
// add new host
preferences.begin("config", false);
if (preferences.putBytes("host", received_msg.mac, sizeof(received_msg.mac)) > 0) {
ESP_LOGI(TAG, "Host MAC address saved to flash %02X:%02X:%02X:%02X:%02X:%02X", received_msg.mac[0],
received_msg.mac[1], received_msg.mac[2], received_msg.mac[3], received_msg.mac[4],
received_msg.mac[5]);
} else {
ESP_LOGI(TAG, "Couldn't save Host Mac to flash");
}
preferences.end();
get_host_mac(old); // maybe problem here, re-opening preferences
esp_now_del_peer(old);
}
// add new host
preferences.begin("config", false);
if (preferences.putBytes("host", received_msg.mac, sizeof(received_msg.mac)) > 0) {
ESP_LOGI(TAG, "Host MAC address saved to flash %02X:%02X:%02X:%02X:%02X:%02X", received_msg.mac[0],
received_msg.mac[1], received_msg.mac[2], received_msg.mac[3], received_msg.mac[4],
received_msg.mac[5]);
} else {
ESP_LOGI(TAG, "Couldn't save Host Mac to flash");
add_host_to_peers(received_msg);
}
preferences.end();
add_host_to_peers(received_msg);
}
case dataAck: {
// ESP_LOGI(TAG, "dataAck received.");
Time::getInstance().setTime(
received_msg.time); // see https://www.esp32.com/viewtopic.php?t=9965, maybe this needs an offset
case dataAck: {
// ESP_LOGI(TAG, "dataAck received.");
Time::getInstance().setTime(
received_msg.time); // see https://www.esp32.com/viewtopic.php?t=9965, maybe this needs an offset
// ESP_LOGI(TAG, "Timestamp received: %ld", Time::getInstance().getEpochSeconds());
preferences.begin("config", false);
if (!preferences.isKey("host")) {
if (preferences.putBytes("host", received_msg.mac, sizeof(received_msg.mac)) > 0) {
// ESP_LOGI(TAG, "host MAC address saved to flash %02X:%02X:%02X:%02X:%02X:%02X",
// received_msg.host[0],
// received_msg.host[1],received_msg.host[2],received_msg.host[3],received_msg.host[4],received_msg.host[5]);
// }
// ESP_LOGI(TAG, "Timestamp received: %ld", Time::getInstance().getEpochSeconds());
preferences.begin("config", false);
if (!preferences.isKey("host")) {
if (preferences.putBytes("host", received_msg.mac, sizeof(received_msg.mac)) > 0) {
// ESP_LOGI(TAG, "host MAC address saved to flash %02X:%02X:%02X:%02X:%02X:%02X",
// received_msg.host[0],
// received_msg.host[1],received_msg.host[2],received_msg.host[3],received_msg.host[4],received_msg.host[5]);
// }
// add host to peers
add_host_to_peers(received_msg);
// add host to peers
add_host_to_peers(received_msg);
}
}
preferences.end();
// delay(50);
msg_recv = true;
}
default: {
break;
}
preferences.end();
// delay(50);
msg_recv = true;
}
default: {
break;
}
}
auto end = millis();
}
......
......@@ -14,8 +14,13 @@
// Print additional SDI12 Information
constexpr bool SDI12_DEBUG = true;
// SDA and SCL pins for I2C. Note that for ESPCam, these are SDA 13 and SCL 12 and defined in the main file of the ESPCam
// SDA and SCL pins for I2C. Note that for ESPCam, these are SDA 13 and SCL 12 and defined in the main file of the
// ESPCam
constexpr int SDA_PIN = 25;
constexpr int SCL_PIN = 26;
// HARDCODED Host
constexpr bool USE_HARDCODED_HOST_MAC = true;
constexpr uint8_t HARDCODED_HOST_MAC[6] = {0x70, 0xB8, 0xF6, 0x05, 0x2C, 0x60};
#endif // CLIENT_SATELLITE_DEFINITIONS_H
......@@ -47,6 +47,14 @@ class Measurement {
[[nodiscard]] int getChannel() const { return protocolAddress; }
[[nodiscard]] uint8_t getProtocolAddress() const {
if (i2cAddress != NO_I2C_ADDRESS) {
return i2cAddress;
} else {
return protocolAddress;
}
}
[[nodiscard]] const ErrorType &getErrorType() const { return errorType; }
[[nodiscard]] std::string getErrorTypeString() const { return ErrorTypes::errorTypeToString(errorType); }
......
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