Advertisement
AsmodHacker

Arduino WIFI Keyboard (WIP)

Mar 12th, 2015
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. //Wifi Keyboard (ARDUINO,ESP8266,USB_HOST_SHIELD)
  2. //Created by Matthew Asmodeus
  3. //WORK IN PROGRESS (Requires final key send data modification)
  4.  
  5. #include "ESP8266.h"
  6. #include <hidboot.h>
  7. #include <usbhub.h>
  8. #ifdef dobogusinclude
  9. #include <spi4teensy3.h>
  10. #endif
  11.  
  12. #define SSID "raprap" //Your Router WiFi SSID
  13. #define PASSWORD "@lol@333£££" //Your Router WiFi Password
  14. #define PORT 8080 //The Port you want the TCP Server to run on
  15.  
  16. ESP8266 wifi(Serial1);
  17.  
  18.  
  19. class KbdRptParser :
  20. public KeyboardReportParser
  21. {
  22. void PrintKey(uint8_t mod, uint8_t key);
  23.  
  24. protected:
  25. virtual void OnControlKeysChanged(uint8_t before, uint8_t after);
  26.  
  27. virtual void OnKeyDown (uint8_t mod, uint8_t key);
  28. virtual void OnKeyUp (uint8_t mod, uint8_t key);
  29. virtual void OnKeyPressed(uint8_t key);
  30. };
  31.  
  32. String pkey;
  33. void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
  34. {
  35. MODIFIERKEYS mod;
  36. *((uint8_t*)&mod) = m;
  37. Serial.print((mod.bmLeftCtrl == 1) ? "C" : " ");
  38. Serial.print((mod.bmLeftShift == 1) ? "S" : " ");
  39. Serial.print((mod.bmLeftAlt == 1) ? "A" : " ");
  40. Serial.print((mod.bmLeftGUI == 1) ? "G" : " ");
  41.  
  42. pkey = (String)uint8_t(key);
  43.  
  44. Serial.print((mod.bmRightCtrl == 1) ? "C" : " ");
  45. Serial.print((mod.bmRightShift == 1) ? "S" : " ");
  46. Serial.print((mod.bmRightAlt == 1) ? "A" : " ");
  47. Serial.println((mod.bmRightGUI == 1) ? "G" : " ");
  48. };
  49.  
  50. void KbdRptParser::OnKeyPressed(uint8_t key)
  51. {
  52. //Serial.println((char)key);
  53. }
  54.  
  55. void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after) {
  56.  
  57. MODIFIERKEYS beforeMod;
  58. *((uint8_t*)&beforeMod) = before;
  59.  
  60. MODIFIERKEYS afterMod;
  61. *((uint8_t*)&afterMod) = after;
  62. String data;
  63.  
  64. if(beforeMod.bmLeftCtrl) {
  65. data = "up,ct";
  66. }
  67. if(beforeMod.bmLeftShift) {
  68. data = "up,sh";
  69. }
  70. if(beforeMod.bmLeftAlt) {
  71. data = "up,sh";
  72. }
  73. if(beforeMod.bmLeftGUI) {
  74. data = "up,sh";
  75. }
  76. if (beforeMod.bmRightCtrl) {
  77. data = "up,ct";
  78. }
  79. if (beforeMod.bmRightShift) {
  80. data = "up,sh";
  81. }
  82. if (beforeMod.bmRightAlt) {
  83. data = "up,al";
  84. }
  85. if (beforeMod.bmRightGUI) {
  86. data = "up,gu";
  87. }
  88.  
  89. if(afterMod.bmLeftCtrl) {
  90. data = "dn,ct";
  91. }
  92. if(afterMod.bmLeftShift) {
  93. data = "dn,sh";
  94. }
  95. if(afterMod.bmLeftAlt) {
  96. data = "dn,sh";
  97. }
  98. if(afterMod.bmLeftGUI) {
  99. data = "dn,sh";
  100. }
  101. if(afterMod.bmRightCtrl) {
  102. data = "dn,ct";
  103. }
  104. if(afterMod.bmRightShift) {
  105. data = "dn,sh";
  106. }
  107. if(afterMod.bmRightAlt) {
  108. data = "dn,al";
  109. }
  110. if(afterMod.bmRightGUI) {
  111. data = "dn,gu";
  112. }
  113.  
  114. char array[data.length()];
  115. data.toCharArray(array, data.length() + 1);
  116. wifi.send(0, (const uint8_t*)array, sizeof(array));
  117. }
  118.  
  119. USB Usb;
  120. //USBHub Hub(&Usb);
  121. HIDBoot<HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);
  122.  
  123. uint32_t next_time;
  124.  
  125. KbdRptParser Prs;
  126.  
  127.  
  128.  
  129.  
  130. void setup(void)
  131. {
  132. Serial.begin(115200);
  133. Serial.println("Starting Boot Up Sequence!");
  134. int trys = 0;
  135. tryAgain:
  136. Serial.println("Wifi Connecting(" + (String)SSID + ") Retrys(" + (String)trys + ")");
  137. wifi.setOprToStationSoftAP(); //Set Mode - Connect to Access Point
  138. if (wifi.joinAP(SSID, PASSWORD)) {
  139. wifi.enableMUX();
  140. wifi.startTCPServer(PORT);
  141. wifi.setTCPServerTimeout(10000);
  142. Serial.print((String)SSID + " TCP Server Port: (" + String(PORT) + ")\n");
  143. Serial.println(wifi.getLocalIP().c_str());
  144. }
  145. else {
  146. Serial.println("Connection Failed - Retrying");
  147. trys++;
  148. goto tryAgain;
  149. }
  150.  
  151. if (Usb.Init() == -1)
  152. {
  153. Serial.println("Keyboard Failed");
  154. }
  155. else {
  156. Serial.println("Initialising Keyboard");
  157. }
  158.  
  159. delay( 200 );
  160. next_time = millis() + 5000;
  161. HidKeyboard.SetReportParser(0, (HIDReportParser*)&Prs);
  162. do{
  163. Usb.Task();
  164. }
  165. while(HidKeyboard.isReady() == false);
  166. Serial.println("Keyboard Ready");
  167. }
  168.  
  169. void loop(void)
  170. {
  171. Usb.Task();
  172. uint8_t buffer[128] = {
  173. 0 };
  174. uint8_t mux_id;
  175. uint32_t len = wifi.recv(&mux_id, buffer, sizeof(buffer), 100);
  176. String Message;
  177. if (len > 0) {
  178. wifi.send(mux_id, buffer, len);//Return message received
  179.  
  180. //Get the message out of the buffer
  181. for(uint32_t i = 0; i < len; i++) {
  182. Message += (String)(char)buffer[i];
  183. }
  184.  
  185. //Print the message
  186. Serial.println("Received: " + Message);
  187.  
  188. if(Message.indexOf("hmm") >= 0)
  189. {
  190. String data = "Send To Client [" + (String)mux_id + "] Command Received";
  191. char array[data.length()];
  192. data.toCharArray(array, data.length() + 1);
  193. wifi.send(0, (const uint8_t*)array, sizeof(array));
  194. }
  195. }
  196. }
  197.  
  198. void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
  199. {
  200. Serial.println("dn," + (String)key);
  201. String data = "dn," + (String)(char)OemToAscii(mod, key);
  202. char array[data.length()];
  203. data.toCharArray(array, data.length() + 1);
  204. wifi.send(0, (const uint8_t*)array, sizeof(array));
  205. }
  206.  
  207. void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
  208. {
  209. //WIFI KEY_CODE SEND HERE
  210. Serial.println("up," + (String)key);
  211. String data = "up," + (String)(char)OemToAscii(mod, key);
  212. char array[data.length()];
  213. data.toCharArray(array, data.length() + 1);
  214. wifi.send(0, (const uint8_t*)array, sizeof(array));
  215. }
  216.  
  217. //Split an string into an array for direct get_val[index]
  218. String split(String data, char separator, int index)
  219. {
  220. int found = 0;
  221. int strIndex[] = {
  222. 0, -1 };
  223. int maxIndex = data.length()-1;
  224. for(int i=0; i<=maxIndex && found<=index; i++){
  225. if(data.charAt(i)==separator || i==maxIndex){
  226. found++;
  227. strIndex[0] = strIndex[1]+1;
  228. strIndex[1] = (i == maxIndex) ? i+1 : i;
  229. }
  230. }
  231. return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement