Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By swilson
#13910 OK, I finally got it to working rather nicely using interrupts now. As you will see in the code I am using gpio4 as input and interrupts on HIGH. I am using gpio5 as input and interrupts on LOW. Those 2 gpio's are tied together. The gpio's go to a relay which is connected to LOW when the relay is off and connected to HIGH (3.3V) when the relay is on. I am feeding the HIGH and LOW signals to the relay from the esp-12 itself. The relay is turned on and off by another relay. The first relay sees a slow voltage rise due to the generator engine starting up and generating finally 14V with a slow rise. It also drops to 0V slowly when the engine shuts off. In that regard I had to add the first relay straight to the 14V and GRD from the engine. The GRD on that relay also connects to 30 (common) and the grd to relay #2 connects to 87 (normally open). When the first relay activates it activates the second relay which causes the esp-12 to see HIGH. When the relay goes off the ESP-12 sees LOW. I know this is a crude, and expensive (relays aren't cheap) way of doing this but I hope this cuts out any chance of false triggers or noise. Had to add a diode across the positive and negative of relay 2's coil to minimize relay shut-off backfeeding and restarting the esp. In the picture assume the symbol to the far right is the generator.

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?

Code: Select all// 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;
}


generator-layout.jpg
You do not have the required permissions to view the files attached to this post.