Weather Station Code
#include <>
/*H*******************************************************
 * Rui Santos 
 * Complete Project Details http://randomnerdtutorials.com
 ********************************************************/
// Load required libraries
#include <WiFi.h>
#include "SD.h"
#include "DHT.h"
#include <Wire.h>
#include <Adafruit_BMP085.h>

//************************* DEFINES ************************************

//************************* PROTOTYPES ************************************

//************************* VARIABLES ************************************
// Replace with your network credentials
const char* ssid     = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

// uncomment one of the lines below for whatever DHT sensor type you're using
//#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

// GPIO the DHT is connected to
const int DHTPin = 15;
//intialize DHT sensor
DHT dht(DHTPin, DHTTYPE);

// create a bmp object
Adafruit_BMP085 bmp;

// Web page file stored on the SD card
File webFile; 

// Set potentiometer GPIO
const int potPin = 32;

// IMPORTANT: At the moment, GPIO 4 doesn't work as an ADC when using the Wi-Fi library
// This is a limitation of this shield, but you can use another GPIO to get the LDR readings
const int LDRPin = 4;

// variables to store temperature and humidity
float tempC;
float tempF;
float humi;

// Variable to store the HTTP request
String header;

// Set web server port number to 80
WiFiServer server(80);


/*F********************************************************************
*
**********************************************************************/
void 
setup()
{    
    Serial.begin( 115200 );                        // INITIALIZE SERIAL PORT
    dht.begin();                                    // INITIALIZE DHT SENSOR

    if( !bmp.begin() )                           // INITIALIZE BMP180 SENSOR
    {
        Serial.println( "Could not find BMP180 or BMP085 sensor");
        while( 1 ) {}
    }
    if( !SD.begin() )                                  // INITIALIZE SD CARD
    {
        Serial.println( "Card Mount Failed" );
        return;
    }
    uint8_t cardType = SD.cardType();
    if( cardType == CARD_NONE )
    {
        Serial.println( "No SD card attached" );
        return;
    }
    Serial.println("Initializing SD card...");
    if( !SD.begin() )                                  // INITIALIZE SD CARD
    {
        Serial.println( "ERROR - SD card initialization failed!" );
        return;                                               // INIT FAILED
    }
    Serial.print( "Connecting to " );
    Serial.println(ssid);
    WiFi.begin( ssid, password );                // CONNECT TO Wi-Fi NETWORK

    while( WiFi.status() != WL_CONNECTED ) 
    {
        delay( 500 );
        Serial.print( "." );
    }
    Serial.println( "" );                          // PRINT LOCAL IP ADDRESS
    Serial.println( "WiFi connected." );
    Serial.println( "IP address: " );
    Serial.println( WiFi.localIP() );
    server.begin();                                      // START WEB SERVER
}
/*F********************************************************************
*
**********************************************************************/
void 
loop()
{
    WiFiClient client = server.available();   // LISTEN FOR INCOMING CLIENTS

    if( client ) 
    {  // IF NEW CLIENT CONNECTS
        boolean currentLineIsBlank = true;
        while( client.connected() ) 
        {
            if( client.available() ) 
            {   // CLIENT DATA AVAILABLE TO READ
                char c = client.read(); // READ 1 BYTE (CHARACTER) FROM CLIENT
                header += c;
                    // IF CURRENT LINE BLANK, GOT TWO NEWLINE CHARS IN A ROW
                    // THAT'S THE END OF THE CLIENT HTTP REQUEST, SO RESPOND:
                if( c == '\n' && currentLineIsBlank) 
                {                    // SEND A STANDARD HTTP RESPONSE HEADER
                    client.println("HTTP/1.1 200 OK");
                                                // SEND XML FILE OR WEB PAGE
                   // iF ALREADY ON WEB PAGE, BROWSER REQUESTS (AJAX) LATEST
                                    // SENSOR READINGS (ESP32 SENDS XML FILE)
                    if( header.indexOf("update_readings") >= 0) 
                    {                            // SEND REST OF HTTP HEADER
                        client.println( "Content-Type: text/xml" );
                        client.println( "Connection: keep-alive"  );
                        client.println();
                                       // SEND XML FILE WITH SENSOR READINGs
                        sendXMLFile( client );
                    }
                    // WHEN CLIENT 1ST CONNECTS, SEND IT INDEX.HTML FILE
                    // STORED IN THE microSD CARD
                    else 
                    {
                                                 // SEND REST OF HTTP HEADER
                        client.println( "Content-Type: text/html" );
                        client.println( "Connection: keep-alive" );
                        client.println();
                        // SEND WEB PAGE STORED IN microSD CARD
                        webFile = SD.open("/index.html" );
                        if( webFile ) 
                        {
                            while( webFile.available() ) 
                            { // SEND WEB PAGE TO CLIENT
                                client.write( webFile.read() ); 
                            }
                            webFile.close();
                        }
                    }
                    break;
                }
                // EVERY LINE OF TEXT RECEIVED FROM THE CLIENT ENDS WITH \r\N
                if( c == '\n' ) 
                {
                    // LAST CHARACTER ON LINE OF RECEIVED TEXT
                    // STARTING NEW LINE WITH NEXT CHARACTER READ
                    currentLineIsBlank = true;
                } 
                else if( c != '\r' ) 
                {               // A TEXT CHARACTER WAS RECEIVED FROM CLIENT
                    currentLineIsBlank = false;
                }
            } // END if( client.available())
        } // END while (client.connected())
        header = "";                                // CLEAR HEADER VARIABLE
        client.stop();                                  // CLOSE CONNECTION
        Serial.println( "Client disconnected." );
    } // end if( client )
}
/*F********************************************************************
* Send XML file with the latest sensor readings
**********************************************************************/
void 
sendXMLFile( WiFiClient cl)
{
    readDHT();                      // READ DHT SENSOR AND UPDATE VARIABLES
    cl.print( "<?xml version = \"1.0\" ?>" );            // PREPARE XML FILE
    cl.print( "<inputs>");
    cl.print( "<reading>");
    cl.print( tempC);
    cl.println( "</reading>");
    cl.print( "<reading>");
    cl.print( tempF);
    cl.println( "</reading>");
    cl.print( "<reading>");
    cl.print( humi );
    cl.println( "</reading>");
    float currentTemperatureC = bmp.readTemperature();
    cl.print( "<reading>" );
    cl.print( currentTemperatureC );
    cl.println( "</reading>" );
    float currentTemperatureF = (9.0/5.0)*currentTemperatureC+32.0;
    cl.print( "<reading>" );
    cl.print( currentTemperatureF );
    cl.println( "</reading>" );
    cl.print( "<reading>" );
    cl.print( bmp.readPressure() );
    cl.println( "</reading>" );
    cl.print( "<reading>" );
    cl.print( analogRead( potPin ) );
    cl.println( "</reading>" );
    // IMPORTANT: READ NOTE ABOUT GPIO 4 AT PIN ASSIGNMENT 
    cl.print( "<reading>" );
    cl.print( analogRead( LDRPin ) );
    cl.println( "</reading>" );
    cl.print( "</inputs>" );
}
/*F********************************************************************
*
**********************************************************************/
void 
readDHT()
{
    // SENSOR READINGS MAY ALSO BE UP TO 2 SECONDS 'OLD' (VERY SLOW SENSOR)
    humi = dht.readHumidity();
    tempC = dht.readTemperature();  // READ TEMPERATURE AS CELSIUS (DEFAULT)
    tempF = dht.readTemperature( true );          // READ TEMP AS fAHRENHEIT 
                                                    // (isFahrenheit = TRUE)
    // CHECK IF ANY READS FAILED AND EXIT EARLY (TO TRY AGAIN)
    if( isnan( humi ) || isnan( tempC ) || isnan( tempF ) ) 
    {
        Serial.println( "Failed to read from DHT sensor!" );
        return;
    }
    /*Serial.print("Humidity: ");
    Serial.print(humi);
    Serial.print(" %\t Temperature: ");
    Serial.print(tempC);
    Serial.print(" *C ");
    Serial.print(tempF);
    Serial.println(" *F");*/
}