From 23818b78095c3a2855ddcebe99e67ae3222a003d Mon Sep 17 00:00:00 2001 From: Chris Todorov Date: Sun, 24 Oct 2021 23:57:36 -0700 Subject: [PATCH] Move Particle variable registration to setup function 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. --- temperature.ino | 36 ++++++++++++++++++++++-------------- version.h | 2 +- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/temperature.ino b/temperature.ino index 89baa01..bc6ee14 100644 --- a/temperature.ino +++ b/temperature.ino @@ -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(){ @@ -43,8 +51,6 @@ void loop(){ dallas.requestTemperatures(); - double temperatures[numberOfDevices] = { 0.0 }; - String values = String("{"); // Loop through each device and build a payload of the data. @@ -52,12 +58,8 @@ void loop(){ // 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); @@ -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: "); @@ -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", @@ -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); + } +} diff --git a/version.h b/version.h index eeefbdf..ef6e3b1 100644 --- a/version.h +++ b/version.h @@ -1 +1 @@ -#define FIRMWARE_VERSION "0.0.1" +#define FIRMWARE_VERSION "0.0.2"