1 — ESP32 simulation

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

Components

Sketch code

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

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
RTC_DS3231 rtc;

#define PIR_PIN 5
#define LED_PIN 2
bool prevMotion = false;
unsigned long detections = 0;

#define BTN_PIN 19
#define CALL_BTN_PIN 18
bool showCallMsg = false;
unsigned long callMsgTimer = 0;
const unsigned long CALL_MSG_DUR = 3000;

// ====== 第二块OLED(0x3D) + HELP按键 ======
#define SCREEN2_WIDTH 128
#define SCREEN2_HEIGHT 64
Adafruit_SSD1306 display2(SCREEN2_WIDTH, SCREEN2_HEIGHT, &Wire, -1);
#define HELP_BTN_PIN 23
bool showHelpMsg = false;
unsigned long helpMsgTimer = 0;
const unsigned long HELP_MSG_DUR = 3000;

bool isOut = false;
int userEvtHour = 0;
int userEvtMinute = 0;
bool showUserEvent = false;
unsigned long userEvtTimer = 0;
const unsigned long USER_EVT_DURATION = 3000;

const int pinTV = 34;
const int pinCook = 33;
const int TV_VOLT_THRESHOLD = 1065;
const int COOK_VOLT_THRESHOLD = 2200;
const int averageValue = 10;

#define MAX_EVENT_COUNT 4
struct Event {
  DateTime time;
  String msg;
};
Event eventList[MAX_EVENT_COUNT];
int eventIndex = 0;

// ========== 语音模块代码 ==========
struct Command { uint8_t code; const char* reply; };
const Command COMMANDS[] = {
  {0x00, "Hi, how can I help?"},
  {0x01, "see you later"},
  {0x03, "ok, turn on the air conditioner"},
  {0x04, "ok, turn off the air conditioner"},
  {0x0C, "call for help"},
  {0x22, "ok, start to fan"},
  {0x23, "ok, stop to fan"},
  {0x27, "ok, turn on the light"},
  {0x28, "ok, turn off the light"},
  {0x2B, "ok, warm light turn on"},
  {0x2C, "ok, warm light turn off"},
};
String voiceText = "";
bool showVoiceMsg = false;
unsigned long voiceMsgTimer = 0;
const unsigned long VOICE_MSG_DUR = 3000;
// ======================================

void addEvent(String message){
  eventList[eventIndex].time = rtc.now();
  eventList[eventIndex].msg = message;
  eventIndex = (eventIndex + 1) % MAX_EVENT_COUNT;
}

float readSigVoltage(int pin)
{
  long sum = 0;
  for (int i = 0; i < averageValue; i++) {
    sum += analogRead(pin);
    delay(2);
  }
  float voltage = (sum / (float)averageValue) * 3300.0 / 4095.0;
  return voltage;
}

bool lastTvState = false;
bool lastCookState = false;

void setup() {
  Serial.begin(115200);
  Serial2.begin(115200, SERIAL_8N1, 16, 17);
  Wire.begin(21, 22);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("SSD1306 not found!");
    while (true) delay(10);
  }
  if (!display2.begin(SSD1306_SWITCHCAPVCC, 0x3D)) {
    Serial.println("SSD1306 screen2 not found!");
  }
  display2.clearDisplay();
  display2.display();

  if (!rtc.begin()) {
    Serial.println("DS3231 RTC NOT FOUND!");
  }

  pinMode(PIR_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  pinMode(BTN_PIN, INPUT_PULLUP);
  pinMode(CALL_BTN_PIN, INPUT_PULLUP);
  pinMode(HELP_BTN_PIN, INPUT_PULLUP);

  addEvent("System Start");
  display.clearDisplay();
  displ