-
Notifications
You must be signed in to change notification settings - Fork 9
/
2sparktemp.ino
409 lines (344 loc) · 11.1 KB
/
2sparktemp.ino
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
/* 8.12.2015 Kyle Bowerman
* sparkcore temprature sensors to UBidots
* from: https://particle.hackster.io/AgustinP/logging-temperature-data-using-the-spark-core
* from: spark-temp
* from OLDETEST
deviceOne: 2856B13A06000082
*/
#include "SparkFun_Micro_OLED_Particle_Library/firmware/SparkFunMicroOLED.h"
#include "math.h"
#include "HttpClient/HttpClient.h"
#include "OneWire/OneWire.h"
#include "spark-dallas-temperature/spark-dallas-temperature.h"
/*
Micro-OLED-Shield-Example.ino
SparkFun Micro OLED Library Hello World Example
Jim Lindblom @ SparkFun Electronics
Original Creation Date: June 22, 2015
This sketch prints a friendly, recognizable logo on the OLED Shield, then
goes on to demo the Micro OLED library's functionality drawing pixels,
lines, shapes, and text.
Hardware Connections:
This sketch was written specifically for the Photon Micro OLED Shield, which does all the wiring for you. If you have a Micro OLED breakout, use the following hardware setup:
MicroOLED ------------- Photon
GND ------------------- GND
VDD ------------------- 3.3V (VCC)
D1/MOSI ----------------- A5 (don't change)
D0/SCK ------------------ A3 (don't change)
D2 -------------------- oneWire bus
D/C ------------------- D6 (can be any digital pin)
RST ------------------- D7 (can be any digital pin)
CS ------------------- A2 (can be any digital pin)
Development environment specifics:
IDE: Particle Build
Hardware Platform: Particle Photon
SparkFun Photon Micro OLED Shield
This code is beerware; if you see me (or any other SparkFun
employee) at the local, and you've found our code helpful,
please buy us a round!
Distributed as-is; no warranty is given.
*/
//////////////////////////////////
// MicroOLED Object Declaration //
//////////////////////////////////
// Declare a MicroOLED object. If no parameters are supplied, default pins are
// used, which will work for the Photon Micro OLED Shield (RST=D7, DC=D6, CS=A2)
MicroOLED oled;
//////////////////////////////////
// other Declaration //
//////////////////////////////////
#define ONE_WIRE_BUS D2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);
#define VARIABLE_ID "55cbc6f2762542069b2798a9" // temp 1
#define TEMP2_VARIABLE_ID "55cbd3277625421c8977ecea"
#define TOKEN "hsFRLnMcucOZlfLsQbH9BRbJpveccOc37ksq7eLOtjztxoEpZDA1D2wnWiuP"
#define UBIVARSIZE 24
#define PUSHFREQ 100
#define FILENAME "2sparktemp"
#define MYVERSION "0.2"
//Globals
char* ubivar[]={"55cbc6f2762542069b2798a9","55cbd3277625421c8977ecea","55cd69c37625426d64a926d3"};
float temperature = 0.0;
char resultstr[64];
HttpClient http;
int deviceCount, lastDeviceCount, mycounter = 0;
DeviceAddress tempDeviceAddress, deviceOne, deviceTwo, deviceThree, deviceFour; // We'll use this variable to store a found device address
http_header_t headers[] = {
{ "Content-Type", "application/json" },
{ "X-Auth-Token" , TOKEN },
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;
int displayMode = 1;
bool debug = false;
//Prototypes
void printAddress(DeviceAddress deviceAddress);
int queryDevices(String command);
int setmode(String command);
int printEEProm(String command);
int regDevice(String command);
void setup()
{
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.
request.port = 80;
request.hostname = "things.ubidots.com";
Serial.begin(9600);
sensor.begin();
Spark.variable("count_devices", &deviceCount, INT);
Spark.function("q", queryDevices);
Spark.function("setmode", setModeFunc);
Spark.function("printEEProm", printEEPROMFunc);
Spark.function("regDevice", regDeviceFunc);
}
void loop()
{
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)
//delay(1000); // Delay 1000 ms
oled.clear(PAGE); // Clear the buffer.
Serial.println("\n\n The device Count Changed ");
}
deviceCount = getDeviceCount();
temperatureJob(); // do the main temprature job
lastDeviceCount = getDeviceCount(); // used to detect
}
void temperatureJob() {
for (int i =0; i < deviceCount; i++ ) {
request.body = getTemp(i);
if (mycounter % PUSHFREQ == 0 ) {
String mypath = String("/api/v1.6/variables/");
mypath.concat(ubivar[i]);
mypath.concat("/values");
request.path = mypath;
http.post(request, response, headers);
}
debugSerial(i);
delay(200);
}
}
void debugSerial(int i ) {
if( debug ) {
Serial.print("\nmycounter ");
Serial.print(mycounter);
Serial.print(" index ");
Serial.print(i);
Serial.print(" ");
Serial.print(request.body);
Serial.print(" dcount ");
Serial.print(deviceCount);
Serial.print(" lcount ");
Serial.println(lastDeviceCount);
}
}
char *getTemp(int tempIndex) {
static char retbuf[64];
sensor.requestTemperatures();
sensor.getAddress(tempDeviceAddress, tempIndex);
temperature=sensor.getTempCByIndex( tempIndex );
sprintf(retbuf, "{\"value\":%.4f}",temperature*1.8 + 32);
// oPrintTemp(tempIndex, temperature);
oDispatch(tempIndex, temperature);
return retbuf;
}
void oDispatch(int tempIndex, float mytemp) {
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 ) {
oPrintInfo();
}
}
void oPrintTemp(int index, float mytemp){
oled.setFontType(0);
oled.setCursor(0,0);
oled.print("devices ");
oled.print(sensor.getDeviceCount());
oled.setCursor(0,index*12+12);
oled.print("T");
oled.print(index);
oled.print(" ");
oled.print(mytemp*1.8 + 32);
oled.display();
}
void oPrintTemp2(int index, float mytemp){
oled.setFontType(0);
oled.setCursor(0,0);
oled.setCursor(0,index*12);
oled.print("T");
oled.print(index);
oled.print(" ");
oled.print(mytemp*1.8 + 32);
oled.display();
}
void oPrintInfo() {
oled.clear(PAGE);
oled.setCursor(0,0);
oled.print(FILENAME);
oled.setCursor(0,10);
oled.print("v");
oled.print(MYVERSION);
oled.setCursor(0,20);
oled.print("pfreq ");
oled.print(PUSHFREQ);
oled.setCursor(0,30);
oled.print("devices ");
oled.print(deviceCount);
oled.setCursor(0,40);
oled.print(convertMillisToHuman(millis()));
oled.display();
}
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);
}
int getDeviceCount() {
sensor.begin();
deviceCount = sensor.getDeviceCount();
return deviceCount;
}
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 == "0" ) {
if (!sensor.getAddress(deviceOne, 0)) Serial.println("\nUnable to find address for Device 0");
Serial.print("\ndeviceOne: ");
printAddress(deviceOne);
Serial.print(" ");
return 1;
}
if(command == "1") {
if (!sensor.getAddress(deviceTwo, 1)) Serial.println("\nUnable to find address for Device 1");
Serial.print("\ndeviceTwo: ");
printAddress(deviceTwo);
Serial.print(" ");
return 1;
}
if(command == "2") {
if (!sensor.getAddress(deviceThree, 2)) Serial.println("\nUnable to find address for Device 2");
Serial.print("\ndeviceThree: ");
printAddress(deviceThree);
Serial.print(" ");
return 1;
}
if(command == "3") {
if (!sensor.getAddress(deviceFour, 3)) Serial.println("\nUnable to find address for Device 3");
Serial.print("\ndeviceFour: ");
printAddress(deviceFour);
Serial.print(" ");
return 1;
}
else return -1;
}
int setModeFunc(String command){ // now used for display mode and to toggle debug
if(command != "debug") {
oled.clear(PAGE);
oled.display();
displayMode = command.toInt();
return displayMode;
}
if(command == "debug"){
debug = !debug;
Serial.print("\n\n debug mode: ");
Serial.println(debug);
return debug;
}
}
int regDeviceFunc(String command){
if(command == "searchReg") {
int regfound = 0;
for(int i = 0; i < 100; i++) {
if(EEPROM.read(i) == 0xFF ) {
regfound++;
Serial.print(i);
Serial.print(" EEPROM ");
Serial.println();
} else {
Serial.print(i);
Serial.println(" EEPROM not matching");
}
}
return regfound;
}
if(command != "searchReg" ) {
int mindex = command.toInt();
int setAddr = mindex * 8;
Serial.print("the address for device ");
Serial.print(mindex);
sensor.getAddress(tempDeviceAddress, mindex);
// Serial.print(" the size of the tempAddress is ");
// Serial.print(arraySize(tempDeviceAddress));
/* got address in array
1. run through eeprom and search for device
2. if no device is found find the available slot, return the location in eeprom
3. loop through the tempaddress and write it to the location.
*/
//Serial.println(tempDeviceAddress[0], HEX); // Works prints 28
//write the address at location 0 - 7
for (uint8_t i = 0; i < 8; i++) {
uint8_t writeLocation = i + setAddr;
EEPROM.write(writeLocation, tempDeviceAddress[i]);
}
return 2;
}
}