Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Wifi Keyboard (ARDUINO,ESP8266,USB_HOST_SHIELD)
- //Created by Matthew Asmodeus
- //WORK IN PROGRESS (Requires final key send data modification)
- #include "ESP8266.h"
- #include <hidboot.h>
- #include <usbhub.h>
- #ifdef dobogusinclude
- #include <spi4teensy3.h>
- #endif
- #define SSID "raprap" //Your Router WiFi SSID
- #define PASSWORD "@lol@333£££" //Your Router WiFi Password
- #define PORT 8080 //The Port you want the TCP Server to run on
- ESP8266 wifi(Serial1);
- class KbdRptParser :
- public KeyboardReportParser
- {
- void PrintKey(uint8_t mod, uint8_t key);
- protected:
- virtual void OnControlKeysChanged(uint8_t before, uint8_t after);
- virtual void OnKeyDown (uint8_t mod, uint8_t key);
- virtual void OnKeyUp (uint8_t mod, uint8_t key);
- virtual void OnKeyPressed(uint8_t key);
- };
- String pkey;
- void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
- {
- MODIFIERKEYS mod;
- *((uint8_t*)&mod) = m;
- Serial.print((mod.bmLeftCtrl == 1) ? "C" : " ");
- Serial.print((mod.bmLeftShift == 1) ? "S" : " ");
- Serial.print((mod.bmLeftAlt == 1) ? "A" : " ");
- Serial.print((mod.bmLeftGUI == 1) ? "G" : " ");
- pkey = (String)uint8_t(key);
- Serial.print((mod.bmRightCtrl == 1) ? "C" : " ");
- Serial.print((mod.bmRightShift == 1) ? "S" : " ");
- Serial.print((mod.bmRightAlt == 1) ? "A" : " ");
- Serial.println((mod.bmRightGUI == 1) ? "G" : " ");
- };
- void KbdRptParser::OnKeyPressed(uint8_t key)
- {
- //Serial.println((char)key);
- }
- void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after) {
- MODIFIERKEYS beforeMod;
- *((uint8_t*)&beforeMod) = before;
- MODIFIERKEYS afterMod;
- *((uint8_t*)&afterMod) = after;
- String data;
- if(beforeMod.bmLeftCtrl) {
- data = "up,ct";
- }
- if(beforeMod.bmLeftShift) {
- data = "up,sh";
- }
- if(beforeMod.bmLeftAlt) {
- data = "up,sh";
- }
- if(beforeMod.bmLeftGUI) {
- data = "up,sh";
- }
- if (beforeMod.bmRightCtrl) {
- data = "up,ct";
- }
- if (beforeMod.bmRightShift) {
- data = "up,sh";
- }
- if (beforeMod.bmRightAlt) {
- data = "up,al";
- }
- if (beforeMod.bmRightGUI) {
- data = "up,gu";
- }
- if(afterMod.bmLeftCtrl) {
- data = "dn,ct";
- }
- if(afterMod.bmLeftShift) {
- data = "dn,sh";
- }
- if(afterMod.bmLeftAlt) {
- data = "dn,sh";
- }
- if(afterMod.bmLeftGUI) {
- data = "dn,sh";
- }
- if(afterMod.bmRightCtrl) {
- data = "dn,ct";
- }
- if(afterMod.bmRightShift) {
- data = "dn,sh";
- }
- if(afterMod.bmRightAlt) {
- data = "dn,al";
- }
- if(afterMod.bmRightGUI) {
- data = "dn,gu";
- }
- char array[data.length()];
- data.toCharArray(array, data.length() + 1);
- wifi.send(0, (const uint8_t*)array, sizeof(array));
- }
- USB Usb;
- //USBHub Hub(&Usb);
- HIDBoot<HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);
- uint32_t next_time;
- KbdRptParser Prs;
- void setup(void)
- {
- Serial.begin(115200);
- Serial.println("Starting Boot Up Sequence!");
- int trys = 0;
- tryAgain:
- Serial.println("Wifi Connecting(" + (String)SSID + ") Retrys(" + (String)trys + ")");
- wifi.setOprToStationSoftAP(); //Set Mode - Connect to Access Point
- if (wifi.joinAP(SSID, PASSWORD)) {
- wifi.enableMUX();
- wifi.startTCPServer(PORT);
- wifi.setTCPServerTimeout(10000);
- Serial.print((String)SSID + " TCP Server Port: (" + String(PORT) + ")\n");
- Serial.println(wifi.getLocalIP().c_str());
- }
- else {
- Serial.println("Connection Failed - Retrying");
- trys++;
- goto tryAgain;
- }
- if (Usb.Init() == -1)
- {
- Serial.println("Keyboard Failed");
- }
- else {
- Serial.println("Initialising Keyboard");
- }
- delay( 200 );
- next_time = millis() + 5000;
- HidKeyboard.SetReportParser(0, (HIDReportParser*)&Prs);
- do{
- Usb.Task();
- }
- while(HidKeyboard.isReady() == false);
- Serial.println("Keyboard Ready");
- }
- void loop(void)
- {
- Usb.Task();
- uint8_t buffer[128] = {
- 0 };
- uint8_t mux_id;
- uint32_t len = wifi.recv(&mux_id, buffer, sizeof(buffer), 100);
- String Message;
- if (len > 0) {
- wifi.send(mux_id, buffer, len);//Return message received
- //Get the message out of the buffer
- for(uint32_t i = 0; i < len; i++) {
- Message += (String)(char)buffer[i];
- }
- //Print the message
- Serial.println("Received: " + Message);
- if(Message.indexOf("hmm") >= 0)
- {
- String data = "Send To Client [" + (String)mux_id + "] Command Received";
- char array[data.length()];
- data.toCharArray(array, data.length() + 1);
- wifi.send(0, (const uint8_t*)array, sizeof(array));
- }
- }
- }
- void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
- {
- Serial.println("dn," + (String)key);
- String data = "dn," + (String)(char)OemToAscii(mod, key);
- char array[data.length()];
- data.toCharArray(array, data.length() + 1);
- wifi.send(0, (const uint8_t*)array, sizeof(array));
- }
- void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
- {
- //WIFI KEY_CODE SEND HERE
- Serial.println("up," + (String)key);
- String data = "up," + (String)(char)OemToAscii(mod, key);
- char array[data.length()];
- data.toCharArray(array, data.length() + 1);
- wifi.send(0, (const uint8_t*)array, sizeof(array));
- }
- //Split an string into an array for direct get_val[index]
- String split(String data, char separator, int index)
- {
- int found = 0;
- int strIndex[] = {
- 0, -1 };
- int maxIndex = data.length()-1;
- for(int i=0; i<=maxIndex && found<=index; i++){
- if(data.charAt(i)==separator || i==maxIndex){
- found++;
- strIndex[0] = strIndex[1]+1;
- strIndex[1] = (i == maxIndex) ? i+1 : i;
- }
- }
- return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement