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