0

I tried to Run this code but the connection fails and HTTPCode returns -1 , I'm using WIFI manager and it works sucessfully connected to the network the link is worked successfully on POSTman and data posted in Database

Hi All, I tried to Run this code but the connection fails and HTTPCode returns -1 , I'm using WIFI manager and it works sucessfully connected to the network the link is worked successfully on POSTman and data posted in Database

#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,168,1,1);
IPAddress subnet750_100(255,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);

}
5
  • http versus https? Commented Oct 23, 2020 at 13:14
  • what do you mean ? Commented Oct 23, 2020 at 13:41
  • any help please ?? Commented Oct 23, 2020 at 14:56
  • client.connect takes only a host name without http://. why do you client.connect if you use HttpClient? Commented Oct 23, 2020 at 15:41
  • 1- I tried without http:// and \it is not working .... 2- I\'m using client.connect to check the server connection it returns connection fails , and I tried to use only http client but it returns -1 Commented Oct 23, 2020 at 15:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.