Do you think I will have a problem with bouncing the way I have it or not? If so got any recommendations for debouncing?
All the client.read stuff is for me to see all of the server replies, it seems to work good, and I am gonna leave it in.
Used a arduino for the fritzing example. Is there a ESP-12 part for fritzing?
// This sketch is to monitor a Generator running and shutting off.
// It will alert the user via text and email when it is running and when it shuts off.
// This sketch is for a ESP-12 module but could be modified for an Arduino.
// This requires 2 relays but could possibly work with one.
// Must add a diode between positive and negative of the coil side of the relay to minimize relay shut-off noise (backfeed)
// Next step is work on minimizing input sensitivity by implementing non-floating pins
// Created by Scott Wilson 04-09-2015
#include <ESP8266WiFi.h>
const int genPin = 4;
const int genPin2 = 5;
volatile int state = 2;
//*-- IoT Information
const char* SSID = "headend";
const char* PASS = "headend1";
const char* host = "xxx.xxx.xxx.xxx"; // SMTP Server IP Address
void setup() {
Serial.begin(115200);
pinMode(4, INPUT);
pinMode(5, INPUT);
attachInterrupt(genPin, genSigUp, RISING);
attachInterrupt(genPin2, genSigDown, FALLING);
delay(50);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(SSID);
WiFi.begin(SSID, PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
delay(1000);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(1000);
}
void loop() {
if (state == 1) {
delay(5000);
if (digitalRead(genPin) == LOW) {
return;
}
else {
Serial.println("Pin just went HIGH");
Serial.print("Connecting to ");
Serial.println(host);
WiFiClient client;
const int httpPort = 25;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
delay(1000);
while (client.available()) {
char c = client.read();
Serial.write(c);
}
Serial.println("SMTP Helo...");
client.print("HELO mail.xxxxxxxxxxxx.net\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
delay(1000);
Serial.println("SMTP Mail From...");
client.print("MAIL FROM:xxxxxxxxxxxx@gmail.com\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
delay(1000);
Serial.println("SMTP RCPT...");
client.print("RCPT TO:xxxxxxxxxxxx@gmail.com\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
delay(1000);
client.print("RCPT TO:xxxxxxxxxxxx@vtext.com\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
delay(1000);
Serial.println("SMTP Data...");
client.print("DATA\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
delay(1000);
Serial.print("Subject: Old Generator Alert!\r\n");
client.print("Subject: Old Generator Alert!\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
Serial.print("Generator at the Old HE is running!!\r\n");
client.print("Generator at the Old HE is running!!\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
client.print("\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
Serial.println(".");
client.print(".\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
Serial.println("SMTP quit...");
client.print("quit\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
state = 2;
client.stop();
}
}
if (state == 0) {
delay(5000);
if (digitalRead(genPin) != LOW) {
return;
}
else {
Serial.println("Pin just went LOW");
Serial.print("Connecting to ");
Serial.println(host);
WiFiClient client;
const int httpPort = 25;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
while (client.available()) {
char c = client.read();
Serial.write(c);
}
delay(1000);
Serial.println("SMTP Helo...");
client.print("HELO mail.xxxxxxxxxxxx.net\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}delay(1000);
Serial.println("SMTP Mail From...");
client.print("MAIL FROM:xxxxxxxxxxxx@gmail.com\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}delay(1000);
Serial.println("SMTP RCPT...");
client.print("RCPT TO:xxxxxxxxxxxx@gmail.com\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
};
client.print("RCPT TO:xxxxxxxxxxxx@vtext.com\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
delay(1000);
Serial.println("SMTP Data...");
client.print("DATA\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
delay(1000);
Serial.print("Subject: Old Generator Alert!\r\n");
client.print("Subject: Old Generator Alert!\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
delay(1000);
Serial.print("Generator at the Old HE is shut down!!\r\n");
client.print("Generator at the Old HE is shut down!!\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
delay(1000);
client.print("\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
delay(1000);
Serial.println(".");
client.print(".\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
delay(1000);
Serial.println("SMTP quit...");
client.print("quit\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
state = 2;
client.stop();
}
}
}
void genSigUp() {
state = 1;
}
void genSigDown() {
state = 0;
}