/* Sketch generated by the Arduino IoT Cloud Thing "Autofeed2.0" https://create.arduino.cc/cloud/things/a4043153-6bfb-4dfa-a055-151cba4627a4 Arduino IoT Cloud Variables description The following variables are automatically generated and updated when changes are made to the Thing CloudCounter motorCloudCycleHours; bool motorCloudControl; bool pumpCloudControl; CloudTime pumpCloudOffSecond; CloudTime pumpCloudOnSecond; Variables which are marked as READ/WRITE in the Cloud Thing will also have functions which are called when their values are changed from the Dashboard. These functions are generated with the Thing and added at the end of this sketch. */ #include"thingProperties.h"
int debouncedLowPinState = LOW; // 经过去抖动后的低水位引脚状态 int debouncedHighPinState = LOW; // 经过去抖动后的高水位引脚状态 int lastLowReading = -1; int lastHighReading = -1;
voidsetup(){ // Initialize serial and wait for port to open: Serial.begin(115200); // This delay gives the chance to wait for a Serial Monitor without blocking if none is found Serial.println("Software Version: " + String(softwareVersion)); delay(100);
Serial.println("Arduino Cloud Connected!"); /* The following function allows you to obtain more information related to the state of network and IoT Cloud connection and errors the higher number the more granular information you’ll get. The default is 0 (only errors). Maximum is 4 */ setDebugMessageLevel(2); ArduinoCloud.printDebugInfo(); }
voidsetPumpState(bool state){ digitalWrite(pumpPin, state ? HIGH : LOW);
}
voidstopPump(){ setPumpState(false); // 直接关闭水泵 }
/* Since PumpCloudControl is READ_WRITE variable, onPumpCloudControlChange() is executed every time a new value is received from IoT Cloud. */ voidonPumpCloudControlChange(){ Serial.printf("pumpCloudControl changed to: %s\n", pumpCloudControl ? "true" : "false"); if (pumpCloudControl) { // 从Cloud开启水泵,立即开始循环 Serial.println("Starting pump cycle from Cloud."); pumpRunning = true; pumpOnPeriod = true; lastPumpToggleTime = millis(); // 重置泵循环计时 setPumpState(pumpOnPeriod); // 立即打开水泵 } else { // 从Cloud关闭水泵,立即停止水泵和循环 Serial.println("Stopping pump cycle from Cloud."); stopPump(); pumpRunning = false; pumpOnPeriod = false; // 确保回到初始状态 } }
/* Since MotorCloudControl is READ_WRITE variable, onMotorCloudControlChange() is executed every time a new value is received from IoT Cloud. */ voidonMotorCloudControlChange(){ Serial.printf("motorCloudControl changed to: %s\n", motorCloudControl ? "true" : "false"); if (motorCloudControl) { // 从Cloud开启电机,运行25秒 startMotor(); motorStartTime = millis(); motorRunning = true; } else { // 从Cloud关闭电机,立即停止 stopMotor(); motorRunning = false; } }
/* Since MotorCloudCycleHours is READ_WRITE variable, onMotorCloudCycleHoursChange() is executed every time a new value is received from IoT Cloud. */ voidonMotorCloudCycleHoursChange(){ motorCycleStartTime = millis(); }
/* Since PumpCloudOffSecond is READ_WRITE variable, onPumpCloudOffSecondChange() is executed every time a new value is received from IoT Cloud. */ voidonPumpCloudOffSecondChange(){ // Add your code here to act upon PumpCloudOffSecond change }
/* Since PumpCloudOnSecond is READ_WRITE variable, onPumpCloudOnSecondChange() is executed every time a new value is received from IoT Cloud. */ voidonPumpCloudOnSecondChange(){ // Add your code here to act upon PumpCloudOnSecond change }