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;
}