Code for Buzzer

The following is the basic outline of the code in use. However, there a few improvements that need to be made. This code was written when there was only the TPMS using the buzzer, since then the system has expanded to other alerts. So, the code needs to be cleaned up a bit. Also, the feature to chirp the alarm five times is coded in node-red, and instead it needs to be moved to this code. Now, just need to find the time to make these changes.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

#ifndef STASSID
#define STASSID "Molly42"
#define STAPSK  "*******"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;
const char* deviceName = "buzzer";

//Static IP address configuration
IPAddress staticIP(192, 168, 0, 245);    //ESP static ip
IPAddress gateway(192, 168, 0, 1);       //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0);      //Subnet mask
IPAddress dns(10, 0, 1, 1);              //DNS

ESP8266WebServer server(80);

const char INDEX_HTML[] =
  "<!DOCTYPE HTML>"
  "<html>"
  "<head>"
  "<style>"
    ".button {"
    "border: none;"
    "color: white;"
    "padding: 15px 32px;"
    "text-align: center;"
    "text-decoration: none;"
    "display: inline-block;"
    "font-size: 64px;"
    "width: 100%;"
    "margin: 4px 2px;"
    "cursor: pointer;"
  "}"
  ".button1 {background-color: #55723A;}"
  ".button2 {background-color: #677821;}"
  ".button3 {background-color: #304529;}"
  ".button4 {background-color: #5D7B53;}"
  ".button5 {background-color: #012217;}"
  "</style>"
  "</head>"
  "<body>"
    "<h1>ESP8266 Test Relay for Molly42</h1>"
    "<br>"
    "<button class=\"button button2\" onclick=\"document.location='/ledon'\">LED On</button>"
    "<br>"
    "<button class=\"button button3\" onclick=\"document.location='/ledoff'\">LED Off</button>"
    "<br>"
    "<br>" 
    "<br>"
    "<button class=\"button button4\" onclick=\"document.location='/relayon'\">Relay On</button>"
    "<br>"
    "<button class=\"button button5\" onclick=\"document.location='/relayoff'\">Relay Off</button>"
  "</body>"
  "</html>";

// Variables will change:
int lastAlarmState = LOW;                         // the current state of the alarm

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastAlarmTime = 0;                  // the last time the alarm was triggered
unsigned long lastCommsTime = 0;                  // the last time we received an alarm message
unsigned long alarmTimeout = 60000;               // timeout to retrigger alarm (1 min)
unsigned long commsTimeout = 600000;              // trigger alarm if no messages received (10 Mins)


void handleRoot() {
  server.send(200, "text/html", INDEX_HTML);
}

void handleNotFound() {
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  message += "\nValid URLs are";
  message += "\n/status, /tirealarm=[on|off], /relayon, /relayoff, /ledon, /ledoff";
  
  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
}

void setup(void) {
  pinMode(LED_BUILTIN, OUTPUT);          // Initialize the LED_BUILTIN pin as an output
  digitalWrite(LED_BUILTIN, HIGH);       // Turn the LED off (Note that HIGH is the voltage level
                                         // but actually the LED is off; this is because
                                         // it is active low on the ESP-01)
  pinMode(D1, OUTPUT);                   // Initialize the D1 pin as an output
  digitalWrite(D1, LOW);                 // Turn the Relay on, noise off, wired to normally closed
  Serial.begin(57600);
  WiFi.hostname(deviceName);             // DHCP Hostname (useful for finding device for static lease)
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);

  server.on("/ledon", []() {
    digitalWrite(LED_BUILTIN, LOW);   // Turn the LED on by making the voltage LOW
    server.send(200, "text/html", INDEX_HTML);
  });

  server.on("/ledoff", []() {
    digitalWrite(LED_BUILTIN, HIGH);  // Turn the LED off by making the voltage HIGH
    server.send(200, "text/html", INDEX_HTML);
  });

  server.on("/relayon", []() {
    digitalWrite(D1, HIGH);           // Turn the D5 off by making the voltage HIGH
    server.send(200, "text/html", INDEX_HTML);
  });

  server.on("/relayoff", []() {
    digitalWrite(D1, LOW);            // Turn the D5 off by making the voltage LOW
    server.send(200, "text/html", INDEX_HTML);
  });

    server.on("/status", []() {
    String statusMessage = "Received Message\n\n";
    statusMessage += "URI: ";
    statusMessage += server.uri();
    statusMessage += "\nMethod: ";
    statusMessage += (server.method() == HTTP_GET) ? "GET" : "POST";
    statusMessage += "\nTime : ";
    statusMessage += millis();
    statusMessage += "\nLast Alatm Time : ";
    statusMessage += lastAlarmTime;
    statusMessage += "\nAlarm Timeout : ";
    statusMessage += alarmTimeout;
    statusMessage += "\nLast Message Received Time : ";
    statusMessage += lastCommsTime;
    statusMessage += "\nComms Timeout : ";
    statusMessage += commsTimeout;
    statusMessage += "\nAlarm State : ";
    statusMessage += lastAlarmState;

    server.send(200, "text/plain", statusMessage);
  });

  server.on("/tirealarm", []() {

    server.send(200, "text/plain", "OK");
    lastCommsTime = millis();

    if (server.arg(0)=="yes") {
       Serial.println("\nReceived a Tire Pressure Alarm");

       if (millis() - lastAlarmTime > alarmTimeout) {
         Serial.println("\nThe previous alarm has timed out");
         lastAlarmState=LOW;
       }

       if (lastAlarmState==LOW) {
         Serial.println("\nWe have a new alarm");
         lastAlarmState = HIGH;
         lastAlarmTime = millis();

         // Action the alarm by sounding the buzzer
         digitalWrite(D1, HIGH);          // Turn the Relay on by making the voltage HIGH
         digitalWrite(LED_BUILTIN, LOW);  // Turn the LED on by making the voltage HIGH
         delay(500);
         digitalWrite(D1, LOW);           // Turn the Relay off by making the voltage LOW
       }
    }

    if (server.arg(0)=="no") {
       lastAlarmState = LOW;
       digitalWrite(LED_BUILTIN, HIGH);  // Turn the LED off by making the voltage LOW
    }
   });

  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");
}

void loop(void) {
  server.handleClient();
  MDNS.update();
  
       if (millis() - lastCommsTime > commsTimeout) {
         Serial.println("\nNo messages received for the last 10 Minutes");
         lastCommsTime = millis();
         
         // Action the alarm by sounding the buzzer
         digitalWrite(D1, HIGH);          // Turn the Relay on by making the voltage HIGH
         digitalWrite(LED_BUILTIN, LOW);  // Turn the LED on by making the voltage HIGH
         delay(250);
         digitalWrite(D1, LOW);           // Turn the Relay off by making the voltage LOW
       }
}