Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27, 20, 4); // change to your I2C address and 16, 2 for a 2 line lcd
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #define ONE_WIRE_BUS 14 //D5
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- /*
- // NETWORK: Static IP details...
- IPAddress ip(192, 168, 1, 10); // a valid IP on your network
- IPAddress gateway(192, 168, 1, 1); //IP of your router / gateway
- IPAddress subnet(255, 255, 255, 0); //subnet mask of your network
- */
- // Replace with your network credentials
- const char* ssid = "YOUR SSID";
- const char* password = "YOUR WIFI PASSWORD";
- ESP8266WebServer server(80); //instantiate server at port 80 (http port)
- String page = "";
- String text = "";
- double data;
- void setup(){
- sensors.begin(); // DS18B20
- //pinMode(A0, INPUT);
- delay(1000);
- Serial.begin(115200);
- // Static IP Setup Info Here...
- //WiFi.config(ip, gateway, subnet);
- WiFi.begin(ssid, password); //begin WiFi connection
- Serial.println("");
- // Wait for connection
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to ");
- Serial.println(ssid);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- //lcd
- lcd.init();
- lcd.backlight();
- lcd.setCursor(5, 2);
- lcd.print(WiFi.localIP());
- server.on("/data.txt", [](){
- text = (String)data;
- server.send(200, "text/html", text);
- });
- server.on("/", [](){
- page = "<h1>Sensor to Node MCU Web Server</h1><h1>Temperature:</h1> <h1 id=\"data\">""</h1>\r\n";
- page += "<script>\r\n";
- page += "var x = setInterval(function() {loadData(\"data.txt\",updateData)}, 1000);\r\n";
- page += "function loadData(url, callback){\r\n";
- page += "var xhttp = new XMLHttpRequest();\r\n";
- page += "xhttp.onreadystatechange = function(){\r\n";
- page += " if(this.readyState == 4 && this.status == 200){\r\n";
- page += " callback.apply(xhttp);\r\n";
- page += " }\r\n";
- page += "};\r\n";
- page += "xhttp.open(\"GET\", url, true);\r\n";
- page += "xhttp.send();\r\n";
- page += "}\r\n";
- page += "function updateData(){\r\n";
- page += " document.getElementById(\"data\").innerHTML = this.responseText;\r\n";
- page += "}\r\n";
- page += "</script>\r\n";
- server.send(200, "text/html", page);
- });
- server.begin();
- Serial.println("Web server started!");
- }
- void loop(){
- sensors.requestTemperatures(); // Send the command to get temperatures
- data = (sensors.getTempFByIndex(0));
- //data = analogRead(A0);
- delay(1000);
- server.handleClient();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement