Wombat Environmental Node 1.0
An environmental node for SDI-12 and digital sensors.
Loading...
Searching...
No Matches
Utils.h
Go to the documentation of this file.
1#ifndef WOMBAT_UTILS_H
2#define WOMBAT_UTILS_H
3
4#include <ArduinoJson.h>
5#include <dpiclimate-12.h>
6
7#include "globals.h"
8
9size_t readFromStreamUntil(Stream& stream, const char delim, char * const buffer, const size_t max);
10void streamPassthrough(Stream* s1, Stream* s2);
11
12int waitForChar(Stream& stream, uint32_t timeout);
13
14JsonObjectConst getSensorDefn(const char* const vendor, const char* const model);
15JsonObjectConst getSensorDefn(const size_t sensor_idx, const sensor_list& sensors);
16
17void enable12V(void);
18void disable12V(void);
19
20const char* iso8601(void);
21void log_to_sdcard(const char * msg);
22void log_to_sdcardf(const char *fmt, ...);
23
33int read_spiffs_file(const char* filename, char* buffer, size_t max_length, size_t &bytes_read);
34
47int read_r5_file(const String& filename, char* buffer, size_t length, size_t &bytes_read, SARA_R5_error_t& r5_err);
48
49bool wait_for_at(void);
50bool connect_to_internet(void);
51
52int get_version_string(char *buffer, size_t length);
53
54template <typename T>
55struct URC {
56 bool valid { false };
57 T command { static_cast<T>(-1) };
58 int result { -1 };
59 int err1 { 0 };
60 int err2 { 0 };
61
62 URC()= default;
63 URC(bool v, T c, int r) {
64 valid = v;
65 command = c;
66 result = r;
67 }
68};
69
70template <typename T>
71class CommandURCVector : public std::vector<URC<T>> {
72public:
73 // Constructor - allocate space for 6 URCs.
74 CommandURCVector() : std::vector<URC<T>>() {}
75
76 // Add a pair to the vector
77 void addPair(const T& value, int intValue) {
78 this->emplace_back(true, value, intValue);
79 }
80
89 bool hasURC(const T command, int *result) {
90 auto iter = this->begin();
91 while (iter != this->end()) {
92 ESP_LOGI("CommandURCVector", "valid = %d, command = %d, result = %d, err1 = %d, err2 = %d", iter->valid, iter->command, iter->result, iter->err1, iter->err2);
93 if (iter->command == command) {
94 *result = iter->result;
95 log_to_sdcardf("valid = %d, command = %d, result = %d, err1 = %d, err2 = %d", iter->valid, iter->command, iter->result, iter->err1, iter->err2);
96 iter->valid = false;
97 this->erase(iter);
98 return true;
99 }
100
101 iter++;
102 }
103
104 return false;
105 }
106
107 bool waitForURC(const T command, int *result, int retries, const long delay_ms) {
108 bool found_urc = false;
109 while (retries > 0) {
110 r5.bufferedPoll();
111 found_urc = hasURC(command, result);
112 if (found_urc) {
113 break;
114 }
115
116 delay(delay_ms);
117 retries--;
118 }
119
120 ESP_LOGI("CommandURCVector", "command = %d, result = %d, found_urc = %d, retries = %d", command, *result, found_urc, retries);
121 log_to_sdcardf("command = %d, result = %d, found_urc = %d, retries = %d", command, *result, found_urc, retries);
122 return found_urc;
123 }
124};
125
126#endif //WOMBAT_UTILS_H
static char msg[CLI::MAX_CLI_MSG_LEN+1]
Message buffer.
Definition: CLI.cpp:26
void disable12V(void)
Disable the SDI-12 power line.
Definition: Utils.cpp:154
void log_to_sdcard(const char *msg)
Definition: Utils.cpp:180
void enable12V(void)
Enable the SDI-12 power line if it is not already enabled.
Definition: Utils.cpp:144
void streamPassthrough(Stream *s1, Stream *s2)
Definition: Utils.cpp:74
size_t readFromStreamUntil(Stream &stream, const char delim, char *const buffer, const size_t max)
Read characters from stream into buffer until the delim char is encountered.
Definition: Utils.cpp:35
void log_to_sdcardf(const char *fmt,...)
Definition: Utils.cpp:189
int read_r5_file(const String &filename, char *buffer, size_t length, size_t &bytes_read, SARA_R5_error_t &r5_err)
Definition: Utils.cpp:246
int waitForChar(Stream &stream, uint32_t timeout)
Wait up to timeout ms for any character to appear in stream.
Definition: Utils.cpp:110
const char * iso8601(void)
Definition: Utils.cpp:163
JsonObjectConst getSensorDefn(const char *const vendor, const char *const model)
Definition: Utils.cpp:124
bool connect_to_internet(void)
Get the R5 modem connected to the internet.
Definition: Utils.cpp:336
int read_spiffs_file(const char *filename, char *buffer, size_t max_length, size_t &bytes_read)
Definition: Utils.cpp:202
int get_version_string(char *buffer, size_t length)
bool wait_for_at(void)
Waits until an AT command to the R5 modem returns OK, or a timeout is reached.
Definition: Utils.cpp:283
Definition: Utils.h:71
CommandURCVector()
Definition: Utils.h:74
void addPair(const T &value, int intValue)
Definition: Utils.h:77
bool hasURC(const T command, int *result)
Definition: Utils.h:89
bool waitForURC(const T command, int *result, int retries, const long delay_ms)
Definition: Utils.h:107
SARA_R5 r5
sensor_list sensors
The ID information of each sensor found on the SDI-12 bus.
Definition: SensorTask.cpp:25
Definition: Utils.h:55
URC()=default
bool valid
Definition: Utils.h:56
int err2
Definition: Utils.h:60
int err1
Definition: Utils.h:59
URC(bool v, T c, int r)
Definition: Utils.h:63
int result
Definition: Utils.h:58
T command
Definition: Utils.h:57