I can't seem to get my arduino to send a basic email. Code is:
// #include "Arduino.h"
#include <EMailSender.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
char emailpass[] = EMAIL_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS; // the WiFi radio's status
EMailSender emailSend("(arduino-email)@gmail.com", emailpass);
//just for testing purposes
int distance = 53;
void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
}
void connectWifi() {
status = WiFi.status();
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
if (status == WL_DISCONNECTED) {
WiFi.end();
Serial.println("Disconnected");
delay(10000);
}
else if (status != WL_DISCONNECTED && status != WL_CONNECTED && status != WL_IDLE_STATUS) {
WiFi.end();
Serial.print("Not connected, not disconnected...Status: ");
Serial.println(status);
delay(10000);
}
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
status = WiFi.status();
Serial.print("WiFi Status: ");
Serial.println(status);
printWifiData();
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}
void printMacAddress(byte mac[]) {
for (int i = 5; i >= 0; i--) {
if (mac[i] < 16) {
Serial.print("0");
}
Serial.print(mac[i], HEX);
if (i > 0) {
Serial.print(":");
}
}
Serial.println();
}
void sendEmail(String subjectStr, String messageStr) {
EMailSender::EMailMessage message;
message.subject = subjectStr;
message.message = messageStr;
status = WiFi.status();
if (status != WL_CONNECTED) {
connectWifi();
}
EMailSender::Response resp = emailSend.send("MY-EMAIL", message);
delay(20000);
Serial.println("Sending status: ");
Serial.println(resp.status);
Serial.println(resp.code);
Serial.println(resp.desc);
}
void setup() {
Serial.begin(38400);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(100); // Clears the trigPin
if (distance >= 50) {
sendEmail("Purification System - Add Salt", "It is time to add more salt to the purification system.");
}
else {
String part1 = "The System is working. The current level of salt is: ";
String part2 = part1 + distance;
String message2 = part2 + " cm. System alert at 50 cm.";
sendEmail("Purification System - Working", message2);
}
delay(60000);
}
Output from that is:
WiFi Status: 3
IP Address: (blanked out), internal IP on my network
MAC address: -------------- (blanked out)
Sending status:
0
2
Could not connect to mail server
Disconnected
Attempting to connect to WPA SSID: (MY-SSID)
WiFi Status: 3
IP Address: (blanked out), internal IP on my network
MAC address: -------------- (blanked out)
Sending status:
0
2
Could not connect to mail server
Before you ask, I know it connects fine to the WiFi. I can see it connect on my router. It is in Power Save mode and disconnects after about a minute. Arduino side, Rx from router 27Mb/s, Tx to router 1Mb/s. I know the Wifi SSID and password are correct as are the Gmail email and password.
Any ideas would be great?