half duplex — ESP32 simulation

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

Components

Sketch code

//--------------------------------------------
// TRANSCEPTOR 1
// ESP32 + POTENCIÓMETRO + OLED
//
// ENVÍA:
//   Valor del potenciómetro
//
// RECIBE:
//   Roll  (X)
//   Pitch (Y)
//   Yaw   (Z)
//--------------------------------------------

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

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

//--------------------------------------------
// OLED
//--------------------------------------------

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

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

//--------------------------------------------
// PIN DEL POTENCIÓMETRO
//--------------------------------------------

#define POT_PIN 34

//--------------------------------------------
// MAC DEL TRANSCEPTOR 2
// ESP32 + MPU6050
//--------------------------------------------

uint8_t RxMACaddress[] = {
  0xA8,
  0x42,
  0xE3,
  0x5B,
  0x45,
  0xE4
};

//--------------------------------------------
// ESTRUCTURA PARA ENVIAR
// POTENCIÓMETRO
//--------------------------------------------

typedef struct {

  int potVal;

} PotData;

PotData sentData;

//--------------------------------------------
// ESTRUCTURA PARA RECIBIR
// MPU6050
//--------------------------------------------

typedef struct {

  float x;
  float y;
  float z;

} MPUData;

MPUData receivedData;

//--------------------------------------------
// CALLBACK DE RECEPCIÓN
//--------------------------------------------

void OnDataRecv(
  const esp_now_recv_info_t *esp_now_info,
  const uint8_t *incomingData,
  int len
) {

  if (len == sizeof(receivedData)) {

    memcpy(
      &receivedData,
      incomingData,
      sizeof(receivedData)
    );

  }

}

//--------------------------------------------
// CALLBACK DE ENVÍO
//--------------------------------------------

void OnDataSent(
  const wifi_tx_info_t *wifi_tx_info,
  esp_now_send_status_t status
) {

  if (status == ESP_NOW_SEND_SUCCESS) {

    Serial.println("Envio OK");

  } else {

    Serial.println("Fallo Envio");

  }

}

//--------------------------------------------
// SETUP
//--------------------------------------------

void setup() {

  Serial.begin(115200);

  delay(1000);

  //------------------------------------------
  // CONFIGURAR ADC
  //------------------------------------------

  analogReadResolution(12);

  //------------------------------------------
  // INICIAR I2C
  //------------------------------------------

  Wire.begin(21, 22);

  //------------------------------------------
  // INICIAR OLED
  //------------------------------------------

  if (
    !display.begin(
      SSD1306_SWITCHCAPVCC,
      0x3C
    )
  ) {

    Serial.println(
      "Fallo al iniciar OLED"
    );

    while (true);

  }

  //------------------------------------------
  // MENSAJE INICIAL
  //------------------------------------------

  display.clearDisplay();

  display.setTextColor(WHITE);

  display.setTextSize(1);

  display.setCurs

More projects by pol2z-polocote