-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_V0.cpp
More file actions
211 lines (167 loc) · 4.69 KB
/
main_V0.cpp
File metadata and controls
211 lines (167 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "Particle.h"
#include "Adafruit_SSD1306/Adafruit_SSD1306.h"
#include "GFX/Adafruit_GFX.h"
#include "GFX/gfxfont.h"
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
SYSTEM_MODE(SEMI_AUTOMATIC);
#define VFD_ON LOW
#define VFD_OFF HIGH
#define LOGGING_ON TRUE
#if (LOGGING_ON)
SerialLogHandler logHandler(LOG_LEVEL_ALL);
#endif
Adafruit_SSD1306 oled(D4);
FuelGauge fuel;
const int SLEEP_INTERVAL = 60 * 20; //defult sleep of 20 minutes - under the 23 minutes needed between mandatory pings to cell tower, otherwise full re-connection will occur
const int VFD_PIN = D2; //attached to AL1 on WJ200 VFD (NC)
const int PULL_UP_PIN = D3; //always inernally pulled-up; hardwired to D2
/*const uint32_t RETAINED_DATA_MAGIC = 0xa2c7206a;
const size_t DEVICE_NAME_MAX_LEN = 31;*/
/*typedef struct {
uint32_t magic;
char deviceName[DEVICE_NAME_MAX_LEN + 1];
} RetainedData;*/
String deviceID;
String firmwareVersion;
/*retained RetainedData retainedData = {0};*/
retained int VFDState = 0;
retained int lastVFDState = 0;
retained int VFDchanged = false;
retained int lastDay = 0;
retained int SoC = 0; //state of charge for battery pack 2000mAh/3.7v
void getData();
void updateOLED();
void publishData();
void syncTime();
/*void getDeviceName();*/
/*void deviceNameHandler(const char *topic, const char *data);*/
ApplicationWatchdog wd(660000, System.reset); //This Watchdog code will reset the processor if the dog is not kicked every 11 mins which gives time for 2 modem reset's.
void setup()
{
Serial.begin(14400);
#if (PLATFORM_ID == PLATFORM_ELECTRON_PRODUCTION)
Cellular.on();
Cellular.connect();
#else
WiFi.on();
WiFi.connect();
#endif
pinMode(VFD_PIN, INPUT);
pinMode(PULL_UP_PIN, INPUT_PULLUP);
//OLED setup
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
delay(50);
oled.setTextSize(1);
oled.setTextColor(WHITE);
//set deviceID and firmwareVersion
deviceID = System.deviceID();
firmwareVersion = System.version();
Log.info("Wake up");
delay(200);
//battery setup
fuel.quickStart();
delay(200);
}
void loop()
{
//if battery drops below 20%, sleep for one hour
if (fuel.getSoC() < 20){
System.sleep(SLEEP_MODE_DEEP, 3600);
}
getData();
updateOLED();
if (!Particle.connected()) Particle.connect();
waitFor(Particle.connected, 300000);
/*getDeviceName();*/
syncTime();
publishData();
System.sleep(VFD_PIN, CHANGE, SLEEP_INTERVAL, SLEEP_NETWORK_STANDBY);
//sleep for SLEEP_INTERVAL, wake on VFD_PIN change
}
void getData()
{
//Get VFD state
//VFD_PIN is
VFDState = (digitalRead(VFD_PIN) == VFD_ON ? 1 : 0);
if (VFDState != lastVFDState) {
VFDchanged = true;
}else {
VFDchanged = false;
}
lastVFDState = VFDState;
Log.info("VFD State: " + String(VFDState));
//Get battery SoC
SoC = map(fuel.getSoC(), 0, 84, 0, 100);
}
/*void getDeviceName()
{
if (retainedData.magic != RETAINED_DATA_MAGIC || retainedData.deviceName[0] == 0)
{
memset(&retainedData, 0, sizeof(retainedData));
retainedData.magic = RETAINED_DATA_MAGIC;
Particle.subscribe("spark/", deviceNameHandler);
Particle.publish("spark/device/name");
}
}*/
void publishData()
{
//publish states to particle cloud -> webhook -> losant
/*String data = String::format(
"{\"v\": %d, \"s\": %d, \"n\": \"%s\"}",
VFDState, SoC, retainedData.deviceName
);*/
String data = String::format(
"{\"v\": %d, \"s\": %d}",
VFDState, SoC
);
Particle.publish("d", data);
Log.info("Publishing currrent state to Particle Cloud");
}
void syncTime()
{
//sync time once every 24hrs
if (Time.day() != lastDay)
{ // a new day calls for a sync
Particle.syncTime();
for(uint32_t ms=millis(); millis()-ms < 1000; Particle.process());
lastDay = Time.day();
}
}
/*void deviceNameHandler(const char *topic, const char *data)
{
if (strlen(data) <= DEVICE_NAME_MAX_LEN) {
strcpy(retainedData.deviceName, data);
}
else {
// Truncate name
strncpy(retainedData.deviceName, data, DEVICE_NAME_MAX_LEN);
retainedData.deviceName[DEVICE_NAME_MAX_LEN] = 0;
}
}*/
//Print to OLED
void updateOLED()
{
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.drawLine(0, 8, 128, 8, WHITE);
oled.setCursor(0,0);
oled.print("SSDS MONITOR");
if (SoC > 100) {
SoC = 100;
}
oled.print(" BAT:");
oled.print(SoC);
oled.print("%");
oled.setCursor(0,10);
oled.print("VFD: ");
oled.print(VFDState ? "ON" : "OFF");
CellularSignal sig = Cellular.RSSI();
oled.setCursor(0,20);
oled.print("RSSI:");
oled.print(sig.rssi);
oled.print(" QUAL:");
oled.print(sig.qual);
oled.display();
delay(200);
}