From 908c96e87020665fe71903c6050c1edbf853eddd Mon Sep 17 00:00:00 2001 From: "Ansgar [Hiajen]" Date: Sun, 27 Feb 2022 13:16:44 +0100 Subject: [PATCH] init code --- Weather.c.ino | 194 ++++++++++++++++++++++++++++++++++++++++++++++++ proj_password.h | 3 + 2 files changed, 197 insertions(+) create mode 100644 Weather.c.ino create mode 100644 proj_password.h diff --git a/Weather.c.ino b/Weather.c.ino new file mode 100644 index 0000000..731b857 --- /dev/null +++ b/Weather.c.ino @@ -0,0 +1,194 @@ +#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

+ + + +
+ + + + + + + + +
TimeTemperature (°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 +} diff --git a/proj_password.h b/proj_password.h new file mode 100644 index 0000000..b946aec --- /dev/null +++ b/proj_password.h @@ -0,0 +1,3 @@ +//SSID and Password of your WiFi router +const char* ssid = ""; +const char* password = ""; \ No newline at end of file