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 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; }
termomether temp_read() { _t.temperature = htu.readTemperature(); _t.humidity = htu.readHumidity(); return _t; }
void temp_init() { htu = Adafruit_HTU21DF(); htu.begin(); }