ESP8266 HTTP客户端连接错误

时间:2018-11-21 09:58:08

标签: arduino esp8266 arduino-esp8266

此演示代码可以正常工作,但我的代码无法正常工作。 它返回“ HTTPC_ERROR_CONNECTION_REFUSED”(我的调试工作表明它无法创建TCP连接) 示例代码使用WiFiMulti,但这不是问题。

我想念什么? 请协助。

工作示例:

/**
 * BasicHTTPClient.ino
 *
 *  Created on: 24.05.2015
 *
 */

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;

void setup() {

    USE_SERIAL.begin(115200);
   // USE_SERIAL.setDebugOutput(true);

    USE_SERIAL.println();
    USE_SERIAL.println();
    USE_SERIAL.println();

    for(uint8_t t = 4; t > 0; t--) {
        USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
        USE_SERIAL.flush();
        delay(1000);
    }

    WiFi.mode(WIFI_STA);
    WiFiMulti.addAP("SSID", "PASSWORD");

}

void loop() {
    // wait for WiFi connection
    if((WiFiMulti.run() == WL_CONNECTED)) {

        HTTPClient http;

        USE_SERIAL.print("[HTTP] begin...\n");
        // configure traged server and url
        //http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
        http.begin("http://192.168.1.12/test.html"); //HTTP

        USE_SERIAL.print("[HTTP] GET...\n");
        // start connection and send HTTP header
        int httpCode = http.GET();

        // httpCode will be negative on error
        if(httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

            // file found at server
            if(httpCode == HTTP_CODE_OK) {
                String payload = http.getString();
                USE_SERIAL.println(payload);
            }
        } else {
            USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }
    http.end();
}

delay(10000);
}

我的代码:

#include <string>

#include <ESP8266HTTPClient.h>
#include <ESP8266WebServer.h>

//EEPROM
#include <EEPROM.h>

//Serial
#include <SoftwareSerial.h>   // We need this even if we're not using a SoftwareSerial object
#include <SerialCommand.h>

//Timer
#include <Ticker.h>





//***************************************************
#include "generalDeclerations.h"
#include "serialSetup.h"
#include "EEPROMAnything.h"
#include "ioControl.h"
#include "webServer.h"
//***************************************************


void setup() {

  Serial.begin(115200);
  EEPROM.begin(100);
  initIO();

  checkFD(); //Factory default ?

  initSerialSetup();  //Initelize serial setup interface
  bootPause();        //give the user a chance to interupt using the serial interface

  //***************************** Starting the flow from here

  //  loadEEPROM();
  initConnection();
  initWebServer();

  ledControl(HB_LED_PIN, 0.5);
  regData.attach(REG_DATA_INTERVAL, regData2DB);
}

void loop() {

  webServer.handleClient();
  SCmd.readSerial();     // We don't do much, just process serial commands

  //  collectData();
  //  regData();


  if ( (wifiMode == WIFI_AP) && (millis() - apTimeStamp > WIFI_RETRY_TIME_MILI_SECONDS)) {
    initConnection();
    apTimeStamp = millis();
  }

}



void regData2DB() {
  if (wifiMode != WIFI_STA) {
    Serial.println("no internet connection");
    return;
  }

  if (WiFi.status() != WL_CONNECTED ) return;


  //  http.begin(msg); //HTTP
  http.begin("http://google.com"); //HTTP


  int httpCode = http.GET();

  Serial.println(httpCode);
  //  http.returnError(httpCode);
  // httpCode will be negative on error
  if (httpCode > 0) {
    Serial.print("got respond");
    // HTTP header has been send and Server response header has been handled
    // file found at server
    if (httpCode == HTTP_CODE_OK) {
      Serial.println("and it is a GOOD one !");
      String payload = http.getString();
      if (http.getString() == "ok") {
        Serial.println(counter_in);
        Serial.println(counter_out);
        counter_in = 0;
        counter_out = 0;
      }

    }
  } else {
    Serial.println("and it is a BAD one !");
    Serial.println(counter_in);
    Serial.println(counter_out);

    //      ledControl(WIFI_LED_PIN, 0.1); //need to return the toggle rate once ok
  }
  http.end();
}


void initConnection() {
  loadEEPROM();
  WiFi.disconnect(true); //disconect from any activity ==> Fresh Start

  delay(3000);
  if (strcmp(eeprom.ssid, "") == 0) {
    //No SSID stored.. init as access point
    Serial.println("No SSID found in eeprom Starting AP mode:");
    WiFi.mode(WIFI_AP);
    WiFi.softAP(AP_SSID, AP_PASS);
    delay(5000);
    Serial.print(WiFi.softAPIP());
    apTimeStamp = millis();
    wifiMode = WIFI_AP;
    ledControl(WIFI_LED_PIN, 0.5);
  } else {
    Serial.print("Found SSID and PASS in eeprom, trying STA connect to: ");    Serial.print(eeprom.ssid);    Serial.print(" | ");    Serial.println(eeprom.pass);
    WiFi.mode(WIFI_STA);
    WiFi.begin(eeprom.ssid, eeprom.pass);
    delay(6000);
    int i = 0;
    while (WiFi.status() != WL_CONNECTED) {
      Serial.print("Failed ==>");
      if (i > 3) {
        Serial.println("Starting AP mode:");
        WiFi.mode(WIFI_AP);
        WiFi.softAP(AP_SSID, AP_PASS);
        apTimeStamp = millis();
        wifiMode = WIFI_AP;
        ledControl(WIFI_LED_PIN, 0.5);
        return;
      }
      else {
        Serial.println("Retrying..");
        WiFi.mode(WIFI_STA);
        WiFi.begin(eeprom.ssid, eeprom.pass);
        delay(5000);
      }
      i++;
    }
    wifiMode = WIFI_STA;
    Serial.print("Connected as STA:");
    Serial.println(WiFi.localIP());
    WiFi.setAutoReconnect(true);

    ledControl(WIFI_LED_PIN, HIGH);
  }
}

1 个答案:

答案 0 :(得分:0)

已解决。

我正在使用计时器调用regData2DB()函数...

当我使用以下命令从主循环调用regData2DB()函数时:

TABLE()

它开始工作。 不知道发生了什么,但似乎很稳定..继续:-)