Smart device to control many things — Dfrobot Firebeetle2 Esp32S3 simulation

An interactive Dfrobot Firebeetle2 Esp32S3 circuit simulation you can run free in your browser on Velxio, by soniyashvi966.

Components

Sketch code

#include <WiFi.h>
#include <esp_now.h>
#include <esp_wifi.h>
#include <ESP32Servo.h>
#include <Adafruit_NeoPixel.h>
#include <math.h>

// -------- GPIO PINS --------
#define MQ2_PIN    4
#define FAN_PIN    5
#define SOIL_PIN   6
#define SERVO_PIN  3
#define LED_PIN    8

// -------- WS2812 --------
#define LED_COUNT 20

Adafruit_NeoPixel strip(
  LED_COUNT,
  LED_PIN,
  NEO_GRB + NEO_KHZ800
);

// -------- SERVO --------
Servo myServo;

// -------- SENSOR SETTINGS --------
const int MQ_ON_THRESHOLD  = 1800;
const int MQ_OFF_THRESHOLD = 1600;

const int SOIL_DRY = 3000;
const int SOIL_WET = 1500;
const int SOIL_LOW_LIMIT = 30;

// -------- ESP-NOW PACKET --------
typedef struct __attribute__((packed)) {
  uint8_t type;
  uint8_t servoOn;
  uint8_t ledOn;
  uint8_t fanOn;
  uint8_t soilLow;
  uint16_t mqRaw;
  uint8_t soilPercent;
} VelxioPacket;

uint8_t broadcastAddress[] = {
  0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF
};

VelxioPacket sendPacket;

// -------- COMMAND VARIABLES --------
volatile bool servoCommand = false;
volatile bool ledCommand = false;

bool servoState = false;
bool ledState = false;
bool fanState = false;

// -------- SENSOR VARIABLES --------
int mqRaw = 0;
int soilRaw = 0;
int soilPercent = 0;
bool soilLow = false;

// -------- RGB BREATHING --------
unsigned long lastLedUpdate = 0;

const uint8_t colors[][3] = {
  {255, 0, 0},
  {0, 255, 0},
  {0, 0, 255},
  {180, 0, 255},
  {0, 255, 255},
  {255, 100, 0}
};

const int COLOR_COUNT = 6;

// -------- RECEIVE COMMANDS --------
void onDataRecv(
  const esp_now_recv_info_t *info,
  const uint8_t *data,
  int len
) {
  Serial.print("RIGHT RECEIVED PACKET. Length: ");
  Serial.println(len);

  if (len != sizeof(VelxioPacket)) {
    Serial.println("PACKET SIZE ERROR");
    return;
  }

  VelxioPacket received;
  memcpy(&received, data, sizeof(received));

  Serial.print("PACKET TYPE: ");
  Serial.println(received.type);

  if (received.type == 1) {

    servoCommand = received.servoOn;
    ledCommand = received.ledOn;

    Serial.print("SERVO COMMAND: ");
    Serial.println(received.servoOn);

    Serial.print("RGB COMMAND: ");
    Serial.println(received.ledOn);
  }
}

// -------- RGB BREATHING --------
void updateBreathing() {

  if (!ledState) return;

  unsigned long now = millis();

  if (now - lastLedUpdate < 20) return;
  lastLedUpdate = now;

  float phase = (now % 4000) / 4000.0;

  float wave =
    (1.0 - cosf(phase * 6.2831853)) / 2.0;

  int brightness = (int)(wave * 255);

  int colorIndex = (now / 4000) % COLOR_COUNT;

  int r = colors[colorIndex][0] * brightness / 255;
  int g = colors[colorIndex][1] * brightness / 255;
  int b = colors[colorIndex][2] * brightness / 255;

  uint32_t color = strip.Color(r, g, b);

  for (int i = 0; i < LED_COUNT; i++) {
    strip.setPixelColor(i, color);
  }

  strip.show();
}

// -------- SENSOR READING --------
void readSensors() {

  mqRaw = analogRead(MQ2_PIN);
  soilRaw = analogRead(SOIL_PIN);

  soilPercent = map(
    soilRaw,
    S

More projects by soniyashvi966