Cut power to arduino when leak is detected

Hello all,

I've spent the last few months creating a portable autosampler meant to collect a series of water samples every 15 minutes over the course of 24 hours, and post data to freeboard.io and send SMS alerts to the operator. I thought I had thought of everything, but yesterday I got the idea to add a water sensor under the tubing for the peristaltic pump if a leak happens which then blinks an LED and a buzzer until it is dry again. Obviously, this wont work as a fail safe.

Is there a way to make all pins on the ESP32 go low when the sensor detects water? or is there a way to shut off power to the esp32 when water is detected until it is reset? Would I need a power relay?

the power source is a 12 Volt battery which is regulated to 12VDC to power the pump and 4.5 VDC to energize the esp32. Is there an example of power from an arduino/esp32 energizing a relay to allow 12vdc input with some sort of trigger that deenergizes the pin of the microcontroller which provides power to the relay? I'll post the part of the code which deals with the water sensor, as well as the full code (its pretty long and could use streamlining)

#define SIGNAL_PIN 36 // ESP32 pin GIOP36 (ADC0) connected to sensor's signal pin
#define samplingRate2 3000UL // the time between sensor readouts
unsigned long counter2 = 0;
int value = 0; // variable to store the sensor value
const int LeakLED = 19;
unsigned long currentMillis;                    // millis() of the current loop.
void setup() {
  Serial.begin(9600);
  pinMode(LeakLED, OUTPUT) ;
}

void loop() {
  currentMillis = millis();
    value = analogRead(SIGNAL_PIN); // read the analog value from sensor
  if (currentMillis - counter2 > samplingRate2) {
    Serial.print("The water sensor value: ");
    Serial.println(value);
    counter2 = millis();
    currentMillis = millis();
  }
  if (value > 1) {
    digitalWrite(LeakLED, HIGH);
    delay(500);
    digitalWrite(LeakLED, LOW);
    delay(500);
    Serial.println("LEAK DETECTED!");
  }
  else {
    digitalWrite(LeakLED, LOW);
  }
}
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 20, 4);
#include <Wire.h>
#include <OneWireNg.h>
#include <DallasTemperature.h>
#include <ezButton.h>
#include <Adafruit_ADS1X15.h>
#include <MultiMap.h>
#define ONE_WIRE_BUS 4
/***********************************GPRS/SMS STUFF ****************************************/
#include "Adafruit_FONA.h" // https://github.com/botletics/SIM7000-LTE-Shield/tree/master/Code
#include <HardwareSerial.h>
// For SIM7000 shield
#define FONA_PWRKEY 18
#define FONA_RST 5
#define FONA_TX 16 // ESP32 hardware serial RX2 (GPIO16)
#define FONA_RX 17 // ESP32 hardware serial TX2 (GPIO17)

#define SIMCOM_7000
#define PROTOCOL_HTTP_GET         // Generic
#define LED 2
#define SIGNAL_PIN 36 // ESP32 pin GIOP36 (ADC0) connected to sensor's signal pin
HardwareSerial fonaSS(1);
Adafruit_FONA_LTE fona = Adafruit_FONA_LTE();

#define samplingRate 300000UL // The time in between posts, in millis
#define samplingRate2 3000UL // the time between sensor readouts
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
char imei[16] = {0}; // Use this for device ID
char replybuffer[255]; // Large buffer for replies
uint8_t type;
unsigned long counter = 0;
unsigned long counter2 = 0;
/**** BOOL VARIABLES FOR SMS ALERTS ****/
bool bat = false;
bool opened = false;
bool cold = false;

char volBuff[12];
char tempBuff[12];
char batBuff[12];

char URL[300];  // Make sure this is long enough for your request URL
char body[200]; // Only need this is you're doing an HTTP POST request

/**** FLOAT SWITCH ****/
const int  buttonPin = 33;
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
const int FloatSwitchLED = 15;
const int ResetLED = 2;

int value = 0; // variable to store the sensor value
const int LeakLED = 19;

const char * phone_number = "+18326831676"; // Include country code, only numbe
/*****                  SMS ALERTS                     ********/
const char * text_message1 = " mL FULL!"; // Change to suit your needs
const char * text_messageV = volBuff;

const char * text_message3 = " % BATTERY LOW!";
const char * text_messageB = batBuff;

const char * text_message2 = "*F FREEZING!"; // Change to suit your needs
const char * text_messageF = tempBuff;

volatile int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.

float tLimit = 30.0; //the amount of liquid in mL that the pump is supposed to collect during pump cycle
int Threshold;       //the threshold for amount of liquid collected before pump returns to OffState
bool ThresholdLimit = false; // flag for Threshold
/**********************************************************************************************************************************/

Adafruit_ADS1115 ads;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;

unsigned long startMillis;                      // Keeps track of millis() when the pumping cycle starts.
unsigned long currentMillis;                    // millis() of the current loop.

// constants for Arduino IO-Pins with SELF-explaining names
const byte samplingBtnPin = 12;
const byte primeBtnPin    = 13;
const byte purgeBtnPin    = 14;
const byte resetBtnPin    = 32;
ezButton button1(12);  // create ezButton object that attach to pin 12;
ezButton button2(32);  // create ezButton object that attach to pin 12;
const byte DRV8876_IN1_PIN    = 26;
const byte DRV8876_IN2_PIN    = 27;

int flowPin = 25;    //This is the input pin on the Arduino, flow switch
float calibrationFactor = 121;                    //calibration factor for sampling is 150, need to make if else statement for it
float tempC;
float tempF;
int percent;

/**************************************************** Battery stuf **************************************************/
int R1 = 27, R2 = 10; // 10k and 27k ; the resistor ratio
float dividerRatio; // pre-calculate in setup
float calFactor = 1.01; // calibrate  if needed
int in[] = {
  10000, 12000, 12500, 12800, 12900, 13000, 13100, 13200, 13300, 13400, 13600, 14600, 14900
};                                                                                         // [13] IN holds the mV inputs
int out [] = {
  0, 9, 14, 17, 20, 30, 40, 70, 90, 99, 100, 110, 120
};                                                                                       // [13] OUT holds the percentage outputs
float bLimit = 20; // the lower limit of the percentage, if battery percent falls below 20, lowbatsMs is sent
int Val;  // holds input and output values for multimap
float vLimit = 32.0;
/*********************************************************************************************************************/


volatile byte pulseCount;                       // counts the pulses of flow sensor
float flowRate;
unsigned int flowMilliLitres;                  //totaltotalMilliLitres used for the interrupts
unsigned long totalMilliLitres;
unsigned long oldTime;
unsigned long cloopTime;                      // to count pulses per second
unsigned long vol;                           //vol tracks total volume throughout loop


// constants for the states with SELF-explaining names
const byte OffState     = 0;
const byte PumpState    = 1;
const byte PausingState = 2;
const byte ReverseState = 3;
const byte RestState    = 4;
const byte FullState     = 5;
const byte PurgeState = 6;
byte state;

#define pressed HIGH
#define released LOW
#define closed HIGH
#define open LOW

// bool primeBtnPinHasBeenPressed = true;
bool purgeBtnPinHasBeenPressed = false;

void pulseCounter()
{
  // Increment the pulse counter
  pulseCount++;
}

void setup() {
  Serial.begin(115200);
  lcd.begin();
  button1.setDebounceTime(50); // set debounce time to 50 milliseconds
  button2.setDebounceTime(50); // set debounce time to 50 milliseconds
  Serial.println("ESP32 PUMP");
  pinMode(primeBtnPin, INPUT_PULLUP);    // pinMode(5, INPUT);
  pinMode(purgeBtnPin, INPUT_PULLUP);    // pinMode(6, INPUT);
  pinMode(samplingBtnPin, INPUT_PULLUP); // pinMode(7, INPUT);
  pinMode(resetBtnPin , INPUT_PULLUP);
  pinMode(FloatSwitchLED, OUTPUT);
  pinMode(ResetLED, OUTPUT);
  pinMode(DRV8876_IN1_PIN, OUTPUT); // pinMode(A0, OUTPUT);
  pinMode(DRV8876_IN2_PIN, OUTPUT); // pinMode(A1, OUTPUT);

  pinMode(flowPin, INPUT_PULLUP);
  digitalWrite(flowPin, LOW);


  pinMode(LeakLED, OUTPUT) ;

  Threshold         = 3400.0; //Threshold limit
  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0.0;
  totalMilliLitres  = 0;
  vol               = 0;
  cloopTime = currentMillis;
  attachInterrupt(digitalPinToInterrupt(flowPin), pulseCounter, FALLING);

  dividerRatio = (R1 + R2) / R1;
  ads.setGain(GAIN_ONE);
  ads.begin();
  /************************************************GRPS/SMS STUFF*************************************************/
#ifdef LED
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
#endif

  pinMode(FONA_RST, OUTPUT);
  digitalWrite(FONA_RST, HIGH); // Default state

  pinMode(FONA_PWRKEY, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  powerOn(true); // Power on the module
  moduleSetup(); // Establish first-time serial comm and print IMEI

  fona.setFunctionality(1); // AT+CFUN=1
  fona.setNetworkSettings(F("hologram")); // For Hologram SIM card, change appropriately

  // Perform first-time GPS/GPRS setup if the shield is going to remain on,
  // otherwise these won't be enabled in loop() and it won't work!
#ifndef turnOffShield

#if !defined(SIMCOM_3G) && !defined(SIMCOM_7500) && !defined(SIMCOM_7600)
  // Disable GPRS just to make sure it was actually off so that we can turn it on
  if (!fona.enableGPRS(false)) Serial.println(F("Failed to disable GPRS!"));

  // Turn on GPRS
  while (!fona.enableGPRS(true) && (millis() < 10000)) {
    Serial.println(F("Failed to enable GPRS, retrying..."));
    delay(2000); // Retry every 2s
  }
  Serial.println(F("Enabled GPRS!"));
#endif
#endif
  /************************************************GRPS/SMS STUFF end*********************************************/

  Serial.print("Locating devices...");
  sensors.begin();
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");

  if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");

  //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
  // show the addresses we found on the bus
  Serial.print("Device 0 Address: ");
  printAddress(insideThermometer);
  Serial.println();

  // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
  sensors.setResolution(insideThermometer, 9);

  Serial.print("Device 0 Resolution: ");





}

void loop() {
  button1.loop(); // MUST call the loop() function first
  button2.loop(); // MUST call the loop() function first
  int btn1State = button1.getState();
  int btn2State = button2.getState();
  //  if (digitalRead(primeBtnPin) == released) primeBtnPinHasBeenPressed = false;
  if (digitalRead(purgeBtnPin) == released) purgeBtnPinHasBeenPressed = true;



  currentMillis = millis();                   // Get the current value of millis().

  buttonState = digitalRead(buttonPin);

  if (digitalRead(samplingBtnPin) == released)
    if (state > OffState)                              // Are we already in a pumping cycle?
      state = OffState;
    else
      state = PumpState;




  //  delay(200);                         // Small delay to debounce the button.
  value = analogRead(SIGNAL_PIN); // read the analog value from sensor

  int adc0 = ads.readADC_SingleEnded(0);
  float battVoltage = ads.computeVolts(adc0) * dividerRatio * calFactor;
  float battVolt = battVoltage * 3.7;
  int battmV = (battVolt * 1000) + 30;
  Val = battmV;
  //  int percent;
  percent =  multiMap(Val, in, out, 13);
  //  delay(1000);
  dtostrf(percent, 1, 2, batBuff);
  dtostrf(tempF, 1, 2, tempBuff);
  dtostrf(vol, 1, 2, volBuff);
  sensors.requestTemperatures(); // Send the command to get temperatures
  //  Serial.println("DONE");
  printTemperature(insideThermometer); // Use a simple function to print out the data
  tempC = sensors.getTempCByIndex(0);
  tempF = (tempC * 1.8) + 32;
  lcd.setCursor(0, 3);
  lcd.print("B%");
  lcd.setCursor(3, 3);
  lcd.print(percent);
  lcd.setCursor(7, 3);
  lcd.print("TEMP:");
  lcd.setCursor(13, 3);
  lcd.print(tempF);



  if (percent < 100) { //if battery percent falls below 100, erase the third digit
    lcd.setCursor(5, 3);
    lcd.print(" ");
  }

  if (percent < 10) {
    lcd.setCursor(4, 3);  //if battery falls below 10 percent, erase second and third digit
    lcd.print("  ");
  }

  if (digitalRead(buttonPin) == HIGH) {
    digitalWrite(FloatSwitchLED, LOW);
  }
  else {
    digitalWrite(FloatSwitchLED, HIGH);
  }



  /*********************************  read sensor stats and print on serial monitor *************************/
  if (currentMillis - counter2 > samplingRate2) {
    Serial.println("-----------------------------------------------------------");
    Serial.print("Batt Volts: "); Serial.print(battVoltage, 3); Serial.print(" "); Serial.print("AIN0: "); Serial.print(adc0 ); Serial.print(" "); Serial.print(" BatVolt: "); Serial.println(battVolt, 3);
    Serial.print("Battery mV: "); Serial.print(battmV); Serial.print(" "); Serial.print("Battery level [%]: "); Serial.println(percent);
    Serial.println(sensors.getResolution(insideThermometer), DEC);
    Serial.print("Temp C: "); Serial.print(tempC); Serial.print(" "); Serial.print(" Temp F: "); Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
    Serial.print("FLOW ML: "); Serial.println(flowMilliLitres);
    if (ThresholdLimit == true) {
      Serial.print("THRESHOLD REACHED"); Serial.println(Threshold);
    } else
    {
      Serial.println("THRESHOLD NOT REACHED");
    }
    if (digitalRead(buttonPin) == HIGH) {
      Serial.println("FLOAT SWITCH OPEN!");
    }
    Serial.print("The water sensor value: ");
    Serial.println(value);
    Serial.println("-----------------------------------------------------------");

    counter2 = millis();
    currentMillis = millis();
  }

  /****** flow rate math ********/
  if (currentMillis >= (cloopTime + 1000))
  {
    cloopTime = currentMillis;
    if (pulseCount != 0) {
      flowRate =  (pulseCount / calibrationFactor);
      //oldTime = millis();
      flowMilliLitres = (flowRate / 60) * 1000;
      totalMilliLitres += flowMilliLitres;
      vol              += flowMilliLitres;
      Serial.print(flowRate); Serial.println(" :L/min");
      Serial.print(totalMilliLitres); Serial.println(" :mL");
      lcd.setCursor(0, 1);
      lcd.print(flowRate);
      lcd.setCursor(5, 1);
      lcd.print(":L/min");
      lcd.setCursor(0, 2);
      lcd.print(vol);
      lcd.setCursor(6, 2);
      lcd.print(": mL");
      pulseCount = 0;
    }
    else {
      Serial.print(flowRate); Serial.println(" :L/min");
      Serial.print(vol); Serial.println(" :mL");
      lcd.setCursor(0, 1);
      lcd.print(flowRate);
      lcd.setCursor(6, 1);
      lcd.print(":L/min");
      lcd.setCursor(0, 2);
      lcd.print(vol);
      lcd.setCursor(6, 2);
      lcd.print(": mL");
      flowRate = 0;

    }
  }
///////////Leak Detection/////////////
    if (value > 1) {
    digitalWrite(LeakLED, HIGH);
    delay(500);
    digitalWrite(LeakLED, LOW);
    delay(500);
    Serial.println("LEAK DETECTED!");
  }
  else {
    digitalWrite(LeakLED, LOW);
  }
/////////////////////////////////////

  switch (state)                        // Check which state we are in.
  {
    case OffState:
      //    manualMode();
      if (digitalRead(primeBtnPin) == pressed) {

        pumpOff();
      }
      else {
        manualMode();
        break;
      }

      if (purgeBtnPinHasBeenPressed = true) {
        state = PurgeState;
      }
      else {
        break;
      }

      if (digitalRead(resetBtnPin) == released) {  // keep this button in OffState case so that if accidently pressed it wont hurt anything
        resetData();
        digitalWrite(ResetLED, HIGH);
      }
      else {
        digitalWrite(ResetLED, LOW);
        break;
      }

      startMillis = currentMillis;      // Keep resetting the start time of the pumping sequence.
      pumpOff();
      break;

    case PurgeState:
      if (digitalRead(purgeBtnPin) == pressed)
        state = OffState;
      else
        manualPurge();
      break;

    case PumpState:
      if (totalMilliLitres > vLimit || currentMillis - startMillis > 106000)        /// did we collect XmL? if not, go to next stage after X millis
        state = PausingState;
      else

        PumpForward();



      break;

    case PausingState:
      if (currentMillis - startMillis > 109000)   // Have we been in this state too long?
        state = ReverseState;
      else
        pumpOff();
      Serial.println("PAUSE!");
      lcd.setCursor(0, 0);
      lcd.print("PAUSED! ");

      break;

    case ReverseState:
      if (currentMillis - startMillis > 209000) // Have we been in this state too long?
        state = RestState;//state++;
      else
        PumpReverse();

      break;

    case RestState:
      if (currentMillis - startMillis > 600000)  // has it been X minutes?
      {

        state = FullState;
        startMillis = currentMillis;            // Reset the start time of the pumping sequence.
        totalMilliLitres = 0;                  //reset totalMilliLitres so it can start counting up from 0 again

      }
      else
        pumpOff();
      Serial.println("REST!");
      lcd.setCursor(0, 0);
      lcd.print("RESTING!");

      break;

    case FullState:
      if (ThresholdLimit == true) { //||  (digitalRead(buttonPin) == HIGH))
        state = OffState;
      }

      else {
        Serial.println(state);
        state = PumpState;
      }
      break;
  }
  /***************************SMS ALERTS***************************************************/
  if (buttonState == HIGH && lastButtonState == LOW || vol > 3100 && vol - flowMilliLitres <= 3100) {
    opened = false;
    ThresholdLimit = true;
    lastButtonState = buttonState;
    FullSMS ();
    pumpOff();
    lcd.setCursor(0, 0);
    lcd.print("FULL          ");
  }


  if (tempF < tLimit && cold == false) {
    ColdSMS ();
    cold = true;
  }
  else if (tempF > tLimit + 2.0 && cold == true) {
    cold = false;
  }
  if (percent < bLimit && bat == false) {
    batSMS ();
    bat = true;
  }
  /***************************SMS ALERTS END***************************************************/
  /**************************** DATA POST TO DWEET *********************************************/
  if (millis() - counter > samplingRate) {
    PostData ();
    counter = millis ();
  }
}

void Full() {
  lcd.setCursor (0, 0);
  lcd.print("FULL");
  digitalWrite(DRV8876_IN1_PIN, LOW);
  digitalWrite(DRV8876_IN2_PIN, LOW);
}



void manualMode() {
  digitalWrite(DRV8876_IN1_PIN, HIGH);  // comment is obsolete code epxlains ITSELF Turn on the pump (forward).
  digitalWrite(DRV8876_IN2_PIN, LOW);
  //  Serial.println("PUMPING!");
  lcd.setCursor(0, 0);
  lcd.print("PUMPING!");
}

void manualPurge() {
  digitalWrite(DRV8876_IN1_PIN, LOW);  // comment is obsolete code epxlains ITSELF Turn on the pump (forward).
  digitalWrite(DRV8876_IN2_PIN, HIGH);
  //  Serial.println("PUMPING!");
  lcd.setCursor(0, 0);
  lcd.print("PURGING!");
}



// senseful SUB-unit of code with a SELF-explaining name
void PumpForward() {
  digitalWrite(DRV8876_IN1_PIN, HIGH);  // comment is obsolete code epxlains ITSELF Turn on the pump (forward).
  digitalWrite(DRV8876_IN2_PIN, LOW);
  //  Serial.println("PUMPING!");
  lcd.setCursor(0, 0);
  lcd.print("PUMPING!");
}

void resetData() {
  currentMillis = millis();
  startMillis = currentMillis;
  cloopTime = currentMillis;
  totalMilliLitres = 0;
  flowRate = 0;
  vol = 0;
  flowMilliLitres = 0;
  ThresholdLimit = false;
  Serial.println("Data Reset");
  lcd.setCursor(0, 2);
  lcd.print(vol);
  lcd.setCursor(1, 2);
  lcd.print("    ");
  pulseCount = 0;
}
// senseful SUB-unit of code with a SELF-explaining name
void PumpReverse() {
  digitalWrite(DRV8876_IN1_PIN, LOW);  // comment is obsolete code epxlains ITSELF Reverse the pump.
  digitalWrite(DRV8876_IN2_PIN, HIGH);
  Serial.println("PURGING!");
  lcd.setCursor(0, 0);
  lcd.print("PURGING!");
}


// senseful SUB-unit of code with a SELF-explaining name
void pumpOff()
{
  digitalWrite(DRV8876_IN1_PIN, LOW);
  digitalWrite(DRV8876_IN2_PIN, LOW);
  lcd.setCursor(0, 0);
  lcd.print("PUMP OFF");
}

void printTemperature(DeviceAddress deviceAddress)
{
  // method 1 - slower
  //Serial.print("Temp C: ");
  //Serial.print(sensors.getTempC(deviceAddress));
  //Serial.print(" Temp F: ");
  //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit

  // method 2 - faster
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == DEVICE_DISCONNECTED_C)
  {
    Serial.println("Error: Could not read temperature data");
    return;
  }

}

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

// Power on/off the module
void powerOn(bool state) {
  if (state) {
    Serial.println(F("Turning on SIM7000..."));
    digitalWrite(FONA_PWRKEY, LOW);
    delay(100); // Turn on module
    digitalWrite(FONA_PWRKEY, HIGH);
    delay(4500); // Give enough time for the module to boot up before communicating with it
  }
  else {
    Serial.println(F("Turning off SIM7000..."));
    fona.powerDown(); // Turn off module
  }
}

void moduleSetup() {
  // SIM7000 takes about 3s to turn on and SIM7500 takes about 15s
  // Press Arduino reset button if the module is still turning on and the board doesn't find it.
  // When the module is on it should communicate right after pressing reset

  // Software serial:
  fonaSS.begin(115200, SERIAL_8N1, FONA_TX, FONA_RX); // baud rate, protocol, ESP32 RX pin, ESP32 TX pin

  Serial.println(F("Configuring to 9600 baud"));
  fonaSS.println("AT+IPR=9600"); // Set baud rate
  delay(500); // Short pause to let the command run
  fonaSS.begin(9600, SERIAL_8N1, FONA_TX, FONA_RX); // Switch to 9600
  if (! fona.begin(fonaSS)) {
    Serial.println(F("Couldn't find FONA"));
    //    while (1); // Don't proceed if it couldn't find the device
  }

  // Hardware serial:
  /*
    fonaSerial->begin(115200); // Default SIM7000 baud rate

    if (! fona.begin(*fonaSerial)) {
    DEBUG_PRINTLN(F("Couldn't find SIM7000"));
    }
  */

  // The commented block of code below is an alternative that will find the module at 115200
  // Then switch it to 9600 without having to wait for the module to turn on and manually
  // press the reset button in order to establish communication. However, once the baud is set
  // this method will be much slower.
  /*
    fonaSerial->begin(115200); // Default LTE shield baud rate
    fona.begin(*fonaSerial); // Don't use if statement because an OK reply could be sent incorrectly at 115200 baud

    Serial.println(F("Configuring to 9600 baud"));
    fona.setBaudrate(9600); // Set to 9600 baud
    fonaSerial->begin(9600);
    if (!fona.begin(*fonaSerial)) {
    Serial.println(F("Couldn't find modem"));
    while(1); // Don't proceed if it couldn't find the device
    }
  */

  type = fona.type();
  Serial.println(F("FONA is OK"));
  Serial.print(F("Found "));
  switch (type) {
    case SIM800L:
      Serial.println(F("SIM800L")); break;
    case SIM800H:
      Serial.println(F("SIM800H")); break;
    case SIM808_V1:
      Serial.println(F("SIM808 (v1)")); break;
    case SIM808_V2:
      Serial.println(F("SIM808 (v2)")); break;
    case SIM5320A:
      Serial.println(F("SIM5320A (American)")); break;
    case SIM5320E:
      Serial.println(F("SIM5320E (European)")); break;
    case SIM7000:
      Serial.println(F("SIM7000")); break;
    case SIM7070:
      Serial.println(F("SIM7070")); break;
    case SIM7500:
      Serial.println(F("SIM7500")); break;
    case SIM7600:
      Serial.println(F("SIM7600")); break;
    default:
      Serial.println(F("???")); break;
  }

  // Print module IMEI number.
  uint8_t imeiLen = fona.getIMEI(imei);
  if (imeiLen > 0) {
    Serial.print("Module IMEI: "); Serial.println(imei);
  }
}

bool netStatus() {
  int n = fona.getNetworkStatus();

  Serial.print(F("Network status ")); Serial.print(n); Serial.print(F(": "));
  if (n == 0) Serial.println(F("Not registered"));
  if (n == 1) Serial.println(F("Registered (home)"));
  if (n == 2) Serial.println(F("Not registered (searching)"));
  if (n == 3) Serial.println(F("Denied"));
  if (n == 4) Serial.println(F("Unknown"));
  if (n == 5) Serial.println(F("Registered roaming"));

  if (!(n == 1 || n == 5))   false;
  else return true;

}



void FullSMS () {
  char message3[20];

  strcpy(message3, volBuff);
  strcat(message3, text_message1);

  pinMode(FONA_RST, OUTPUT);
  digitalWrite(FONA_RST, HIGH); // Default state
  pinMode(FONA_PWRKEY, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
//  powerOn(true); // Power on the module
//  moduleSetup(); // Establish first-time serial comm and print IMEI

  fona.setNetworkSettings(F("hologram")); // For Hologram SIM card, change appropriately
  while (!netStatus() && (millis() < 30000)) {
    Serial.println(F("Failed to connect to cell network, retrying..."));
    delay(2000); // Retry every 2s
  }
  Serial.println(F("Connected to cell network!"));

  // Send a text to your phone!
  if (!fona.sendSMS(phone_number, message3)) {
    Serial.println(F("Failed to send text!"));
  }
  else {
    Serial.println(F("Sent text alert!"));
  }
}


void ColdSMS () {
  char message[20];

  strcpy(message, tempBuff);
  strcat(message, text_message2);
  pinMode(FONA_RST, OUTPUT);
  digitalWrite(FONA_RST, HIGH); // Default state
  pinMode(FONA_PWRKEY, OUTPUT);

//  powerOn(true); // Power on the module
//  moduleSetup(); // Establish first-time serial comm and print IMEI

  fona.setNetworkSettings(F("hologram")); // For Hologram SIM card, change appropriately
  while (!netStatus() && (millis() < 30000)) {
    Serial.println(F("Failed to connect to cell network, retrying..."));
    delay(2000); // Retry every 2s
  }
  Serial.println(F("Connected to cell network!"));

  // Send a text to your phone!
  if (!fona.sendSMS(phone_number, message)) {
    Serial.println(F("Failed to send text!"));
  }
  else {
    Serial.println(F("Sent text alert!"));
  }
}

void batSMS () {
  char message2[25];

  strcpy(message2, batBuff);
  strcat(message2, text_message3);
  pinMode(FONA_RST, OUTPUT);
  digitalWrite(FONA_RST, HIGH); // Default state
  pinMode(FONA_PWRKEY, OUTPUT);

//  powerOn(true); // Power on the module
//  moduleSetup(); // Establish first-time serial comm and print IMEI

  fona.setNetworkSettings(F("hologram")); // For Hologram SIM card, change appropriately
  while (!netStatus() && (millis() < 30000)) {
    Serial.println(F("Failed to connect to cell network, retrying..."));
    delay(2000); // Retry every 2s
  }
  Serial.println(F("Connected to cell network!"));

  // Send a text to your phone!
  if (!fona.sendSMS(phone_number, message2)) {
    Serial.println(F("Failed to send text!"));
  }
  else {
    Serial.println(F("Sent text alert!"));
  }
}

void PostData ()
{
  // If the shield was already on, no need to re-enable
#if defined(turnOffShield) && !defined(SIMCOM_3G) && !defined(SIMCOM_7500) && !defined(SIMCOM_7600)
  // Disable GPRS just to make sure it was actually off so that we can turn it on
  if (!fona.enableGPRS(false)) Serial.println(F("Failed to disable GPRS!"));

  // Turn on GPRS
  while (!fona.enableGPRS(true) && (millis() < 30000)) {
    Serial.println(F("Failed to enable GPRS, retrying..."));
    delay(2000); // Retry every 2s
  }
  Serial.println(F("Enabled GPRS!"));
#endif

  // Post something like temperature and battery level to the web API
  // Construct URL and post the data to the web API

  // Format the floating point numbers



  // Also construct a combined, comma-separated location array
  // (many platforms require this for dashboards, like Adafruit IO):
  //sprintf(locBuff, "%s,%s,%s,%s", speedBuff, latBuff, longBuff, altBuff); // This could look like "10,33.123456,-85.123456,120.5"

  // Construct the appropriate URL's and body, depending on request type
  // In this example we use the IMEI as device ID

#ifdef PROTOCOL_HTTP_GET
  // GET request

  counter = 0; // This counts the number of failed attempts tries

#if defined(SIMCOM_3G) || defined(SIMCOM_7500) || defined(SIMCOM_7600)
  // You can adjust the contents of the request if you don't need certain things like speed, altitude, etc.
  sprintf(URL, "GET /dweet/for/%s?temp=%s&bat=%s&vol=%s HTTP/1.1\r\nHost: dweet.io\r\n\r\n",
          imei, tempBuff, batBuff, volBuff);

  // Try a total of three times if the post was unsuccessful (try additional 2 times)
  while (counter < 3 && !fona.postData("www.dweet.io", 443, "HTTPS", URL)) { // Server, port, connection type, URL
    Serial.println(F("Failed to complete HTTP/HTTPS request..."));
    counter++; // Increment counter
    delay(1000);
  }
#else
  sprintf(URL, "http://dweet.io/dweet/for/%s?temp=%s&bat=%s&vol=%s", imei, tempBuff, batBuff, volBuff);

  while (counter < 3 && !fona.postData("GET", URL)) {
    Serial.println(F("Failed to post data, retrying..."));
    counter++; // Increment counter
    delay(1000);
  }
#endif

#elif defined(PROTOCOL_HTTP_POST)
  // You can also do a POST request instead

  counter = 0; // This counts the number of failed attempts tries

#if defined(SIMCOM_3G) || defined(SIMCOM_7500) || defined(SIMCOM_7600)
  sprintf(body, "{\"lat\":%s,\"long\":%s}\r\n", latBuff, longBuff); // Terminate with CR+NL
  sprintf(URL, "POST /dweet/for/%s HTTP/1.1\r\nHost: dweet.io\r\nContent-Length: %i\r\n\r\n", imei, strlen(body));

  while (counter < 3 && !fona.postData("www.dweet.io", 443, "HTTPS", URL, body)) { // Server, port, connection type, URL
    Serial.println(F("Failed to complete HTTP/HTTPS request..."));
    counter++; // Increment counter
    delay(1000);
  }
#else
  sprintf(URL, "http://dweet.io/dweet/for/%s", imei);
  sprintf(body, "{\"lat\":%s,\"long\":%s}", latBuff, longBuff);

  // Let's try a POST request to thingsboard.io
  // Please note this can me memory-intensive for the Arduino Uno
  // and may not work. You might have to split it up into a couple requests
  // and send part of the data in one request, and the rest in the other, etc.
  // Perhaps an easier solution is to swap out the Uno with an Arduino Mega.
  /*
    const char * token = "qFeFpQIC9C69GDFLWdAv"; // From thingsboard.io device
    sprintf(URL, "http://demo.thingsboard.io/api/v1/%s/telemetry", token);
    sprintf(body, "{\"lat\":%s,\"long\":%s,\"speed\":%s,\"head\":%s,\"alt\":%s,\"temp\":%s,\"batt\":%s}", latBuff, longBuff,
          speedBuff, headBuff, altBuff, tempBuff, battBuff);
    //  sprintf(body, "{\"lat\":%s,\"long\":%s}", latBuff, longBuff); // If all you want is lat/long
  */

  while (counter < 3 && !fona.postData("POST", URL, body)) {
    Serial.println(F("Failed to complete HTTP POST..."));
    counter++;
    delay(1000);
  }
#endif


#endif

  //Only run the code below if you want to turn off the shield after posting data
#ifdef turnOffShield
  // Disable GPRS
  // Note that you might not want to check if this was successful, but just run it
  // since the next command is to turn off the module anyway
  if (!fona.enableGPRS(false)) Serial.println(F("Failed to disable GPRS!"));

  // Turn off GPS
  if (!fona.enableGPS(false)) Serial.println(F("Failed to turn off GPS!"));

  // Power off the module. Note that you could instead put it in minimum functionality mode
  // instead of completely turning it off. Experiment different ways depending on your application!
  // You should see the "PWR" LED turn off after this command
  //  if (!fona.powerDown()) Serial.println(F("Failed to power down FONA!")); // No retries
  counter = 0;
  while (counter < 3 && !fona.powerDown()) { // Try shutting down
    Serial.println(F("Failed to power down FONA!"));
    counter++; // Increment counter
    delay(1000);
  }
#endif

  // Alternative to the AT command method above:
  // If your FONA has a PWRKEY pin connected to your MCU, you can pulse PWRKEY
  // LOW for a little bit, then pull it back HIGH, like this:
  //  digitalWrite(PWRKEY, LOW);
  //  delay(600); // Minimum of 64ms to turn on and 500ms to turn off for FONA 3G. Check spec sheet for other types
  //  delay(1300); // Minimum of 1.2s for SIM7000
  //  digitalWrite(PWRKEY, HIGH);

  // Shut down the MCU to save power
#ifndef samplingRate
  Serial.println(F("Shutting down..."));
  delay(5); // This is just to read the response of the last AT command before shutting down
  MCU_powerDown(); // You could also write your own function to make it sleep for a certain duration instead
#else
  //  // The following lines are for if you want to periodically post data (like GPS tracker)
  //  Serial.print(F("Waiting for ")); Serial.print(samplingRate); Serial.println(F(" seconds\r\n"));
  //  delay(samplingRate * 1000UL); // Delay

  // Only run the initialization again if the module was powered off
  // since it resets back to 115200 baud instead of 4800.
#ifdef turnOffShield
  fona.powerOn(FONA_PWRKEY); // Powers on the module if it was off previously
  moduleSetup();
#endif

#endif
}

Here is all the deets on the ESP32's GPIO/RTC pins.

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/gpio.html

Hope it gives you the info you need.

Not quite sure I quite understand what you are asking. Can you post a picture/schematic of your set up?

In my experience, adding stuff like this just increases the number of things that can fail. If you're concerned with the tubing leaking, address that directly first.

Im trying to add a relay that shuts off power to the microcontroller when water is detected. Im trying to use GPIO 26 to power the relay since the relay was working fine with 3 volts, but when I connect GPIO 26 to the relay, the pins voltages drops to 2 volts and the relay wont click. I tried to instead connect the relay power pin to vcc and the ground to GPIO26 and just do the opposite (GPIO is low when no water detected, providing a ground, and high when water detected, removing ground) but this doesnt work either. The pin does go low when the button is open and meters to ground, but it still wont actuate the relay

I've done what I could, the first leak was due to nicking the tubing after removing zip ties, the second leak was because I forgot to replace the zip ties before starting the pump.

Other than that, there is not much else I could do short of redesigning the enclosure where the pump head protrudes from the enclosure like on my first prototype, which wasn't a popular choice with field testers. I plan to coat everything electronic in epoxy to prevent shorts due to water contamination, but this feature is more of a "peace of mind" thing as there were concerns that if the device operates in a hazardous environment it will cause an explosion. In this image you can see the setup for the leak detector, it is just a regular water level sensor that rests under the tubing, and there are drain holes below the mesh

One could use an old smoke alarm......been done before to detect water.

I'll keep that in mind, I only had one water sensor and its attached to a circuit board now. Hopefully these picture can help explain what im seeing. When the switch is closed (green LED OFF), you can see GPIO 26 supply voltage to the relay on the meter, and when the switch is open (green LED on), the power goes away so the ESP32 is doing what its supposed to but it seems like the relay draws too much current and reduces the output voltage of gpio26 from 3.3 to 2.2 volts. when i use 3.3 vdc pin relay works, but not with gpio pin.

Is there a way to make the voltage of GPIO 26 stronger? all I can think of is using a DC-DC boost converter as a voltage regulator but that would take up a lot of space


  1. Check your work after changing anything.
  2. Have a process for changing things.
  3. Use better quality tubing and/or actual tube clamps instead of zip ties.

Those are things you could have done.

And that doesn't concern you? Intrinsic safety is a thing!

I'm not trying to be difficult, but it doesn't sound like you're going down a good path here.

Yes, which is why I'm implementing a failsafe that will completely shutoff power when a leak is detected.

I'm not doing this because I was asked to, I'm doing this because I know eventually someone will ask "well what happens when the tubing is over-pressurized and ruptures and leaks sewage everywhere?". When someone asks me that, I'll be able to say "oh well you see Mr. Regulatory guy, that would never happen because the device automatically shuts off when a leak or loss of pressure is detected".

Hose clamps are a good idea, I didn't consider them because I didn't think they made them small enough for this diameter of tubing.

I was able to get the relay going, I just needed to add a resistor, transistor, and diode and now the relay switches off when the switch is actuated. Now I need to redesign the power button for the device to where it is held down by the relay after initially applying power, and is then released by pressing the button again or if the relay is deenergized

Edit: now that I think about it, maybe I could have used a MOSFET?

Googling
arduino self power off
will show you some mosfet ideas.

Operating a DIY device in a hazardous environment is definitely not a good idea.

If this is the case the device MUST be certified as suitable for that atmosphere, you cannot add what you think is appropriate.

As for turning it off , ( as per the previous post ) you can use a FET in the power supply line ,switched on via a push button and held on with a digital output . The processor can then just switch itself off .

As for leaks , I would put the pump and plumbing in a separate partition in the box , with a drain hole .

You are relying on an electronic and coding failsafe for a device that you have chosen to put in a hazardous environment. It is not the best solution by any means. Why are your electronics any where near the hazardous liquids/gases? You can't know that any leak will drip directly on your detection circuit or that by the time it is 'detected' the electronics are not so wet as to be unable to do anything reasonable about it.

If your electronics must be 'near' the hazardous environment then make as much effort to protect and isolate them. Think about conformal coatings and watertight enclosures. Think about gas safe mesh and appropriate grounding etc to avoid sparks. Sensors can be similarly spec'ed and should be the only things actually close to the hazardous stuff being measured. Also your tubing and cable ties (its in the name) looks like a problem waiting to happen. There are hose clamps for every size of hose.

  1. Make it so it doesn't leak
  2. Make it so if it does leak it doesn't do any damage/pose any danger

n. Make a nice detector that doesn't make 1 or 2 less reliable to advise you if 1 or 2 have failed.

yes but the arduino needs to cut power that is holding a push button and "release it" because that power goes through two dc-dc converters so it would do nothin if the only thing being deenergized is the microcontroller because it would just be stuck in a loop of turning off before turning back on, detecting the leak and turning off, then turning back on and repeating the cycle.

the current button I use for power is a mechanical latching button, so the plan is to replace it with a "momentary" push-button held on by power from the arduino. that way, if power is cut, the push-button is reset so that power doesnt get to the dc-dc converters.

as far as for operating this in a hazardous environment, we don't have a choice. the commercial options for these devices cost $5,000-$10,000 USD and all vendors of these devices have them on backorder. we don't have the luxury of waiting for someone to donate these devices out of the kindness of their hearts.

this was the design of the first prototype, but feedback from field testers said they would like it to be more compact by not having the pump stick out.

All right so, all I've accomplished is adding two buttons instead of one. One button turns power on, and the other shuts power off. The one button can be interrupted by the water sensor, which shuts power off. the second button just turns power off. how to make it to where only one button turns power on and off?


#define POWER_PIN 26 // ESP32 pin GIOP36 (ADC0) connected to relay positive volt terminal
#define samplingRate2 3000UL // the time between sensor readouts
unsigned long counter2 = 0;
const int  buttonPin = 33;  // used to simulate water sensor. if buttonpin is open, power to relay is cut.
unsigned long currentMillis;                    // millis() of the current loop.
const int FloatSwitchLED = 15;
#define SHUTOFF_BUTTON 4 ///turns off power 


void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(FloatSwitchLED, OUTPUT);
  pinMode(POWER_PIN, OUTPUT);
  digitalWrite(POWER_PIN, HIGH);
  pinMode(SHUTOFF_BUTTON, INPUT_PULLUP);
  digitalWrite(SHUTOFF_BUTTON, LOW);
}

void loop() {
  currentMillis = millis();
  if (currentMillis - counter2 > samplingRate2) {
    Serial.print("The water sensor value: ");
    Serial.println("power ok");
    counter2 = millis();
    currentMillis = millis();
  }
  if (digitalRead(SHUTOFF_BUTTON) == LOW) {
  digitalWrite(POWER_PIN, LOW);
    delay(7000);
    Serial.println("bye bye");
  }
  else {
    digitalWrite(POWER_PIN, HIGH);
  }
  if (digitalRead(buttonPin) == HIGH ) {
  digitalWrite(POWER_PIN, LOW);
    delay(7000);
    Serial.println("bye bye");
    digitalWrite(FloatSwitchLED, HIGH);

    Serial.println("LEAK DETECTED!");
  }
  else {
    digitalWrite(FloatSwitchLED, LOW);
    digitalWrite(POWER_PIN, HIGH);

  }
}

That does not solve the intrinsic safety issue , the design must be certified as being IS and carry the appropiate logo on its case. This is done by an external agency .

You simply cannot do what you propose !!! you are opening yourself up to prosecution

Read ALL of this

1 Like

Jesus christ, you're right! I'm an absolute moron and have no idea what I'm doing or any understanding of the dark forces that Arduino is based on. Clearly, if this device were to be operation, it could potentially open a gateway to hell or even throw the earth off its orbit, leading to massive tidal forces wiping out hundreds of millions of people across the globe.

I was foolish for thinking I could do something as simple as program a relay, I had no idea that tampering with this dark technology could lead to catastrophic consequences. I will immediately destroy the devices and all prototypes, and associated schematics, codes and blueprints and I will turn myself into the FBI first thing in the morning. Thank you for opening my eyes, you have no idea how many lives you have saved!

What do you think of this bulkhead? Would a bulkhead increase the possibility of prosecution and perhaps capital punishment?

Maybe like Figure 1 in this

https://www.edn.com/latching-power-switch-uses-momentary-pushbutton/

It works in ‘toggle’ mode; that is, the first switch closure applies power to the load, the second removes power, and so on.

May not be exactly what you want, but could stimulate some ideas.

Edit to add: truly, only certified devices should be used in classified areas. What would it take to accomplish the goal while keeping energized parts outside of classified area boundaries?