Skip to content
Snippets Groups Projects
Commit 7ce2815c authored by User expired's avatar User expired
Browse files

fixed horrible espnow sending implementation +more

parent a3dc59a7
No related branches found
No related tags found
3 merge requests!39Merge Develop into Main,!19development into master,!18Power management satellite
......@@ -293,3 +293,6 @@ node_modules/
CMakeLists.txt
CmakeListsPrivate.txt
forte.code-workspace
client/client_satellite/.vscode/extensions.json
client/client_satellite/.vscode/settings.json
#include "dr26.hpp"
#include "ESPNow.hpp"
#include "LC709203F.h"
#include "NoDataAvailableException.hpp"
#include "dr26.hpp"
#include "f_deep_sleep.hpp"
#include <Arduino.h>
#include "ESPNow.hpp"
#include "LC709203F.h"
// FIXME: Use descriptive names for the constants below.
LC709203F gg;
static const std::string TAG = "MAIN";
// Execution time:
// FIXME: Boot: needs to be measured with oscilloscope and optimised. should be around a few hundres ms, can be <100ms
// https://github.com/makermoekoe/Picoclick-C3#speed-up-boot-process
// Part [ms]
// First setup: 150
// Data aquisition: 500
// ESPNow setup: 220
// Sending 1200
// TOTAL: 2200
static const char *TAG = "MAIN";
LC709203F battery_monitor;
ForteDR26 dr26_channel0;
ForteDR26 dr26_channel3;
ForteDR26 dr26_channel1;
ForteDR26 dr26_channel2;
ForteDR26 dr26_channel0_power;
void setup() {
Serial.begin(115200);
//Set the GPIO which conrtols the step up to OUTPUT
gpio_set_direction(GPIO_NUM_32, GPIO_MODE_OUTPUT);
gg.begin();
gg.setCellCapacity(LC709203F_APA_1000MAH);
DeepSleep::print_wakeup_reason();
DeepSleep::bootCount++;
void send_msgs(const std::__cxx11::list<Message> msgs)
{
for (const Message &msg : msgs) {
if (msg.send() != ESP_OK) {
RtcMemory::store(msg.getMessageAsMinifiedJsonString());
}
unsigned long ts = millis();
// it takes ~110ms for receiving an acknowledgement by the host in perfect conditions
uint16_t message_timeout = 2000;
while (!was_msg_received()) {
if ((millis() - ts) > message_timeout) {
RtcMemory::store(msg.getMessageAsMinifiedJsonString());
ESP_LOGE(TAG, "Timeout: Host not available\n");
break;
}
}
ESP_LOGD(TAG, "Time until acknowledgement: %ld", millis() - ts);
}
}
// one loop takes ~2200 ms
void setup()
{
unsigned long ts = millis();
Serial.begin(115200);
// Set the GPIO which conrtols the step up to OUTPUT
gpio_set_direction(GPIO_NUM_32, GPIO_MODE_OUTPUT);
// blinking led for debug
// gpio_set_direction(GPIO_NUM_17, GPIO_MODE_OUTPUT);
// gpio_set_level(GPIO_NUM_17, 1);
battery_monitor.begin();
DeepSleep::print_wakeup_reason();
DeepSleep::bootCount++;
ESP_LOGD(TAG, "Boot number: %d", DeepSleep::bootCount);
gpio_set_level(GPIO_NUM_32, 1);
dr26_channel1.setup();
dr26_channel3.setChannel(3);
dr26_channel1.setChannel(1);
dr26_channel2.setChannel(2);
dr26_channel0_power.setChannel(0);
gpio_set_level(GPIO_NUM_32, 0);
espnow_setup();
// log_e("Setup complete.");
gpio_set_level(GPIO_NUM_32, 1);
// delay(100);
dr26_channel1.setup();
dr26_channel0.setChannel(0);
dr26_channel1.setChannel(1);
dr26_channel2.setChannel(2);
dr26_channel3.setChannel(3);
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 = dr26_channel0.buildMessages();
auto messages1 = dr26_channel1.buildMessages();
auto messages2 = dr26_channel2.buildMessages();
auto messages3 = dr26_channel3.buildMessages();
auto messages4 = battery_monitor.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);
// FIXME: put this outside the try loop?
ts = millis();
espnow_setup();
ESP_LOGD(TAG, "EPSNow setup took %ld ms", millis() - ts);
ts = millis();
send_msgs(messages0);
send_msgs(messages1);
send_msgs(messages2);
send_msgs(messages3);
send_msgs(messages4);
// roughly takes 3s in ideal conditions
ESP_LOGD(TAG, "Sending messages took %ld ms", millis() - ts);
} catch (const NoDataAvailableException &e) {
std::cerr << e.what() << '\n';
}
// just to be safe in case exception happens above
gpio_set_level(GPIO_NUM_32, 0);
// keep it in deep sleep
gpio_hold_en((gpio_num_t)GPIO_NUM_32);
battery_monitor.setPowerMode(LC709203F_POWER_SLEEP);
// battery protection: go to deep sleep for unlimited time when voltage less than 3.2V
if (battery_monitor.cellVoltage_mV() > 3200) {
DeepSleep::deep_sleep(10 * 60);
} else {
esp_deep_sleep_start();
}
}
void loop() {
gpio_set_level(GPIO_NUM_32, 1);
Serial.println(
"***********************1-DRS26 NOT YET CONNECTED**********************************");
Serial.println(dr26_channel1.readData(), 5);
Serial.println("***********************2-DRS26**********************************");
Serial.println(dr26_channel2.readData(), 5);
Serial.println("***********************3-DRS26 *********************************");
Serial.println(dr26_channel3.readData(), 5);
Serial.println("***********************0-Voltage-Battery**********************************");
dr26_channel3.changeGain(GAIN_TWOTHIRDS);
Serial.println(dr26_channel0_power.readData(), 5);
Serial.println("***********************REAL-Voltage-Battery**********************************");
Serial.println(gg.cellVoltage_mV() / 1000.0);
try {
auto messages = dr26_channel0_power.buildMessages();
for (const Message &message: messages) {
if (message.send() != ESP_OK) {
RtcMemory::store(message.getMessageAsMinifiedJsonString());
}
delay(5000);
if (!was_msg_received()) {
RtcMemory::store(message.getMessageAsMinifiedJsonString());
}
}
dr26_channel3.changeGain(GAIN_ONE);
auto messages2 = dr26_channel1.buildMessages(1, "CIRCUMFERENCE_INCREMENT");
for (const Message &message2: messages2) {
if (message2.send() != ESP_OK) {
RtcMemory::store(message2.getMessageAsMinifiedJsonString());
}
delay(5000);
if (!was_msg_received()) {
RtcMemory::store(message2.getMessageAsMinifiedJsonString());
}
}
auto messages3 = dr26_channel2.buildMessages(2, "CIRCUMFERENCE_INCREMENT");
for (const Message &message3: messages3) {
if (message3.send() != ESP_OK) {
RtcMemory::store(message3.getMessageAsMinifiedJsonString());
}
delay(5000);
if (!was_msg_received()) {
RtcMemory::store(message3.getMessageAsMinifiedJsonString());
}
}
auto messages4 = gg.buildMessages();
for (const Message &message4: messages4) {
if (message4.send() != ESP_OK) {
RtcMemory::store(message4.getMessageAsMinifiedJsonString());
}
delay(5000);
if (!was_msg_received()) {
RtcMemory::store(message4.getMessageAsMinifiedJsonString());
}
}
} catch (const NoDataAvailableException &e) {
std::cerr << e.what() << '\n';
}
Serial.print("This device: ");
Serial.println("\n");
gpio_set_level(GPIO_NUM_32, 0);
delay(5000);
DeepSleep::deep_sleep(100);
}
void loop() {}
\ No newline at end of file
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