Need help quickly! Arduino Code. Fixed

I wanna make an eMail program, that sends a mail if a button is pushed. I got it working to a point where it sends a mail on button push. The problem is I don't want it to send 1000 Mails as long as the button is pushed, but only one Mail per button push phase. I got my code like this now but I'm stuck. Can anybody help?

void setup() {
Serial.begin(9600);
Serial.println();
Serial.print("Connecting to AP");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(200);
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
if (!WiFi.config(local_IP, gateway, subnet)) {
Serial.println("STA Failed to configure");
}

Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);

// initialize the pushbutton pin as an input:
pinMode(BUTTON_PIN, INPUT);
}

void loop() {

buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW && zaehler == 0) {

zaehler++;
smtp.debug(1);
smtp.callback(smtpCallback);
ESP_Mail_Session session;
session.server.host_name = SMTP_HOST;
session.server.port = SMTP_PORT;
session.login.email = AUTHOR_EMAIL;
session.login.password = AUTHOR_PASSWORD;
session.login.user_domain = "";

   SMTP_Message message;
             message.sender.name = "ESP";
             message.sender.email = AUTHOR_EMAIL;
             message.subject = "ESP Test Email";
             message.addRecipient("Test", RECIPIENT_EMAIL);


   String htmlMsg = "<div style=\"color:#2f4468;\"><h1>Hello World!</h1><p>- Sent from ESP board</p></div>";
          message.html.content = htmlMsg.c_str();
          message.html.content = htmlMsg.c_str();
          message.text.charSet = "us-ascii";
          message.html.transfer_encoding = Content_Transfer_Encoding::enc_7bit;

if (!smtp.connect(&session)){
return;
}

       if (!MailClient.sendMail(&smtp, &message))
               Serial.println("Error sending Email, " + smtp.errorReason());

  if (zaehler >= 1) {
    Serial.println(".");
   } 

}  

}

Hi, @lnowak
Welcome to the forum.

You need to detect when the button is pushed, not while the button is pushed.

Google;

arduino button change of state

Tom.. :smiley: :+1: :coffee: :australia:

Help us help you.

......................................................................

Thank you very much!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.