This text file contains the code from the Appendix in Open File Report ME 2019-003, entitled "Instructions for Building a Real-Time Well Water Level Meter". It is recommended that the code be copied and pasted directly from this file to ensure the code formatting is correctly maintained, rather than copying the code from the PDF version of the above referenced report. Copying the code from the PDF report can introduce formatting errors which may prevent the code from running correctly. Copy the code below starting from the line that reads "#include ". This line should be identified as line 1 when the code is pasted into the Particle App web IDE. //------------------------------------------------------------------------- #include // Coded by J.Drage, 17-Jun-2019 STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY)); SYSTEM_THREAD(ENABLED); STARTUP(WiFi.selectAntenna(ANT_EXTERNAL)); // selects the u.FL antenna //-------------------------------------------------------------------------- // Photon pins: // MAX_AI_PIN Pin A0, input from the sensor // MAX_EN_PIN Pin D2, powers the sensor on //-------------------------------------------------------------------------- const int MAX_EN_PIN = D2; const int MAX_AI_PIN = A0; const float V_PER_ADC_CODE = 805.86e-6; //3.3V/4095codes=0.00080586; scale factor const float CM_PER_V = 155.152; // 512cm/3.3V=155.152cm/V; scale factor //-------------------------------------------------------------------------- // Variables //-------------------------------------------------------------------------- double range_m=0; //Measured range from the sensor (metres) double GWelevation=0; // Water table elevation (metres) double HangDown=0.0; // Distance from datum to sensor, need to enter this for each well (metres) double Datum=0.0; //Elevation of datum, typically top of casing is used for the datum (metres) //-------------------------------------------------------------------------- void setup() { //-------------------------------------------------------------------------- // Configure pins on the Photon //-------------------------------------------------------------------------- digitalWrite(MAX_EN_PIN, HIGH); pinMode(MAX_EN_PIN, OUTPUT); digitalWrite(MAX_EN_PIN, HIGH); Serial.begin(9600); } void loop() { //-------------------------------------------------------------------------- // Read the range from the sensor //-------------------------------------------------------------------------- delay(1000); int adcReading = analogRead(MAX_AI_PIN); range_m = adcReading*V_PER_ADC_CODE*CM_PER_V/100; GWelevation = (Datum-HangDown-range_m); //-------------------------------------------------------------------------- // Check to see if Photon is connected to Wifi, if not then go to sleep to preserve the batteries //-------------------------------------------------------------------------- if( !Particle.connected() ) { Particle.connect(); if ( !waitFor(Particle.connected, 25000) ) { digitalWrite(MAX_EN_PIN, LOW); System.sleep(SLEEP_MODE_DEEP, 120); } } //-------------------------------------------------------------------------- // Check to see if the water depth is out of sensor range and if so, go to sleep without reporting the result //-------------------------------------------------------------------------- if (range_m<0.31) { delay(20000); System.sleep(SLEEP_MODE_DEEP, 120); } if (range_m>4.93) { delay(20000); System.sleep(SLEEP_MODE_DEEP, 120); } //-------------------------------------------------------------------------- // Send the water level to real-time chart at ThingSpeak.com //-------------------------------------------------------------------------- String level = String(GWelevation); Particle.publish("delete this text and insert webhook name inside these parentheses", level, PRIVATE); // Wait 20 seconds delay(20000); digitalWrite(MAX_EN_PIN, LOW); System.sleep(SLEEP_MODE_DEEP, 120); }