Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <SoftwareSerial.h>
  3. #include <ESP8266_Simple.h>
  4.  
  5. // Setup SoftwareSerial to pins (RX, TX)
  6. ESP8266_Simple wifi(8,9);
  7.  
  8. // Motor Pins
  9. const int left = 11;
  10. const int middle = 3;
  11. const int right = 5;
  12.  
  13. // Values
  14. const int idle = 2500; //Time in between vibrations
  15. const int active = 1500; //Time in ms the motor vibrates each cicle
  16. const int strength = 800; //Regulates voltage (WARNING: Max 4V!)
  17. String proxRouter ; //the number of the router thats closest.,,
  18.  
  19. void setup()
  20. {
  21. Serial.begin(115200);
  22. Serial.println("ESP8266 Demo Sketch");
  23. wifi.begin(9600);
  24. pinMode(left,OUTPUT);
  25. pinMode(middle,OUTPUT);
  26. pinMode(right,OUTPUT);
  27. // A blank line just for debug formatting
  28. Serial.println();
  29. }
  30.  
  31. void loop()
  32. {
  33. proxRouter = "No zone in range";
  34. String scan_results;
  35. String Zone1 = "Zone 1";
  36. String Zone2 = "Zone 2";
  37. String Zone3 = "Zone 3";
  38. byte responseCode;
  39. char buffer[500];
  40. // Debug formatting
  41. Serial.println();
  42. Serial.print("Getting Access Points: ");
  43. // Send to scan networks command to the ESP8266
  44. if((responseCode = wifi.sendCommand(F("AT+CWLAP" ), buffer, sizeof(buffer))) == ESP8266_OK)
  45. {
  46. // The command worked:
  47. Serial.println("OK");
  48. Serial.println(buffer);
  49. scan_results = buffer;
  50. int strongest_RSSI;
  51. String strongest_SSID;
  52. // Loop through each line, the start of a line is defined by a + .
  53. for (int line = scan_results.indexOf('+'); line < scan_results.length();)
  54. {
  55. // At the start of the loop, proxRouter is wiped to prevent motors being
  56. // activated when one of our routers has been the proxRouter in the previous loop(s).
  57. // To retreive the signal strength and SSID the index of multiple ','
  58. // are found and used to only get the usefull parts of the output.
  59. int commaIndex = scan_results.indexOf(',', line);
  60. int secondCommaIndex = scan_results.indexOf(',', commaIndex + 1);
  61. int thirdCommaIndex = scan_results.indexOf(',', secondCommaIndex + 1);
  62. int fourthCommaIndex = scan_results.indexOf(',', thirdCommaIndex + 1);
  63. int fifthCommaIndex = scan_results.indexOf(',', fourthCommaIndex + 1);
  64. String SSID_line_raw = scan_results.substring(commaIndex + 1, secondCommaIndex);
  65. String RSSI_line_noInt = scan_results.substring(secondCommaIndex +1, thirdCommaIndex);
  66. String SSID_line_raw2 = SSID_line_raw;
  67. SSID_line_raw2.remove(SSID_line_raw.length() -1);
  68. String SSID_line = SSID_line_raw2;
  69. SSID_line.remove(0, 1);
  70. int RSSI_line = abs(RSSI_line_noInt.toInt());
  71. // At the start of the first line, the strongest SSID cannot be encoutered yet so:
  72. if(line == 0){
  73. strongest_RSSI = 100;
  74. strongest_SSID = "No Zones in range";
  75. }
  76. // Find the stongest network in the scan.
  77. if ((RSSI_line < strongest_RSSI) && (SSID_line.equals(Zone1) || SSID_line.equals(Zone2) || SSID_line.equals(Zone3))){
  78. strongest_RSSI = RSSI_line;
  79. strongest_SSID = SSID_line;
  80. proxRouter = SSID_line;
  81. }
  82. // Iterate to the next line.
  83. line = scan_results.indexOf('+', line + 1);
  84. }
  85. // Print results. Later this can be saved to output for vib. motors.
  86. Serial.print(strongest_SSID);
  87. Serial.print(", strength: ");
  88. Serial.println(strongest_RSSI);
  89. if(proxRouter.equals(Zone1))
  90. {
  91. Serial.println("Starting vibration Zone 1");
  92. digitalWrite(left, HIGH);
  93. delay(active);
  94. digitalWrite(left, LOW);
  95. Serial.println("Stopping vibration Zone 1");
  96. }
  97. else if(proxRouter.equals(Zone2))
  98. {
  99. Serial.println("Starting vibration Zone 2");
  100. digitalWrite(middle, HIGH);
  101. delay(active);
  102. digitalWrite(middle, LOW);
  103. Serial.println("Stopping vibration Zone 2");
  104. }
  105. else if(proxRouter.equals(Zone3))
  106. {
  107. Serial.println("Starting vibration Zone 3");
  108. digitalWrite(right, HIGH);
  109. delay(active);
  110. digitalWrite(right,LOW);
  111. Serial.println("Stopping vibration Zone 3");
  112. }
  113. }
  114. // Debug formatting
  115. Serial.println();
  116. // Wait before scanning again.
  117. delay(5);
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement