Using WiFiClient: compilation error ' "server" not declared in scope '

When compiling this program from IoT with ESP8266: BMP280 and ESP8266 I get no error, here below:

/*
   http://www.instructables.com/id/Send-sensor-data-DHT11-BMP180-to-ThingSpeak-with-a/  for use of ESP8266 as controller
   https://techtutorialsx.com/2016/05/22/esp8266-connection-to-ds3231-rtc/  for correct use of DS3231
   https://github.com/Makuna/Rtc/blob/master/examples/DS3231_Simple/DS3231_Simple.ino for an example by the author of the library

  v2: added I2C code for BMP280 from following site:
   https://myesp8266.blogspot.be/2016/12/bmp280-and-esp8266.html
  v3: exact copy from above link

*/
 /**********************************************
 * Catalin Batrinu bcatalin@gmail.com 
 * Read temperature and pressure from BMP280
 * and send it to thingspeaks.com
**********************************************/

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <ESP8266WiFi.h>
/*
#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11 
#define BMP_CS 10
*/
Adafruit_BMP280 bme; // I2C
// replace with your channel’s thingspeak API key,
String apiKey = "YOUR-API-KEY";
const char* ssid = "YOUR-SSID";
const char* password = "YOUR-ROUTER-PASSWORD";
const char* server = "api.thingspeak.com";
WiFiClient client;


/**************************  
 *   S E T U P
 **************************/
void setup() {
  Serial.begin(9600);
  Serial.println(F("BMP280 test"));
  
  if (!bme.begin()) {  
    Serial.println("Could not find a valid BMP280 sensor, check wiring!");
    while (1);
  }
  WiFi.begin(ssid, password);
  
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");  
}

  /**************************  
 *  L O O P
 **************************/
void loop() {
    Serial.print("T=");
    Serial.print(bme.readTemperature());
    Serial.print(" *C");
    
    Serial.print(" P=");
    Serial.print(bme.readPressure());
    Serial.print(" Pa");

    Serial.print(" A= ");
    Serial.print(bme.readAltitude(1013.25)); // this should be adjusted to your local forcase
    Serial.println(" m");

    if (client.connect(server,80))  // "184.106.153.149" or api.thingspeak.com
    {
        String postStr = apiKey;
        postStr +="&field1=";
        postStr += String(bme.readTemperature());
        postStr +="&field2=";
        postStr += String(bme.readPressure());
        postStr += "\r\n\r\n";
        
        client.print("POST /update HTTP/1.1\n");
        client.print("Host: api.thingspeak.com\n");
        client.print("Connection: close\n");
        client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
        client.print("Content-Type: application/x-www-form-urlencoded\n");
        client.print("Content-Length: ");
        client.print(postStr.length());
        client.print("\n\n");
        client.print(postStr);    
    }
    client.stop(); 
    //every 20 sec   
    delay(20000);
}

When compiling this version below which uses the same libraries, I get a library related error (copy in attachment). What might be wrong here?

As a bonus: why does the compiler often use libraries not stored in my standard Arduino Library folder but uses a folder in C:\Users\Myname\AppData\Local\Arduino15\packages.... ? Annoying because -as far as I know- I have no control over what is stored there..

/*
   http://www.instructables.com/id/Send-sensor-data-DHT11-BMP180-to-ThingSpeak-with-a/  for use of ESP8266 as controller
   https://techtutorialsx.com/2016/05/22/esp8266-connection-to-ds3231-rtc/  for correct use of DS3231
   https://github.com/Makuna/Rtc/blob/master/examples/DS3231_Simple/DS3231_Simple.ino for an example by the author of the library

  v2: added I2C code for BMP280 from following site:
   https://myesp8266.blogspot.be/2016/12/bmp280-and-esp8266.html

*/
#include <Wire.h>
#include "Adafruit_HTU21DF.h"
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <RtcDS3231.h>
#include <ESP8266WiFi.h> // ESP8266WiFi.h library
Adafruit_BMP280 bmp; // I2C

RtcDS3231<TwoWire> rtcObject(Wire);
#define SQW_RATE_1K
#define OUTPUT_SQW
byte setOutput = 0;
int setSQWRate = 2;

const char* ssid     = "Lieve-router";
const char* password = "xxxxx";
const char* host = "api.thingspeak.com";
const char* writeAPIKey = "xxxxx";
//  String apiKey = "YOUR-API-KEY";
WiFiClient client;

unsigned long previousMillis = 1000;
int previousTime = 0;
int currentTime;
const long interval = 1000; //3600000 = elk uur
int pressure;   //To store the barometric pressure (Pa)
int temperature;
// float temperature;  //To store the temperature (oC)
int altimeter;    //To store the altimeter (m) (you can also use it as a float variable)


void setup() {

  rtcObject.Begin();    //Starts I2C
  RtcDateTime compileDateTime(__DATE__, __TIME__);
  // RtcDateTime currentTime = RtcDateTime(16,05,18,21,20,0); //define date and time object
  rtcObject.SetDateTime(currentTime);                      //configure the RTC with object
  // Initialize sensor
  bmp.begin();
  Serial.begin(9600);
  //pinMode(chip_select_pin, OUTPUT);

  //  Connect to WiFi network
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

void loop() {
  RtcDateTime currentTime = rtcObject.GetDateTime();    //get the time from the RTC

  if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
  {
    String postStr = apiKey;
    postStr += "&field1=";
    postStr += String(bme.readTemperature());
    postStr += "&field2=";
    postStr += String(bme.readPressure());
    postStr += "\r\n\r\n";

    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);
  }
  client.stop();

  /*
    char str[15];   //declare a string as an array of chars

    sprintf(str, "%d/%d/%d %d:%d:%d",     //%d allows to print an integer to the string
            currentTime.Year(),   //get year method
            currentTime.Month(),  //get month method
            currentTime.Day(),    //get day method
            currentTime.Hour(),   //get hour method
            currentTime.Minute(), //get minute method
            currentTime.Second()  //get second method
           );

    Serial.println(str); //print the string to the serial port
  */

  delay(60000); //60 seconds delay


}

ESP8266_BMP280_HTU21_thingspeak_v2-error-code.txt (46.7 KB)

In the working code you have a declaration of server:

const char* server = "api.thingspeak.com";

In the non-working code you do not, thus the error.

Although it seems counterintuitive, it's actually better to turn off verbose compilation output when posting errors/warnings to the forum (File > Preferences > Show verbose output during > compilation (uncheck) > OK). The information the verbose output provides is only needed very rarely and the extra characters can push you over the 9000 character limit. Attaching a txt file with the error message is fine when absolutely necessary but it may cause some people to not bother/be able to download and open the file so you have a lesser chance of getting help. If we do end up needing verbose output we can always ask for it. Now for upload errors I think it's always useful to have the verbose output since the non-verbose output doesn't give much helpful information.

brice3010:
As a bonus: why does the compiler often use libraries not stored in my standard Arduino Library folder but uses a folder in C:\Users\Myname\AppData\Local\Arduino15\packages.... ? Annoying because -as far as I know- I have no control over what is stored there..

The Arduino IDE tries to automatically figure out which is the best library to use when multiple libraries contain the filename you include . One of the primary things that determines which library gets include priority is architecture matching. The architecture of a library is defined in its library.properties file. The order is: exact match > wildcard match > mismatch. Another criterion is library folder name. If the library folder name matches the included filename that file gets priority. If both of those are equal then the library in the sketchbook folder gets include priority. The libraries at C:\Users\Myname\AppData\Local\Arduino15\packages.... are those bundled with the hardware package. They are written specifically for the the board you have currently selected in the Tools > Board menu.

Hi pert, thank you for your time to answer so extensively.
I am sorry, I should have seen such a stupid error.