void loop() {
/************************************************************************************************************
*** MOTION DELAY TURN OF LED/MOTOR DELAY ***
***********************************************************************************************************/
if (webCheckedTrue != "1") {
if (delayMotionRunning && ((millis() - delayMotionStart) > (timeMotionSeconds*1000))) {
static uint32_t hourTimer = millis();
if (millis() - hourTimer >= millisPerHour) {
hourTimer = millis();
motionCounter = 0;
}
motionCounter = motionCounter + 1;
digitalWrite(ledMotor, LOW);
digitalWrite(onMotor, LOW);
delayMotionRunning = false;
ThingSpeak.setField(3, motionCounter);
if (client.connect(thingspeakServer,80)) {
int x = ThingSpeak.writeFields(myChannelNumber, apiKey);
}
client.stop();
}
}
else {
if (delayMotionRunning && ((millis() - delayMotionStart) > (timeMotionSeconds*1000))) {
Serial.println("Motion IDLE...");
static uint32_t hourTimer = millis();
if (millis() - hourTimer >= millisPerHour) {
hourTimer = millis();
motionCounter = 0;
}
motionCounter = motionCounter + 1;
digitalWrite(onMotor, LOW);
delayMotionRunning = false;
ThingSpeak.setField(3, motionCounter);
Serial.println(motionCounter);
if (client.connect(thingspeakServer,80)) {
int x = ThingSpeak.writeFields(myChannelNumber, apiKey);
}
client.stop();
}
}
/************************************************************************************************************
*** AMOUNT DELAY TO CHCK BATTERY STATUS ***
***********************************************************************************************************/
if (voltDelay.justFinished()) {
getVolt();
voltDelay.repeat();
int memHeap = (ESP.getFreeHeap());
ThingSpeak.setField(1, voltage_V);
ThingSpeak.setField(4, memHeap);
if (client.connect(thingspeakServer,80)) {
int x = ThingSpeak.writeFields(myChannelNumber, apiKey);
}
client.stop();
}
/************************************************************************************************************
*** CHECK WEIGHT IF BELOW THRESHOLD_AMOUNT AND REPORT ***
***********************************************************************************************************/
if (amountDelay.justFinished()) {
scale.power_up();
float weight0 = scale.get_units(10);
ThingSpeak.setField(2, weight0);
if (weight0 > THRESHOLD_AMOUNT) {
sendSMTP();
}
scale.power_down();
amountDelay.repeat();
if (client.connect(thingspeakServer,80)) {
int x = ThingSpeak.writeFields(myChannelNumber, apiKey);
}
client.stop();
}
}