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 <Arduino.h>
#include <Wire.h>

#define MPU_SDA 8
#define MPU_SCL 9
#define MPU_ADDR 0x68

// تحديد أرجُل الـ Serial2 الخاصة بالـ Patient
#define TX_PIN 17
#define RX_PIN 16

typedef struct __attribute__((packed)) {
  float accelX;
  float accelY;
  float accelZ;
  float temperature;
  uint8_t currentChannel;
} PatientData;

PatientData patientData;

bool writeMPU6050(uint8_t reg, uint8_t value) {
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(reg);
  Wire.write(value);
  return Wire.endTransmission() == 0;
}

bool readMPU6050(float &ax, float &ay, float &az, float &temp) {
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x3B);
  if (Wire.endTransmission(false) != 0) return false;

  if (Wire.requestFrom((uint8_t)MPU_ADDR, (uint8_t)14) != 14) return false;

  int16_t rawAx   = (Wire.read() << 8) | Wire.read();
  int16_t rawAy   = (Wire.read() << 8) | Wire.read();
  int16_t rawAz   = (Wire.read() << 8) | Wire.read();
  int16_t rawTemp = (Wire.read() << 8) | Wire.read();

  for (int i = 0; i < 6; i++) Wire.read();

  ax = rawAx / 16384.0f;
  ay = rawAy / 16384.0f;
  az = rawAz / 16384.0f;
  temp = (rawTemp / 340.0f) + 36.53f;
  return true;
}

void setup() {
  Serial.begin(115200);
  
  // تهيئة الـ Serial2 بأرجُل Patient (RX=16, TX=17)
  Serial2.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);

  Wire.begin(MPU_SDA, MPU_SCL);
  writeMPU6050(0x6B, 0x00);

  Serial.println("PATIENT READY (TX: Pin 17)");
}

void loop() {
  float ax = 0, ay = 0, az = 0, temp = 25.0f;

  if (!readMPU6050(ax, ay, az, temp)) {
    ax = 1.0f; ay = 2.0f; az = 3.0f; temp = 36.5f;
  }

  patientData.accelX = ax;
  patientData.accelY = ay;
  patientData.accelZ = az;
  patientData.temperature = temp;
  patientData.currentChannel = 1;

  // إرسال الـ Struct عبر السلك
  Serial2.write((uint8_t*)&patientData, sizeof(patientData));
  
  Serial.println("Data Sent via Wire!");
  delay(2000);
}

More projects by ahmedabdelrahmanahmedfouad