Controller.ino 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include <helper_3dmath.h>
  2. #include <MPU6050_6Axis_MotionApps20.h>
  3. #include <MPU6050.h>
  4. #include <Wire.h>
  5. #include <WiFiUdp.h>
  6. #include <ESP8266WiFi.h>
  7. #include <EEPROM.h>
  8. String WifiSSIDPrefix = "CrystalPoint";
  9. uint16_t BasestationPort = 2730;
  10. IPAddress BasestationIp = IPAddress(192, 168, 4, 1);
  11. struct MPU6050Data{
  12. int gx;
  13. int gy;
  14. int gz;
  15. int ax;
  16. int ay;
  17. int az;
  18. int fx;
  19. int fy;
  20. };
  21. struct JoystickData{
  22. int x;
  23. int y;
  24. char button;
  25. };
  26. struct SwitchData{
  27. char backSwitch;
  28. char magnetSwitch;
  29. };
  30. //Data buffer for sensors
  31. struct MPU6050Data mpu6050data = {0};
  32. struct JoystickData joystickdata = {0};
  33. struct SwitchData switchdata = {0};
  34. //UDP server (receiving) setup
  35. unsigned int udpPort = 2730;
  36. byte packetBuffer[512]; //udp package buffer
  37. WiFiUDP Udp;
  38. void setup(void){
  39. Serial.begin(115200);
  40. Serial.println();
  41. WiFi.mode(WIFI_STA);
  42. WiFi.disconnect();
  43. EEPROM.begin(512);
  44. Udp.begin(udpPort);
  45. //clearEeprom();
  46. //connectLastBasestation();
  47. searchBasestation();
  48. }
  49. void loop(void){
  50. //receiving rumble or shock data from basestation
  51. int noBytes = Udp.parsePacket();
  52. if ( noBytes ) {
  53. Serial.print(millis() / 1000);
  54. Serial.print(":Packet of ");
  55. Serial.print(noBytes);
  56. Serial.print(" received from ");
  57. Serial.print(Udp.remoteIP());
  58. Serial.print(":");
  59. Serial.println(Udp.remotePort());
  60. // We've received a packet, read the data from it
  61. Udp.read(packetBuffer,noBytes); // read the packet into the buffer
  62. // display the packet contents in HEX
  63. for (int i=1;i<=noBytes;i++){
  64. Serial.print(packetBuffer[i-1],HEX);
  65. if (i % 32 == 0){
  66. Serial.println();
  67. }
  68. else Serial.print(' ');
  69. }
  70. Serial.println();
  71. }
  72. //Send new data
  73. joystickdata.x = 1;
  74. joystickdata.y = 2;
  75. joystickdata.button = 0;
  76. mpu6050data.gx = 1;
  77. mpu6050data.gy = 2;
  78. mpu6050data.gz = 3;
  79. mpu6050data.ax = 4;
  80. mpu6050data.ay = 5;
  81. mpu6050data.az = 6;
  82. mpu6050data.fx = 7;
  83. mpu6050data.fy = 8;
  84. switchdata.backSwitch = 9;
  85. switchdata.magnetSwitch = 10;
  86. sendUdpMessage(&joystickdata, &mpu6050data, &switchdata);
  87. //30 times per second new data
  88. delay(33);
  89. }
  90. void searchBasestation(void){
  91. Serial.print("Scanning for basestation ");
  92. for(int j = 0; j < 10; j++){
  93. int n = WiFi.scanNetworks();
  94. for (int i = 0; i < n; ++i){
  95. if(WiFi.SSID(i).startsWith(WifiSSIDPrefix)){ //We've found a basestation
  96. char WifiPassword[10];
  97. char WifiSSID[17];
  98. WiFi.SSID(i).toCharArray(WifiSSID, WiFi.SSID(i).length() + 1);
  99. calculateWiFiPassword(WiFi.SSID(i), WifiPassword);
  100. Serial.printf("Basestation found! SSID: %s", WifiSSID);
  101. Serial.printf("\nBasestation password: %s", WifiPassword);
  102. //Connect to the basestation
  103. WiFi.begin(WifiSSID, WifiPassword);
  104. ///Writing data to eeprom
  105. writeEeprom(WifiSSID, WifiPassword);
  106. //Break out of the loop and stop scanning
  107. n = 254;
  108. j = 254;
  109. }
  110. }
  111. Serial.print(".");
  112. delay(1000);
  113. }
  114. }
  115. void connectLastBasestation(void){
  116. Serial.println("Reading information from EEPROM");
  117. char eepromWifiSSID[17];
  118. char eepromWifiPassword[10];
  119. //Read SSID
  120. for (int i = 0; i < 17; i++){
  121. eepromWifiSSID[i] = char(EEPROM.read(i));
  122. }
  123. eepromWifiSSID[17] = '\0';
  124. //Read Password
  125. for (int i = 0; i < 10; i++){
  126. eepromWifiPassword[i]= char(EEPROM.read(17+i));
  127. }
  128. eepromWifiPassword[10] = '\0';
  129. //Verify valid memory
  130. if(String(eepromWifiSSID).startsWith(WifiSSIDPrefix)){
  131. Serial.printf("Valid basestation information in eeprom found! \nEeprom SSID: %s \nEeprom Password: %s", eepromWifiSSID, eepromWifiPassword);
  132. //Connect to the basestation
  133. WiFi.begin(eepromWifiSSID, eepromWifiPassword);
  134. }
  135. }
  136. void calculateWiFiPassword(String WifiSSID, char *WifiPassword)
  137. {
  138. String WifiStringPassword;
  139. for(char i=0; i < 17; i++){
  140. String s = String(WifiSSID[i], HEX);
  141. WifiStringPassword = s + WifiStringPassword;
  142. }
  143. for (int i=0; i < 10; i++)
  144. WifiPassword[i] = WifiStringPassword.charAt(i);
  145. WifiPassword[9] = '\0';
  146. }
  147. void sendUdpMessage(struct JoystickData *joystick, struct MPU6050Data *mpu6050data, struct SwitchData *switchdata){
  148. char sendBuffer[512];
  149. Udp.beginPacket(BasestationIp, BasestationPort);
  150. sprintf(sendBuffer, "%06d%06d%01d", joystick->x, joystick->y, joystick->button);
  151. sprintf(sendBuffer, "%s%06d%06d%06d%06d%06d%06d%06d%06d", sendBuffer, mpu6050data->gx, mpu6050data->gy, mpu6050data->gz, mpu6050data->ax, mpu6050data->ay, mpu6050data->az, mpu6050data->fx, mpu6050data->fy);
  152. sprintf(sendBuffer, "%s%01d%01d", sendBuffer, switchdata->backSwitch, switchdata->magnetSwitch);
  153. Serial.printf("%s", sendBuffer);
  154. Serial.println();
  155. Udp.write(sendBuffer);
  156. Udp.endPacket();
  157. }
  158. void writeEeprom(char *WifiSSID, char *WifiPassword){
  159. //Clean memory
  160. clearEeprom();
  161. //Write ssid
  162. for(int i = 0; i < 17; i++){
  163. EEPROM.write(i, WifiSSID[i]);
  164. }
  165. //Write password
  166. for(int i = 0; i < 10; i++){
  167. EEPROM.write(17+i, WifiPassword[i]);
  168. }
  169. EEPROM.commit();
  170. }
  171. void clearEeprom(){
  172. for (int i = 0; i < 30; i++) { EEPROM.write(i, 0); }
  173. }