דברים שרואים מכאן לא רואים בכלל
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

HuzzahAPDistance.ino 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include "config.h" // if this file doesn't exist, copy config.h.example to it and edit
  5. IPAddress local_IP(192, 168, 115, 23);
  6. IPAddress gateway(192, 168, 115, 1); // bogus
  7. IPAddress subnet(255, 255, 255, 0);
  8. /* Set these to your desired credentials. */
  9. const char *ssid = APSSID;
  10. const char *password = APPSK;
  11. ESP8266WebServer server(80);
  12. const int trigPin = 12;
  13. const int echoPin = 13;
  14. unsigned long measureDistance() {
  15. digitalWrite(trigPin, LOW);
  16. delayMicroseconds(2);
  17. digitalWrite(trigPin, HIGH);
  18. delayMicroseconds(10);
  19. digitalWrite(trigPin, LOW);
  20. unsigned long duration = pulseIn(echoPin, HIGH);
  21. return (duration * 343) / 2000; // speed of sound is 0.343 mm/microsec (halve because 2 way)
  22. }
  23. void handleRoot() {
  24. struct station_info *stat_info;
  25. stat_info = wifi_softap_get_station_info();
  26. uint8_t client_count = wifi_softap_get_station_num();
  27. u32_t address;
  28. String str = "Number of clients = ";
  29. str += String(client_count);
  30. str += "<br/>\r\nList of clients : <br/>\r\n";
  31. int i = 1;
  32. while (stat_info != NULL) {
  33. str += "Station #";
  34. str += String(i);
  35. str += " : ";
  36. str += String(stat_info->bssid[0], HEX);
  37. str += ":";
  38. str += String(stat_info->bssid[1], HEX);
  39. str += ":";
  40. str += String(stat_info->bssid[2], HEX);
  41. str += ":";
  42. str += String(stat_info->bssid[3], HEX);
  43. str += ":";
  44. str += String(stat_info->bssid[4], HEX);
  45. str += ":";
  46. str += String(stat_info->bssid[5], HEX);
  47. str += " ";
  48. address = (&stat_info->ip)->addr;
  49. str += ((unsigned char*)&address)[0];
  50. str += ".";
  51. str += ((unsigned char*)&address)[1];
  52. str += ".";
  53. str += ((unsigned char*)&address)[2];
  54. str += ".";
  55. str += ((unsigned char*)&address)[3];
  56. str += "<br/>\r\n";
  57. i++;
  58. stat_info = STAILQ_NEXT(stat_info, next);
  59. }
  60. str += "<a target=\"_blank\" href=\"d\">Distance</a><br/>\r\n";
  61. server.send(200, "text/html", str);
  62. }
  63. void handleDistance() {
  64. unsigned long distance = measureDistance();
  65. // Serial.println(distance);
  66. String str = "{\"distance\":" + String(distance, DEC) + "}";
  67. server.send(200, "application/json", str);
  68. }
  69. void setup() {
  70. pinMode(trigPin, OUTPUT);
  71. pinMode(echoPin, INPUT);
  72. delay(1000);
  73. Serial.begin(115200);
  74. Serial.println();
  75. Serial.print("Configuring access point...");
  76. /* You can remove the password parameter if you want the AP to be open. */
  77. Serial.print("Setting soft-AP configuration ... ");
  78. Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
  79. WiFi.softAP(ssid, password);
  80. IPAddress myIP = WiFi.softAPIP();
  81. Serial.print("AP IP address: ");
  82. Serial.println(myIP);
  83. server.on("/", handleRoot);
  84. server.on("/d", handleDistance);
  85. server.begin();
  86. Serial.println("HTTP server started");
  87. }
  88. void loop() {
  89. server.handleClient();
  90. }