Hello, this is a specific question which will require some in depth coding knowledge to answer. We are using a Particle Photon, DS18B20 Temperature Sensor and the 1-Channel SPDT 10 Amp Particle relay shield from controleverything.com for a school project. Our knowledge of coding for these devices is very limited and we are using code tutorials and the libraries included on Particle Build so far. (NCD1Relay, OneWire, and Spark-Dallas-Temperature are the libraries included).
What we are trying to accomplish is to control the relay wirelessly using a HTTP request widget from an Android phone (turn on or off) and also have a setting (automatic) to integrate the relay with the temperature sensor such that it turns on or off the relay based on certain temperature threshold values received from the sensor. So far, the two programs (one using the NCD1Relay library and the other using the OneWire and spark-dallas-temperature libraries) work separately with the phone app, but we cannot figure out how to make them work together.
Below I have included our current code. At this point what happens is only the temperature sensing part of the code functions, the relay turning on or off will only happen if the temperature part in the loop is commented out. Any suggestions or advice is greatly appreciated, or recommendations of where to take this problem. Thanks for your time!
#include <NCD1Relay.h>
#include "spark-dallas-temperaturetest.h"
#include <OneWire.h>
NCD1Relay relayController;
SYSTEM_MODE(AUTOMATIC);
OneWire oneWire(D0);
DallasTemperature dallas(&oneWire);
double temperature = 0.0;
double temperatureF = 0.0;
int triggerRelay(String command);
bool tripped[7];
int debugTrips[7];
int minTrips = 5;
void setup()
{
Particle.variable("temperature", &temperature, DOUBLE);
Particle.variable("temperatureF", &temperatureF, DOUBLE);
dallas.begin();
Particle.function("controlRelay", triggerRelay);
Serial.begin(115200);
relayController.setAddress(0,0,0);
}
void loop()
{
int status = relayController.readAllInputs();
int a = 0;
for(int i = 1; i < 65; i*=2){
if(status & i){
debugTrips[a]++;
if(debugTrips[a] >= minTrips){
if(!tripped[a]){
tripped[a] = true;
//set input trip event to true
String eventName = "Input_";
eventName+=(a+1);
Particle.publish(eventName, "ON");
Serial.print("eventName: ");
Serial.println(eventName);
Serial.print("eventContents: ");
Serial.println("ON");
}
}
}else{
debugTrips[a] = 0;
if(tripped[a]){
tripped[a] = false;
//set input trip event to false
String eventName = "Input_";
eventName+=(a+1);
Particle.publish(eventName, "OFF");
Serial.print("eventName: ");
Serial.println(eventName);
Serial.print("eventContents: ");
Serial.println("OFF");
}
}
a++;
}
dallas.requestTemperatures();
// get the temperature in Celcius
float tempC = dallas.getTempCByIndex(0);
// convert to double
temperature = (double)tempC;
// convert to Fahrenheit
float tempF = DallasTemperature::toFahrenheit( tempC );
// convert to double
temperatureF = (double)tempF;
delay(5000);
}
int triggerRelay(String command){
if(command.equalsIgnoreCase("on")){
Serial.println("Turning on relay");
relayController.turnOnRelay();
Serial.println("returning");
return 1;
}
if(command.equalsIgnoreCase("off")){
relayController.turnOffRelay();
return 1;
}
if(command.equalsIgnoreCase("toggle")){
relayController.toggleRelay();
return 1;
}
if(command.equalsIgnoreCase("momentary")){
relayController.turnOnRelay();
delay(300);
relayController.turnOffRelay();
return 1;
}
return 0;
}