Particle Photon Relay Shield and Temperature Sensor Integration

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

Hello,

Let me ping one of my engineers that works both with shields and firmware and might be able to help here. @mohit would you be able to help out here?

Kyle

Hi @BMET,

You have a really good start with two projects, so you have all your “building blocks”. It looks like it is just an issue of organizing your code.

Depending on what you want to accomplish, I assume you need to structure your loop code as follows:

  1. Read the temperature sensor
  2. Send the temperature sensor to the phone
  3. Get the command from the phone (on, off, automatic)
  4. Set the relay according to the command.
    The code you have now does not seem to flow in an organized path.

Also, you have a bunch of problems with variables. All your information you store in variables is not being made available to other parts of your code.
I suggest to read up on the differences between Local and Global variables and in general the scope of variables … this will help out a lot.

Here are a couple of links to help with variable scope:
http://en.cppreference.com/w/cpp/language/scope

By the way, I did pretty much the same thing to control a ventilation fan in a dog kennel, so it is definitely possible.

2 Likes