void loop() { unsigned long currentTime = Time.now(); //TODO: if targets -1, don't do any control //Request time synchronization from the Particle Cloud every 24 hours if (millis() - lastTimeSync > ONE_DAY_MILLIS) { Particle.syncTime(); lastTimeSync = millis(); } //Update time zone at DSTJumpHour incase DST is now in effect if(Time.hour(currentTime) == DSTJumpHour && Time.minute(currentTime) == 0) { Time.zone(isDST(Time.day(currentTime), Time.month(currentTime), Time.weekday(currentTime), preferences.DSTRule) ? preferences.timeZone + 1 : preferences.timeZone); } //Record data from sensors every minute if(millis() - lastDataSync > dataSyncFrequency) { temperature = tempHumidSensor.readTemperature(); humidity = tempHumidSensor.readHumidity(); light = lightSensor.getFullLuminosity(); fullLight = light & 0xFFFF; irLight = light >> 16; visibleLight = fullLight - irLight; lux = lightSensor.calculateLux(fullLight, irLight); //TODO: soil moisture sensor if(postToPhant() == 0) { //Success } else { //failed //TODO: Handle failure } }
void sensor_setup() { bool status; // BME280 pressure, humidity, temperature status = bme.begin(); if (!status) { Serial.println("Could not find a valid BME280 sensor, check wiring!"); while (1); } // TSL2591 Lux sensor if (tsl.begin()) { Serial.println(F("Found a TSL2591 sensor")); /* Configure the sensor */ configureTLS2591(); } else { Serial.println(F("No sensor found ... check your wiring?")); while (1); } #ifdef pmsEnable pinMode(pmsEnable,OUTPUT); digitalWrite(pmsEnable,LOW); #endif // PMS5003 Particulate sensor pmsSerial.begin(9600); }
void setup() { //Check for current EEPROM data if(EEPROM.read(0) == 117) { EEPROM.get(1, preferences); } else { preferences = {-8, 1000, -1, -1, -1, -1, -1.0, -1.0, -1.0, "US"}; EEPROM.put(1, preferences); EEPROM.put(0, 117); } //Update settings according to EEPROM data if(preferences.DSTRule == "US") { DSTJumpHour = 2; } else if(preferences.DSTRule == "EU") { DSTJumpHour = 1 + preferences.timeZone; } else { DSTJumpHour = 0; } dataSyncFrequency = preferences.dataSyncFrequency; lightCycleLength = preferences.lightCycleLength; waterCycleLength = preferences.waterCycleLength; targetTemperature = preferences.targetTemperature; targetHumidity = preferences.targetHumidity; targetSoilMoisture = preferences.targetSoilMoisture; //Set the proper time zone according to DST status Time.zone(isDST(Time.day(), Time.month(), Time.weekday(), preferences.DSTRule) ? preferences.timeZone + 1 : preferences.timeZone); //Setup sensors tempHumidSensor.begin(); lightSensor.begin(); lightSensor.setGain(TSL2591_GAIN_MED); lightSensor.setTiming(TSL2591_INTEGRATIONTIME_100MS); pinMode(pumpPWMPin, OUTPUT); pinMode(pumpIn1Pin, OUTPUT); pinMode(pumpIn2Pin, OUTPUT); pinMode(growLEDPin, OUTPUT); pinMode(socketPin, OUTPUT); pinMode(fanPin, OUTPUT); //TEST ONLY lightCycleStartHour = Time.hour(); lightCycleStartMinute = Time.minute()+1; lightCycleLength = 1 * 60 * 1000; }
void configureTLS2591(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(F("------------------------------------")); Serial.print (F("Gain: ")); tsl2591Gain_t gain = tsl.getGain(); switch(gain) { case TSL2591_GAIN_LOW: Serial.println(F("1x (Low)")); break; case TSL2591_GAIN_MED: Serial.println(F("25x (Medium)")); break; case TSL2591_GAIN_HIGH: Serial.println(F("428x (High)")); break; case TSL2591_GAIN_MAX: Serial.println(F("9876x (Max)")); break; } Serial.print (F("Timing: ")); Serial.print((tsl.getTiming() + 1) * 100, DEC); Serial.println(F(" ms")); Serial.println(F("------------------------------------")); Serial.println(F("")); }
void tsl2591_loop(void) { // More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum // That way you can do whatever math and comparisons you want! float lux; uint32_t lum = tsl.getFullLuminosity(); uint16_t ir, full; ir = lum >> 16; full = lum & 0xFFFF; // the second argument should be IR, but there are some weird faults // and that results in negative values often. So cheat and just provide // a 0 there. lux=tsl.calculateLux(full, 0); if(false) { Serial.print(F("[ ")); Serial.print(millis()); Serial.print(F(" ms ] ")); Serial.print(F("IR: ")); Serial.print(ir); Serial.print(F(" ")); Serial.print(F("Full: ")); Serial.print(full); Serial.print(F(" ")); Serial.print(F("Visible: ")); Serial.print(full - ir); Serial.print(F(" ")); Serial.print(F("Lux: ")); Serial.println(lux,6); } publish_value("lux",lux,2); }