openlab — ESP32 simulation

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

Components

Sketch code

#include <Wire.h>

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>

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


// =====================================
// SLAVE IDENTIFICATION
// =====================================

#define SLAVE_ID 1


// =====================================
// FAN 1 RPM
// =====================================

#define FAN1_RPM_PIN 34


// =====================================
// FAN 2 RPM
// =====================================

#define FAN2_RPM_PIN 35


// =====================================
// I2C PINS
// =====================================

#define SDA_PIN 21
#define SCL_PIN 22


// =====================================
// OLED
// =====================================

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

#define OLED_ADDRESS 0x3C


Adafruit_SSD1306 display(
  SCREEN_WIDTH,
  SCREEN_HEIGHT,
  &Wire,
  -1
);


// =====================================
// MPU6050
// =====================================

// Fan 1 → 0x68
Adafruit_MPU6050 mpu1;

// Fan 2 → 0x68
Adafruit_MPU6050 mpu2;


// =====================================
// FAN VARIABLES
// =====================================

int rpm1 = 0;
int rpm2 = 0;

float vibration1 = 0;
float vibration2 = 0;

String condition1 = "NORMAL";
String condition2 = "NORMAL";


unsigned long lastUpdate = 0;

const unsigned long UPDATE_INTERVAL = 1000;


// =====================================
// CALCULATE VIBRATION
// =====================================

float calculateVibration(
  sensors_event_t &accel
)
{
  float ax = accel.acceleration.x;
  float ay = accel.acceleration.y;
  float az = accel.acceleration.z;

  float magnitude =
    sqrt(
      ax * ax +
      ay * ay +
      az * az
    );

  float vibration =
    fabs(magnitude - 9.81);

  vibration =
    vibration / 9.81;

  return vibration;
}


// =====================================
// SETUP
// =====================================

void setup()
{
  Serial.begin(115200);


  // -----------------------------
  // I2C
  // -----------------------------

  Wire.begin(
    SDA_PIN,
    SCL_PIN
  );


  // -----------------------------
  // RPM INPUTS
  // -----------------------------

  pinMode(
    FAN1_RPM_PIN,
    INPUT
  );

  pinMode(
    FAN2_RPM_PIN,
    INPUT
  );


  // -----------------------------
  // FAN 1 MPU6050
  // Address = 0x68
  // -----------------------------

  if (!mpu1.begin(0x68))
  {
    Serial.println(
      "FAN 1 MPU6050 NOT FOUND"
    );

    while (1)
    {
      delay(100);
    }
  }

  Serial.println(
    "FAN 1 MPU6050 FOUND"
  );


  // -----------------------------
  // FAN 2 MPU6050
  // Address = 0x68
  // TEST MODE
  // -----------------------------

  if (!mpu2.begin(0x68))
  {
    Serial.println(
      "FAN 2 MPU6050 NOT FOUND"
    );

    while (1)
    {
      delay(100);
    }
  }

  Serial.println(
    "FAN 2 MPU6050 FOUND"
  );


  // -----------------------------
  // SENSOR SETTINGS
  // -----------------------------

  mpu1.setAcceler