I have a project that have one ESP8266 as AP and others ESP8266 as STA.
As it is obvious, the ESP stations conect on ESP AP.
When the station start and conect to AP, it send a hello with some parameters behind httpClient.
Look this:
#include "Arduino.h"
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <FS.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "AsyncJson.h"
#include "ArduinoJson.h"
#include <StreamString.h>
#include "TimeLib.h"
#include <ESP8266HTTPClient.h>
#include <stdio.h>
#include "lib.h"
#include "version.h"
#include "wifihwtypes.h"
void WiFiDCC::hello() {
#ifndef RELEASE
Serial.printf_P( pgm_INFunction, "hello()", ESP.getFreeHeap() );
#endif
HTTPClient http;
String payload;
String url;
url = "http://" + wifi.gateway.toString() + "/gsr?qt=run&q=hello&hwType=" + hw.HW.Type + "&hwSN=" + lib.urlencode(searchSN()) + "&IP=" + wifi.IP.toString() + "&hwName=" + lib.urlencode(hw.hwName);
#ifndef RELEASE
Serial.println(url);
#endif
if ( WiFi.status() == WL_CONNECTED ) {
http.begin(url);
int httpCode = http.GET();
#ifndef RELEASE
if (httpCode < 0) {
Serial.printf("\n[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
#endif
payload = http.getString();
http.end();
#ifndef RELEASE
Serial.print("payload: ");
Serial.println( payload );
#endif
DynamicJsonBuffer jsonBuffer(32);
JsonObject& root = jsonBuffer.parseObject(payload);
IPWiFiStation = lib.strToIP(root["ip"]);
} else {
#ifndef RELEASE
Serial.println( F("Desconectado enquanto iniciava hello()") );
#endif
}
payload = String();
url = String();
#ifndef RELEASE
Serial.printf_P( pgm_OUTFunction, "hello()", ESP.getFreeHeap() );
#endif
}
The problem is: The hello() function works perfectly at the first time that is called.
Next call of hello(), I receive: -1 Connection Refused
Somebody have a tip, trick, a magic, or who know the answer for this?
Ricardo B. Pacheco