ControllerBaseStation.ino 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. extern "C" {
  2. #define LWIP_OPEN_SRC
  3. #include "user_interface.h"
  4. }
  5. #include <Wire.h>
  6. #include <ESP8266WiFi.h>
  7. #include <WiFiUdp.h>
  8. #define MAX_CLIENTS 4
  9. String WifiSSIDPrefix = "CrystalPoint";
  10. char WifiPassword[10];
  11. char WifiSSID[17];
  12. struct controllerInformation{
  13. IPAddress ipaddr;
  14. u_long lastPackage;
  15. };
  16. controllerInformation ConnectionList[MAX_CLIENTS];
  17. u_long lastDisconnectionCheck = 0;
  18. //Serial buffer string
  19. String serialInput = "";
  20. boolean serialDataReady = false;
  21. //UDP server (receiving) setup
  22. unsigned int udpPort = 2730;
  23. byte packetBuffer[512]; //udp package buffer
  24. WiFiUDP Udp;
  25. boolean started = false;
  26. void setup(void){
  27. Serial.begin(115200);
  28. Serial.println();
  29. //LED
  30. pinMode(2,OUTPUT);
  31. digitalWrite(2,LOW);
  32. //BUTTON
  33. pinMode(0,INPUT);
  34. Udp.begin(udpPort);
  35. delay(100);
  36. setupWifi();
  37. serialInput.reserve(200);
  38. }
  39. int SSIDVisibleTimeout = 0;
  40. char SSIDVisible = 0;
  41. void loop(void){
  42. int noBytes = Udp.parsePacket();
  43. if ( noBytes ) {
  44. Udp.read(packetBuffer,noBytes);
  45. if(started){
  46. Serial.printf("1|%d|", getControllerId(Udp.remoteIP()));
  47. for (int i=1;i<=noBytes;i++){
  48. Serial.printf("%c", packetBuffer[i-1]);
  49. }
  50. Serial.println();
  51. }else{
  52. getControllerId(Udp.remoteIP());
  53. }
  54. }
  55. if (serialDataReady) {
  56. Serial.println("0|" + serialInput);
  57. if(serialInput == "start"){
  58. started = true;
  59. }else if(serialInput == "stop"){
  60. started = false;
  61. }else if(serialInput == "clientlist"){
  62. getControllerList();
  63. }
  64. serialInput = "";
  65. serialDataReady = false;
  66. }
  67. if (Serial.available()) serialEvent();
  68. if(SSIDVisible == 1 && (SSIDVisibleTimeout + 30000) < millis())
  69. {
  70. SSIDVisibleTimeout = 0;
  71. ToggleWifiVisibility(0);
  72. }
  73. if(digitalRead(0) == 0 && SSIDVisible == 0)
  74. {
  75. Serial.println("0|Button Pressed");
  76. SSIDVisibleTimeout = millis();
  77. ToggleWifiVisibility(1);
  78. }
  79. if(millis() - lastDisconnectionCheck > 10000){
  80. checkControllerDisconnect();
  81. }
  82. }
  83. void serialEvent() {
  84. while (Serial.available()) {
  85. // get the new byte:
  86. char inChar = (char)Serial.read();
  87. if (inChar == '\n' || inChar == '\r') {
  88. serialDataReady = true;
  89. }else{
  90. serialInput += inChar;
  91. }
  92. }
  93. }
  94. void ToggleWifiVisibility(char v)
  95. {
  96. if(v == 1){
  97. SSIDVisible = 1;
  98. WiFi.softAP(WifiSSID, WifiPassword, 6, 0);
  99. digitalWrite(2, HIGH);
  100. Serial.println("0|Wifi Visible");
  101. }else if(v == 0){
  102. SSIDVisible = 0;
  103. WiFi.softAP(WifiSSID, WifiPassword, 6, 1);
  104. digitalWrite(2, LOW);
  105. Serial.println("0|Wifi Hidden");
  106. }
  107. }
  108. char getControllerId(IPAddress ipaddr){
  109. for(char i=0; i < MAX_CLIENTS; i++){
  110. if(ConnectionList[i].ipaddr == ipaddr){ //Yay, controller found. Return the id.
  111. ConnectionList[i].lastPackage = millis();
  112. return i;
  113. }
  114. }
  115. //We didn't found the controller, so add it and return a new id.
  116. for(char i=0; i < MAX_CLIENTS; i++){
  117. if(ConnectionList[i].ipaddr == INADDR_NONE){
  118. ConnectionList[i].ipaddr = ipaddr;
  119. ConnectionList[i].lastPackage = millis();
  120. Serial.printf("2|connected|%d\n\r", i);
  121. return i;
  122. }
  123. }
  124. //No room for new controllers.
  125. return -1;
  126. }
  127. void checkControllerDisconnect(){
  128. for(char i=0; i < MAX_CLIENTS; i++){
  129. if(ConnectionList[i].ipaddr != INADDR_NONE && millis() - ConnectionList[i].lastPackage > 10000){ //Controller didn't send any messages last 10 seconds, disconnected.
  130. ConnectionList[i].ipaddr = INADDR_NONE;
  131. Serial.printf("2|disconnected|%d\n\r", i);
  132. }
  133. }
  134. }
  135. void setupWifi(void){
  136. WiFi.disconnect(true);
  137. WiFi.mode(WIFI_AP);
  138. generateWiFiSSID();
  139. generateWiFiPassword();
  140. WiFi.softAP(WifiSSID, WifiPassword, 6, 1);
  141. }
  142. void generateWiFiSSID()
  143. {
  144. //Last 4 digits mac adress
  145. uint8_t mac[WL_MAC_ADDR_LENGTH];
  146. WiFi.softAPmacAddress(mac);
  147. String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
  148. String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
  149. macID.toUpperCase();
  150. WifiSSIDPrefix += macID;
  151. memset(WifiSSID, 0, WifiSSIDPrefix.length() + 1);
  152. for (int i=0; i<WifiSSIDPrefix.length(); i++)
  153. WifiSSID[i] = WifiSSIDPrefix.charAt(i);
  154. }
  155. void generateWiFiPassword()
  156. {
  157. String WifiStringPassword;
  158. for(char i=0; i < 17; i++){
  159. String s = String(WifiSSID[i], HEX);
  160. WifiStringPassword = s + WifiStringPassword;
  161. }
  162. for (int i=0; i < 10; i++)
  163. WifiPassword[i] = WifiStringPassword.charAt(i);
  164. WifiPassword[9] = '\0';
  165. Serial.print("0|Wifi password: ");
  166. Serial.println(WifiPassword);
  167. }
  168. //Serial commands
  169. void getControllerList(){
  170. Serial.printf("3|");
  171. for(char i=0; i < MAX_CLIENTS; i++){
  172. if(ConnectionList[i].ipaddr != INADDR_NONE){
  173. Serial.printf("%d|", i);
  174. }
  175. }
  176. Serial.println();
  177. }
  178. void sendRumble(String data);
  179. void sendShock(String data);