init code
This commit is contained in:
parent
9fd0ea3b2c
commit
908c96e870
2 changed files with 197 additions and 0 deletions
194
Weather.c.ino
Normal file
194
Weather.c.ino
Normal file
|
@ -0,0 +1,194 @@
|
||||||
|
#include <Adafruit_BMP280.h>
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <WiFiClient.h>
|
||||||
|
#include <ESP8266WebServer.h>
|
||||||
|
#include "DHT.h"
|
||||||
|
#include <SPI.h>
|
||||||
|
|
||||||
|
#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"=====(
|
||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Weather</title>
|
||||||
|
<h1 style="text-align:center; color:red;">Some Weather ....</h1>
|
||||||
|
<h3 style="text-align:center;">NodeMCU Data Logger</h3>
|
||||||
|
<style>
|
||||||
|
canvas{
|
||||||
|
-moz-user-select: none;
|
||||||
|
-webkit-user-select: none;
|
||||||
|
-ms-user-select: none;
|
||||||
|
}
|
||||||
|
/* Data Table Styling*/
|
||||||
|
#dataTable {
|
||||||
|
font-family: sans-serif;
|
||||||
|
border-collapse: collapse;
|
||||||
|
width: 100%;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
#dataTable td, #dataTable th {
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
padding: 8px;
|
||||||
|
}
|
||||||
|
#dataTable tr:nth-child(even){background-color: #f2f2f2;}
|
||||||
|
#dataTable tr:hover {background-color: #ddd;}
|
||||||
|
#dataTable th {
|
||||||
|
padding-top: 12px;
|
||||||
|
padding-bottom: 12px;
|
||||||
|
text-align: center;
|
||||||
|
background-color: #050505;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
<table id="dataTable">
|
||||||
|
<tr>
|
||||||
|
<th>Time</th>
|
||||||
|
<th>Temperature (°C)</th>
|
||||||
|
<th>Temperature 2 (°C)</th>
|
||||||
|
<th>Humidity (%)</th>
|
||||||
|
<th>Pressure (hPa)</th>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<br>
|
||||||
|
<br>
|
||||||
|
<script>
|
||||||
|
var Tvalues = [];
|
||||||
|
var Hvalues = [];
|
||||||
|
var timeStamp = [];
|
||||||
|
var T2value = [];
|
||||||
|
var Pvalue = [];
|
||||||
|
setInterval(function() {
|
||||||
|
// Call a function repetatively with 5 Second interval
|
||||||
|
getData();
|
||||||
|
}, 5000); //5000mSeconds update rate
|
||||||
|
function getData() {
|
||||||
|
var xhttp = new XMLHttpRequest();
|
||||||
|
xhttp.onreadystatechange = function() {
|
||||||
|
if (this.readyState == 4 && this.status == 200) {
|
||||||
|
//Push the data in array
|
||||||
|
var time = new Date().toLocaleTimeString();
|
||||||
|
var txt = this.responseText;
|
||||||
|
var obj = JSON.parse(txt);
|
||||||
|
Tvalues.push(obj.Temperature);
|
||||||
|
Hvalues.push(obj.Humidity);
|
||||||
|
T2value.push(obj.Temperature_2);
|
||||||
|
Pvalue.push(obj.Pressure);
|
||||||
|
timeStamp.push(time);
|
||||||
|
|
||||||
|
if(Tvalues.length > 10){
|
||||||
|
Tvalues.pop();
|
||||||
|
Hvalues.pop();
|
||||||
|
timeStamp.pop();
|
||||||
|
T2value.pop();
|
||||||
|
Pvalue.pop();
|
||||||
|
document.getElementById("dataTable").deleteRow(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Update Data Table
|
||||||
|
var table = document.getElementById("dataTable");
|
||||||
|
var row = table.insertRow(1); //Add after headings
|
||||||
|
var cell1 = row.insertCell(0);
|
||||||
|
var cell2 = row.insertCell(1);
|
||||||
|
var cell3 = row.insertCell(2);
|
||||||
|
var cell4 = row.insertCell(3);
|
||||||
|
var cell5 = row.insertCell(4);
|
||||||
|
|
||||||
|
cell1.innerHTML = time;
|
||||||
|
cell2.innerHTML = obj.Temperature;
|
||||||
|
cell3.innerHTML = obj.Temperature_2;
|
||||||
|
cell4.innerHTML = obj.Humidity;
|
||||||
|
cell5.innerHTML = obj.Pressure;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xhttp.open("GET", "readData", true); //Handle readData server on ESP8266
|
||||||
|
xhttp.send();
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
|
)=====";
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
3
proj_password.h
Normal file
3
proj_password.h
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
//SSID and Password of your WiFi router
|
||||||
|
const char* ssid = "";
|
||||||
|
const char* password = "";
|
Loading…
Reference in a new issue