<style> 태그로, 화면 중앙, 색을 설정합ㄴ다.
주의 : 모든 기기는 같은 ssid,password 이어야 합니다.
(온도센서 _AM2320_ESP8266_B 계속입니다.)
<body> </body> code
<body> <h2>ESP8266 AM2320 Server</h2> <p> Temperature <span id="temperature">%TEMPERATURE%</span> </p> </body> |
<style>이용합니다.
<body> <h2 style="text-align:center">Thermometer</h2> <p style="text-align:center"> Temperature <span id="temperature"style="color:red">%TEMPERATURE%</span> </p> </body> |
Source code
#include <Arduino.h> #include <ESP8266WiFi.h> #include <Hash.h> #include <ESPAsyncTCP.h> #include <ESPAsyncWebServer.h> #include <Adafruit_Sensor.h> #include <Adafruit_AM2320.h> Adafruit_AM2320 am2320 = Adafruit_AM2320(); const char* ssid = "spring99"; const char* password = "99gnirps"; float t = 0.0; float h = 0.0; AsyncWebServer server(80); unsigned long previousMillis = 0; const long interval = 10000; const char index_html[] PROGMEM = R"rawliteral( <!DOCTYPE HTML><html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <h2 style="text-align:center">Thermometer</h2> <p style="text-align:center"> Temperature <span id="temperature"style="color:red">%TEMPERATURE%</span> </p> </body> <script> setInterval(function ( ) { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("temperature").innerHTML = this.responseText; } }; xhttp.open("GET", "/temperature", true); xhttp.send(); }, 10000 ) ; </script> </html>)rawliteral"; String processor(const String& var){ if(var == "TEMPERATURE"){ return String(t); } return String(); } void setup() { Serial.begin(115200); while (!Serial) { delay(10); } Serial.println("Adafruit AM2320 Basic Test"); am2320.begin(); WiFi.begin(ssid, password); Serial.println("Connecting to WiFi"); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("."); } Serial.println(WiFi.localIP()); // web page server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/html", index_html, processor); }); server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/plain", String(t).c_str()); }); // Start server server.begin(); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { // save the last time you updated the AM20320values previousMillis = currentMillis; // Read temperature as Celsius (the default) float newT = am2320.readTemperature(); if (isnan(newT)) { Serial.println("Failed to read from AM2320 sensor!"); } else { //t = newT; //orror=-2; //temperature_offset=-2 t = newT-2; Serial.println(t); } } delay(2000); } |
'IOT(Arduino+Inventor)' 카테고리의 다른 글
온도센서_AM2320_ESP8266_Inventor_D (0) | 2023.03.06 |
---|---|
온도센서_AM2320_ESP8266_CSS_C (0) | 2023.03.06 |
온도센서 _AM2320_ESP8266 _A (0) | 2023.03.06 |
WebServer Esp8266 (0) | 2022.11.06 |
서보 모터 제어[6] (0) | 2022.04.05 |