0

localhost(127.0.0.1)Arduino ESP8266またはWeMos mini や NodeMCU などの互換ボードをローカル サーバーに接続しようとしています172.xx.xx.xxx port 80が、httpResponseCode-1 エラーが発生します。しかし、リモートサーバーに接続している場合、httpResponseCode 200 (OK).

私はサーバーを実行していXMPPます (Coldfusion サーバーでテスト済みです) 私のコードは以下の通りです。誰でも助けることができますか?

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

const char* ssid = "ssid";
const char* password = "password";

//Your Domain name with URL path or IP address with path
String serverName = "http://stackoverflow.com";// returns 200 (ok results)
//String serverName = "127.0.0.1"; // Gives -1 error, Tried IP addresses with 172.xx.xx.xx or even 192.168.xx.xx.

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
// unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

void setup() {
  Serial.begin(115200); 
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if (WiFi.status() == WL_CONNECTED){
      HTTPClient http;
      String serverPath = serverName + "?temperature=24.37";
      // Your Domain name with URL path or IP address with path
      //http.begin(serverPath.c_str());
        http.begin(serverName);
      // Send HTTP GET request
        int httpResponseCode = http.GET();
      
        if (httpResponseCode>0) {
          Serial.print("HTTP Response code: ");
          Serial.println(httpResponseCode);
          String payload = http.getString();
          Serial.println(payload);
        } else {
          Serial.print("Error code: ");
          Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    } else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}
4

1 に答える 1