Search code examples
jsonserial-portesp8266arduino-ide

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


The code should transfer an JSON object and the print "hello" in the void loop, but it just repeat the "dot" from the void setup. What did i miss?

I tried different serial prints in the main loop but none of them is actually transferred. Maybe there is an infinite loop in the 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");

}

It should transfer the dot, a json object and the word "hallo"


Solution

  • This is because the ESP8266 can't find a WiFi access point with SSID client-ssid and password client-password.

    Unless it gets connected to the said access point it will print . on the screen.

    You can change the access point parameters to one that visible in the range of ESP8266 alternatively you can change the access point SSID and password to one in the code.

    You can use the WiFi manager to avoid hard-code SSID names and password.