├── .gitattributes ├── README.md └── ESP32-S2-MQTT-Keyboard └── ESP32-S2-MQTT-Keyboard.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP32-S2 2 | 3 | [![MIT License](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) 4 | [![GitHub stars](https://img.shields.io/github/stars/SensorsIot/ESP32-S2)](https://github.com/SensorsIot/ESP32-S2/stargazers) 5 | 6 | ![ESP32-S2](https://img.shields.io/badge/ESP32--S2-Supported-green?logo=espressif) 7 | ![Arduino](https://img.shields.io/badge/Arduino-Compatible-00979D?logo=arduino) 8 | ![USB HID](https://img.shields.io/badge/USB-HID%20Keyboard-blue) 9 | 10 | ESP32-S2 USB HID Keyboard with MQTT control. 11 | 12 | ## 📦 Requirements 13 | 14 | - **ESP32 Board Package** (version 2.0.0 or later) - includes `USBHIDKeyboard.h` 15 | - Install via Arduino IDE: Tools → Board → Boards Manager → search "ESP32" 16 | 17 | ## 📁 Examples 18 | 19 | | Folder | Description | 20 | |--------|-------------| 21 | | `ESP32-S2-MQTT-Keyboard/` | USB HID Keyboard controlled via MQTT | 22 | 23 | ## 💡 Other Examples 24 | 25 | Check the ESP32 board package examples for: 26 | - `USBMSC` (USB folder) - USB Mass Storage 27 | - `WiFiTelnetToSerial` (WiFi folder) - Telnet bridge 28 | -------------------------------------------------------------------------------- /ESP32-S2-MQTT-Keyboard/ESP32-S2-MQTT-Keyboard.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Basic ESP8266 MQTT example 3 | This sketch demonstrates the capabilities of the pubsub library in combination 4 | with the ESP8266 board/library. 5 | It connects to an MQTT server then: 6 | - publishes "hello world" to the topic "outTopic" every two seconds 7 | - subscribes to the topic "inTopic", printing out any messages 8 | it receives. NB - it assumes the received payloads are strings not binary 9 | - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led, 10 | else switch it off 11 | It will reconnect to the server if the connection is lost using a blocking 12 | reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to 13 | achieve the same result without blocking the main loop. 14 | To install the ESP8266 board, (using Arduino 1.6.4+): 15 | - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs": 16 | http://arduino.esp8266.com/stable/package_esp8266com_index.json 17 | - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266" 18 | - Select your ESP8266 in "Tools -> Board" 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | #include "USB.h" 26 | #include "USBHIDKeyboard.h" 27 | USBHIDKeyboard Keyboard; 28 | 29 | #define BUILTIN_LED 13 30 | 31 | // Update these with values suitable for your network. 32 | 33 | // Replace credentials and server name with yours 34 | const char* ssid = mySSID; 35 | const char* password = myPASSWORD; 36 | const char* mqtt_server = MQTT_BROKER; 37 | 38 | WiFiClient espClient; 39 | PubSubClient client(espClient); 40 | unsigned long lastMsg = 0; 41 | #define MSG_BUFFER_SIZE (50) 42 | char msg[MSG_BUFFER_SIZE]; 43 | int value = 0; 44 | 45 | void setup_wifi() { 46 | 47 | delay(10); 48 | // We start by connecting to a WiFi network 49 | Serial.println(); 50 | Serial.print("Connecting to "); 51 | Serial.println(ssid); 52 | 53 | WiFi.mode(WIFI_STA); 54 | WiFi.begin(ssid, password); 55 | 56 | while (WiFi.status() != WL_CONNECTED) { 57 | delay(500); 58 | Serial.print("."); 59 | } 60 | 61 | randomSeed(micros()); 62 | 63 | Serial.println(""); 64 | Serial.println("WiFi connected"); 65 | Serial.println("IP address: "); 66 | Serial.println(WiFi.localIP()); 67 | } 68 | 69 | void callback(char* topic, byte* payload, unsigned int length) { 70 | String sentence = ""; 71 | Serial.print("Message arrived ["); 72 | Serial.print(topic); 73 | Serial.print("] "); 74 | for (int i = 0; i < length; i++) { 75 | keyLetter(payload[i]); 76 | } 77 | delay(5); 78 | Keyboard.println(); 79 | Serial.println(); 80 | 81 | // Switch on the LED if an 1 was received as first character 82 | if ((char)payload[0] == '1') { 83 | digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level 84 | // but actually the LED is on; this is because 85 | // it is active low on the ESP-01) 86 | } else { 87 | digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH 88 | } 89 | 90 | } 91 | 92 | void reconnect() { 93 | // Loop until we're reconnected 94 | while (!client.connected()) { 95 | Serial.print("Attempting MQTT connection..."); 96 | // Create a random client ID 97 | String clientId = "ESP8266Client-"; 98 | clientId += String(random(0xffff), HEX); 99 | // Attempt to connect 100 | if (client.connect(clientId.c_str())) { 101 | Serial.println("connected"); 102 | // Once connected, publish an announcement... 103 | client.publish("outTopic", "hello world"); 104 | // ... and resubscribe 105 | client.subscribe("remoteKeyboard"); 106 | } else { 107 | Serial.print("failed, rc="); 108 | Serial.print(client.state()); 109 | Serial.println(" try again in 5 seconds"); 110 | // Wait 5 seconds before retrying 111 | delay(5000); 112 | } 113 | } 114 | } 115 | 116 | void setup() { 117 | pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output 118 | Serial.begin(115200); 119 | setup_wifi(); 120 | client.setServer(mqtt_server, 1883); 121 | client.setCallback(callback); 122 | Keyboard.begin(); 123 | USB.begin(); 124 | 125 | for (int i = 0; i < 127; i++) { 126 | Keyboard.print(i); 127 | keyLetter(i); 128 | Keyboard.println(); 129 | delay(50); 130 | } 131 | } 132 | 133 | void loop() { 134 | 135 | if (!client.connected()) { 136 | reconnect(); 137 | } 138 | client.loop(); 139 | } 140 | 141 | void keyLetter(byte _in) { 142 | char _out=_in; 143 | if (_in < '0')_out = 0x2c; 144 | if (_in < 'A' && _in > '9')_out = 0x2c; 145 | if (_in > 'Z' && _in < 'a')_out = 0x2c; 146 | if (_in > 'z')_out = 0x2c; 147 | if (_in=='.') _out=0x2E; 148 | Keyboard.write(_out); 149 | Serial.print((char)_out); 150 | } 151 | 152 | /* 153 | void keyLetter(byte _in) { 154 | Serial.print((char)_in); 155 | } 156 | */ 157 | --------------------------------------------------------------------------------