// Coded by J.Drage, 20-Feb-2022 STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY)); SYSTEM_THREAD(ENABLED); SystemSleepConfiguration config; //-------------------------------------------------------------------------- // Define Boron pins: //-------------------------------------------------------------------------- #define WLsensor_power D2 // Pin D2 powers the water level sensor on #define WLsensor_data D3 // Pin D3 is data output from the water level sensor //-------------------------------------------------------------------------- // Variables to be entered by user //-------------------------------------------------------------------------- int MeasureTime=86400000; // Time between water level measurements; entered by user (milliseconds) int ConnectTime=120000; // Time allowed for Particle Boron to connect to the cloud before going back to sleep; should not be less than 90000; entered by user (milliseconds) double Datum=10; // Datum elevation; typically top of well casing is used; entered by user (metres) double HangDown=0; // Distance from datum to sensor; entered by user (metres) //-------------------------------------------------------------------------- // Variables NOT entered by user //-------------------------------------------------------------------------- int MeasureTime2=(MeasureTime-60000); // Subtract 60000 milliseconds from MeasureTime to account for time needed to connect to the cloud; this keeps the measurement time to about the same time each day double WLSensorValue=0; // Reading from water level sensor (mm) double Depth=0; // Depth to water; measured by the sensor and then converted from mm to m (metres) double GWelevation=0; // Water table elevation; calculated by code (metres) void setup() { //-------------------------------------------------------------------------- // Configure pins on the Boron //-------------------------------------------------------------------------- pinMode(WLsensor_power, OUTPUT); pinMode(WLsensor_data, INPUT); Serial.begin(9600); PMIC().disableCharging(); config.mode(SystemSleepMode::ULTRA_LOW_POWER).duration(MeasureTime2); } void loop() { //-------------------------------------------------------------------------- // Check to see if Boron is connected to the cloud, if not then go to sleep to preserve the batteries //-------------------------------------------------------------------------- restart: if( !Particle.connected() ) { Particle.connect(); if ( !waitFor(Particle.connected, ConnectTime) ) { System.sleep(config); goto restart; } } //-------------------------------------------------------------------------- // Read the water level from the sensor //-------------------------------------------------------------------------- digitalWrite(WLsensor_power, HIGH); delay(1s); WLSensorValue = pulseIn(WLsensor_data, HIGH); digitalWrite(WLsensor_power, LOW); Depth = (WLSensorValue/1000); GWelevation = (Datum-HangDown-Depth); //-------------------------------------------------------------------------- // Check to see if the water depth is out of sensor range and if so, go to sleep without reporting the result; sensor range is 0.3-5m (5m model) or 0.5-10m (10m model) //-------------------------------------------------------------------------- if (Depth<0.31) { delay(2s); System.sleep(config); goto restart; } if (Depth>4.99) { delay(2s); System.sleep(config); goto restart; } //-------------------------------------------------------------------------- // Send the water level data to ThingSpeak.com //-------------------------------------------------------------------------- Particle.publish("Insert_Webhook_Name_Here", String(GWelevation, 2), PRIVATE); delay(2s); System.sleep(config); goto restart; }