tugas iot — ESP32 simulation

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by lulussnbp01.

Components

Sketch code

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// ==================================================
// PIN CONFIGURATION
// ==================================================

#define PIR_PIN       27
#define DOOR_PIN      26
#define BUZZER_PIN    25

#define GREEN_LED     18
#define RED_LED       19

#define BUTTON_PIN    14


// ==================================================
// OLED CONFIGURATION
// ==================================================

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define OLED_ADDRESS 0x3C

Adafruit_SSD1306 display(
  SCREEN_WIDTH,
  SCREEN_HEIGHT,
  &Wire,
  OLED_RESET
);


// ==================================================
// SYSTEM STATE
// ==================================================

bool armed = false;
bool alarmActive = false;


// ==================================================
// SENSOR STATE
// ==================================================

bool lastMotionState = false;
bool lastDoorState = false;


// ==================================================
// BUTTON STATE
// ==================================================

int lastButtonState = HIGH;


// ==================================================
// OLED - NORMAL STATUS
// ==================================================

void showNormalStatus(
  bool systemArmed,
  bool motionDetected,
  bool doorOpen
) {

  display.clearDisplay();

  display.setTextColor(SSD1306_WHITE);

  // Header
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println("SMART SECURITY");

  // Separator
  display.drawLine(
    0, 10,
    127, 10,
    SSD1306_WHITE
  );

  // System
  display.setCursor(0, 17);
  display.print("SYSTEM : ");

  if (systemArmed) {
    display.println("ARMED");
  }
  else {
    display.println("OFF");
  }

  // PIR
  display.setCursor(0, 32);
  display.print("PIR    : ");

  if (motionDetected) {
    display.println("MOTION");
  }
  else {
    display.println("SAFE");
  }

  // Door
  display.setCursor(0, 47);
  display.print("DOOR   : ");

  if (doorOpen) {
    display.println("OPEN");
  }
  else {
    display.println("CLOSED");
  }

  display.display();
}


// ==================================================
// OLED - ALARM STATUS
// ==================================================

void showAlarmStatus(
  bool motionDetected,
  bool doorOpen
) {

  display.clearDisplay();

  display.setTextColor(SSD1306_WHITE);

  // Header
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println("SMART SECURITY");

  // Separator
  display.drawLine(
    0, 10,
    127, 10,
    SSD1306_WHITE
  );

  // Alarm title
  display.setTextSize(2);
  display.setCursor(22, 17);
  display.println("ALARM!");

  // Reason
  display.setTextSize(1);

  // Motion + Door
  if (motionDetected && doorOpen) {

    display.setCursor(0, 40);
    display.println("REASON:");

    display.setCursor(0, 53);
    display.println("MOTION + DOOR");
  }

  // Motion only
  else if