// Coded by J.Drage, 6-Jul-2020 STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY)); SYSTEM_THREAD(ENABLED); //STARTUP(WiFi.selectAntenna(ANT_EXTERNAL)); // Selects the external antenna; this line of code is disabled in the cellular version of the meter //-------------------------------------------------------------------------- // Define Electron 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=120; // Time between water level measurements; entered by user (seconds) int DelayTime=1; // Delay time used to postpone the water level measurement to a later time of day; this activates on the 2nd measurement; entered by user (seconds) int ConnectTime=90000; // Time allowed for Particle Electron 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-25); // Subtract 25 seconds 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) retained int Counter=0; // A counter used to activate the one-time only user specified delay time void setup() { //-------------------------------------------------------------------------- // Configure pins on the Electron //-------------------------------------------------------------------------- pinMode(WLsensor_power, OUTPUT); pinMode(WLsensor_data, INPUT); Serial.begin(9600); PMIC().disableCharging(); } void loop() { //-------------------------------------------------------------------------- // Check to see if Electron is connected to the cloud, if not then go to sleep to preserve the batteries //-------------------------------------------------------------------------- if( !Particle.connected() ) { Particle.connect(); if ( !waitFor(Particle.connected, ConnectTime) ) { System.sleep(SLEEP_MODE_DEEP, MeasureTime2); } } //-------------------------------------------------------------------------- // Delay the water level measurement by the user specified delay time //-------------------------------------------------------------------------- if (Counter==1) { Counter++; delay(20000); System.sleep(SLEEP_MODE_DEEP, DelayTime); } Counter++; Counter=0; //-------------------------------------------------------------------------- // Read the water level from the sensor //-------------------------------------------------------------------------- digitalWrite(WLsensor_power, HIGH); delay(1000); 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(20000); System.sleep(SLEEP_MODE_DEEP, MeasureTime2); } if (Depth>4.99) { delay(20000); System.sleep(SLEEP_MODE_DEEP, MeasureTime2); } //-------------------------------------------------------------------------- // Send the water level data to ThingSpeak.com //-------------------------------------------------------------------------- Particle.publish("Insert_Webhook_Name_Inside_These_Quotes", String(GWelevation, 2), PRIVATE); delay(20000); System.sleep(SLEEP_MODE_DEEP, MeasureTime2); }