Defining a desired parameter in Loop with Particle.function

Very new.

Currently successful at controlling an ultrasonic humidifier based on sensor readings by comparing the sensor value against a global int. Would love to be able to change this variable via particle.function, but I don’t know how. Here’s what I have so far.

//Particle Photon placed in garden to first control humidity, eventually will read sensors, 
//spit out data to Thingspeak, and take input for parameters. 
//For now, it is keeping humidity in this room at 60% by turning on and off an ultrasonic humidifier based on sensor readings
//Would like to control what the desired RH is via particle.Function

#include "math.h"
#include "adafruit-sht31.h"
int humdisc = D4; //24v ultrasonic disc
int setHumidity =60; //desired %RH

Adafruit_SHT31 sht31 = Adafruit_SHT31();

void setup() {
  Serial.begin(115200);
  Serial.println("SHT31 test");
  Particle.publish("Oh, here we go again");     //following the train, publishing to Particle about it.
  if (! sht31.begin(0x44)) {   // Copied from sht31test.ino
    Serial.println("Couldn't find SHT31");
    Particle.publish("All you had to do...."); //bad references
    }
    pinMode(humdisc, OUTPUT); //wired to relay.

  }
 


void loop() {
    double t = sht31.readTemperature();
    double h = sht31.readHumidity();
    Particle.variable("Temp", t);
    Particle.variable("Hum", h);
   
    if (setHumidity > (h)){ //Would like to edit setHumidity from a Particle.function, how do?
        digitalWrite(humdisc, LOW);
    } else {
        digitalWrite(humdisc, HIGH);
    }
 
    
    
    if (! isnan(t)) {  // check if 'is not a number'
        //Temperature in C
        Serial.print("Temp C = "); Serial.println(t);
    } else {
        Serial.println("Failed to read temperature");
    }


    if (! isnan(h)) {  // check if 'is not a number'
        Serial.print("Hum. % = "); Serial.println(h);
    } else {
        Serial.println("Failed to read humidity");
    }
    
    String output = "T:" + String(t,2) + ",H:" + String(h,2);
    Serial.println(output);
    Particle.publish("TempHumidity", output, PRIVATE);
    delay(5000);
}

Very eager to learn the required vocabulary to articulate what I’m tryna do here. Thanks for any time you spend on this in advance.

Welcome,

You could use Particle.variable() in setup(), to always show current values to the console

Particle.variable("humiditySetPoint", setHumidity);

then create a function

// ------------------------------------------------------------------------------
int humidityFunction(String humidtySetting)
// ------------------------------------------------------------------------------
{
   setHumidity = humiditySetting.toInt();
   Serial.printlnf("Set humidity level to %d", setHumidity);
   return 0;
}

Define this in function callback setup())

Particle.function("setHumidity", humidityFunction);

and then you will see current humidity set point and be able to change it.

*Please excuse any code errors - have not tested this in IDE…

2 Likes

This is a statment I see often in Particle.function() handlers (even in the docs) but it doesn't really pay credit to the actual intent of the function signature.

A return value should add some value to the process.
Why not return a meaningful value - e.g.

  • return setHumidity; to indicate what result the .toInt() call rendered, or
  • this
int humidityFunction(String humidtySetting) {
  int retVal = setHumidity; // remember previous value
  setHumidity = humiditySetting.toInt();
  Serial.printlnf("Set humidity level to %d", setHumidity);
  return retVal;            // tell us what we've just overwritten
}
  • or the delta between new and old value
  • ...

just not some meaningless constant.

3 Likes

Now that is a really valuable piece of wisdom. Thanks @ScruffR

1 Like

Wow, thank you for clarifying. Photon is operating as desired! Biggest mental hurdles were needing the starting placeholder int and how the .toInt() call is used, and making sure the references are in the correct order. Thanks again!