Data Gator
Hardware and software documentation for the Data Gator project.
Loading...
Searching...
No Matches
setup_util.hpp
Go to the documentation of this file.
1
7#ifndef SETUP_UTIL_H
8#define SETUP_UTIL_H
9
10// Preferences Access Modes
11#define RO_MODE true
12#define RW_MODE false
13
14#include <scheduler.hpp>
15
16#include <OWMAdafruit_ADS1015.h>
17#include <Adafruit_MAX1704X.h>
18
19#include <WiFiClientSecure.h>
20#include <WiFi.h>
21#include <NimBLEDevice.h>
22#include <NimBLEAddress.h>
23#include <NimBLEAdvertisedDevice.h>
24
25// ADC
27// Fuel Gauge
29bool maxlipo_attached = true;
30// NTP Client
33
34extern int reset_count;
35
40
41 // power switch pin
44
45 // sd card power switch pin
48
49 // WDT pins
52
53
54}
55
59void setup_adc(){
60
61 // setup ADS1x1x
62 ads.begin();
63 //ads.setGain(GAIN_TWO);
64
65}
66
71
72 // setup Fuel Gauge
73 if(!maxlipo.begin()) maxlipo_attached = false;
74
75}
76
81
82 // enable power to sensors for setup
84
85 setup_adc();
87
88 // disable power to sensors
90}
91
96
97 // configue mqtt client connection
98 if(USB_DEBUG){
99 Serial.print("[DEBUG] bytes free = ");
100 double perc = ESP.getFreeHeap();
101 Serial.println(perc);
102 }
103 mqtt_client.setBufferSize(30000);
104 mqtt_client.setKeepAlive(120);
106 mqtt_client.setCallback(callback);
107 if(USB_DEBUG){
108 Serial.print("[DEBUG] bytes free after setting heap size = ");
109 double perc = ESP.getFreeHeap();
110 Serial.println(perc);
111 }
112
113 // connect to MQTT
114 string mqtt_client_id = string("dg_") + WiFi.macAddress().c_str();
115 // connect as persistent/durable client
116 bool mqtt_client_connected = mqtt_client.connect(mqtt_client_id.c_str(), NULL, NULL, NULL, 0, false, NULL, false);
117
119 std::string unique_topic = "datagator/cmd/#";
120 Serial.print("Subscribing to ");
121 Serial.println(unique_topic.c_str());
122 mqtt_client.subscribe(unique_topic.c_str(), 1);
123 /*
124 if(USB_DEBUG){
125 Serial.print("[DEBUG] bytes free after connecting & subscribing = ");
126 double perc = ESP.getFreeHeap();
127 Serial.println(perc);
128 }
129 */
130 if(USB_DEBUG) Serial.println("connected to MQTT client!");
131 }else{
132 if(USB_DEBUG) Serial.println("[WARNING] error with MQTT connection");
133 delay(2000);
134 }
135
136}
137
142
143 // initialize BLE
144 NimBLEDevice::init("datagator");
145 /*
146 if(USB_DEBUG){
147 Serial.print("[DEBUG] bytes free after BLE init = ");
148 double perc = ESP.getFreeHeap();
149 Serial.println(perc);
150 }
151 */
152
153}
154
159
160 WiFi.begin(NETWORK, PSSWD);
161 //wifi_client.setInsecure();
162
163 // default version print
164 Serial.printf("Gator MAC address: %s\n", WiFi.macAddress().c_str());
165 Serial.printf("FIRMWARE VERSION v%i.%i.%i\n", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
166 Serial.printf("WiFi connecting %s, %s\n", NETWORK, PSSWD);
167
168 long int t0 = millis();
169 long int time_elapsed = 0;
170 while(WiFi.status() != WL_CONNECTED){
171 time_elapsed = millis() - t0;
172 delay(1000);
173 Serial.print(".");
174
175 if (time_elapsed > (WIFI_TIMEOUT)){
176 Serial.println("\n[WARNING] WIFI_TIMEOUT");
177 break;
178 // collect data, but store locally
179 }
180 }
181
182 // can start time client w/o wifi
183 // but can't update
184 timeClient.begin();
186
187 if(DEBUG && WiFi.status() == WL_CONNECTED){
188 Serial.println("\nCONNECTED");
189 Serial.println();
190 Serial.print("Gator connected @ ");
191 Serial.println(WiFi.localIP());
192 Serial.printf("Gator MAC address: %s\n", WiFi.macAddress().c_str());
193 }
194
195}
196
209
215
216 // initialized data logger and set state flag
217 // NOTE: power needs to be turned on for initialization
218 // turning off power to card while initialized
219 // seems to terminate the connection
221 if(WiFi.status() == WL_CONNECTED){
222 // update with NTP
223 timeClient.update();
225 }
226
228
229}
230
231#endif
void begin(void)
Sets up the HW (reads coefficients values, etc.)
Definition OWMAdafruit_ADS1015.cpp:112
Definition OWMAdafruit_ADS1015.h:150
Definition TimeStamp.hpp:91
#define PSSWD
Definition config.hpp:11
#define MQTT_BROKER_ADDR
Definition config.hpp:13
#define MQTT_PORT
Definition config.hpp:15
#define WIFI_TIMEOUT
Definition config.hpp:19
#define DEBUG
Definition config.hpp:17
#define NETWORK
Definition config.hpp:9
bool logging_available
is some logging interface available?
Definition logging_util.cpp:35
TimeStampBuilder * tsb
builds timestamp strings
Definition logging_util.cpp:52
bool absolute_timestamp_available
is an exact/accurate timestamp available?
Definition logging_util.cpp:37
bool init_data_logger()
Initialize logging interfaces.
Definition logging_util.cpp:80
void callback(char *topic, byte *message, unsigned int length)
Called when MQTT message is passed to the device by the broker.
Definition mqtt_util.hpp:146
#define SD_PWR_EN
uSD card power enable pin
Definition pinout.hpp:47
#define PWR_EN
Power enable pin for sensors.
Definition pinout.hpp:48
#define DONE
Done pin, for watchdog timer.
Definition pinout.hpp:50
Utilities for scheduling tasks and saving persistent data.
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 setup_i2c_sensors()
Initialize ALL i2c bus dependent sensors.
Definition setup_util.hpp:80
Adafruit_MAX17048 maxlipo
MAX17048 battery Fuel Gauge.
Definition setup_util.hpp:28
void setup_fuel_gauge()
Initialize and set the state variable for the fuel gauge.
Definition setup_util.hpp:70
WiFiUDP ntpUDP
WiFi stack object to pass to the NTPClient.
Definition setup_util.hpp:31
void setup_wifi_connection()
Connect to WiFi, set flags, timeout, etc.
Definition setup_util.hpp:158
void setup_adc()
Initialize the analog to digital converter.
Definition setup_util.hpp:59
int reset_count
number of resets retrieved for NVS
Definition scheduler.hpp:31
void setup_mqtt_connection()
Configure mqtt client settings.
Definition setup_util.hpp:95
NTPClient timeClient(ntpUDP)
network time protocol object to get current time
Adafruit_ADS1115 ads
Analog to digital converter object (I2C)
Definition setup_util.hpp:26
void setup_wireless_connections()
Setup all wireless interfaces including WiFi, BLE.
Definition setup_util.hpp:200
void setup_ble()
Initialize BLE stack.
Definition setup_util.hpp:141
void setup_logging()
Initialize logging and get timestamp from network, if available, for logging.
Definition setup_util.hpp:214
bool maxlipo_attached
Fuel Gauge successfully initialized?
Definition setup_util.hpp:29
void setup_gpio()
Initialize pins as input/output and set default state (HIGH/LOW)
Definition setup_util.hpp:39
const bool USB_DEBUG
USB serial debugging enabled.
Definition main.cpp:62
PubSubClient mqtt_client
MQTT client object for logging.