Help needed with Particle Function not working

Hello all i have an issue in my code where i can pull a location on request as well as battery info, however trying to trip a simple relay is not working properly. could anyone advise ?

here is the code


#include "AssetTracker.h"

int transmittingData = 1;

// Used to keep track of the last time we published data
long lastPublish = 0;

// How many minutes between publishes? 10+ recommended for long-time continuous publishing!
int delayMinutes = 1;

// Creating an AssetTracker named 't' for us to reference
AssetTracker t = AssetTracker();

// A FuelGauge named 'fuel' for checking on the battery state
FuelGauge fuel;

//Name the Relays
int Relay1 = D1;
int Relay2 = D2;
int Relay3 = D3;
int Relay4 = D4;

//Set Default Relay Start State
int Relay1_state = 0;
int Relay2_state = 0;
int Relay3_state = 0;
int Relay4_state = 0;

void setup() {
    
    //Enable Relays
    pinMode(Relay1, OUTPUT); 
    pinMode(Relay2, OUTPUT); 
    pinMode(Relay3, OUTPUT); 
    pinMode(Relay4, OUTPUT); 
    
    // Start the Asset Tracker with new name
    t.begin();

    // Enable the GPS module. Defaults to off to save power.
    t.gpsOn();

    // Opens up a Serial port so you can listen over USB
    Serial.begin(9600);

    // These three functions are useful for remote diagnostics. Read more below.
    Particle.function("Update-Mode", transmitMode);
    Particle.function("Battery", batteryStatus);
    Particle.function("Location", gpsPublish);
    Particle.function("Relay Trigger", RelayFire);

}


void loop() {
    // GPS Update
    t.updateGPS();

    // if the current time - the last time we published is greater than your set delay...
    if (millis()-lastPublish > delayMinutes*60*1000) {
        // Remember when we published
        lastPublish = millis();

        if (t.gpsFix()) {
            // Only publish if we're in transmittingData mode 1;
            if (transmittingData) {
                // Short publish names save data!
                Particle.publish("Location", t.readLatLon(), 60, PRIVATE);
            }
        }
    }
}

//Transmit Mode Function
int transmitMode(String command) {
    transmittingData = atoi(command);
    return 1;
}

//Location Function
int gpsPublish(String command) {
    if (t.gpsFix()) {
        Particle.publish("Location", t.readLatLon(), 60, PRIVATE);

        // uncomment next line if you want a manual publish to reset delay counter
        // lastPublish = millis();
        return 1;
    } else {
      return 0;
    }
}

// Lets you remotely check the battery status by calling the function "batt"
// Triggers a publish with the info (so subscribe or watch the dashboard)
// and also returns a '1' if there's >10% battery left and a '0' if below
int batteryStatus(String command){
    // Publish the battery voltage and percentage of battery remaining
    // if you want to be really efficient, just report one of these
    // the String::format("%f.2") part gives us a string to publish,
    // but with only 2 decimal points to save space
    Particle.publish("Battery Health",
          "V=" + String::format("%.2f",fuel.getVCell()) +
          "Cell=" + String::format("%.2f""%",fuel.getSoC()),
          60, PRIVATE
    );
    // if there's more than 10% of the battery left, then return 1
    if (fuel.getSoC()>10){ return 1;}
    // if you're running out of battery, return 0
    else { return 0;}
}

int RelayFire(String command) {
if(command == "Relay1")
  {
    digitalWrite(Relay1, (Relay1_state) ? HIGH : LOW);
    Relay1_state = !Relay1_state;
    return 1;
  } else if(command == "Relay2")
  {
    digitalWrite(Relay2, (Relay2_state) ? HIGH : LOW);
    Relay2_state = !Relay2_state;
    return 1;
  } else if(command == "Relay3")
  {
    digitalWrite(Relay3, (Relay3_state) ? HIGH : LOW);
    Relay3_state = !Relay3_state;
    return 1;
  } else if(command == "Relay4")
  {
    digitalWrite(Relay4, (Relay4_state) ? HIGH : LOW);
    Relay4_state = !Relay4_state;
    return 1;
  } else if(command == "turnoff")
  {
    digitalWrite(Relay1, LOW); 
    digitalWrite(Relay2, LOW); 
    digitalWrite(Relay3, LOW); 
    digitalWrite(Relay4, LOW); 
    return 1;
  } else if(command == "turnon")
  {
    digitalWrite(Relay1, HIGH); 
    digitalWrite(Relay2, HIGH); 
    digitalWrite(Relay3, HIGH); 
    digitalWrite(Relay4, HIGH); 
    return 1;
  }
  else return -1;
}

How about this?
https://docs.particle.io/reference/device-os/firmware/electron/#particle-function-

Also

Explaining a bit clearer what "not working properly" actually means would be good.

1 Like

That was exactly it your amazing, my code now looks like this, as you will see i attempted to begin adding in another function to call temperature and humidity data as well but the floats seem to cause hell, i am refrencing [https://docs.particle.io/tutorials/hardware-projects/e-series-eval-kit/] to try and manage this, you have already helped plenty but if you have anything that jumps out at you please let me know

#include <Adafruit_DHT.h>
#include "AssetTracker.h"

#define DHTPIN D1
#define DHTTYPE DHT11 

DHT dht(DHTPIN, DHTTYPE);

int transmittingData = 0;

// Used to keep track of the last time we published data
long lastPublish = 0;

// How many minutes between publishes? 10+ recommended for long-time continuous publishing!
int delayMinutes = 1;

// Creating an AssetTracker named 't' for us to reference
AssetTracker trk = AssetTracker();

// A FuelGauge named 'fuel' for checking on the battery state
FuelGauge fuel;

//Name the Relays
int Relay1 = D1;

//Set Default Relay Start State
int Relay1_state = 1;

void setup() {
    
    //Enable DHT
    dht.begin();
    
    //Enable Relays
    pinMode(Relay1, OUTPUT); 
    
    // Start the Asset Tracker with new name
    trk.begin();

    // Enable the GPS module. Defaults to off to save power.
    trk.gpsOn();

    // Opens up a Serial port so you can listen over USB
    Serial.begin(9600);

    // These three functions are useful for remote diagnostics. Read more below.
    Particle.function("Update-Mode", transmitMode);
    Particle.function("Battery", batteryStatus);
    Particle.function("Location", gpsPublish);
    Particle.function("Relay-Trigger", relayFire);
    //Particle.function("Temperature", tempStatus);
    //Particle.function("Humidity", humiState);

}


void loop() {

    // GPS Update
    trk.updateGPS();

    // if the current time - the last time we published is greater than your set delay...
    if (millis()-lastPublish > delayMinutes*60*1000) {
        // Remember when we published
        lastPublish = millis();

        if (trk.gpsFix()) {
            // Only publish if we're in transmittingData mode 1;
            if (transmittingData) {
                // Short publish names save data!
                Particle.publish("Location", trk.readLatLon(), 60, PRIVATE);
            }
        }
    }
}

        
    
//Transmit Mode Function
int transmitMode(String command) {
    transmittingData = atoi(command);
    return 1;
}

//Location Function
int gpsPublish(String command) {
    if (trk.gpsFix()) {
        Particle.publish("Location", trk.readLatLon(), 60, PRIVATE);

        // uncomment next line if you want a manual publish to reset delay counter
        // lastPublish = millis();
        return 1;
    } else {
      return 0;
    }
}

// Lets you remotely check the battery status by calling the function "batt"
// Triggers a publish with the info (so subscribe or watch the dashboard)
// and also returns a '1' if there's >10% battery left and a '0' if below
int batteryStatus(String command){
    // Publish the battery voltage and percentage of battery remaining
    // if you want to be really efficient, just report one of these
    // the String::format("%f.2") part gives us a string to publish,
    // but with only 2 decimal points to save space
    Particle.publish("Battery Health",
          "V=" + String::format("%.2f",fuel.getVCell()) +
          "Cell=" + String::format("%.2f""%",fuel.getSoC()),
          60, PRIVATE
    );
    // if there's more than 10% of the battery left, then return 1
    if (fuel.getSoC()>10){ return 1;}
    // if you're running out of battery, return 0
    else { return 0;}
}

int relayFire(String command){
if(command == "High") {
    digitalWrite(Relay1, HIGH);
    return 1;
}

if(command == "LOW") {
    digitalWrite(Relay1, LOW);
    return 1;    

  } else {
  return 0;
  }
}  
  




Again, could you be a bit more specific and technical about what the actual problem is?
What are the symptoms?
What do you expect to happen and what do you see happening?

Which function?
I can see two Particle.function() calls commented out, but I don't see any implementation of the respecitve callback functions :confused:

BTW, the way to add a percent sign to a formatted string is like this

String::format("%.2f\%",fuel.getSoC())

And if you already use String::format() (which I'd rather replace with the use of snprintf()) you don't need to use it multiple times. One String::format() can format the entire string in one go.

 Particle.publish("Battery Health"
                 , String::format("V=%.2fV Cell=%.2f\%"
                 , fuel.getVCell(), fuel.getSoC()) 
                 , PRIVATE);

but I'd go with

  char data[32];
  snprintf(data, sizeof(data)
          , "V=%.2fV Cell=%.2f\%"
          , fuel.getVCell(), fuel.getSoC());
  Particle.publish("Battery Health", data, PRIVATE);
4 Likes