มอนิเตอร์อุณหภูมิออนไลน์ด้วย ESP8266 และ DHT11


ESP8266 เป็นโมดูลรับ-ส่งข้อมูลไร้สายผ่าน WiFI ที่สามารถเขียนโปรแกรมสั่งงานลงไปได้ ในบทความนี้เราก็จะมาลองเขียนโปรแกรมสร้าง Web Server ง่ายๆ ที่ใช้สำหรับแสดงค่าอุณหภูมิบนเว็บกันครับ
#include "DHT.h"
#include "ESP8266WiFi.h"
#include "WiFiClient.h"
#include "ESP8266WebServer.h"
#include "ESP8266mDNS.h"

#define DHTPIN D0
#define DHTTYPE DHT11
const char* ssid = "...";
const char* password = "...";

// HTML Page
String IndexPage =
"<!DOCTYPE HTML>\n"
"<html>\n"
"<head>\n"
"<meta charset=\"utf-8\">\n"
"<title>ESP-DHT By IOXhop</title>\n"
"<script>\n"
"function loadXMLDoc() {\n"
"  var xmlhttp;\n"
"  if (window.XMLHttpRequest) { \n"
"    xmlhttp=new XMLHttpRequest();\n"
"  } else { \n"
"    xmlhttp=new ActiveXObject(\"Microsoft.XMLHTTP\");\n"
"  }\n"
"  \n"
"  xmlhttp.onreadystatechange = function() {\n"
"    if (xmlhttp.readyState==4 && xmlhttp.status==200) {\n"
"      var objJSON = JSON.parse(xmlhttp.responseText);\n"
"      var d = new Date();\n"
"      document.getElementById(\"update\").innerHTML = d.getHours() + \":\" + d.getMinutes() + \":\" + d.getSeconds();\n"
"      document.getElementsByTagName(\"output\")[0].innerHTML=objJSON.temp+\" *C\";\n"
"      setTimeout(function(){ loadXMLDoc() }, 1000);\n"
"    }\n"
"  }\n"
"  xmlhttp.open(\"GET\", \"./temperature\", true);\n"
"  xmlhttp.send();\n"
"}\n"
"\n"
"loadXMLDoc();\n"
"</script>\n"
"</head>\n"
"\n"
"<body style=\"font-family: Arial, Helvetica, sans-serif;\">\n"
"<h1 style=\"text-align: center;\">E-TECH Temp Node</h1>\n"
"<h2 style=\"text-align: center;\">Temperature</h2>\n"
"<output style=\"text-align: center; font-size: 220px; display: block;\">0</output>\n"
"<p style=\"text-align: center;\">Update: <span id=\"update\">00:00</span></p>\n"
"</body>\n"
"</html>\n"
;

ESP8266WebServer server(80);
DHT dht(DHTPIN, DHTTYPE, 15);

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  dht.begin();
  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());
  
  if (!MDNS.begin("temp")) {
    Serial.println("Error setting up MDNS responder!");
    while(1) {
      delay(1000);
    }
  }
  Serial.println("mDNS responder started");

  server.on("/", [](){
    server.send(200, "text/html", IndexPage);
  });

  server.on("/temperature", [](){
    int t = dht.readTemperature();
    server.send(200, "text/html", "{\"temp\":" + (String)(t) + "}");
  });

  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
}
https://www.ioxhop.com/article/20/%E0%B8%A1%E0%B8%AD%E0%B8%99%E0%B8%B4%E0%B9%80
%E0%B8%95%E0%B8%AD%E0%B8%A3%E0%B9%8C%E0%B8%AD%E0%B8%B8%E0%B8%93%E0%B8%AB%E0%B8%A0%
E0%B8%B9%E0%B8%A1%E0%B8%B4%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E
0%B9%8C%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-esp8266-%E0%B9%81%E0%B8%A5%E0%B8%B0-dht11

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

Coin Validator เครื่องรับเหรียญ เครื่องหยอดเหรียญ Multi Coin รุ่น SG-6

Learning 7 Segment + Arduino