-
Notifications
You must be signed in to change notification settings - Fork 0
/
temperature.ino
117 lines (84 loc) · 2.93 KB
/
temperature.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
// This #include statement was automatically added by the Particle IDE.
#include "spark-dallas-temperature.h"
// This #include statement was automatically added by the Particle IDE.
#include <OneWire.h>
#include "version.h"
#include <sstream>
#include <cstring>
// Init Dallas one-wire temperature sensors on pin 6.
const int TEMPERATURE_SENSOR_PIN = D6;
// The on-board LED is on pin 7.
const int LED_PIN = D7;
const int DALLAS_RESOLUTION = 12;
// This determines how much space we allocate ahead of time
// for storing temperature readings.
const int MAX_NUMBER_OF_SENSORS = 8;
// Wait 60 sec between loop itterations.
const int LOOP_DELAY = 60000;
DallasTemperature dallas(new OneWire(TEMPERATURE_SENSOR_PIN));
// The number of temperature sensors detected on the one-wire pin.
int numberOfDevices = 0;
// Allocates the array where we store current temperature readings.
double temperatures[MAX_NUMBER_OF_SENSORS] = { 0.0 };
void setup(){
pinMode(LED_PIN, OUTPUT);
dallas.begin();
dallas.setResolution(DALLAS_RESOLUTION);
// Get the actual number of connected devices.
numberOfDevices = dallas.getDeviceCount();
setupParticleVariables(&numberOfDevices, temperatures);
}
void loop(){
// Turn LED On
digitalWrite(LED_PIN, HIGH);
dallas.requestTemperatures();
String values = String("{");
// Loop through each device and build a payload of the data.
for (int i = 0; i < numberOfDevices; i++) {
// Search the wire for address
DeviceAddress thermometerAddress;
if (dallas.getAddress(thermometerAddress, i)){
int sensorNumber = i + 1;
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i, DEC);
double tempTemperature = dallas.getTempC(thermometerAddress);
if (tempTemperature != DEVICE_DISCONNECTED_C) {
temperatures[i]= tempTemperature;
values.concat(
String::format(
" \"temperature_%d\": %f, ",
sensorNumber,
tempTemperature
)
);
Serial.print("Temp C: ");
Serial.print(tempTemperature);
Serial.print(" Temp F: ");
Serial.println(DallasTemperature::toFahrenheit(tempTemperature));
}
}
}
values.concat(
String::format("\"devices:\": %d }", numberOfDevices)
);
String data = String::format(
"{ \"tags\" : {\"id\": \"%s\", \"location\": \"%s\", \"firmware\": \"%s\"}, \"values\": %s }",
"t2",
"fermentor 1",
FIRMWARE_VERSION,
values.c_str()
);
Particle.publish("temperature", data, PRIVATE);
// Turn the LED Off
digitalWrite(LED_PIN, LOW);
delay(LOOP_DELAY);
}
void setupParticleVariables(int *numberOfDevices, double *temperatures) {
Particle.variable("devices", numberOfDevices, INT);
for (int i = 0; i < *numberOfDevices; i++) {
int sensorNumber = i + 1;
String temperatureVar = "temperature" + String(sensorNumber);
Particle.variable(temperatureVar, &temperatures[i], DOUBLE);
}
}