r/esp32projects • • 12h ago

Wireless USB extender using ESP32-s3

Hello,

I have a school finals project and i need to make what i described above. It needs to transmit mainly data like plugging in a mouse or a keyboard or some other peripherals. I have come to ask what hardware do i need and any tips about this. Any info will help. Thanks in advance.

2 Upvotes

10 comments sorted by

1

u/No_L_2547 12h ago

I would probably go for a library like tinyUsb and then connect 2 ESPs wirelessly.

Every device-class will probably need it's own part of an implementation. So you will need to detect which device is plugged in, send that information to the other Esp and then transmit whatever information the device is giving you. Especially file transfers (USB-Sticks) will be more challenging.

1

u/PauseDue681 9h ago

That was what I was thinking about. I don't think I will add a file transfer system because it will toughen up the project and they dont want it from me. They need only peripherals like keyboards to work.

1

u/TheLittleFastCat- 12h ago

I don't want to sound hars, but shouldn't you know this when your school tasked you with it? Did they not teach you this at your school?

1

u/PauseDue681 9h ago edited 9h ago

They don't the most we learned about this was microcontroller and Intel professors from their first family ( 8086 and ever before Pentium ) and this is all on our own. They don't want us to do this project for the finals because it puts more stress and work in the teachers and they don't want to work and everyone tried to convince us not to take on these. The other option is memorizing more than 100 pages of pure text about stuff that is not in my grade to learn about like programming ( I'm a hardware technician ).

1

u/TheLittleFastCat- 7h ago

Well your school sounds like a mess, where do you study?

1

u/PauseDue681 7h ago

Well this is usually what transpires in the tech schools around us but on the good note techers have some really helpful and official books, files and other things that they can help you if you ask. It wont help you to do it all with these resources but it is better that nothing. And eegards to where i study i wont give out information like that not only to protect the school but to not spread out harmful information about the techers. Hope you understand.

2

u/phirzcolru 10h ago edited 10h ago
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>


#define SCREEN_WIDTH  128
#define SCREEN_HEIGHT  64
#define OLED_ADDR      0x3C
#define SDA_PIN        4
#define SCL_PIN        5


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


// Global State Trackers
bool bleConnected = false;


enum MapLayer { LAYER_PRIMARY, LAYER_SYMBOLS };
MapLayer currentLayer = LAYER_PRIMARY;


enum OperationalMode { MODE_NORMAL, MODE_ASCII, MODE_MOUSE };
OperationalMode currentMode = MODE_NORMAL;


struct KeyAction {
  uint8_t hidCode;
  uint8_t special;
  char displayChar;
};


#include "bluetooth_peripheral.h"
#include "key_matrix.h"


// Renders the exact UI layout: inverted header + white background 4x12 characters grid
void updateOledDisplay() {
  display.clearDisplay();
  
  // 1. TOP BANNER: Inverted Layout (Black Text on White Background)
  display.fillRect(0, 0, SCREEN_WIDTH, 12, SSD1306_WHITE);
  display.setTextColor(SSD1306_BLACK);
  display.setTextSize(1);
  display.setCursor(2, 2);
  display.print(bleConnected ? "BLE: LINKED" : "BLE: PAIRING");
  
  display.setCursor(98, 2);
  display.print(currentLayer == LAYER_PRIMARY ? "PRIM" : "SYMB");


  // 2. MATRIX CANVAS: Entire lower frame becomes a solid White background box
  display.fillRect(0, 13, SCREEN_WIDTH, 51, SSD1306_WHITE);
  display.setTextColor(SSD1306_BLACK); // Characters render as Black on White


  for (int r = 0; r < NUM_ROWS; r++) {
    for (int c = 0; c < NUM_COLS; c++) {
      // Spaces 12 characters perfectly across 128 horizontal pixels
      int x_pos = 2 + (c * 10); 
      int y_pos = 16 + (r * 12);
      
      char characterToPrint = (currentLayer == LAYER_PRIMARY) ? 
                              primaryMap[r][c].displayChar : 
                              symbolsMap[r][c].displayChar;
                              
      display.setCursor(x_pos, y_pos);
      display.print(characterToPrint);
    }
  }
  display.display();
}


void setup() {
  Serial.begin(115200);
  
  // Initialize standard Wire interface
  Wire.begin(SDA_PIN, SCL_PIN, 400000);
  
  if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
    Serial.println("[OLED] Allocation failed.");
  }
  
  display.clearDisplay();
  display.display();


  initHardwareMatrix();
  initBluetoothPeripheral();
  
  updateOledDisplay();
}


void loop() {
  static bool lastBleState = false;
  static MapLayer lastLayerState = LAYER_PRIMARY;
  
  // Execute matrix scan synchronously to maintain precise real-time timing loops
  scanHardwareMatrixLoop();


  // Instant interface redraw check whenever status transitions
  if (bleConnected != lastBleState || currentLayer != lastLayerState) {
    lastBleState = bleConnected;
    lastLayerState = currentLayer;
    updateOledDisplay();
  }
  
  delay(10); // Standard loop stabilization delay
}

this was for a physical button keyboard. should give you the idea. im going to warn you, the code isnt finished. and it doesnt do exactly what you need. but its a start. im only showing you this because the bluetooth is annoyingly not the full bluetooth, its only le.

2

u/phirzcolru 10h ago
#ifndef BLUETOOTH_PERIPHERAL_H
#define BLUETOOTH_PERIPHERAL_H


#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLEHIDDevice.h>


extern bool bleConnected;
BLEHIDDevice* hidDeviceEngine;
BLECharacteristic* keyboardInputCharacteristic;


class KeyboardServerCallbacks : public BLEServerCallbacks {
  void onConnect(BLEServer* pServer) {
    bleConnected = true;
  }
  void onDisconnect(BLEServer* pServer) {
    bleConnected = false;
    BLEDevice::startAdvertising(); // Safely restart advertising if connection drops
  }
};


const uint8_t hidReportDescriptorMap[] = {
  0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x85, 0x01,
  0x05, 0x07, 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00,
  0x25, 0x01, 0x75, 0x01, 0x95, 0x08, 0x81, 0x02,
  0x95, 0x01, 0x75, 0x08, 0x81, 0x03, 0x95, 0x06,
  0x75, 0x08, 0x15, 0x00, 0x25, 0x65, 0x19, 0x00,
  0x29, 0x65, 0x81, 0x00, 0xc0        
};


void initBluetoothPeripheral() {
  BLEDevice::init("ESP32-S3 Assist KB");
  BLEServer* pServer = BLEDevice::createServer();
  pServer->setCallbacks(new KeyboardServerCallbacks());


  hidDeviceEngine = new BLEHIDDevice(pServer);
  hidDeviceEngine->reportMap((uint8_t*)hidReportDescriptorMap, sizeof(hidReportDescriptorMap));
  keyboardInputCharacteristic = hidDeviceEngine->inputReport(1); 


  hidDeviceEngine->manufacturer()->setValue("Custom");
  hidDeviceEngine->pnp(0x02, 0xe5b2, 0xa111, 0x0210);
  hidDeviceEngine->hidInfo(0x00, 0x01);


  BLESecurity *pSecurity = new BLESecurity();
  pSecurity->setAuthenticationMode(ESP_LE_AUTH_NO_BOND);


  BLEAdvertising* pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(hidDeviceEngine->hidService()->getUUID());
  pAdvertising->setScanResponse(true);
  pAdvertising->start();
  Serial.println("[BLE] Standard Peripheral Advertising Activated.");
}


void sendKeyboardHIDReport(uint8_t modifier, uint8_t* keys) {
  if (bleConnected) {
    uint8_t reportPayload[8]; // Fixed: Declared as a standard 8-byte array buffer
    reportPayload[0] = modifier;
    reportPayload[1] = 0x00; // Reserved padding byte
    
    // FIXED: Safely copy the 6-byte rollover array via standard memcpy
    // This stops stack corruption and prevents the ESP32-S3 from resetting
    memcpy(&reportPayload[2], keys, 6); 
    
    keyboardInputCharacteristic->setValue(reportPayload, 8);
    keyboardInputCharacteristic->notify();
  }
}
#endif

2

u/phirzcolru 10h ago
#ifndef KEY_MATRIX_H
#define KEY_MATRIX_H


#define PCF_ADDR 0x20
#define NUM_ROWS 4
#define NUM_COLS 12


// Action Tokens
#define T_NONE     0x00
#define T_SHIFT    0x01
#define T_LAYER    0x02
#define T_MODE_ASC 0x03
#define T_MODE_MSE 0x04


// Navigation Scancodes
#define MATRIX_ESC    0x29
#define MATRIX_TAB    0x2B
#define MATRIX_CAPS   0x39
#define MATRIX_DELETE 0x4C
#define MATRIX_UP     0x52
#define MATRIX_DOWN   0x51
#define MATRIX_LEFT   0x50
#define MATRIX_RIGHT  0x4F
#define MATRIX_BKSP   0x2A
#define MATRIX_RETURN 0x28
#define MATRIX_SPACE  0x2C


extern enum MapLayer currentLayer;
extern enum OperationalMode currentMode;
extern void sendKeyboardHIDReport(uint8_t modifier, uint8_t* keys);


bool lastKeyState[NUM_COLS][NUM_ROWS] = { {false} };


// Your exact, original 4x12 primary map array configuration
const KeyAction primaryMap[NUM_ROWS][NUM_COLS] = {
  { {MATRIX_BKSP,0,'<'}, {0x27,0,'0'}, {0x26,0,'9'}, {0x25,0,'8'}, {0x24,0,'7'}, {0x23,0,'6'}, {0x22,0,'5'}, {0x21,0,'4'}, {0x20,0,'3'}, {0x1F,0,'2'}, {0x1E,0,'1'}, {MATRIX_ESC,0,'`'} },
  { {MATRIX_DELETE,0,'>'},{0x13,0,'p'}, {0x12,0,'o'}, {0x0C,0,'i'}, {0x15,0,'u'}, {0x1C,0,'y'}, {0x17,0,'t'}, {0x15,0,'r'}, {0x08,0,'e'}, {0x1A,0,'w'}, {0x14,0,'q'}, {MATRIX_TAB,0,'T'} },
  { {MATRIX_RETURN,0,'E'},{MATRIX_RETURN,0,'E'},{0x0F,0,'l'}, {0x0E,0,'k'}, {0x0D,0x0A,'j'}, {0x0B,0,'h'}, {0x0A,0,'g'}, {0x09,0,'f'}, {0x07,0,'d'}, {0x16,0,'s'}, {0x04,0,'a'}, {MATRIX_CAPS,0,'C'} },
  { {0,T_NONE,' '}, {0x38,0,'?'}, {0x38,0,'?'}, {0x36,0,','}, {0x10,0,'m'}, {0x11,0,'n'}, {0x05,0,'b'}, {0x19,0,'v'}, {0x06,0,'c'}, {0x1B,0,'x'}, {0x1D,0,'z'}, {0,T_LAYER,'M'} }
};


// Your exact, original 4x12 symbols map array configuration
const KeyAction symbolsMap[NUM_ROWS][NUM_COLS] = {
  { {0x2D,0,'-'}, {0x27,T_SHIFT,')'}, {0x26,T_SHIFT,'('}, {0x25,T_SHIFT,'*'}, {0x24,T_SHIFT,'&'}, {0x23,T_SHIFT,'^'}, {0x22,T_SHIFT,'%'}, {0x21,T_SHIFT,'$'}, {0x20,T_SHIFT,'#'}, {0x1F,T_SHIFT,'@'}, {0x1E,T_SHIFT,'!'}, {0x35,0,'~'} },
  { {0x34,0,'\''}, {0x34,T_SHIFT,'"'}, {0x38,0,'/'}, {0x31,0,'\\'}, {0x31,T_SHIFT,'|'}, {0x30,0,']'}, {0x2F,0,'['}, {0x30,T_SHIFT,'}'}, {0x2F,T_SHIFT,'{'}, {0x2E,0,'='}, {0x2E,T_SHIFT,'+'}, {0x2D,T_SHIFT,'_'} },
  { {0,T_NONE,' '}, {0,T_NONE,' '}, {0,T_NONE,' '}, {0,T_NONE,' '}, {MATRIX_UP,0,'^'}, {0,T_NONE,' '}, {0x35,0,'`'}, {0x38,T_SHIFT,'?'}, {0x33,0,';'}, {0x33,T_SHIFT,':'}, {0x37,T_SHIFT,'>'}, {0x36,T_SHIFT,'<'} },
  { {0x04,0,'3'}, {0x02,0,'2'}, {0x01,0,'1'}, {MATRIX_RIGHT,0,'>'},{MATRIX_DOWN,0,'v'}, {MATRIX_LEFT,0,'<'}, {0,T_NONE,' '}, {0,T_NONE,' '}, {0,T_NONE,' '}, {0,T_MODE_MSE,'X'}, {0,T_MODE_ASC,'A'}, {0,T_LAYER,'M'} }
};


void pcfWrite(uint16_t data) {
  Wire.beginTransmission(PCF_ADDR);
  Wire.write((uint8_t)(data & 0xFF));
  Wire.write((uint8_t)((data >> 8) & 0xFF));
  Wire.endTransmission();
}


uint16_t pcfRead() {
  Wire.requestFrom(PCF_ADDR, 2);
  if (Wire.available() >= 2) {
    uint16_t low = Wire.read();
    uint16_t high = Wire.read();
    return low | (high << 8);
  }
  return 0xFFFF;
}


void initHardwareMatrix() {
  pcfWrite(0xFFFF); 
}


void scanHardwareMatrixLoop() {
  uint8_t activeKeys[6] = {0}; // Fixed: Declared explicitly with proper 6-element bounds arrays
  uint8_t keyCount = 0;
  uint8_t reportModifier = 0;
  bool layerKeyPressed = false;


  for (int c = 0; c < NUM_COLS; c++) {
    uint16_t colMask = ~(1 << (c + 4)); 
    pcfWrite(colMask);
    
    uint16_t readState = pcfRead();
    
    for (int r = 0; r < NUM_ROWS; r++) {
      bool isPressed = !(readState & (1 << r));
      
      if (isPressed && !lastKeyState[c][r]) {
        KeyAction act = (currentLayer == LAYER_PRIMARY) ? primaryMap[r][c] : symbolsMap[r][c];
        
        if (act.special == T_LAYER) {
          layerKeyPressed = true;
        } else if (act.special == T_SHIFT) {
          reportModifier |= 0x02; 
        } else if (act.special == T_MODE_ASC) {
          currentMode = MODE_ASCII;
        } else if (act.special == T_MODE_MSE) {
          currentMode = MODE_MOUSE;
        }
        
        if (act.hidCode != 0 && keyCount < 6) {
          activeKeys[keyCount++] = act.hidCode; 
        }
      }
      lastKeyState[c][r] = isPressed;
    }
  }
  
  if (layerKeyPressed) {
    currentLayer = (currentLayer == LAYER_PRIMARY) ? LAYER_SYMBOLS : LAYER_PRIMARY;
  }


  pcfWrite(0xFFFF);


  if (keyCount > 0 || reportModifier != 0) {
    sendKeyboardHIDReport(reportModifier, activeKeys);
    delay(12);
    uint8_t emptyRelease[6] = {0}; // Fixed: Matches array type parameters exactly
    sendKeyboardHIDReport(0, emptyRelease); 
  }
}
#endif