Skip to content
Snippets Groups Projects

Create a simple mock client that sends pseudo random values

Merged Zoe Pfister requested to merge 43-create-a-mock-client into develop
8 files
+ 210
2
Compare changes
  • Side-by-side
  • Inline
Files
8
+ 101
0
#include "ESPNow.hpp"
#include "MockSensor.hpp"
#include "NoDataAvailableException.hpp"
#include "f_deep_sleep.hpp"
#include <Arduino.h>
static const char *TAG = "MAIN";
MockSensor mock_channel0;
MockSensor mock_channel1;
MockSensor mock_channel2;
MockSensor mock_channel3;
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);
DeepSleep::print_wakeup_reason();
DeepSleep::bootCount++;
ESP_LOGD(TAG, "Boot number: %d", DeepSleep::bootCount);
gpio_set_level(GPIO_NUM_32, 1);
// delay(100);
mock_channel0.setup();
mock_channel1.setup();
mock_channel2.setup();
mock_channel3.setup();
mock_channel0.setChannel(0);
mock_channel1.setChannel(1);
mock_channel2.setChannel(2);
mock_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 = mock_channel0.buildMessages();
auto messages1 = mock_channel1.buildMessages();
auto messages2 = mock_channel2.buildMessages();
auto messages3 = mock_channel3.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);
// 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 protection: go to deep sleep for unlimited time when voltage less than 3.2V
DeepSleep::deep_sleep(20);
}
void loop() {}
\ No newline at end of file
Loading