ESP — ESP32-S3 simulation

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

Components

Sketch code

#include <WiFi.h>
#include <esp_now.h>
#include <esp_wifi.h>
#include <Wire.h>

// =====================================================
// ESP32-S3 MPU6050 ESP-NOW SENDER
// =====================================================

// ---------- ESP-NOW SETTINGS ----------
#define ESPNOW_CHANNEL 1

// Receiver Custom MAC
uint8_t receiverMAC[] = {
  0x02, 0x00, 0x00, 0x12, 0x34, 0x57
};

// Sender Custom MAC
uint8_t senderMAC[] = {
  0x02, 0x00, 0x00, 0x12, 0x34, 0x56
};

// ---------- MPU6050 ----------
#define MPU6050_ADDR 0x68

#define SDA_PIN 8
#define SCL_PIN 9

// ---------- DATA STRUCTURE ----------
typedef struct __attribute__((packed)) {

  uint8_t header1;
  uint8_t header2;

  float accelX;
  float accelY;
  float accelZ;

  float gyroX;
  float gyroY;
  float gyroZ;

  float temperature;

  uint8_t channel;

} SensorData;

SensorData sensorData;

// ---------- TIMING ----------
unsigned long lastSendTime = 0;
const unsigned long sendInterval = 1000;

// =====================================================
// READ MPU6050
// =====================================================

bool readMPU6050()
{
  Wire.beginTransmission(MPU6050_ADDR);
  Wire.write(0x3B);

  if (Wire.endTransmission(false) != 0)
  {
    return false;
  }

  Wire.requestFrom(MPU6050_ADDR, 14);

  if (Wire.available() < 14)
  {
    return false;
  }

  int16_t accelXRaw = Wire.read() << 8 | Wire.read();
  int16_t accelYRaw = Wire.read() << 8 | Wire.read();
  int16_t accelZRaw = Wire.read() << 8 | Wire.read();

  int16_t temperatureRaw = Wire.read() << 8 | Wire.read();

  int16_t gyroXRaw = Wire.read() << 8 | Wire.read();
  int16_t gyroYRaw = Wire.read() << 8 | Wire.read();
  int16_t gyroZRaw = Wire.read() << 8 | Wire.read();

  sensorData.accelX = accelXRaw / 16384.0;
  sensorData.accelY = accelYRaw / 16384.0;
  sensorData.accelZ = accelZRaw / 16384.0;

  sensorData.gyroX = gyroXRaw / 131.0;
  sensorData.gyroY = gyroYRaw / 131.0;
  sensorData.gyroZ = gyroZRaw / 131.0;

  sensorData.temperature =
      (temperatureRaw / 340.0) + 36.53;

  return true;
}

// =====================================================
// SEND CALLBACK
// =====================================================

void OnDataSent(const wifi_tx_info_t *info,
                esp_now_send_status_t status)
{
  Serial.print("ESP-NOW SEND STATUS: ");

  if (status == ESP_NOW_SEND_SUCCESS)
  {
    Serial.println("SUCCESS");
  }
  else
  {
    Serial.println("FAILED");
  }
}

// =====================================================
// SET CUSTOM MAC
// =====================================================

bool setCustomMAC()
{
  esp_err_t result =
      esp_wifi_set_mac(WIFI_IF_STA, senderMAC);

  if (result != ESP_OK)
  {
    Serial.print("ERROR SETTING SENDER MAC: ");
    Serial.println(result);
    return false;
  }

  return true;
}

// =====================================================
// SET WIFI CHANNEL
// =====================================================

bool setWiFiChannel(uint8_t channel)
{

More projects by ahmedabdelrahmanahmedfouad