#include #include #include #include #include "DHT.h" #include #include "proj_password.h" #define LED 2 //On board LED #define DHTTYPE DHT22 // DHT 11 uint8_t DHTPin = 12; DHT dht(DHTPin, DHTTYPE); // define device I2C address: 0x76 or 0x77 (0x77 is library default address) #define BMP280_I2C_ADDRESS 0x76 // initialize Adafruit BMP280 library Adafruit_BMP280 bmp280; float humidity, temperature, temperature_2, pressure; ESP8266WebServer server(80); //Server on port 80 const char MAIN_page[] PROGMEM = R"=====( Weather

Some Weather ....

NodeMCU Data Logger

Time Temperature (°C) Temperature 2 (°C) Humidity (%) Pressure (hPa)


)====="; void handleRoot() { String s = MAIN_page; //Read HTML contents server.send(200, "text/html", s); //Send web page } void readData() { String data = "{\"Temperature\":\"" + String(temperature) + "\", \"Humidity\":\"" + String(humidity) + "\", \"Temperature_2\":\"" + String(temperature_2) + "\", \"Pressure\":\"" + String(pressure) + "\" }"; digitalWrite(LED,!digitalRead(LED)); //Toggle LED on data request ajax server.send(200, "text/plane", data); //Send ADC value, temperature and humidity JSON to client ajax request delay(2000); temperature = (dht.readTemperature()); humidity = dht.readHumidity(); // read temperature and pressure from BMP280 sensor temperature_2 = (bmp280.readTemperature()); // get temperature pressure = (bmp280.readPressure()/100); // get pressure Serial.println("T1: " + String(temperature) + " T2:" + String(temperature_2) + " H:" + String(humidity) + " P: " + String(pressure)); } void setup () { Serial.begin(115200); Serial.println(); pinMode(DHTPin, INPUT); dht.begin(); // initialize the BMP280 sensor //Wire.begin(D1, D2); // set I2C pins [SDA = D2, SCL = D1], default clock is 100kHz //Wire.begin(4, 0); Wire.begin(); while(!bmp280.begin(BMP280_I2C_ADDRESS)){ Serial.println("BPM280 put"); delay(1000); } WiFi.begin(ssid, password); //Connect to your WiFi router Serial.println(""); //Onboard LED port Direction output pinMode(LED,OUTPUT); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } //If connection successful show IP address in serial monitor Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); //IP address assigned to your ESP server.on("/", handleRoot); //Which routine to handle at root location. This is display page server.on("/readData", readData); //This page is called by java Script AJAX server.begin(); //Start server Serial.println("HTTP server started"); } void loop(void){ server.handleClient(); //Handle client requests }