18#include <NimBLEDevice.h>
19#include <Adafruit_MAX1704X.h>
20#include <OWMAdafruit_ADS1015.h>
28extern Adafruit_MAX17048
maxlipo;
100 if( run_vwc || run_ht || run_ota_update || run_tlm){
111 if(
DEBUG) Serial.println(
"[HT]");
112 NimBLEScan* scanner = NimBLEDevice::getScan();
114 scanner->setActiveScan(
true);
115 scanner->setInterval(100);
116 scanner->setWindow(99);
121 BLEScanResults res = scanner->start(scanTime,
false);
122 scanner->clearResults();
130 if(
DEBUG) Serial.println(
"[VWC/WIRED SENSORS] queueing data");
133 digitalWrite(
PWR_EN, HIGH);
141 MQTTMailer instance = MQTTMailer::getInstance();
143 std::string mac_str(WiFi.macAddress().c_str());
152 voltage[0] = raw_analog[0] * 0.0001875;
155 voltage[1] = raw_analog[1] * 0.0001875;
158 voltage[2] = raw_analog[2] * 0.0001875;
161 voltage[3] = raw_analog[3] * 0.0001875;
171 ezopH_converter->clear_pH_str();
173 std::string topic = ezopH_converter->
getSensorType() +
"/pH/normal/" + mac_str;
175 std::string msg =
"{\"MAC\": \"" + mac_str +
"\", \"PH\":" + pH_normal +
"}";
180 Serial.println(
"\tno pH at addr 99");
185 ezopH_converter->clear_pH_str();
187 std::string topic = ezopH_converter->
getSensorType() +
"/pH/shallow/" + mac_str;
189 std::string msg =
"{\"MAC\": \"" + mac_str +
"\", \"PH\":" + pH_shallow +
"}";
194 Serial.println(
"\tno pH at addr shallow");
199 ezopH_converter->clear_pH_str();
200 std::string topic = ezopH_converter->
getSensorType() +
"/pH/middle/" + mac_str;
202 std::string msg =
"{\"MAC\": \"" + mac_str +
"\", \"PH\":" + pH_middle +
"}";
207 Serial.println(
"\tno pH at addr middle");
212 ezopH_converter->clear_pH_str();
213 std::string topic = ezopH_converter->
getSensorType() +
"/pH/deep/" + mac_str;
215 std::string msg =
"{\"MAC\": \"" + mac_str +
"\", \"PH\":" + pH_deep +
"}";
220 Serial.println(
"\tno pH at addr deep");
226 digitalWrite(
PWR_EN, LOW);
231 if(WiFi.status() == WL_CONNECTED && !
mqtt_client.connected()){
232 if(
DEBUG) Serial.println(
"\t-> not connected");
236 for(
int i = 0; i < 3; i++){
238 std::string depth =
"";
251 std::string topic = brand +
"/" + std::to_string(i) + std::string(
"_") + depth + std::string(
"/") + mac_str;
252 std::string msg =
"{\"MAC\": \"" + mac_str +
"\", \"DEPTH\": \"" + depth +
"\", " + vwc_converter->
toJSON(voltage[i]) +
"}";
263 if(
USB_DEBUG) Serial.println(
"[OTA] ran");
271 if(
USB_DEBUG) Serial.println(
"[WDT] ran");
272 digitalWrite(
DONE, HIGH);
273 vTaskDelay(pdMS_TO_TICKS(10));
274 digitalWrite(
DONE, LOW);
282 Serial.println(
"[TLM] queueing data");
285 digitalWrite(
PWR_EN, HIGH);
287 MQTTMailer instance = MQTTMailer::getInstance();
289 if(WiFi.status() == WL_CONNECTED && !
mqtt_client.connected()){
290 if(
USB_DEBUG) Serial.println(
"\t-> not connected");
294 std::string fw_version =
"V" + std::to_string(VERSION_MAJOR) +
"." + std::to_string(VERSION_MINOR) +
"." + std::to_string(VERSION_PATCH);
295 std::string topic =
"datagator/tlm/" + std::string(WiFi.macAddress().c_str());
296 std::string msg =
"{ \"MAC\": \"" + std::string(WiFi.macAddress().c_str()) +
297 "\", \"FIRMWARE_VERSION\": \"" + fw_version +
298 "\", \"RSSI\": " + std::to_string(WiFi.RSSI()) +
299 ", \"BSSID\": \"" + WiFi.BSSIDstr().c_str() +
"\"";
302 msg = msg +
", \"BATT_VOLTAGE\": " + std::to_string(
maxlipo.cellVoltage()) +
303 ", \"BATT_PERCENTAGE\": " + std::to_string(
maxlipo.cellPercent()) +
"}";
305 msg = msg +
", \"BATT_VOLTAGE\": -1" +
306 ", \"BATT_PERCENTAGE\": -1}";
309 digitalWrite(
PWR_EN, LOW);
324 if(
DEBUG)
Serial.println(
"[ERROR] over ran max reset count without reseting count, check if variable is being reset or if tasks are not completing");
Implements I2C pH sensor which inherits from pHSensor.hpp.
#define EZO_I2C_SHALLOW_ADDR
I2C address assigned to sensors buried at shallow depth (~1ft)
Definition Atlas_EZO-pH.hpp:19
#define EZO_I2C_MIDDLE_ADDR
I2C address assigned to sensors buried at medium depth (~2ft)
Definition Atlas_EZO-pH.hpp:20
#define EZO_I2C_ADDR
Default I2C address for sensor.
Definition Atlas_EZO-pH.hpp:18
#define EZO_I2C_DEEP_ADDR
I2C address assigned to deepest sensors (~3ft)
Definition Atlas_EZO-pH.hpp:21
This file implements an analog pH sensor which inherits from pHSensor.hpp.
Interface for VWC sensors.
Utilities such as callbacks for parsing bluetooth packets.
uint16_t readADC_SingleEnded(uint8_t channel)
Gets a single-ended ADC reading from the specified channel.
Definition OWMAdafruit_ADS1015.cpp:141
Definition OWMAdafruit_ADS1015.h:150
Defines an I2C pH sensor which inherits from pHSensor.hpp.
Definition Atlas_EZO-pH.hpp:34
bool sensor_at_address(int address)
Check if sensor present at address.
Definition Atlas_EZO-pH.hpp:64
char * getpH_str(int address)
Get the pH reading as a string.
Definition Atlas_EZO-pH.hpp:158
std::string getSensorType(void)
Get the string identifier for the Atlas EZO pH sensor. For use in MQTT topics and debugging.
Definition Atlas_EZO-pH.hpp:174
Defineds an analog pH sensor interface for the Atlas Gravity pH.
Definition Atlas_Gravity_pH.hpp:18
Converts MQTTMail objects into viable MQTT messages so that they can be published.
Definition MQTTMailer.hpp:60
void reconnect(PubSubClient mqtt_client)
Attempt to reconnect to MQTT broker every 5 seconds until successful.
Definition MQTTMailer.cpp:48
Callbacks for BLE packets.
Definition ble_util.hpp:35
Definition Teros10.hpp:15
Interface for converting a voltage measurement from an analog sensor into a double,...
Definition VWCSensor.hpp:23
virtual std::string toJSON(double voltage)
Convert data to JSON key value pairs.
virtual std::string getSensorType()
Return brand+sensor model string for topic.
Interface for pH sensors.
Definition pHSensor.hpp:19
#define TLM_FREQ
Definition config.hpp:29
#define OTA_FREQ
Definition config.hpp:23
#define VWC_FREQ
Definition config.hpp:25
#define HT_FREQ
Definition config.hpp:27
#define DEBUG
Definition config.hpp:17
#define MAX_COUNT
Definition config.hpp:21
void log_data(std::string topic, std::string message)
Definition logger.hpp:32
#define PWR_EN
Power enable pin for sensors.
Definition pinout.hpp:48
#define DONE
Done pin, for watchdog timer.
Definition pinout.hpp:50
Adafruit_MAX17048 maxlipo
MAX17048 battery Fuel Gauge.
Definition setup_util.hpp:28
void OTAUpdate()
Checks and updates from new code published at HTTPS address.
Definition scheduler.hpp:262
void ReadHT()
Reads temperature and humidity sensors via BLE and then sends complete data to the database.
Definition scheduler.hpp:110
int reset_count
number of resets retrieved for NVS
Definition scheduler.hpp:31
void Scheduler(int reset_count)
Perform a state transition based on the number of reset counts.
Definition scheduler.hpp:320
void PatWDT()
Pat the watchdog by raising watchdog done pin high, and then setting it low.
Definition scheduler.hpp:270
Adafruit_ADS1115 ads
Analog to digital converter object (I2C)
Definition setup_util.hpp:26
void init_nvs()
Open NVS, check if it is initialized with data, if not, initialize it.
Definition scheduler.hpp:58
bool maxlipo_attached
Fuel Gauge successfully initialized?
Definition setup_util.hpp:29
bool task_is_scheduled(int reset_count)
Check if a task will run this time, NVS must be initialized first!
Definition scheduler.hpp:94
void ReadWired()
Reads all wired sensors attached to the aggregator.
Definition scheduler.hpp:128
void SchedulerClearAll(int reset_count)
Clear all tasks so that none are scheduled to run.
Definition scheduler.hpp:375
void SendTLM()
Send a telemetry message to the MQTT broker.
Definition scheduler.hpp:280
#define RW_MODE
define read/write access mode for SD card files
Definition setup_util.hpp:12
const bool USB_DEBUG
USB serial debugging enabled.
Definition main.cpp:62
Preferences gator_prefs
NVS memory object.
Definition main.cpp:67
Data structure for task scheduling stored in NVS.
Definition scheduler.hpp:44
int ota_t0
Definition scheduler.hpp:50
int ht_t0
Definition scheduler.hpp:48
int tlm_t0
Definition scheduler.hpp:52
int analog_t0
Definition scheduler.hpp:46
PubSubClient mqtt_client
MQTT client object for logging.
void attempt_update()
Attempts OTA firmware update from server.
Definition update.cpp:178