-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpooltemplevel.ino photon code
267 lines (242 loc) · 7.49 KB
/
pooltemplevel.ino photon code
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
SYSTEM_MODE(SEMI_AUTOMATIC); // take control of the wifi behavior to avoid battery dying when wifi is down
SYSTEM_THREAD(ENABLED); // enable so loop and setup run immediately as well as allows wait for to work properly for the timeout
// This #include statement was automatically added by the Particle IDE.
#include <OneWire.h>
#include "VL53L0X.h"
VL53L0X sensor;
#define HIGH_ACCURACY // for lidar high accuracy mode
// Define pins
#define ppin D2
#define tppin D3
#define tground D4
// global variables
int tries = 0;
float averagetemp = 0;
int averagelevel = 0;
// variables for temperature smoothing
float goodtemp1 = 0;
float goodtemp2 = 0;
float goodtemp3 = 0;
float goodtemp4 = 0;
int tempattempt = 1;
// variables for level smoothing
int mm = 0;
int mm1 = 0;
int mm2 = 0;
int mm3 = 0;
int mm4 = 0;
int levelattempt =1;
// one wire temp variables
OneWire ds = OneWire(D5); //** 1-wire signal on pin D5
unsigned long lastUpdate = 0;
float lastTemp;
void setup()
{
RGB.control(true); // take control of the LED
RGB.color(0, 0, 0); // the following sets the RGB LED off:
// pinmode
pinMode(ppin, OUTPUT);
pinMode(tppin, OUTPUT);
pinMode(tground, OUTPUT);
// turn on the sensors
digitalWrite(ppin, HIGH);
digitalWrite(tppin, HIGH);
// temp sensor ground
digitalWrite(tground, LOW);
// Lidar setup
Serial.begin(9600);
Wire.begin();
sensor.init();
sensor.setTimeout(500);
delay(2500); // delay 2.5 secodns to allow sensor time to boot before taking a reading this is to eliminate the 185 temp error also allows time to store sensor in a manual boot
#if defined HIGH_ACCURACY
// increase timing budget to 200 ms
sensor.setMeasurementTimingBudget(200000);
#endif
// connect to the cloud
Particle.connect(); // attempt to connect to the cloud
if(!waitFor(Particle.connected, 30000)){ // if it takes longer than 30 seconds sleep anyways without sending message to conserve battery
System.sleep(SLEEP_MODE_DEEP, 14400);
}
}
void loop()
{
//** Temp Probe loop basically all the temperature code copy pasted from the example code from the library
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
if ( !ds.search(addr)) {
ds.reset_search();
delay(250);
return;
}
switch (addr[0]) {
case 0x10:
type_s = 1;
break;
case 0x28:
type_s = 0;
break;
case 0x22:
type_s = 0;
break;
case 0x26:
type_s = 2;
break;
default:
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 0);
delay(1000);
present = ds.reset();
ds.select(addr);
ds.write(0xB8,0);
ds.write(0x00,0);
present = ds.reset();
ds.select(addr);
ds.write(0xBE,0);
if (type_s == 2) {
ds.write(0x00,0);
}
for ( i = 0; i < 9; i++) {
data[i] = ds.read();
}
int16_t raw = (data[1] << 8) | data[0];
if (type_s == 2) raw = (data[2] << 8) | data[1];
byte cfg = (data[4] & 0x60);
switch (type_s) {
case 1:
raw = raw << 3;
if (data[7] == 0x10) {
raw = (raw & 0xFFF0) + 12 - data[6];
}
celsius = (float)raw * 0.0625;
break;
case 0:
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
celsius = (float)raw * 0.0625;
break;
case 2:
data[1] = (data[1] >> 3) & 0x1f;
if (data[2] > 127) {
celsius = (float)data[2] - ((float)data[1] * .03125);
}else{
celsius = (float)data[2] + ((float)data[1] * .03125);
}
}
if((((celsius <= 0 && celsius > -1) && lastTemp > 5)) || celsius > 125) {
celsius = lastTemp;
}
fahrenheit = celsius * 1.8 + 32.0;
lastTemp = celsius;
// my addition to smooth temperature take four samples
if (tempattempt == 1){
if (fahrenheit > 35 && fahrenheit < 100 && fahrenheit != 185){
goodtemp1 = fahrenheit;
tempattempt ++;
}
}
if (tempattempt == 2){
if (fahrenheit > 35 && fahrenheit < 100 && fahrenheit != 185){
goodtemp2 = fahrenheit;
tempattempt++;
}
}
if (tempattempt == 3){
if (fahrenheit > 35 && fahrenheit < 100 && fahrenheit != 185){
goodtemp3 = fahrenheit;
tempattempt++;
}
}
if (tempattempt == 4){
if (fahrenheit > 35 && fahrenheit < 100 && fahrenheit != 185){
goodtemp4 = fahrenheit;
tempattempt++;
// find max
float max1v2t = max(goodtemp1, goodtemp2);
float max3v4t = max(goodtemp3, goodtemp4);
float maxt = max(max1v2t, max3v4t);
// find min
float min1v2t = min(goodtemp1, goodtemp2);
float min3v4t = min(goodtemp3, goodtemp4);
float mint = min(min1v2t, min3v4t);
averagetemp = (((goodtemp1 + goodtemp2 + goodtemp3 + goodtemp4) - (maxt) - (mint)) / 2); // take away the max and min and average two middle numbers
// publish string for eror checking
String data = String(goodtemp1) + ", " + String(goodtemp2) + ", " + String(goodtemp3) + ", " + String(goodtemp4) + ", " + String(maxt) + ", " + String(mint) + ", " + String(averagetemp);
Particle.publish("T1234xmav", data, PRIVATE);
}
}
// Take lidar readings
// filter the data to smooth eroneous distances
if (levelattempt == 1){
mm = sensor.readRangeSingleMillimeters();
if (mm > 50 && mm < 160){
mm1 = mm;
levelattempt++;
}
}
if (levelattempt == 2){
mm = sensor.readRangeSingleMillimeters();
if (mm > 50 && mm < 160){
mm2 = mm;
levelattempt++;
}
}
if (levelattempt == 3){
mm = sensor.readRangeSingleMillimeters();
if (mm > 50 && mm < 160){
mm3 = mm;
levelattempt++;
}
}
if (levelattempt == 4){
mm = sensor.readRangeSingleMillimeters();
if (mm > 50 && mm < 160){
mm4 = mm;
levelattempt++;
//find max
int max1v2l = max(mm1, mm2);
int max3v4l = max(mm3, mm4);
int maxl = max(max1v2l, max3v4l);
// find min
int min1v2l = min(mm1, mm2);
int min3v4l = min(mm3, mm4);
int minl = min(min1v2l, min3v4l);
averagelevel = (((mm1 + mm2 + mm3 + mm4) - (maxl) - (minl)) / 2); // take away max and min and average two middle readings
// for error checking
String data2 = String(mm1) + ", " + String(mm2) + ", " + String(mm3) + ", " + String(mm4) + ", " + String(maxl) + ", " + String(minl) + ", " + String(averagelevel);
Particle.publish("L1234xmav", data2, PRIVATE);
}
}
// Publish the results and shutoff sensors if you get good data
if(tempattempt >= 4 && levelattempt >= 4 && averagelevel > 50 && averagelevel < 160 && averagetemp != 185 && averagetemp > 35 && averagetemp < 100)
{
digitalWrite(ppin, LOW);
digitalWrite(tppin, LOW);
Particle.publish("CurrentTemperature", String(averagetemp), PRIVATE);
delay (2000);
Particle.publish("waterlevel" , String(averagelevel), PRIVATE);
delay (2000);
System.sleep(SLEEP_MODE_DEEP, 14400);
}
else
{
tries++; // track failed attempts
}
// if it fails to get a good level after 10 loops send an error message and sleep anyways
if (tries == 10)
{
digitalWrite(ppin, LOW);
digitalWrite(tppin, LOW);
Particle.publish("failwaterlevel" , "true", PRIVATE);
delay (2000);
System.sleep(SLEEP_MODE_DEEP, 14400);
}
}