微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nodemcu esp8266 http请求返回-1“连接失败”

如何解决nodemcu esp8266 http请求返回-1“连接失败”

我尝试运行此代码,但连接失败并且HTTPCode返回-1,我使用的是WIFI管理器,并且已成功连接到网络 该链接已在POSTman上成功工作,并且数据已发布在数据库

大家好,我尝试运行此代码,但是连接失败并且HTTPCode返回-1,我使用的是WIFI管理器,并且可以成功连接到网络 该链接已在POSTman上成功工作,并且数据已发布在数据库

#include <OneWire.h>
#include <Dallastemperature.h>
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#include <ESP8266HTTPClient.h>
#include <Wire.h>

IPAddress staticIP750_100(192,168,1,16);
IPAddress gateway750_100(192,1);
IPAddress subnet750_100(255,255,0);

   HTTPClient http; 



#define ONE_WIRE_BUS 2 // DS18B20 on NodeMCU pin D4 

OneWire oneWire(ONE_WIRE_BUS);
Dallastemperature DS18B20(&oneWire);

float temp_0;
  const char* host = "http://mysite2020.info";
  

void setup() 
{
 

  Serial.begin(115200);
   WiFiManager wifimanger ;

  DS18B20.begin();

Wire.begin(D2,D1)
 
 readTemp();
 
  WiFi.mode(WIFI_STA);

  wifimanger.autoConnect("Inlet Device","12345");
  
  while ((!(WiFi.status() == WL_CONNECTED))){
    delay(300);
    Serial.println("...");

  }

  Serial.println("WiFi connected");  

  WiFi.config(staticIP750_100,gateway750_100,subnet750_100);
  WiFi.hostname("Inlet Device") ;
  
delay(3000);
Serial.println("Welcome To Device No : 1");

  Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

 
    Serial.println("Your Gateway is : ");

    Serial.println((WiFi.gatewayIP().toString().c_str()));

  Serial.println("Your Hostname is : ");

  Serial.println((WiFi.hostname()));


delay(3000);

   Serial.println("Welcome to my  Project!");

 delay(3000);


}
void loop() {
  
Serial.println(host); // Use WiFiClient class to create TCP connections 
WiFiClient client; 
const int httpPort = 80; 
if (!client.connect(host,httpPort)) 
{ 
Serial.println("connection Failed"); 
return; 
} 
Serial.print("Requesting URL: "); 


sendtemp ();
}

void sendtemp ()
{

 temp_0=23.26;
 String  url = "http://mysite2020.info/Api/insert_mssqlserver.PHP?Reading=" + String(temp_0);
 
  Serial.println(url);
    http.begin(url); 
http.addHeader("Content-Type","application/x-www-form-urlencoded"); 
auto httpCode = http.GET(); 
Serial.println(httpCode); //Print HTTP return code 
String payload = http.getString(); 
Serial.println(payload); //Print request response payload 
http.end(); //Close connection Serial.println(); 
Serial.println("closing connection");

  delay(500);

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。