nodeMCU GPIO2 기본적으로 연결된 LED를 ON, OFF 합니다.
스마트폰에서 실행
(온도센서_AM2320_ECP8266_LED_E에서 계속합니다.)
source code
/********* Rui Santos Complete project details at The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. *********/ // Import required libraries #include <ESP8266WiFi.h> #include <ESPAsyncTCP.h> #include <ESPAsyncWebServer.h> #include <Adafruit_Sensor.h> #include <Adafruit_AM2320.h> Adafruit_AM2320 am2320 = Adafruit_AM2320(); // Replace with your network credentials const char* ssid = "IOT999"; const char* password = "kbs48752"; const char* PARAM_INPUT_1 = "output"; const char* PARAM_INPUT_2 = "state"; int val; float t = 0.0; float h = 0.0; // Create AsyncWebServer object on port 80 AsyncWebServer server(80); const char index_html[] PROGMEM = R"rawliteral( <!DOCTYPE HTML><html> <head> <title>ESP Web Server</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" href="data:,"> <style> html {font-family: Arial; display: inline-block; text-align: center;} h2 {font-size: 2.0rem;} p {font-size: 2.0rem;} body {max-width: 600px; margin:0px auto; padding-bottom: 25px;} .switch {position: relative; display: inline-block; width: 120px; height: 68px} .switch input {display: none} .slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 6px} .slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 3px} input:checked+.slider {background-color: #b30000} input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)} </style> </head> <body> <h2>ESP Web Server</h2> <p> <span id="temperature">%TEMPERATURE%</span> <sup>o</sup> <span>C</span> </p> %BUTTONPLACEHOLDER% <script>function toggleCheckbox(element) { var xhr = new XMLHttpRequest(); if(element.checked){ xhr.open("GET", "/update?output="+element.id+"&state=1", true); } else { xhr.open("GET", "/update?output="+element.id+"&state=0", true); } xhr.send();} xhr.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("temperature").innerHTML = this.responseText; } xhr.open("GET", "/temperature", true); xhr.send(); } </script> </body> </html> )rawliteral"; // Replaces placeholder with button section in your web page String processor(const String& var){ //Serial.println(var); if(var == "BUTTONPLACEHOLDER"){ String buttons = ""; buttons += "<h4>GPIO2</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"2\" " + outputState(2) + "><span class=\"slider\"></span></label>"; return buttons; } if(var == "TEMPERATURE"){ return String(t); } return String(); } String outputState(int output){ if(digitalRead(output)){ return "checked"; } else { return ""; } } unsigned long previousMillis = 0; const long interval = 10000; void setup(){ // Serial port for debugging purposes Serial.begin(115200); pinMode(2, OUTPUT); digitalWrite(2, LOW); // Connect to Wi-Fi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi.."); } // Print ESP Local IP Address Serial.println(WiFi.localIP()); // Route for root / 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()); }); // Send a GET request to <ESP_IP>/update?output=<inputMessage1>&state=<inputMessage2> server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) { String inputData1; String inputData2; // GET input1 value on <ESP_IP>/update?output=<inputMessage1>&state=<inputMessage2> if (request->hasParam(PARAM_INPUT_1) && request->hasParam(PARAM_INPUT_2)) { inputData1 = request->getParam(PARAM_INPUT_1)->value(); inputData2 = request->getParam(PARAM_INPUT_2)->value(); digitalWrite(inputData1.toInt(), inputData2.toInt()); } else { inputData1 = "No message sent"; inputData2 = "No message sent"; } Serial.print(inputData1); Serial.println(inputData2); request->send(200, "text/plain", "OK"); }); Serial.println("Adafruit AM2320 Basic Test"); am2320.begin(); // Start server server.begin(); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; float newT = am2320.readTemperature(); if (isnan(newT)) { Serial.println("Failed to read from AM2320 sensor!"); } else { t = newT-2; Serial.println(t); } } } |
'IOT(Arduino+Inventor)' 카테고리의 다른 글
esp8266_LED_On_OFF (1) | 2023.03.13 |
---|---|
esp8266_led_On_off (0) | 2023.03.12 |
온도센서_AM2320_ESP8266_Inventor_D (0) | 2023.03.06 |
온도센서_AM2320_ESP8266_CSS_C (0) | 2023.03.06 |
온도센서_AM2320_ESP8266_B (0) | 2023.03.06 |