How to connect USB keyboard to ESP32?

User avatar
HermannSW
Posts: 97
Joined: Fri Oct 27, 2017 6:58 am
Location: Eberbach, Germany
Contact:

How to connect USB keyboard to ESP32?

Postby HermannSW » Wed Sep 05, 2018 10:16 pm

I have a ESP32 module with OLED display.
I want to connect USB keyboard to ESP32 and use MicroPyhon repl with that display and keyboard.
How can USB keyboard be connected to ESP32 module and utilized?
Image

ESP_Sprite
Posts: 8922
Joined: Thu Nov 26, 2015 4:08 am

Re: How to connect USB keyboard to ESP32?

Postby ESP_Sprite » Thu Sep 06, 2018 2:50 am

That's somewhat more difficult than you may think, as the ESP32 does not have any USB hardware. (The USB port on your devboard is provided via a hardware USB-to-serial-port chip.) Using an USB keyboard would either mean using an external USB host chip (like the MAX3421) and implementing the stack to use this in software, or hoping the keyboard still supports PS/2 signaling and bitbanging that protocol.

User avatar
HermannSW
Posts: 97
Joined: Fri Oct 27, 2017 6:58 am
Location: Eberbach, Germany
Contact:

Re: How to connect USB keyboard to ESP32?

Postby HermannSW » Fri Sep 07, 2018 7:13 am

Thanks for confirming that adding keyboard is not easy.the Pi ZeroW

I do have some 4x4 matrix keyboards, and found MicroPython code to deal with:
https://github.com/BrendanSimon/micropy ... ter/keypad

The problem for my application is that the MicroPython I have flashed on my ESP32 (with telnet and ftp server running) does not allow to "import websocket_client" which I need:
viewtopic.php?t=3476

I am using Arduinos, Raspberry PIs and ESPs, even ESP-01s recently:
https://twitter.com/HermannSW/status/10 ... 4718894082

So for using my wireless keyboard with USB dongle
https://twitter.com/HermannSW/status/10 ... 1288014849

I will simply switch to Pi ZeroW. The python on that allows for "import websocket_client", and I have 320x240 displays with self made (superglued) connectors that work fine with Pi ZeroW
https://www.raspberrypi.org/forums/view ... 8#p1214626
Image

and give me 40x15 console:
Image

With the keyboard and webrepl_client.py
https://forum.micropython.org/viewtopic ... 865#p29865

the Pi ZeroW will connect to ESP-01s attached to flying drone and allow to remote control servo bomb drop mechanism with MicroPython on the ESP-01s..

chegewara
Posts: 2207
Joined: Wed Jun 14, 2017 9:00 pm

Re: How to connect USB keyboard to ESP32?

Postby chegewara » Fri Sep 07, 2018 8:55 am

Because you can't connect USB keyboard that doesn't mean you can't use keyboard at all. You can use 4x4 matrix keypad or even ble keyboard.

User avatar
HermannSW
Posts: 97
Joined: Fri Oct 27, 2017 6:58 am
Location: Eberbach, Germany
Contact:

Re: How to connect USB keyboard to ESP32?

Postby HermannSW » Sat Sep 08, 2018 1:11 pm

chegewara wrote:Because you can't connect USB keyboard that doesn't mean you can't use keyboard at all.
True.
You can use 4x4 matrix keypad
I thought about that, and using its "#" key as "E" and "*" key as "F" I could enter one ASCII character for ESP32 MicroPython console with two key presses hexadecimally. But that keyboard input is cumbersome.
or even ble keyboard.
That sounds interesting. But my searches showed that ble keyboard support for ESP32 is in development and not ready. Do you have link showing working ble keyboard solution for ESP32?

I just completed my 1st mobile Pi ZeroW with 40x15 console and wireless keyboard. I will use that for running remote MicroPython shell on ESP-01s attached to flying drone:
Image

chegewara
Posts: 2207
Joined: Wed Jun 14, 2017 9:00 pm

Re: How to connect USB keyboard to ESP32?

Postby chegewara » Sat Sep 08, 2018 3:09 pm

This should be pretty solid ble keyboard solution:
https://github.com/asterics/esp32_mouse_keyboard

You can also with minimal amount of code write ble hid keyboard/mouse app for esp32 in arduino-ide.

But if you are looking for hid host code then i dont know any. I had to write my own but its commercial app and cant share it.

Can you confirm it is ble keyboard on that picture?

User avatar
HermannSW
Posts: 97
Joined: Fri Oct 27, 2017 6:58 am
Location: Eberbach, Germany
Contact:

Re: How to connect USB keyboard to ESP32?

Postby HermannSW » Wed Oct 03, 2018 9:44 pm

> Can you confirm it is ble keyboard on that picture?
>
It is not, it has USB dongle, so it is a USB keyboard.

I was fine with no keyboard for ESP32.
Until I just now was able to use boochow's gist for 128x64 Oled console and redirecting MicroPython REPL to it.
https://forum.micropython.org/viewtopic ... 699#p30697

Just cool:
Image

Perhaps now its time to think again on a (wireless) keyboard for ESP32+Oled ...

This is from matching "screen" session:

Code: Select all

...
>>> import sys
>>> sys.implementation
(name='micropython', version=(1, 9, 4))
>>> 5**4**3
542101086242752217003726400434970855712890625
>>> 

User avatar
HermannSW
Posts: 97
Joined: Fri Oct 27, 2017 6:58 am
Location: Eberbach, Germany
Contact:

Re: How to connect USB keyboard to ESP32?

Postby HermannSW » Tue Dec 18, 2018 8:59 am

I found a way to use wireless USB keyboard with ESP32, just use Arduino Due with its USBHost library:
https://www.arduino.cc/en/Reference/USBHost
Image


I ordered two <4$ "USB host mini shield" for Arduino Pro Minis I have for much smaller (and cheaper) form factor:
https://www.aliexpress.com/wholesale?Se ... ini+shield
Image

Just connecting the Due to ESP32 UART is next step, if that works Pro Mini USB host mini form factor is the way to go.

This is interesting post on USB hosts shield:
https://www.circuitsathome.com/usb-host ... re-manual/

User avatar
HermannSW
Posts: 97
Joined: Fri Oct 27, 2017 6:58 am
Location: Eberbach, Germany
Contact:

Re: How to connect USB keyboard to ESP32?

Postby HermannSW » Sun Dec 23, 2018 8:26 pm

It is so simple to drive ESP32 (MicroPython) from Arduino Due.
Due has 4 hardware serial interfaces, I used Serial1 and connected Pin 18 TX1 to ESP32 RX (and GND/3V).
This sketch just triggers several MicroPython calculations:

Code: Select all

void setup() {
 int a,b,c;
 Serial1.begin(115200);

 for(a=2; a<=5; ++a)
   for(b=2; b<=4; ++b)
     for(c=2; c<=3; ++c) {
       Serial1.print(a);
       Serial1.print("**");
       Serial1.print(b);
       Serial1.print("**");
       Serial1.println(c);
     }
 delay(1000);
}

void loop() {
}
This is the youtube video demonstrating sketch:
https://www.youtube.com/watch?v=uNrtUPG ... e=youtu.be
Image


Next step:
Combine Arduino USBHost library KeyboardController sketch with Serial1 code to use wireless USB keyboard connetced to native Due MicroUSB port for ESP32 MicroPython REPL input.


P.S
This is bottom part of ESP32 MicroPython boot.py, responsible for redirection of REPL output to Oled:

Code: Select all

...
import machine, ssd1306, os
i2c = machine.I2C(scl=machine.Pin(4), sda=machine.Pin(5))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)

from fbconsole import FBConsole
scr = FBConsole(oled)
os.dupterm(scr)

User avatar
HermannSW
Posts: 97
Joined: Fri Oct 27, 2017 6:58 am
Location: Eberbach, Germany
Contact:

Re: How to connect USB keyboard to ESP32?

Postby HermannSW » Mon Dec 24, 2018 3:43 pm

It works!
This is video of wireless USB keyboard connected to native Arduino Due port, its key presses being forwarded to ESP32 MicroPython REPL with Oled display. The video shows all, but the Oled display cannot be seen detailed enough. The sketch does use Arduino USBHost library KeyboardController, handles normal keys as well as CTRL keys, and return key. Does not handle up/down/left/right keys (OEM codes 82/81/80/79) yet. But CTRL-P goes back in REPL command history like up key, and CTRL-H does as backspace key should do:

Code: Select all

#include <KeyboardController.h>

USBHost usb;

KeyboardController keyboard(usb);

void keyPressed() {
  int mod = keyboard.getModifiers();
  char ch = keyboard.getKey();

  if (ch == 0x13) { // Return
    ch = 0x0D;
  }
  
  if (mod & (LeftCtrl|RightCtrl)) {
    ch &= 0x1F;
  }

  Serial1.write(ch);
}

void keyReleased() {
}

void setup() {
  Serial1.begin(115200);
  delay(200);
}

void loop() {
  usb.Task();
}
This is the video taken:
https://www.youtube.com/watch?v=aK93u34 ... e=youtu.be
Image


The whole setup does neither work when powered from laptop USB, nor when powered from 5V 1A mains adapter. In video I used official Raspberry mains adapter with 5.1V 2.5A.

Time to get the setup smaller when Arduino Pro mini USB host shield will arrive, with Arduino Pro mini.

Later it is time to find out how the Pro mini drives the mini USB host shield, and try to use that shield directly with ESP32:
  1. first with Pro mini USB host shield sketch running on ESP32 flashed with Arduino IDE
  2. later (hopefully) directly with MicroPython(!)

Who is online

Users browsing this forum: Majestic-12 [Bot] and 92 guests