Advertisement
sspence65

DS18B20 ESP8266

Dec 13th, 2018
1,150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include <LiquidCrystal_I2C.h>
  2. LiquidCrystal_I2C lcd(0x27, 20, 4); // change to your I2C address and 16, 2 for a 2 line lcd
  3.  
  4. #include <OneWire.h>
  5. #include <DallasTemperature.h>
  6. #define ONE_WIRE_BUS 14 //D5
  7. OneWire oneWire(ONE_WIRE_BUS);
  8. DallasTemperature sensors(&oneWire);
  9.  
  10. #include <ESP8266WiFi.h>
  11. #include <WiFiClient.h>
  12. #include <ESP8266WebServer.h>
  13.  
  14. /*
  15. // NETWORK: Static IP details...
  16. IPAddress ip(192, 168, 1, 10); // a valid IP on your network
  17. IPAddress gateway(192, 168, 1, 1); //IP of your router / gateway
  18. IPAddress subnet(255, 255, 255, 0); //subnet mask of your network
  19. */
  20.  
  21. // Replace with your network credentials
  22. const char* ssid = "YOUR SSID";
  23. const char* password = "YOUR WIFI PASSWORD";
  24. ESP8266WebServer server(80); //instantiate server at port 80 (http port)
  25.  
  26. String page = "";
  27. String text = "";
  28. double data;
  29.  
  30.  
  31. void setup(){
  32. sensors.begin(); // DS18B20
  33. //pinMode(A0, INPUT);
  34. delay(1000);
  35. Serial.begin(115200);
  36.  
  37. // Static IP Setup Info Here...
  38. //WiFi.config(ip, gateway, subnet);
  39.  
  40. WiFi.begin(ssid, password); //begin WiFi connection
  41. Serial.println("");
  42.  
  43. // Wait for connection
  44. while (WiFi.status() != WL_CONNECTED) {
  45. delay(500);
  46. Serial.print(".");
  47. }
  48.  
  49. Serial.println("");
  50. Serial.print("Connected to ");
  51. Serial.println(ssid);
  52. Serial.print("IP address: ");
  53. Serial.println(WiFi.localIP());
  54. //lcd
  55. lcd.init();
  56. lcd.backlight();
  57. lcd.setCursor(5, 2);
  58. lcd.print(WiFi.localIP());
  59.  
  60. server.on("/data.txt", [](){
  61. text = (String)data;
  62. server.send(200, "text/html", text);
  63. });
  64.  
  65. server.on("/", [](){
  66. page = "<h1>Sensor to Node MCU Web Server</h1><h1>Temperature:</h1> <h1 id=\"data\">""</h1>\r\n";
  67. page += "<script>\r\n";
  68. page += "var x = setInterval(function() {loadData(\"data.txt\",updateData)}, 1000);\r\n";
  69. page += "function loadData(url, callback){\r\n";
  70. page += "var xhttp = new XMLHttpRequest();\r\n";
  71. page += "xhttp.onreadystatechange = function(){\r\n";
  72. page += " if(this.readyState == 4 && this.status == 200){\r\n";
  73. page += " callback.apply(xhttp);\r\n";
  74. page += " }\r\n";
  75. page += "};\r\n";
  76. page += "xhttp.open(\"GET\", url, true);\r\n";
  77. page += "xhttp.send();\r\n";
  78. page += "}\r\n";
  79. page += "function updateData(){\r\n";
  80. page += " document.getElementById(\"data\").innerHTML = this.responseText;\r\n";
  81. page += "}\r\n";
  82. page += "</script>\r\n";
  83. server.send(200, "text/html", page);
  84. });
  85.  
  86. server.begin();
  87. Serial.println("Web server started!");
  88. }
  89.  
  90.  
  91. void loop(){
  92. sensors.requestTemperatures(); // Send the command to get temperatures
  93. data = (sensors.getTempFByIndex(0));
  94. //data = analogRead(A0);
  95. delay(1000);
  96. server.handleClient();
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement