HACKTHONE — ESP32 simulation

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

Components

Sketch code

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

// =================================================
// PINS
// =================================================

#define SDA_PIN 18
#define SCL_PIN 33

// Water level sensor
#define WATER_SENSOR_ADDR 0x77

// DHT22
#define DHT_PIN 26
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);

// HC-SR04
#define TRIG_PIN 32
#define ECHO_PIN 25

// RGB LED
#define RED_PIN 22
#define GREEN_PIN 21
#define BLUE_PIN 19

// Buzzer
#define BUZZER_PIN 4

// GPS
#define GPS_RX 16
#define GPS_TX 17

HardwareSerial GPSserial(2);
TinyGPSPlus gps;

// OLED
Adafruit_SSD1306 display(128, 64, &Wire, -1);

// =================================================
// SETTINGS
// =================================================

// Your water sensor:
// ~256 = no water
// ~41800 = water detected

#define FLOOD_WATER_VALUE 40000

// Ultrasonic warning distance
// Water/object closer than this = WARNING
#define WARNING_DISTANCE 50.0

// =================================================
// VARIABLES
// =================================================

int waterLevel = 0;

float distance = -1;

float temperature = NAN;
float humidity = NAN;

String status = "SAFE";

// =================================================
// WATER SENSOR
// =================================================

int readWaterLevel() {

  Wire.beginTransmission(WATER_SENSOR_ADDR);
  Wire.write(0x00);
  Wire.endTransmission();

  Wire.requestFrom(WATER_SENSOR_ADDR, 2);

  if (Wire.available() >= 2) {

    int value = (Wire.read() << 8) | Wire.read();

    return value;
  }

  return -1;
}

// =================================================
// ULTRASONIC SENSOR
// =================================================

float readDistance() {

  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);

  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);

  digitalWrite(TRIG_PIN, LOW);

  long duration = pulseIn(ECHO_PIN, HIGH, 30000);

  if (duration == 0) {
    return -1;
  }

  return duration * 0.0343 / 2.0;
}

// =================================================
// RGB LED
// =================================================

void setRGB(bool red, bool green, bool blue) {

  digitalWrite(RED_PIN, red);
  digitalWrite(GREEN_PIN, green);
  digitalWrite(BLUE_PIN, blue);
}

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

void showDisplay() {

  display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);

  display.setCursor(0, 0);

  display.println("FLOOD MONITOR");

  display.print("Water: ");
  display.println(waterLevel);

  display.print("Dist: ");

  if (distance < 0) {
    display.println("--");
  }
  else {
    display.print(distance, 1);
    display.println(" cm");
  }

  display.print("Status: ");
  display.println(status);

  display.print("Temp: ");

  if (isnan(temperature)) {
    displa