leak alarm system noise redundace

Hello all! I have made leak alarm system, which uses adafruit fona to send sms when button state is changing. It have worked fine in tests at home, but now in our cottage the electricity is so bad, that every stronger machine (like washing machine) generated noise to system and it will send sms almost every time when some machine starts or stops. I was thinking use DELAY function to wait for example twice 5 minute, and check the status button status three times before generate sms send.. i am not just sure how to change the code in loop.. If you can help me i will appreciate!

void loop() { // read the pushbutton input pin: buttonState3 = digitalRead(buttonPin3);

// compare the buttonState to its previous state if (buttonState3 != lastButtonState3) {

// if the state has changed, increment the counter if (buttonState3 == HIGH) { // if the current state is HIGH then the button // wend from off to on: sendSMS3(); // send sms

} // Delay a little bit to avoid bouncing delay(1000);

// save the current state as the last state, //for next time through the loop lastButtonState3 = buttonState3; }

Don't use the delay() function, use millis() for timing instead.

Make sending the SMS dependant on a boolean variable being true. When you send an SMS set the boolean to false and save the value of millis() as the start time. Each time through loop() check whether the current value of millis() minus the start time is greater than your required wait time. If it is then set the boolean to true ready to send another SMS when required.

See Using millis() for timing. A beginners guide

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What is your leak detector and how are you supplying power to your product?

Thanks… Tom… :slight_smile:

thanks for instructions!

It´s using basic pull-down button with 10k ohm resistor, and the Fona is getting own power. I have tested few different usb Powers, but no one them are able to filter the noise.

jehol: but now in our cottage the electricity is so bad, that every stronger machine (like washing machine) generated noise to system and it will send sms almost every time when some machine starts or stops.

Is the fluctuation in the mains voltage causing your Arduino to reset?

You have not posted your complete program so I can't see if you have a print() statement in setup() that woould let you know that the Arduino is resetting.

If the problem is that the Arduino is resetting I suggest you read the sensor in setup() to initialize the vaalue and then when you take a reading in loop() it will be compared with that initial value.

If that is not the problem then I wonder how variations in the mains voltage are causing your sensor to trigger?

Maybe it would be useful to run the Arduino from a battery that is trickle-charged by the mains?

...R

so the float sensor is actually same as in wiring diagram here: https://www.arduino.cc/en/Tutorial/Button

and no it´s not resetting. i am able to monitor the noise in serial plotter. It´s generating peak to signal.

wiring to sensor is quite long. but it is working very well - except that noise from machines..

here is the complete code :slight_smile:

// Adafruit FONA Library - Version: Latest 
#include <Adafruit_FONA.h>

#define FONA_RX 3  // GSM piirin pin portit
#define FONA_TX 2
#define FONA_RST 4

// this is a large buffer for replies
char replybuffer[255];
char sms_response[52];  //textarivastaus

#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;

Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

const int  buttonPin3 = 8;    // the pin that the pushbutton is attached to

int buttonState3 = 0;         // current state of the button
int lastButtonState3 = 0;     // previous state of the button

int JepunNumero = "+35840000000";     // Jepun numero

int slot1 = 1; // SIM KORTIN SMS PAIKKOJA
int slot2 = 2; // TYHJENTÄMISTÄ VARTEN
int slot3 = 3;
int slot4 = 4;
int slot5 = 5;
int slot6 = 6;
int slot7 = 7;
int slot8 = 8;
int slot9 = 9;
int slot10 = 10;
int slot11 = 11;
int slot12 = 12;

//liittyy numeron tunnistus ja vastaus scriptiin
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
char fonaInBuffer[64];          //for notifications from the FONA

void setup() {

  Serial.begin(115200);

  fonaSerial->begin(4800);
  if (! fona.begin(*fonaSerial)) {
    Serial.println(F("Couldn't find FONA"));
    while (1);
  }
  //SIM900power_on();
  pinMode(buttonPin3, INPUT);

}

void sendSMS3()
{
  char sendto[21] = "+35840000000", message[141] = "Unkari, Hazavarlak, Umpisailion tayttymisaste on nyt yli 75%";
  fona.sendSMS(sendto, message);
  Serial.println(F("Sent!"));
  delay(100);
  Serial.println("sms test3");                                            // message to send
  delay(100);
  Serial.println((char)26);                                              // End AT command with a ^Z, ASCII code 26
  delay(100);
  Serial.println();
  delay(10000);                                                          // give module time to send SMS
}

void loop() {
  // read the pushbutton input pin:
  buttonState3 = digitalRead(buttonPin3);

  // compare the buttonState to its previous state
  if (buttonState3 != lastButtonState3) {

    // if the state has changed, increment the counter
    if (buttonState3 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      sendSMS3();                                                        // send sms

    }
    // Delay a little bit to avoid bouncing
    delay(1000);

    // save the current state as the last state,
    //for next time through the loop
    lastButtonState3 = buttonState3;
  }

  char* bufPtr = fonaInBuffer;    //handy buffer pointer
  
  if (fona.available())      //any data available from the FONA?
  {
    int slot = 0;            //this will be the slot number of the SMS
    int charCount = 0;
    //Read the notification into fonaInBuffer
    do  {
      *bufPtr = fona.read();
      Serial.write(*bufPtr);
      delay(1);
    } while ((*bufPtr++ != '\n') && (fona.available()) && (++charCount < (sizeof(fonaInBuffer)-1)));
    
    //Add a terminal NULL to the notification string
    *bufPtr = 0;
    
    //Scan the notification string for an SMS received notification.
    //  If it's an SMS message, we'll get the slot number in 'slot'
    if (1 == sscanf(fonaInBuffer, "+CMTI: \"SM\",%d", &slot)) {
      Serial.print("slot: "); Serial.println(slot);
      
      char callerIDbuffer[32];  //we'll store the SMS sender number in here
      
      // Retrieve SMS sender address/phone number.
      if (! fona.getSMSSender(slot, callerIDbuffer, 31)) {
        Serial.println("Didn't find SMS message in slot!");
      }
      Serial.print(F("FROM: ")); Serial.println(callerIDbuffer);
      
      //Send back an automatic response
      sprintf (sms_response, "Unkari, Hazavarlak, OK. Anturi:%d (0=ei 1=on ..anturin taso saavutettu). SMS kyselyn lahettajan numero: %s",buttonState3,callerIDbuffer); // Lähetettävät tiedot
      Serial.println("Sending reponse...");
      if (!fona.sendSMS(JepunNumero, sms_response)) {
        Serial.println(F("Failed"));
      } else {
        Serial.println(F("Sent!"));
      }
      
      // delete the original msg after it is processed
      //   otherwise, we will fill up all the slots
      //   and then we won't be able to receive SMS anymore
      if (fona.deleteSMS(slot)) {
      // if (fona.deleteAllSMS()) {
        Serial.println(F("OK! Viestin poisto onnistui"));
      } else {
        Serial.print(F("Couldn't delete SMS in slot ")); Serial.println(slot);
        fona.deleteSMS(slot12);  // TYHJENNETÄÄN
        fona.deleteSMS(slot11); // SIM KORTIN
        fona.deleteSMS(slot10); // SMS PAIKKOJA
        fona.deleteSMS(slot9);
        fona.deleteSMS(slot8);
        fona.deleteSMS(slot7);
        fona.deleteSMS(slot6);
        fona.deleteSMS(slot5);
        fona.deleteSMS(slot4);
        fona.deleteSMS(slot3);
        fona.deleteSMS(slot2);
        fona.deleteSMS(slot1);
        fona.print(F("AT+CMGD=?\r\n"));
      }
    }
  }
}

jehol: Hello all! I have made leak alarm system, which uses adafruit fona to send sms when button state is changing. It have worked fine in tests at home, but now in our cottage the electricity is so bad, that every stronger machine (like washing machine) generated noise to system and it will send sms almost every time when some machine starts or stops.

You could use a line conditioner to give clean, stable power to your project. It may be overkill but it's plug and play. Tripplite made good ones, probably still do. A line conditioner will convert voltage and current to keep voltage stable. I've had two customers in industrial parks get them, it fixed their PC problems. When I moved in here and the power showed fluctuations, I got a UPS to protect my PC.

Almost the same would be to run the project off batteries or a capacitor bank and have house power feed that through a charger.

the funny thing is, i have tried few diffrent usb power, and thru computer, and also from Aliexpress one small ups, but still same problem. It might be from long sensor cable or even gsm antenna witch get noise from network? .. And the debugging at the moment is little bit difficult because the device is with my father in different country - than I :) anyway i have connections to device, so i though to try resolve the issue in code if possible :)

br. Jens

the Fona is getting own power.

Has it got a common GND connection with the Arduino ?

Long wires make good antennae. Can you get shielded cable like CAT cable? Shield must be grounded. Or run the cable through a grounded steel or iron tube or like.

How long is any disturbance? You may need to have X reads all the same before sending SMS.

Thinking a bit more about your program ...

I suspect this is a case where the change of input state is not relevant. It may make more sense to sample the input at (maybe) 1 second intervals and if it is HIGH for (say) 4 occasions in a row it signifies a fault.

Something like this

if (millis() - previousReadingMillis >= 1000) {
   previousReadingMillis += 1000;
   sensorState = digitalRead(sensorPin);
   if (sensorState == HIGH) {
       highCount ++;
   else {
       highCount = 0;
   }
   if (highCount == 4 and smsSent == false) {
      highCount = 0;
      smsSent = true;
      // send SMS
   }
}

...R

Hi,
Can you post a picture of your project, so we can see your component layout?

Thanks… Tom… :slight_smile:

well… this is only one i have at home. As i wrote, the device is now in different country… And i managed to make changes to code, so big thanks!! now it´s “filtering” 2 minutes sensor status to check if it´s static and then will send sms. Will stay following how well it´s now working, but all test went right after code change.
br. Jens

Hi, A 0.1uF capacitor across those two black input terminals would help.

Tom... :)

Based on the information here i would consentrate on the button cable.

This is the most likely place for the problem to be, but do not assume tha is the only place.

To test this can you disconnect the cable and see if the unit sends messages when the machines go on and off.

If this does happen then it is most likely not the cable.

Does the switch close or open when it activates?

The fact that you have tried different power supplies and through the computer and i think you mentioned serial monitoring makes me think it is not a power issue but an interference issue.

Daz

If the problem only arises when some electrical appliance turns on (or off - but less likely, i suspect) and if that does not happen very often then I reckon it is a waste of time trying to eliminate the problem when it would be so easy to work around the problem for this particular project.

Leaks are not intermittent things and they do not require instant reporting.

…R

PS … Fridges and freezers are the real trouble-makers as they use synchronous motors that have huge inrush currents.