thesis1 — ESP32 simulation

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

Components

Sketch code

// microSD card over SPI (ESP32, VSPI default)
// Wiring: CS->5  MOSI->23  MISO->19  SCK->18  VCC->3V3  GND->GND

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc;

// Pins
int btn = 13;
int printLogs = 12;

// Variables
bool btnLastState = false;
bool bpLastState = false;
bool allowScan = false;
String currSection = "";

// Debug Function
void printFileContent(const char* filename) {
  File file = SD.open(filename, FILE_READ);
  if (file) {
    Serial.println("--- Reading " + String(filename) + " ---");
    while (file.available()) {
      Serial.write(file.read());
    }
    Serial.println("\n--- End of file ---");
    file.close();
  }
}

// Time to minutes
unsigned int timeToMinutes(String time) {
  int hours = time.substring(0, time.indexOf(':')).toInt();
  int mins = time.substring(time.indexOf(':') + 1).toInt();
  return (hours*60) + mins;
}

void setup() {
  Serial.begin(115200);
  pinMode(btn, INPUT_PULLUP);
  pinMode(printLogs, INPUT_PULLUP);

// SD card
  Serial.print("Initializing SD card...");
  if (!SD.begin(5)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

// RTC
  Wire.begin();
  if (!rtc.begin()) {
    Serial.println("RTC not detected!");
    return;
  }
  Serial.println("RTC detected!");
  // Set the time only when necessary.
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  Serial.println("RTC setup complete!");
}

void loop() {
  DateTime now = rtc.now(); // Get time
  if (Serial.available()) { // Temporary replacement for QR scanner
    String strData = Serial.readStringUntil('\n');
    strData.trim();

    if (allowScan) {
      String newContent = "";
      String newLogContent = "";
      char timestamp[20];
      char toolTime[5];

      sprintf(timestamp, "%02d/%02d/%04d | %02d:%02d",
              now.day(),
              now.month(),
              now.year(),
              now.hour(),
              now.minute());
      sprintf(toolTime, "%02d-%02d",
              now.month(),
              now.day());
      bool found = false;

    // Update tool status
      File myFile = SD.open("/tools.txt", FILE_READ);
      if (myFile) {
        while (myFile.available()) {
          String line = myFile.readStringUntil('\n');
          line.trim();

        // Separate lines
          char buffer[100];
          line.toCharArray(buffer, sizeof(buffer));
          char *tool = strtok(buffer, ","); String item = tool;
          char *itemStatus = strtok(NULL, ","); String status = itemStatus;
          char *borrowedDate = strtok(NULL, ","); String date = date;
          char *itemCondition = strtok(NULL, ","); String condition = itemCondition;
          char *classSection = strtok(NULL, ","); String section = classSection;
        
        // 
          if (item.equals(strData)) {
            found = true;
            if (status == "in") {
              status = "out";
              Serial.println(item + " is now out");
            

More projects by aleinliwag