为什么串行监视器只显示 "dot" 的无限循环而不是其他串行打印?

Why does the serial monitor just show an endless loop of the "dot" and not the other serial prints?

代码应该在 void 循环中传输一个 JSON 对象和打印 "hello",但它只是重复 void 设置中的 "dot"。我错过了什么?

我在主循环中尝试了不同的串行打印,但其中 none 实际上已传输。也许在 void setup 中有一个无限循环?

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#include <vector>

const char* apSsid     = "ap-ssid";
const char* apPassword = "ap-password";
const char* clientSsid     = "client-ssid";
const char* clientPassword = "client-password";


WiFiEventHandler probeRequestPrintHandler;

String macToString(const unsigned char* mac) {
  char buf[20];
  snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
           mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  return String(buf);
}

std::vector<WiFiEventSoftAPModeProbeRequestReceived> myList;

void onProbeRequestPrint(const WiFiEventSoftAPModeProbeRequestReceived& evt) {
  myList.push_back(evt);
}

void setup() {
  Serial.begin(115200);
  Serial.print("Hello!");

  // Don't save WiFi configuration in flash - optional
  WiFi.persistent(false);

  WiFi.mode(WIFI_AP_STA);
  WiFi.softAP(apSsid, apPassword);
  WiFi.begin(clientSsid, clientPassword);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(100);
  }
  Serial.print("");
  probeRequestPrintHandler = WiFi.onSoftAPModeProbeRequestReceived(&onProbeRequestPrint);
}

void loop() {
  delay(3000);
  String json = "";
  DynamicJsonBuffer jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  JsonArray& probes = root.createNestedArray("probes");
  for(WiFiEventSoftAPModeProbeRequestReceived w : myList){
    JsonObject& probe = probes.createNestedObject();
    probe["address"] = macToString(w.mac);
    probe["rssi"] = w.rssi;
  }
  myList.clear();
  root.printTo(json);
  Serial.print(json);
  Serial.print("hallo");

}

它应该转移点,一个json对象和单词"hallo"

这是因为ESP8266找不到SSID为client-ssid和密码为client-password的WiFi接入点。

除非它连接到所述接入点,否则它将在屏幕上打印 .

您可以将接入点参数更改为ESP8266范围内可见的参数,或者您可以将接入点SSID和密码更改为代码中的一个。

您可以使用 WiFi manager 来避免 hard-code SSID 名称和密码。