I'm really into this IoT stuff but I'm still amateur with how it mainly works.
Fortunately, I got a code to work and send an-email (finally!) using this:
I'm using a Wemos D1-Mini with ESP8266 as my micro-controller and a vibration switch sensor for the input.
Now, my next problem is to have the sensor input be read correctly and will only send an email IF the sensor reads anything. I tried but that's as good as I can get. Any help would be appreciated, as always!
#include <ESP8266WiFi.h> // the ESP8266WiFi.h lib
const char* SSID = "";
const char* PASS = "";
char server[] = "mail.smtp2go.com";
//ADC_MODE(ADC_VCC);
int sensorPin = A0; // select the input pin for the vibration sensor
int motion = 0; // variable to store the value coming from the sensor
WiFiClient client;
void setup()
{
Serial.begin(115200);
delay(10);
Serial.println("");
Serial.println("");
Serial.print("Connecting");
Serial.println(SSID);
WiFi.begin(SSID, PASS);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi Connected");
Serial.print("IPess: ");
Serial.println(WiFi.localIP());
// The code to read the sensor input.... any suggestions?
motion = analogRead(sensorPin);
String motionString = String(motion, 1);
byte ret = sendEmail(motionString);
}
void loop()
{
// I have a feeling the motion sensor input must be here?
}
byte sendEmail(String motion)
{
byte thisByte = 0;
byte respCode;
if (client.connect(server, 2525) == 1) {
Serial.println(F("connected"));
} else {
Serial.println(F("connection failed"));
return 0;
}
if (!eRcv()) return 0;
Serial.println(F("Sending EHLO"));
client.println("EHLO www.example.com");
if (!eRcv()) return 0;
Serial.println(F("Sending auth login"));
client.println("auth login");
if (!eRcv()) return 0;
Serial.println(F("Sending User"));
// Change to your base64, ASCII encoded user
client.println(""); // SMTP UserID
if (!eRcv()) return 0;
Serial.println(F("Sending Password"));
// change to your base64, ASCII encoded password
client.println("");// SMTP Passw
if (!eRcv()) return 0;
Serial.println(F("Sending From")); // change to your email address (sender)
client.println(F("MAIL From: @gmail.com"));// not important
if (!eRcv()) return 0; // change to recipient address
Serial.println(F("Sending To"));
client.println(F("RCPT To: c@my"));
if (!eRcv()) return 0;
Serial.println(F("Sending DATA"));
client.println(F("DATA"));
if (!eRcv()) return 0;
Serial.println(F("Sending email")); // change to recipient address
client.println(F("To: @my")); // change to your address
client.println(F("From: @gmail.com"));
client.println(F("Subject: ORAL BEARDUINO ALERT!\r\n"));
client.println(F("Toothbrush has been recently activated!"));
client.print(F("Motion:"));
Serial.println(motion);
if (!eRcv()) return 0;
Serial.println(F("Sending QUIT"));
client.println(F("QUIT"));
if (!eRcv()) return 0;
client.stop();
Serial.println(F("disconnected"));
return 1;
}
byte eRcv()
{
byte respCode;
byte thisByte;
int loopCount = 0;
while (!client.available())
{
delay(1);
loopCount++; // if nothing received for 10 seconds, timeout
if (loopCount > 10000) {
client.stop();
Serial.println(F("\r\nTimeout"));
return 0;
}
}
respCode = client.peek();
while (client.available())
{
thisByte = client.read();
Serial.write(thisByte);
}
if (respCode >= '4')
{
// efail();
return 0;
}
return 1;
}