diff --git a/client/client_mock/src/main.cpp b/client/client_mock/src/main.cpp index cfb525b69297fe4ddef828f1925cb64160053f0b..d547f0d11994db815a1454bc45e3753c0b9160f9 100644 --- a/client/client_mock/src/main.cpp +++ b/client/client_mock/src/main.cpp @@ -1,11 +1,10 @@ +#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 diff --git a/client/libs/DR26/DR26.cpp b/client/libs/DR26/DR26.cpp index fddaed0bf36e7833c35f231468b3f4a6ed7c1d97..d558facab8a8b472cf4dc37f060c5a6aded314b9 100644 --- a/client/libs/DR26/DR26.cpp +++ b/client/libs/DR26/DR26.cpp @@ -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); +} diff --git a/client/libs/DR26/DR26.hpp b/client/libs/DR26/DR26.hpp index 4e37efbeb39cc1a58dd5d4883f1b3f71dd4274ca..a63db8cb3f3842d0cd538ab9fad8f7978f4273d7 100644 --- a/client/libs/DR26/DR26.hpp +++ b/client/libs/DR26/DR26.hpp @@ -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); diff --git a/client/libs/MockSensor/MockSensor.cpp b/client/libs/MockSensor/MockSensor.cpp index 5de08499abbf10ec5a0917ae87b1cf8c170bc1a8..60cec0ccdd47454eae46da56a674d49a13a95807 100644 --- a/client/libs/MockSensor/MockSensor.cpp +++ b/client/libs/MockSensor/MockSensor.cpp @@ -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}; } diff --git a/client/libs/SHT85/SHT85.hpp b/client/libs/SHT85/SHT85.hpp index b56b89216de3cefbe986efff9b7319500afcb06b..2789fbd51a94ec81cbe568128499e66c39dafe6f 100644 --- a/client/libs/SHT85/SHT85.hpp +++ b/client/libs/SHT85/SHT85.hpp @@ -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" diff --git a/client/libs/TEROS10/TEROS10.cpp b/client/libs/TEROS10/TEROS10.cpp index 8e67db4d137b971df36cd95e62d316e832fc15ac..89ebac8d9902a8d99968b7f2b46718a61dd4106a 100644 --- a/client/libs/TEROS10/TEROS10.cpp +++ b/client/libs/TEROS10/TEROS10.cpp @@ -1,6 +1,6 @@ #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 diff --git a/client/libs/TEROS10/TEROS10.hpp b/client/libs/TEROS10/TEROS10.hpp index fb03d871c06a9fee0372c83972a45d3dd1271fed..a7560436d1bb2261c37a36a03e8a020c6445a85c 100644 --- a/client/libs/TEROS10/TEROS10.hpp +++ b/client/libs/TEROS10/TEROS10.hpp @@ -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); diff --git a/client/libs/caching/ram_caching.cpp b/client/libs/caching/ram_caching.cpp index b6346909e606d9c9052fb1e3e084093a7baf90f5..8fb35af71a036ef7613a3028d03386d28b18fded 100644 --- a/client/libs/caching/ram_caching.cpp +++ b/client/libs/caching/ram_caching.cpp @@ -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(); diff --git a/client/libs/caching/ram_caching.hpp b/client/libs/caching/ram_caching.hpp index 57140cd67fd404bd8340ea8da6a99feb290a6feb..8e82516d19666188528f44927864a982647e1518 100644 --- a/client/libs/caching/ram_caching.hpp +++ b/client/libs/caching/ram_caching.hpp @@ -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 diff --git a/client/libs/espnow/ESPNow.cpp b/client/libs/espnow/ESPNow.cpp index 035c925fb7b098083a06df436c86e7ab968735e7..70e96281f65852ace15791af5085f3448cbbbddc 100644 --- a/client/libs/espnow/ESPNow.cpp +++ b/client/libs/espnow/ESPNow.cpp @@ -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(); } diff --git a/client/libs/includes/Definitions.h b/client/libs/includes/Definitions.h index 1c5323e3cdd042d4bf8a4b2e1e6a95c5e89a837a..e013fcba82da06c1d5362c9b179218a411fd783f 100644 --- a/client/libs/includes/Definitions.h +++ b/client/libs/includes/Definitions.h @@ -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 diff --git a/shared-libs/DataTransfer/Measurement.hpp b/shared-libs/DataTransfer/Measurement.hpp index f40af8869c5a38b0f24dbe8b5f9a84608fd82f86..df69e465cf34bbbb70eefa65a112007e71a65afd 100644 --- a/shared-libs/DataTransfer/Measurement.hpp +++ b/shared-libs/DataTransfer/Measurement.hpp @@ -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); }