My goal is to send email notifications when an alarm is detected via my sound sensor.
I have an ESP8622 ESP01 WiFi transceiver that I was able to connect to my network. I have the code for the Arduino/sound sensor (and it works) but I am having trouble with the code for the ESP8622.
Here is my Arduino Code:
const int soundPin = 2; // Digital pin connected to sound sensor
int soundState;
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(soundPin, INPUT); // Set the sound sensor pin as input
}
void loop() {
// Read the state of the sound sensor
soundState = digitalRead(soundPin);
// Check if sound is detected (HIGH = sound detected)
if (soundState == LOW) {
Serial.println("Alarm detected!");
delay(1000); // Add delay to prevent rapid triggering
}
// Optional: Add a small delay to reduce serial monitor flooding
delay(100);
}
Here is the code for the ESP8622 (does not work):
#include <SMTPClient.h>
// Replace with your network credentials
const char* ssid = "****";
const char* password = "*****";
// Email credentials
const char* smtp_server = "smtp.gmail.com"; // e.g., smtp.gmail.com
const int smtp_port = 587; // common port for SMTP
const char* email_user = "****@gmail.com"; // Your email
const char* email_password = "******"; // Your email password
const char* recipient_email = "******"; // Recipient email
SMTPClient smtpClient;
void setup() {
Serial.begin(9600);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
smtpClient.setServer(smtp_server, smtp_port);
smtpClient.setLogin(email_user, email_password);
}
void loop() {
// Check for incoming data from Arduino
if (Serial.available()) {
String command = Serial.readStringUntil('\n');
if (command.equals("SEND_EMAIL")) {
sendEmail();
}
}
}
void sendEmail() {
// Set email properties
smtpClient.setSender(email_user);
smtpClient.addRecipient(recipient_email);
smtpClient.setSubject("Alarm Notification");
smtpClient.setMessage("Alarm detected! An alert has been triggered.");
// Send the email
if (smtpClient.send()) {
Serial.println("Email sent successfully");
} else {
Serial.print("Error sending email: ");
Serial.println(smtpClient.getError());
}
// Clear the client for the next email
smtpClient.clearAllRecipients();
}
Compilation error: 'class SMTPClient' has no member named 'setServer'; did you mean 'String SMTPClient::server'? (not accessible from this context)


