דברים שרואים מכאן לא רואים בכלל
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

HuzzahAPDistance.ino 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Copyright (c) 2015, Majenko Technologies
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright notice, this
  7. list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright notice, this
  9. list of conditions and the following disclaimer in the documentation and/or
  10. other materials provided with the distribution.
  11. * * Neither the name of Majenko Technologies nor the names of its
  12. contributors may be used to endorse or promote products derived from
  13. this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /* Create a WiFi access point and provide a web server on it. */
  26. #include <ESP8266WiFi.h>
  27. #include <WiFiClient.h>
  28. #include <ESP8266WebServer.h>
  29. IPAddress local_IP(192, 168, 115, 23);
  30. IPAddress gateway(192, 168, 115, 1); // bogus
  31. IPAddress subnet(255, 255, 255, 0);
  32. #ifndef APSSID
  33. #define APSSID "batcave"
  34. #define APPSK "superfokensonic"
  35. #endif
  36. /* Set these to your desired credentials. */
  37. const char *ssid = APSSID;
  38. const char *password = APPSK;
  39. ESP8266WebServer server(80);
  40. const int trigPin = 12;
  41. const int echoPin = 13;
  42. unsigned long measureDistance() {
  43. digitalWrite(trigPin, LOW);
  44. delayMicroseconds(2);
  45. digitalWrite(trigPin, HIGH);
  46. delayMicroseconds(10);
  47. digitalWrite(trigPin, LOW);
  48. unsigned long duration = pulseIn(echoPin, HIGH);
  49. return (duration * 343) / 2000; // speed of sound is 0.343 mm/microsec (halve because 2 way)
  50. }
  51. void handleRoot() {
  52. struct station_info *stat_info;
  53. stat_info = wifi_softap_get_station_info();
  54. uint8_t client_count = wifi_softap_get_station_num();
  55. u32_t address;
  56. String str = "Number of clients = ";
  57. str += String(client_count);
  58. str += "<br/>\r\nList of clients : <br/>\r\n";
  59. int i = 1;
  60. while (stat_info != NULL) {
  61. str += "Station #";
  62. str += String(i);
  63. str += " : ";
  64. str += String(stat_info->bssid[0], HEX);
  65. str += ":";
  66. str += String(stat_info->bssid[1], HEX);
  67. str += ":";
  68. str += String(stat_info->bssid[2], HEX);
  69. str += ":";
  70. str += String(stat_info->bssid[3], HEX);
  71. str += ":";
  72. str += String(stat_info->bssid[4], HEX);
  73. str += ":";
  74. str += String(stat_info->bssid[5], HEX);
  75. str += " ";
  76. address = (&stat_info->ip)->addr;
  77. str += ((unsigned char*)&address)[0];
  78. str += ".";
  79. str += ((unsigned char*)&address)[1];
  80. str += ".";
  81. str += ((unsigned char*)&address)[2];
  82. str += ".";
  83. str += ((unsigned char*)&address)[3];
  84. str += "<br/>\r\n";
  85. i++;
  86. stat_info = STAILQ_NEXT(stat_info, next);
  87. }
  88. str += "<a target=\"_blank\" href=\"d\">Distance</a><br/>\r\n";
  89. server.send(200, "text/html", str);
  90. }
  91. void handleDistance() {
  92. unsigned long distance = measureDistance();
  93. // Serial.println(distance);
  94. String str = "{\"distance\":" + String(distance, DEC) + "}";
  95. server.send(200, "application/json", str);
  96. }
  97. void setup() {
  98. pinMode(trigPin, OUTPUT);
  99. pinMode(echoPin, INPUT);
  100. delay(1000);
  101. Serial.begin(115200);
  102. Serial.println();
  103. Serial.print("Configuring access point...");
  104. /* You can remove the password parameter if you want the AP to be open. */
  105. Serial.print("Setting soft-AP configuration ... ");
  106. Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
  107. WiFi.softAP(ssid, password);
  108. IPAddress myIP = WiFi.softAPIP();
  109. Serial.print("AP IP address: ");
  110. Serial.println(myIP);
  111. server.on("/", handleRoot);
  112. server.on("/d", handleDistance);
  113. server.begin();
  114. Serial.println("HTTP server started");
  115. }
  116. void loop() {
  117. server.handleClient();
  118. }