Skip to content

Commit

Permalink
Move Particle variable registration to setup function
Browse files Browse the repository at this point in the history
Previously these were being registered in the main loop
but that is not the correct way to expose them to the Particle
cloud. This change refactors the variables we want to be accessible
through the cloud API to be globals and registers them in the `setup`
function.
  • Loading branch information
forkata committed Oct 25, 2021
1 parent 8d42782 commit 23818b7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
36 changes: 22 additions & 14 deletions temperature.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,32 @@ 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;
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);

numberOfDevices = 0;

dallas.begin();

dallas.setResolution(DALLAS_RESOLUTION);

// Get the actual number of connected devices.
numberOfDevices = dallas.getDeviceCount();

setupParticleVariables(&numberOfDevices, temperatures);
}

void loop(){
Expand All @@ -43,21 +51,15 @@ void loop(){

dallas.requestTemperatures();

double temperatures[numberOfDevices] = { 0.0 };

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 resolution = dallas.getResolution(thermometerAddress);
int sensorNumber = i + 1;

String resolutionVar = "resolution" + String(sensorNumber);
Particle.variable(resolutionVar, &resolution, INT);

// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i, DEC);
Expand All @@ -75,9 +77,6 @@ void loop(){
)
);

String temperatureVar = "temperature" + String(sensorNumber);
Particle.variable(temperatureVar, &temperatures[i], DOUBLE);

Serial.print("Temp C: ");
Serial.print(tempTemperature);
Serial.print(" Temp F: ");
Expand All @@ -90,8 +89,6 @@ void loop(){
String::format("\"devices:\": %d }", numberOfDevices)
);

Particle.variable("devices", &numberOfDevices, INT);

String data = String::format(
"{ \"tags\" : {\"id\": \"%s\", \"location\": \"%s\", \"firmware\": \"%s\"}, \"values\": %s }",
"t2",
Expand All @@ -107,3 +104,14 @@ void loop(){

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);
}
}
2 changes: 1 addition & 1 deletion version.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define FIRMWARE_VERSION "0.0.1"
#define FIRMWARE_VERSION "0.0.2"

0 comments on commit 23818b7

Please sign in to comment.