/
banjo_monkey.cpp
441 lines (386 loc) · 13.3 KB
/
banjo_monkey.cpp
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* 8.12.2015 Kyle Bowerman
* Last updated on 11.23.2015
* sparkcore temprature sensors to UBidots
* from: https://particle.hackster.io/AgustinP/logging-temperature-data-using-the-spark-core
* from: spark-temp, OLEDTEST, 6SPARKTEMP
* 11.23.2015 created realy screen (vx.x13)
* push button on realy screen (vx.x14)
* moisture on setmode 7 (v0.7.15)
* 11.24.2015 Stablized enocerPos / displayMode (v0.7.16)
* 11.24.2015 Trying to get timer for moisture checker, kills encoder #3
* Switch to do every 7200 cycles seems to be stable (v.0.8.16)
* 8.31.2016 Publish IP (v8.21.d)
* purpose:
* 1. uses hardcoded address in array instead of calling by index.
* 2. fix getting, prinint and pushing temp values when they are disconnected (-196)
* 3. fix ubidots to always go to the right source
* 10.11.2017 wasnt registring variables or functions with cloud so I moved those to the begining of the setup.
I guess there is a 5 second timout or something.
*
*/
#include "application.h"
#include "lib/streaming/firmware/spark-streaming.h"
#include "lib/SparkFun_Micro_OLED_Particle_Library/firmware/SparkFunMicroOLED.h"
#include "math.h"
#include "lib/HttpClient/firmware/HttpClient.h"
#include "lib/OneWire/OneWire.h"
#include "lib/SparkDallas/spark-dallas-temperature.h"
#include "banjo_monkey.h"
#include "oPrint.h"
void setup()
{
Particle.variable("devices",deviceCount);
Particle.variable("m1pct",M1PCT);
Particle.variable("version",MYVERSION);
Particle.variable("file",FILENAME);
Particle.variable("outside_temp",temp1);
Particle.variable("room_temp",temp2);
Particle.variable("pit_temp",temp3);
Particle.variable("board_temp",temp4);
Particle.variable("m1desc",M1DESC);
Particle.function("q", queryDevices);
Particle.function("setmode", setModeFunc);
Particle.function("printEEProm", printEEPROMFunc);
Particle.function("relay", relayFunc);
oled.begin(); // Initialize the OLED
oled.clear(ALL); // Clear the display's internal memory
oled.display(); // Display what's in the buffer (splashscreen)
delay(1000); // Delay 1000 ms
oled.clear(PAGE); // Clear the buffer.
// display the version at boot for 2 seconds
oled.setFontType(1);
oled.setCursor(0,8);
oled.print(FILENAME);
oled.setCursor(0,24);
oled.print(MYVERSION);
oled.display();
oled.setFontType(0);
delay(5000);
request.port = 80;
request.hostname = "things.ubidots.com";
Serial.begin(9600);
sensor.begin();
/*
Particle.variable("devices",deviceCount);
Particle.variable("m1pct",M1PCT);
Particle.variable("version",MYVERSION);
Particle.variable("file",FILENAME);
Particle.variable("outside_temp",temp1);
Particle.variable("room_temp",temp2);
Particle.variable("pit_temp",temp3);
Particle.variable("board_temp",temp4);
Particle.variable("m1desc",M1DESC);
Particle.function("q", queryDevices);
Particle.function("setmode", setModeFunc);
Particle.function("printEEProm", printEEPROMFunc);
Particle.function("relay", relayFunc);
*/
//Need to set the device Index Array at startup
deviceCount = getDeviceCount();
queryDevices("auto");
Particle.publish("reboot",Time.timeStr() );
//encoder
pinMode(encoderA, INPUT_PULLUP);
pinMode(encoderB, INPUT_PULLUP);
pinMode(button,INPUT_PULLUP);
pinMode(M1,INPUT_PULLDOWN);
pinMode(relay, OUTPUT);
pinMode(M1POWER, OUTPUT);
attachInterrupt(encoderA, doEncoderA, CHANGE);
attachInterrupt(encoderB, doEncoderB, CHANGE);
IPAddress myIP = WiFi.localIP();
String ipStr = String(myIP[0])+"."+String(myIP[1])+"."+String(myIP[2])+"."+String(myIP[3]);
Particle.publish("LocalIP", ipStr, 60,PRIVATE);
String myVersion = System.version().c_str();
delay(2000);
Particle.publish("Version", myVersion, 60,PRIVATE);
Particle.publish("rssi", String( WiFi.RSSI()), 60, PRIVATE);
Particle.publish("SSID", String( WiFi.SSID()), 60, PRIVATE);
}
void loop()
{
// New block to identify device count changes
lastDeviceCount = deviceCount;
deviceCount = getDeviceCount();
//end New block
mycounter++;
if(lastDeviceCount != deviceCount ) { //device count changes this never works
oled.clear(ALL); // Clear the display's internal memory
oled.display(); // Display what's in the buffer (splashscreen)
oled.setCursor(0,0);
//oled << "Count " << endl << "changed " << endl << lastDeviceCount << " " << deviceCount << endl;
oled.display();
//delay(5000); // Delay 1000 ms
oled.clear(PAGE); // Clear the buffer.
Serial << " The device Count Changed " << lastDeviceCount << " " << deviceCount << endl;
}
// only do these things every GETTEMPFEQ loops or before I get to the first GETTEMPFREQ
if (mycounter % GETTEMPFEQ == 0 || mycounter < GETTEMPFEQ ) {
deviceCount = getDeviceCount();
if ( deviceCount > 0 ) {
temperatureJob(); // do the main temprature job
}
// I think this is wrong
// lastDeviceCount = getDeviceCount(); // used to detect
}
buttonvalue = digitalRead(button);
if( debug ) {
Serial <<" mycounter: " << mycounter << " freq: " << freqChecker() << "Hz | encoderPos: ";
Serial << encoderPos << " | buttonvalue: " << buttonvalue << " displayMode: " << displayMode << endl;
}
//encoder
if (prevPos != encoderPos) {
prevPos = encoderPos;
Serial << "encoder position: " << encoderPos << endl;
dispatchEncoder();
}
if (encoderPos == 4 ) oPrintInfo();
if (encoderPos == 5 ) oPrintInfo5();
if (encoderPos == 6 ) oPrintRelayMode();
if (encoderPos == 7 ) oPrintMoisture();
// Don't intrrupt info screens to report no device
if (deviceCount == 0 && encoderPos < 4 && encoderPos > 0 ) oPrintNoDevices() ;
// check the moisure every moistureCheckerFreq (60 -about 20 seconds) currently 7200 ~ 1 hour
if ( mycounter % moistureCheckerFreq == 0 ) {
int lastDispalyMode = displayMode;
oPrintMoisture();
displayMode = encoderPos = lastDispalyMode;
}
// always last
lastime = thistime; // for frequency checker
delay(mydelay);
thistime = millis();
}
// -------- Functions --------------------
char *formatTempToBody(float temperature, int tempIndex) {
static char retbuf[64];
String s = "{\"value\": ";
s.concat(String(temperature));
s.concat("}");
s.toCharArray(retbuf, 64);
oDispatch(tempIndex, temperature);
return retbuf;
}
String convertMillisToHuman(int ms) {
int seconds, minutes, hours, days = 0;
int x = ms / 1000;
seconds = x % 60;
x /= 60;
minutes = x % 60;
x /= 60;
hours = x % 24;
x /= 24;
days = x;
String t = String(days);
t.concat(":");
t.concat(String(hours));
t.concat(":");
t.concat(String(minutes));
t.concat(":");
t.concat(String(seconds));
return String(t);
}
void debugSerial(int i ) {
Serial << "count: " << mycounter << " index " << i << " " << request.body << " dcount "<< deviceCount << " lcount " << lastDeviceCount << endl;
}
void dispatchEncoder(){
if (encoderPos > 7 ) encoderPos = 7;
if (encoderPos < 0 ) encoderPos = 0;
setModeFunc(String(encoderPos));
if (deviceCount > 0 ) temperatureJob();
}
void doEncoderA(){
if( digitalRead(encoderA) != A_set ) { // debounce once more
A_set = !A_set;
// adjust counter + if A leads B
if ( A_set && !B_set )
encoderPos += 1;
}
}
void doEncoderB(){
// Interrupt on B changing state, same as A above
if( digitalRead(encoderB) != B_set ) {
B_set = !B_set;
// adjust counter - 1 if B leads A
if( B_set && !A_set )
encoderPos -= 1;
}
}
double freqChecker() {
double myperiod = (thistime - lastime);
double myfrequency = ( 1 / myperiod ) * 1000;
return myfrequency;
}
int getDeviceCount() {
sensor.begin();
deviceCount = sensor.getDeviceCount();
return deviceCount;
}
void oDispatch(int tempIndex, float temperature) {
if (displayMode == 0 ) {
// turn off display
oled.clear(PAGE); // this really clears the screen but need the oled.display()
oled.display();
}
if (displayMode == 1 ) {
oPrintTemp(tempIndex, temperature);
}
if (displayMode == 2 ) {
oPrintTemp2(tempIndex, temperature);
}
if (displayMode == 3 ) {
oPrintTemp3(tempIndex, temperature);
}
if (displayMode == 4 ) oPrintInfo();
if (displayMode == 5 ) oPrintInfo5();
if (displayMode == 6 ) oPrintRelayMode();
if (displayMode == 7 ) oPrintMoisture();
//Turn off moisture Power if not in dsiaplyMode = 7
if (displayMode != 7 ) digitalWrite(M1POWER, LOW);
Serial << "oled dispatch called " << endl;
}
void printAddress(DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++)
{
Serial.print(" ");
// zero pad the address if necessary
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
int printEEPROMFunc(String command){
if(command == "all") {
for(int i = 0; i < 100; i++) {
if(EEPROM.read(i) != 0xFF ) { //only print if it is used
Serial.print(i);
Serial.print(" EEPROM ");
Serial.print(EEPROM.read(i),HEX);
Serial.println();
}
}
return 1;
}
if(command != "all" ){
Serial.print(command);
Serial.print(" EEPROM ");
Serial.print(EEPROM.read(command.toInt()));
//Now in hex
Serial.print(" EEPROM 0x ");
Serial.print(EEPROM.read(command.toInt()),HEX);
Serial.println();
return EEPROM.read(command.toInt());
}
}
int queryDevices(String command) {
if(command == "auto") {
// sets and prints the device array
for(int i=0; i < deviceCount; i++ ) {
sensor.getAddress(deviceIndexArray[i], i);
Serial.print("Device Index ");
Serial.print(i);
printAddress(deviceIndexArray[i]);
Serial.print("\n");
}
Serial.print("----------------------------------\n");
return 1;
}
if(command == "array" ) {
// prints the device Array but does not set it
Serial << "Display deviceIndexArray " << endl << "----------------------------------\n" << endl;
for ( int i=0; i < 5; i++ ) {
Serial << "\n deviceIndexArray[" << i << "]: ";
printAddress(deviceIndexArray[i]);
}
Serial << "\n----------------------------------\n" << endl;
return 1;
}
if(command == "invert" ) {
oled.command(INVERTDISPLAY);
Serial << "normal display " << endl;
return 1;
}
if(command == "normal" ) {
Serial << "normal display " << endl;
oled.command(NORMALDISPLAY);
return 1;
}
if(command == "sysver" ) {
Serial << "System Version " << System.version().c_str() << endl;
return 1;
}
else return -1;
}
int relayFunc(String command) {
// need to set the displayMode and encoderPos otherwise it wont display
displayMode = encoderPos = 6;
if(command == "on" ) {
//first check the moisture
oPrintMoisture(); // will also publish
// Now I have to return to postion 6 since the check mositure wants me to got ot 4
displayMode = encoderPos = 6;
digitalWrite(relay, HIGH);
oPrintRelayMode();
Particle.publish("relay", "API on");
relayTimer.start();
return 1;
}
if(command == "off") {
expireRelay();
oPrintRelayMode();
Particle.publish("relay", "API off");
relayTimer.stop();
return 0;
}
}
void expireRelay(){
//now check the moisture again
oPrintMoisture(); // will also publish
//and return to posttion 6
displayMode = encoderPos = 6;
digitalWrite(relay, LOW);
oPrintRelayMode();
Particle.publish("relay", "expired off");
relayTimer.stop();
}
int setModeFunc(String command){ // now used for display mode and to toggle debug
if(command != "debug") {
oled.clear(PAGE);
oled.display();
displayMode = command.toInt();
encoderPos = displayMode;
return displayMode;
}
if(command == "debug"){
debug = !debug;
Serial.print("\n\n debug mode: ");
Serial.println(debug);
return debug;
}
}
void temperatureJob() {
float gotTemp = 0;
Serial << "the device count is " << deviceCount << endl;
sensor.requestTemperatures(); // get all the tempratures first to speed up, moved up from getTemp()
for (int i =0; i < deviceCount; i++ ) {
gotTemp = sensor.getTempF(*deviceAddressArray[i]);
if (gotTemp < -195 ) continue;
if (i+1 == 1 ) temp1 = gotTemp;
if (i+1 == 2 ) temp2 = gotTemp;
if (i+1 == 3 ) temp3 = gotTemp;
if (i+1 == 4 ) temp4 = gotTemp;
Serial << "gotTemp() = " << i << " " << gotTemp << endl;
request.body = formatTempToBody(gotTemp, i);
// if (mycounter % PUSHFREQ == 0 && PUSHTOUBIFLAG == 1 ) {
if (mycounter % PUSHFREQ == 0 && PUSHTOUBIFLAG == 1 ) {
String mypath = String("/api/v1.6/variables/");
mypath.concat(ubivar[i]);
mypath.concat("/values");
Serial << "going to push "<< request.body << " to " << mypath << endl;
request.path = mypath;
http.post(request, response, headers);
if( debug ) Serial << "http body: " << request.body << endl;
Serial << " Did we reboot? I hope not ";
}
if( debug) debugSerial(i);
}
}