Programmable Relay Timer (Relay shield, Ubidots, IFTT?)

I have this project I've been working on, see the post linked below for an intro.

I now have this project installed in my greenhouse and reporting values to ubidots. I have connected the lights, pump, and fans to 3 of the relays on the relay shield. All I need at this stage is a simple timer to program the relays. I need to tell each relay how many times per day to turn on, and for how long. I connected all the loads to the normally closed terminals as a fail safe. If the photon looses power, the equipment stays on continuously, which is "safe" in this application.

lights - these will be simplest, 20 hours on, 4 hours off.

fans - these need to turn on when the temp is too high, and a couple times per day regardless. An over-ride button in Ubidots would be helpful.

pumps - these will need to cycle on/off a few times a day, for a few minutes. An over-ride button in Ubidots would be helpful.

In the end, I will want to control the variables from an app or web. For now however, changing them in the webIDE is fine for now. I was looking into the time libraries, then read that they are now baked into the firmware. So I assume I should use the built-in functions.

The variables should look something like this:

Relay1 start time
Relay1 time between intervals (in hours)
Relay1 length of each interval (in minutes)

Can a local expert point me in the right direction? Do I need to use the relay shield library for this at all?

Thanks!

Sounds like you're trying to do the same thing as here, might be interesting.

Thanks. That thread is a bit of a nightmare, but will be a great resource as things progress. For now, I’d like to keep it way, way, way simpler.

I would be happy with hard-coded values for one relay with an on time and off time each day.

if time = xx
relay on
if time = xx
relay off

You could use the timeAlarms library for that, or alternatively compare values to the built in time functions: https://docs.particle.io/reference/firmware/photon/#time

1 Like

Thanks! That really got me a good start. I looked at the Timealarms library and it seems to fit the bill at this stage.

For now, I setup a single timer to turn on relay 1 and relay 2, as both are connected to lights. I want the relays on for 20 hours, and off for 4. So I used one alarm that will turn them on at 04:00 (4AM), and another to turn them off at 00:00 (midnight).

Additionally, I included the relayControl command out of the particle documentation, so that I can send an API call to turn on or off any of the relays remotely. Since I already have this app communicating values to Ubidots, I hope to use buttons on the ubidots dashboard to send these API calls (will that work?). This is a simple over-ride, so that I can turn the lights off if I need to change a bulb or something like that.

I started with this up top:

//setup the relays
int RELAY1 = D3;
int RELAY2 = D4;
int RELAY3 = D5;
int RELAY4 = D6;

// function for relay control from particle documentation
// command format r1,HIGH
int relayControl(String command)
    {
    int relayState = 0;
    // parse the relay number
    int relayNumber = command.charAt(1) - '0';
    // do a sanity check
    if (relayNumber < 1 || relayNumber > 4) return -1;

    // find out the state of the relay
    if (command.substring(3,7) == "HIGH") relayState = 1;
    else if (command.substring(3,6) == "LOW") relayState = 0;
    else return -1;

    // write to the appropriate relay
    digitalWrite(relayNumber+2, relayState);
    return 1;
    }
    
// defines what the LightsOn, Lights off commands do
    void LightsOn()
        {
        digitalWrite(RELAY1, HIGH); // turn on the lights connected to relay 1
        digitalWrite(RELAY2, HIGH); // turn on the lights connected to relay 2
        Serial.println("Lights are turned on");  // print the status of the lights
        }
    void LightsOff()
        {
        digitalWrite(RELAY1,LOW); // turn off lights on relay 1
        digitalWrite(RELAY2,LOW); // turn off lights on relay 2
        Serial.println("Lights are turned off"); // print the status of the lights
        }

then in my setup:

    // set the time zone
    Time.zone(-8);
    
    // Setup the Relay Shield pins
    pinMode(RELAY1, OUTPUT);
    pinMode(RELAY2, OUTPUT);
    pinMode(RELAY3, OUTPUT);
    pinMode(RELAY4, OUTPUT);
    // Initialize all relays to an OFF state
    digitalWrite(RELAY1, LOW);
    digitalWrite(RELAY2, LOW);
    digitalWrite(RELAY3, LOW);
    digitalWrite(RELAY4, LOW);

    // Setup Particle variables
    Particle.variable("i2cdevice", "SHT31");
    Particle.variable("cTemp", cTemp);
    Particle.variable("humidity", humidity);
    Particle.variable("lux", tLux);
    Particle.variable("Lights", LightsOn);

    
    // Setup Particle function for relay control
    Particle.function("relay", relayControl);
    
     // create the alarms 
    Alarm.alarmRepeat(4,00,0, LightsOn);  // 4:00am every day
    Alarm.alarmRepeat(0,00,10,LightsOff);  // 00:00am every day 

nothing related to the timer goes into the loop afaik.

The code builds without errors. Can you help me improve my code and skills by suggesting where you would do things differently? I am not very confident with what order everything should be in.

My next step is to setup an over-ride button in Ubidots to manually turn the lights on and off. This should not mess with or disable the alarms, so if I over-ride the lights on, when the alarm comes, they should just stay on.

The path I am paving here is hopefully leading to an android homescreen widget that displays my garden parameters and allows some basic control of lights, fans, and solenoids. I am not sure if ubidots is the way to this goal, but for now, it seems to be an easy way to bring the data to life.

Actually there is. You need to call Alarm.delay(unsigned long ms) in loop() so that the alarm gets serviced.

2 Likes

Thanks!

well the code doesn’t seem to work, the relays are not responding at the programmed alarm times.

Hi @vinistois

Can you show us your whole program?

In particular pay attention to the advice from @Ric above to call Alarm.delay(1); in your loop() section. The way the alarms work is that they cannot fire unless you do this.

1 Like

I really appreciate your assistance. I do have that in my loop, but I think I may be having other issues. They don’t have anything to do with this topic so I have another thread. Once that is sorted out I will try again. In the meantime, here is the code in entirety. The D7/led stuff is an attempt at troubleshooting.



#include "TimeAlarms/TimeAlarms.h"
#include "RelayShield/RelayShield.h"
#include "HttpClient/HttpClient.h"
#include "Adafruit_TSL2591/Adafruit_TSL2591.h"
#include "Adafruit_TSL2591/Adafruit_Sensor.h"
#include "application.h"
#include "spark_wiring_i2c.h"

// SHT31 I2C address is 0x44(68)
#define Addr 0x44


//ubidots setup
#define VARIABLE_ID "56ede920762542261507ae9e"
#define VARIABLE_ID2 "56ee40fb76254256205bcdcf"
#define VARIABLE_ID3 "56ee46947625427931a1fba6"
#define TOKEN "b9vOW8InVPmY0BHPkNTvzhprGcHTFD"
HttpClient http;
http_header_t headers[] = {
        { "Content-Type", "application/json" },
        { NULL, NULL } // NOTE: Always terminate headers will NULL
    };
http_request_t request;
http_response_t response;
//end ubidots setup


// set the temp, humidity, and lux vairables to zero
double cTemp = 0.0, fTemp = 0.0, humidity = 0.0, tLux = 0.0;

// selects the external u.FL antenna
STARTUP(WiFi.selectAntenna(ANT_EXTERNAL)); 

//setup the relays
int RELAY1 = D3;
int RELAY2 = D4;
int RELAY3 = D5;
int RELAY4 = D6;

int LED = D7;

// function for relay control
// command format r1,HIGH
int relayControl(String command)
    {
    int relayState = 0;
    // parse the relay number
    int relayNumber = command.charAt(1) - '0';
    // do a sanity check
    if (relayNumber < 1 || relayNumber > 4) return -1;

    // find out the state of the relay
    if (command.substring(3,7) == "HIGH") relayState = 1;
    else if (command.substring(3,6) == "LOW") relayState = 0;
    else return -1;

    // write to the appropriate relay
    digitalWrite(relayNumber+2, relayState);
    return 1;
    }
    
// Function defines what the LightsOn, Lights off commands do
    void LightsOn()
        {
        digitalWrite(RELAY1, HIGH);
        digitalWrite(RELAY2, HIGH);
        Serial.println("Lights are turned on");
        }
    void LightsOff()
        {
        digitalWrite(RELAY1,LOW);
        digitalWrite(RELAY2,LOW);
        Serial.println("Lights are turned off");
        }
    

//lux sensor setup:
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // pass in a number for the sensor identifier (for your use later)

/**************************************************************************/
/*
    Displays some basic information on this sensor from the unified
    sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
/**************************************************************************/
void displaySensorDetails(void)
    {
    sensor_t sensor;
    tsl.getSensor(&sensor);
    Serial.println("------------------------------------");
    Serial.print  ("Sensor:       "); Serial.println(sensor.name);
    Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
    Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
    Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" lux");
    Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" lux");
    Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" lux");
    Serial.println("------------------------------------");
    Serial.println("");
    delay(500);
    }

/**************************************************************************/
/*
    Configures the gain and integration time for the TSL2591
*/
/**************************************************************************/
void configureSensor(void)
    {
    // You can change the gain on the fly, to adapt to brighter/dimmer light situations
    //tsl.setGain(TSL2591_GAIN_LOW);    // 1x gain (bright light)
    tsl.setGain(TSL2591_GAIN_MED);      // 25x gain
    // tsl.setGain(TSL2591_GAIN_HIGH);   // 428x gain

    // Changing the integration time gives you a longer time over which to sense light
    // longer timelines are slower, but are good in very low light situtations!
    // tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS);  // shortest integration time (bright light)
     tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
    // tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
    // tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
    // tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
    // tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS);  // longest integration time (dim light)

    /* Display the gain and integration time for reference sake */
    Serial.println("------------------------------------");
    Serial.print  ("Gain:         ");
    tsl2591Gain_t gain = tsl.getGain();
    switch(gain)
    {
        case TSL2591_GAIN_LOW:
            Serial.println("1x (Low)");
        break;
        case TSL2591_GAIN_MED:
            Serial.println("25x (Medium)");
        break;
        case TSL2591_GAIN_HIGH:
            Serial.println("428x (High)");
        break;
        case TSL2591_GAIN_MAX:
            Serial.println("9876x (Max)");
        break;
    }
    Serial.print  ("Timing:       ");
    Serial.print((tsl.getTiming() + 1) * 100, DEC);
    Serial.println(" ms");
    Serial.println("------------------------------------");
    Serial.println("");
    }
// end lux sensor setup


//setup 
void setup(void)
    {
    // setup adafruit HDR light sensor    

    Serial.begin(9600);

    Serial.println("Starting Adafruit TSL2591 Test!");

    if (tsl.begin())
        {
        Serial.println("Found a TSL2591 sensor");
        }
    else
        {
        Serial.println("No sensor found ... check your wiring?");
    while (1);
    }

    // Configure the sensor
    configureSensor();

    // set the time zone
    Time.zone(-8);
    
    // Setup the Relay Shield pins
    pinMode(RELAY1, OUTPUT);
    pinMode(RELAY2, OUTPUT);
    pinMode(RELAY3, OUTPUT);
    pinMode(RELAY4, OUTPUT);
    
    pinMode(LED, OUTPUT);
    
    Particle.function("ledtest", LedTest);
    
    
    
    // Initialize all relays to an OFF state
    digitalWrite(RELAY1, LOW);
    digitalWrite(RELAY2, LOW);
    digitalWrite(RELAY3, LOW);
    digitalWrite(RELAY4, LOW);
    
    digitalWrite(LED, LOW);
    

    // Setup Particle variables
    Particle.variable("i2cdevice", "SHT31");
    Particle.variable("cTemp", cTemp);
    Particle.variable("humidity", humidity);
    Particle.variable("lux", tLux);
    Particle.variable("Lights", LightsOn);

    
    // Setup Particle function for relay control
    int relayControl(String command);
    Particle.function("relay", relayControl);
    
     // create the alarms 
    Alarm.alarmRepeat(4,00,0, LightsOn);  // 4:00am every day
    Alarm.alarmRepeat(0,00,10,LightsOff);  // 00:00am every day 
    // Alarm.timerRepeat(15, Repeats);            // timer for every 15 seconds    
    // Alarm.timerOnce(10, OnceOnly);             // called once after 10 seconds

    
    // Initialise I2C communication as MASTER 
    Wire.begin();
    // Initialise serial communication, set baud rate = 9600
    Serial.begin(9600);
    
    }
    
    


    int LedTest(String command){
        if (command == "on"){
           digitalWrite(LED, HIGH);
           return 1;
           }
        
    }




void loop(void)
{
    Alarm.delay(500); // Call Alarm Function    

    // lux calculations
    uint32_t lum = tsl.getFullLuminosity();
    uint16_t ir, full;
    ir = lum >> 16;
    full = lum & 0xFFFF;
    Serial.print("[ "); Serial.print(millis()); Serial.print(" ms ] ");
    Serial.print("IR: "); Serial.print(ir);  Serial.print("  ");
    Serial.print("Full: "); Serial.print(full); Serial.print("  ");
    Serial.print("Visible: "); Serial.print(full - ir); Serial.print("  ");
    Serial.print("Lux: "); Serial.println(tsl.calculateLux(full, ir));
    float tLux = (tsl.calculateLux(full, ir));
    delay(300);
  
  
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
   
    // Send 16-bit command byte          
    Wire.write(0x2C);
    Wire.write(0x06);
   
    // Stop I2C transmission
    Wire.endTransmission();
    delay(500);

    unsigned int data[6];
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Stop I2C Transmission
    Wire.endTransmission();
    
    // Request 6 bytes of data from the device
    Wire.requestFrom(Addr,6);
    
    // Read 6 bytes of data
    // temp msb, temp lsb, crc, hum msb, hum lsb, crc
    if(Wire.available() == 6)
    {
        data[0] = Wire.read();
        data[1] = Wire.read();
        data[2] = Wire.read();
        data[3] = Wire.read();
        data[4] = Wire.read();
        data[5] = Wire.read();
    }
    delay(300);
  
    // Convert the data
    float cTemp = ((((data[0] * 256.0) + data[1]) * 175.72) / 65536.0) - 46.85;
    float fTemp = (cTemp * 1.8) + 32;
    float humidity = ((((data[3] * 256.0) + data[4]) * 125) / 65535.0) - 6;
    
    
    // Output data to dashboard
    Particle.publish("Temperature in Celsius: ",  String(cTemp));
    delay(300);
    Particle.publish("Temperature in Fahrenheit: ", String(fTemp));
    delay(300);
    Particle.publish("Relative Humidity: ",  String(humidity));
    delay(300);
    Particle.publish("Lux: ",  String(tLux));
    delay(300);
   
    
    //ubidots
    delay(100);
    // setting ubidots request path 1
    request.hostname = "things.ubidots.com";
    request.port = 80;
    request.path = "/api/v1.6/variables/"VARIABLE_ID"/values?token="TOKEN;
    
    // sending variable 1 to ubidots
    Serial.println("Sending data ...");
    request.body = "{\"value\":" + String(cTemp) + "}";

    // Ubidots Variable 1 Post request
    http.post(request, response, headers);
    Serial.println(response.status);
    Serial.println(response.body);
    
    delay(100);
    
    // setting ubidots request path 2
    request.hostname = "things.ubidots.com";
    request.port = 80;
    request.path = "/api/v1.6/variables/"VARIABLE_ID2"/values?token="TOKEN;        
    
     // sending varible 2 to ubidots
    Serial.println("Sending data ...");
    request.body = "{\"value\":" + String(humidity) + "}";

    // Ubidots variable 2 Post request
    http.post(request, response, headers);
    Serial.println(response.status);
    Serial.println(response.body);
    
    delay(100);
    
    // setting ubidots request path 3
    request.hostname = "things.ubidots.com";
    request.port = 80;
    request.path = "/api/v1.6/variables/"VARIABLE_ID3"/values?token="TOKEN;        
    
    // sending varible 3 to ubidots
    Serial.println("Sending data ...");
    request.body = "{\"value\":" + String(tLux) + "}";

    // Ubidots variable 3 Post request
    http.post(request, response, headers);
    Serial.println(response.status);
    Serial.println(response.body);
    
    delay(10000);
    
}

Hi @vinistois

My first suggestion is to move the Particle variable and function declarations to the very top of the setup() function. There is a cloud setup message that is sent asynchronously and long delays at the start of the setup() function can mean that the cloud does not "see" your variables and functions.

My second suggestion is to cut your code down to the minimum test case that shows the problem. If you can get it to where any of us can run your test case, then we can debug it. Right now I don't have the hardware to drive or the Ubidots account to test with, for instance. The relays are not problem since they are output only.

If you can't cut the code down, then you will have to provide debugging logs such as the output from the serial port debug statements and the event logs for your published events for anyone to make progress.

That is fantastic advice, hugely appreciated.

Is there a way to break up the code into tabs? I would rather have all the relay stuff on one tab, all the sensor stuff on another, ubidots on its own, etc. Right now it’s super easy to lose track of what I’m looking for, especially being such a noob.

I would love if the main program screen could just reference the various tabs, then I could just easily comment out that whole part for troubleshooting.

Thanks again.

Alright, I’m back in business.

I moved things around in the code as per your suggestion. Now the photon starts breathing green instead of cyan.

I also write pin 7 high then low a couple of times in the code with a delay so that I can see if its actually stepping through that portion. Well it seems like it’s not even making it through the setup.

here’s my current code:


#include "TimeAlarms/TimeAlarms.h"
#include "RelayShield/RelayShield.h"
#include "HttpClient/HttpClient.h"
#include "Adafruit_TSL2591/Adafruit_TSL2591.h"
#include "Adafruit_TSL2591/Adafruit_Sensor.h"
#include "application.h"
#include "spark_wiring_i2c.h"

// SHT31 I2C address is 0x44(68)
#define Addr 0x44


//ubidots setup
#define VARIABLE_ID "56ede920762542261507ae9e"
#define VARIABLE_ID2 "56ee40fb76254256205bcdcf"
#define VARIABLE_ID3 "56ee46947625427931a1fba6"
#define TOKEN "b9vOW8InVPmY0BHPkNTvzhprGcHTFD"
HttpClient http;
http_header_t headers[] = {
        { "Content-Type", "application/json" },
        { NULL, NULL } // NOTE: Always terminate headers will NULL
    };
http_request_t request;
http_response_t response;
//end ubidots setup


// set the temp, humidity, and lux vairables to zero
double cTemp = 0.0, fTemp = 0.0, humidity = 0.0, tLux = 0.0;


//setup the relays
int RELAY1 = D3;
int RELAY2 = D4;
int RELAY3 = D5;
int RELAY4 = D6;

int LED = D7;

// function for relay control
// command format r1,HIGH
int relayControl(String command)
    {
    int relayState = 0;
    // parse the relay number
    int relayNumber = command.charAt(1) - '0';
    // do a sanity check
    if (relayNumber < 1 || relayNumber > 4) return -1;

    // find out the state of the relay
    if (command.substring(3,7) == "HIGH") relayState = 1;
    else if (command.substring(3,6) == "LOW") relayState = 0;
    else return -1;

    // write to the appropriate relay
    digitalWrite(relayNumber+2, relayState);
    return 1;
    }
    
// Function defines what the LightsOn, Lights off commands do
    void LightsOn()
        {
        digitalWrite(RELAY1, HIGH);
        digitalWrite(RELAY2, HIGH);
        Serial.println("Lights are turned on");
        }
    void LightsOff()
        {
        digitalWrite(RELAY1,LOW);
        digitalWrite(RELAY2,LOW);
        Serial.println("Lights are turned off");
        }
    

//lux sensor setup:
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // pass in a number for the sensor identifier (for your use later)

/**************************************************************************/
/*
    Displays some basic information on this sensor from the unified
    sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
/**************************************************************************/
void displaySensorDetails(void)
    {
    sensor_t sensor;
    tsl.getSensor(&sensor);
    Serial.println("------------------------------------");
    Serial.print  ("Sensor:       "); Serial.println(sensor.name);
    Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
    Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
    Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" lux");
    Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" lux");
    Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" lux");
    Serial.println("------------------------------------");
    Serial.println("");
    delay(500);
    }

/**************************************************************************/
/*
    Configures the gain and integration time for the TSL2591
*/
/**************************************************************************/
void configureSensor(void)
    {
    // You can change the gain on the fly, to adapt to brighter/dimmer light situations
    //tsl.setGain(TSL2591_GAIN_LOW);    // 1x gain (bright light)
    tsl.setGain(TSL2591_GAIN_MED);      // 25x gain
    // tsl.setGain(TSL2591_GAIN_HIGH);   // 428x gain

    // Changing the integration time gives you a longer time over which to sense light
    // longer timelines are slower, but are good in very low light situtations!
    // tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS);  // shortest integration time (bright light)
     tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
    // tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
    // tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
    // tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
    // tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS);  // longest integration time (dim light)

    /* Display the gain and integration time for reference sake */
    Serial.println("------------------------------------");
    Serial.print  ("Gain:         ");
    tsl2591Gain_t gain = tsl.getGain();
    switch(gain)
    {
        case TSL2591_GAIN_LOW:
            Serial.println("1x (Low)");
        break;
        case TSL2591_GAIN_MED:
            Serial.println("25x (Medium)");
        break;
        case TSL2591_GAIN_HIGH:
            Serial.println("428x (High)");
        break;
        case TSL2591_GAIN_MAX:
            Serial.println("9876x (Max)");
        break;
    }
    Serial.print  ("Timing:       ");
    Serial.print((tsl.getTiming() + 1) * 100, DEC);
    Serial.println(" ms");
    Serial.println("------------------------------------");
    Serial.println("");
    }
// end lux sensor setup


//setup 
void setup(void)
    {
        
    // set the time zone
    Time.zone(-8);
    
    // Setup the Relay Shield pins
    pinMode(RELAY1, OUTPUT);
    pinMode(RELAY2, OUTPUT);
    pinMode(RELAY3, OUTPUT);
    pinMode(RELAY4, OUTPUT);
    
    pinMode(LED, OUTPUT);
    Particle.function("ledtest", LedTest);
    

    // Initialize all relays to an OFF state
    digitalWrite(RELAY1, LOW);
    digitalWrite(RELAY2, LOW);
    digitalWrite(RELAY3, LOW);
    digitalWrite(RELAY4, LOW);
    
    digitalWrite(LED, HIGH);
    

    // Setup Particle variables
    Particle.variable("i2cdevice", "SHT31");
    Particle.variable("cTemp", cTemp);
    Particle.variable("humidity", humidity);
    Particle.variable("lux", tLux);
    Particle.variable("Lights", LightsOn);

    
    // Setup Particle function for relay control
    int relayControl(String command);
    Particle.function("relay", relayControl);
    
     // create the alarms 
    Alarm.alarmRepeat(4,00,0, LightsOn);  // 4:00am every day
    Alarm.alarmRepeat(0,00,10,LightsOff);  // 00:00am every day 
    // Alarm.timerRepeat(15, Repeats);            // timer for every 15 seconds    
    // Alarm.timerOnce(10, OnceOnly);             // called once after 10 seconds    
        
    
    delay(1000);
    
    digitalWrite(LED, LOW);    
    
    // setup adafruit HDR light sensor    

    Serial.begin(9600);

    Serial.println("Starting Adafruit TSL2591 Test!");

    if (tsl.begin())
        {
        Serial.println("Found a TSL2591 sensor");
        }
    else
        {
        Serial.println("No sensor found ... check your wiring?");
   // while (1);
    }

    // Configure the sensor
    configureSensor();


    // Initialise I2C communication as MASTER 
    Wire.begin();
    // Initialise serial communication, set baud rate = 9600
    Serial.begin(9600);
    
    }
    
    
    int LedTest(String command){
        if (command == "on"){
           digitalWrite(LED, HIGH);
           return 1;
           }
        else if (command == "off"){
            digitalWrite(LED, LOW);
            return 0;
        }
        else {
            return -1;
        }
    }





void loop(void)
{
    Alarm.delay(500); // Call Alarm Function    

    // lux calculations
    uint32_t lum = tsl.getFullLuminosity();
    uint16_t ir, full;
    ir = lum >> 16;
    full = lum & 0xFFFF;
    Serial.print("[ "); Serial.print(millis()); Serial.print(" ms ] ");
    Serial.print("IR: "); Serial.print(ir);  Serial.print("  ");
    Serial.print("Full: "); Serial.print(full); Serial.print("  ");
    Serial.print("Visible: "); Serial.print(full - ir); Serial.print("  ");
    Serial.print("Lux: "); Serial.println(tsl.calculateLux(full, ir));
    float tLux = (tsl.calculateLux(full, ir));
    delay(300);
  
  
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
   
    // Send 16-bit command byte          
    Wire.write(0x2C);
    Wire.write(0x06);
   
    // Stop I2C transmission
    Wire.endTransmission();
    delay(500);

    unsigned int data[6];
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Stop I2C Transmission
    Wire.endTransmission();
    
    // Request 6 bytes of data from the device
    Wire.requestFrom(Addr,6);
    
    // Read 6 bytes of data
    // temp msb, temp lsb, crc, hum msb, hum lsb, crc
    if(Wire.available() == 6)
    {
        data[0] = Wire.read();
        data[1] = Wire.read();
        data[2] = Wire.read();
        data[3] = Wire.read();
        data[4] = Wire.read();
        data[5] = Wire.read();
    }
    delay(300);
  
    // Convert the data
    float cTemp = ((((data[0] * 256.0) + data[1]) * 175.72) / 65536.0) - 46.85;
    float fTemp = (cTemp * 1.8) + 32;
    float humidity = ((((data[3] * 256.0) + data[4]) * 125) / 65535.0) - 6;
    
    
    // Output data to dashboard
    Particle.publish("Temperature in Celsius: ",  String(cTemp));
    delay(300);
    Particle.publish("Temperature in Fahrenheit: ", String(fTemp));
    delay(300);
    Particle.publish("Relative Humidity: ",  String(humidity));
    delay(300);
    Particle.publish("Lux: ",  String(tLux));
    delay(300);
   
    
    //ubidots
    delay(100);
    // setting ubidots request path 1
    request.hostname = "things.ubidots.com";
    request.port = 80;
    request.path = "/api/v1.6/variables/"VARIABLE_ID"/values?token="TOKEN;
    
    // sending variable 1 to ubidots
    Serial.println("Sending data ...");
    request.body = "{\"value\":" + String(cTemp) + "}";

    // Ubidots Variable 1 Post request
    http.post(request, response, headers);
    Serial.println(response.status);
    Serial.println(response.body);
    
    delay(100);
    
    // setting ubidots request path 2
    request.hostname = "things.ubidots.com";
    request.port = 80;
    request.path = "/api/v1.6/variables/"VARIABLE_ID2"/values?token="TOKEN;        
    
     // sending varible 2 to ubidots
    Serial.println("Sending data ...");
    request.body = "{\"value\":" + String(humidity) + "}";

    // Ubidots variable 2 Post request
    http.post(request, response, headers);
    Serial.println(response.status);
    Serial.println(response.body);
    
    delay(100);
    
    // setting ubidots request path 3
    request.hostname = "things.ubidots.com";
    request.port = 80;
    request.path = "/api/v1.6/variables/"VARIABLE_ID3"/values?token="TOKEN;        
    
    // sending varible 3 to ubidots
    Serial.println("Sending data ...");
    request.body = "{\"value\":" + String(tLux) + "}";

    // Ubidots variable 3 Post request
    http.post(request, response, headers);
    Serial.println(response.status);
    Serial.println(response.body);
    
    delay(10000);
    
}
1 Like

Hey Vinistois,

If you’re not getting through the Setup then the best thing to do would be to move Serial.begin(9600); to the top of Setup and use the serial port to debug.

add things like:

Serial.println("Creating Particle Variables");

Serial.println("Creating Particle Function");
    
Serial.println("Creating Alarms");
    
Serial.println("Starting Light sensor");

etc

if you don’t get the “Starting Light sensor” in your terminal then the problem will be above that.

Also what’s int relayControl(String command); for??? I think you can remove that or at least put it above Setup.

I’m not sure if it will make a difference to things once it’s up and running but you may also consider changing all of the delays in Loop to Alarm.delay as you have quite a lot of them and it won’t hurt.

Adam