ControllerBaseStation.ino 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. ip_addr ConnectionList[MAX_CLIENTS] = {0};
  13. //Serial buffer string
  14. String serialInput = "";
  15. boolean serialDataReady = false;
  16. //UDP server (receiving) setup
  17. unsigned int udpPort = 2730;
  18. byte packetBuffer[512]; //udp package buffer
  19. WiFiUDP Udp;
  20. void WiFiEvent(WiFiEvent_t event) {
  21. switch(event) {
  22. case WIFI_EVENT_SOFTAPMODE_STACONNECTED:
  23. Serial.println("0|NEW CONNECTED");
  24. break;
  25. case WIFI_EVENT_SOFTAPMODE_STADISCONNECTED:
  26. Serial.println("0|DISCONNECTED");
  27. break;
  28. }
  29. }
  30. void setup(void){
  31. Serial.begin(115200);
  32. Serial.println();
  33. WiFi.onEvent(WiFiEvent);
  34. //LED
  35. pinMode(2,OUTPUT);
  36. digitalWrite(2,LOW);
  37. //BUTTON
  38. pinMode(0,INPUT);
  39. Udp.begin(udpPort);
  40. delay(100);
  41. setupWifi();
  42. serialInput.reserve(200);
  43. }
  44. int SSIDVisibleTimeout = 0;
  45. char SSIDVisible = 0;
  46. void loop(void){
  47. int noBytes = Udp.parsePacket();
  48. if ( noBytes ) {
  49. Serial.print("1|");
  50. Serial.print(Udp.remoteIP());
  51. Serial.print("|");
  52. // We've received a packet, read the data from it
  53. Udp.read(packetBuffer,noBytes); // read the packet into the buffer
  54. for (int i=1;i<=noBytes;i++){
  55. Serial.printf("%c", packetBuffer[i-1]);
  56. }
  57. Serial.println();
  58. }
  59. if (serialDataReady) {
  60. serialInput = "";
  61. serialDataReady = false;
  62. //todo: get type number and execute from command array
  63. }
  64. if(SSIDVisible == 1 && (SSIDVisibleTimeout + 30000) < millis())
  65. {
  66. SSIDVisibleTimeout = 0;
  67. ToggleWifiVisibility(0);
  68. }
  69. if(digitalRead(0) == 0 && SSIDVisible == 0)
  70. {
  71. Serial.println("Button Pressed");
  72. SSIDVisibleTimeout = millis();
  73. ToggleWifiVisibility(1);
  74. }
  75. }
  76. void serialEvent() {
  77. while (Serial.available()) {
  78. // get the new byte:
  79. char inChar = (char)Serial.read();
  80. // add it to the inputString:
  81. serialInput += inChar;
  82. // if the incoming character is a newline, set a flag
  83. // so the main loop can do something about it:
  84. if (inChar == '\n') {
  85. serialDataReady = true;
  86. }
  87. }
  88. }
  89. void ToggleWifiVisibility(char v)
  90. {
  91. if(v == 1){
  92. SSIDVisible = 1;
  93. WiFi.softAP(WifiSSID, WifiPassword, 6, 0);
  94. digitalWrite(2, HIGH);
  95. Serial.println("Wifi Visible");
  96. }else if(v == 0){
  97. SSIDVisible = 0;
  98. WiFi.softAP(WifiSSID, WifiPassword, 6, 1);
  99. digitalWrite(2, LOW);
  100. Serial.println("Wifi Hidden");
  101. }
  102. }
  103. void RepopulateConnectionList(char n)
  104. {
  105. struct station_info *stat_info;
  106. stat_info = wifi_softap_get_station_info();
  107. //Controller disconnected
  108. if(n == 0){
  109. Serial.println("Old controller disconnected..");
  110. for(char i=0; i < MAX_CLIENTS; i++){
  111. char missingIP = 0;
  112. while (stat_info != NULL) {
  113. if(ip_addr_cmp(&ConnectionList[i], &stat_info->ip)){
  114. missingIP = 255;
  115. break;
  116. }else{
  117. missingIP = i;
  118. }
  119. //str += IPAddress((stat_info->ip).addr).toString();
  120. stat_info = STAILQ_NEXT(stat_info, next);
  121. }
  122. if(missingIP != 255 && ConnectionList[missingIP].addr != 0){
  123. //Ip NOT found; This controller is gone.
  124. ConnectionList[missingIP].addr = 0;
  125. Serial.printf("Controller %d disconnected \n\r", missingIP);
  126. }
  127. }
  128. }
  129. //new controller
  130. else if(n == 1){
  131. Serial.println("New controller connected..");
  132. while (stat_info != NULL){
  133. Serial.println(IPAddress((stat_info->ip).addr).toString());
  134. char addedIP = 0;
  135. for(char i=0; i < MAX_CLIENTS; i++){
  136. Serial.println(ip_addr_cmp(&ConnectionList[i], &stat_info->ip));
  137. if(ip_addr_cmp(&ConnectionList[i], &stat_info->ip)){
  138. addedIP = 255;
  139. break;
  140. }else{
  141. addedIP = i;
  142. }
  143. }
  144. if(addedIP != 255){
  145. //Ip NOT found; This controller is new.
  146. for(char i=0; i < MAX_CLIENTS; i++){
  147. if(ConnectionList[i].addr == 0){
  148. ConnectionList[i] = stat_info->ip;
  149. Serial.printf("Controller connect, place of index: %d \n\r", i);
  150. break;
  151. }
  152. }
  153. }
  154. Serial.println("---");
  155. stat_info = STAILQ_NEXT(stat_info, next);
  156. }
  157. }
  158. }
  159. void setupWifi(void){
  160. WiFi.disconnect(true);
  161. WiFi.mode(WIFI_AP);
  162. generateWiFiSSID();
  163. generateWiFiPassword();
  164. WiFi.softAP(WifiSSID, WifiPassword, 6, 1);
  165. }
  166. void generateWiFiSSID()
  167. {
  168. //Last 4 digits mac adress
  169. uint8_t mac[WL_MAC_ADDR_LENGTH];
  170. WiFi.softAPmacAddress(mac);
  171. String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
  172. String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
  173. macID.toUpperCase();
  174. WifiSSIDPrefix += macID;
  175. memset(WifiSSID, 0, WifiSSIDPrefix.length() + 1);
  176. for (int i=0; i<WifiSSIDPrefix.length(); i++)
  177. WifiSSID[i] = WifiSSIDPrefix.charAt(i);
  178. }
  179. void generateWiFiPassword()
  180. {
  181. String WifiStringPassword;
  182. for(char i=0; i < 17; i++){
  183. String s = String(WifiSSID[i], HEX);
  184. WifiStringPassword = s + WifiStringPassword;
  185. }
  186. for (int i=0; i < 10; i++)
  187. WifiPassword[i] = WifiStringPassword.charAt(i);
  188. WifiPassword[9] = '\0';
  189. Serial.print("Wifi password: ");
  190. Serial.println(WifiPassword);
  191. }
  192. //Serial commands
  193. void versionInfo(String data){
  194. Serial.println("Version 1.0");
  195. }
  196. void sendRumble(String data);
  197. void sendShock(String data);
  198. void (*commandList[3])(String data) = {
  199. versionInfo,
  200. sendRumble,
  201. sendShock
  202. };