WiFi Client timeout


I am trying to make a wifi project, where data is read from a sensor, transmitted by wifi to a receiving unit and distributed to and eeprom and a display. But in the client part of the code, I keep getting “Client timeout” in my serial monitor. As far as I can see, that shouldn’t be the case… but it is, and I dont understand why. Can anyone of you help?

#include <ESP8266WiFi.h>
#include <Wire.h>

const char* ssid     = "16HF15SV4";         //Name of router acting as Access Point
const char* password = "12345678";          //Password set to access the Router
const char* host = "";          //IP address of RECEIVER
int Temp = 0x4D;                            //decimal address of TempSensor
int data = 0;
int value = 0;                              //Creating an integer 'value' to store our temperature reading in

void setup() {
  Wire.begin();                             //Start command to initialize the I2C function
  Serial.begin(9600);                       //Setting Baud Rate - must be the same all over
  delay(10);                                //Inserting a small delay to ensure the program works

  // We start by connecting to a WiFi network

  Serial.println();                         //New line in Serial Monitor
  Serial.print("Connecting to ");     
  Serial.println(ssid);                     //Printing the router name in Serial Monitor
  WiFi.begin(ssid, password);               //Logging on to the router using the router ID and the set password
  while (WiFi.status() != WL_CONNECTED) {   //If not connected to the router
    delay(500);                             //Wait 500 ms
    Serial.print(".");                      //Print a '.' in Serial Monitor to indicate waiting process

  Serial.println();                         //New line in Serial Monitor
  Serial.println("WiFi connected");         
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());           //Printing the IP address of the transmitting WIFI Module

void loop() {
  delay(5000);                              //Wait 1000 ms
  readtemp();                               //Calling routine 'readtemp()' (reading temp every 1 seconds-ish)

  Serial.print("connecting to ");
  Serial.println(host);                     //Printing the router name in Serial Monitor

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");

  // We now create a URI for the request
  String url = "/temp";
  url += "?value=";
  url += data;    

  Serial.print("Requesting URL: ");

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  unsigned long timeout = millis();           //Making a var named timeout. Timeout equals number of milliseconds the sketch has run so far
  while (client.available() == 0) {           //While client is NOT available
    if (millis() - timeout > 5000) {          //If the current number of milliseconds - the number stored in 'timeout' is greater than 5000
      Serial.println(">>> Client Timeout !"); //Print to serial monitor
      client.stop();                          //End client connection

  // Read all the lines of the reply from server and print them to Serial
  while (client.available()) {
    String line = client.readStringUntil('\r');

  Serial.println("closing connection");

void readtemp(void) {
  Wire.beginTransmission(Temp);     //start the communication with IC with the address xx
  Wire.write(0);                    //send a bit and ask for register zero
  Wire.endTransmission();           //end transmission
  Wire.requestFrom(Temp, 1);        //request 1 byte from address xx

  while (Wire.available() == 0);    //wait for response
  data = Wire.read();               //put the temperature in variable data

Are you certain this is doing what you expect?

  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");

Not too sure no.. as the sketch is created mainly from examples put together. But the way I see it, is that it sends a string to the host including the URL containing the data read from the temp sensor.

Try a Serial.print of that string of stuff. Does it look right?

The default behavior is to close the connection when the interaction is complete. There is no need to specifically request that action.

The Host line is needed only if the server is hosting multiple domains. It is EXTREMELY unlikely that a device in your local network is doing that.

Using Strings, just so you can concatenate stuff, is a waste of resources.

client.print("GET ");
client.println(" HTTP/1.1");

Better late than never. Thank you guys for the always competent help you provide. PaulS.. did as you suggested, and removed that part. Now it looks and works like a charm. :D