본문 바로가기

IOT(Arduino+Inventor)

ESP8266_AM2320_LED

현제온도 보다 낮거나 높으면, 제어 할 수 있는 기초입니다.

LED 포트에 릴레이, 서보모터를 이용하여 전열기를 ON,OFF를 할 예정입니다.

source code


#include <ESP8266WiFi.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_AM2320.h>

Adafruit_AM2320 am2320 = Adafruit_AM2320();

const char* ssid = "IOT999";
const char* password = "kbs48752";
// Port 80
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80); 
float temper = 0.0;
float h = 0.0;
unsigned long previousMillis = 0;    

const long interval = 10000;


#define LED2  2    // LED2 is a Built-in LED.
String situation = "";
int wait30 = 30000; // time to reconnect when connection is lost.

void setup() {
  Serial.begin(115200);
  pinMode(LED2, OUTPUT);

// Connect WiFi net.
  Serial.println();
  Serial.print("Connecting with ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected with WiFi.");
 
  // Start Web Server.
  server.begin();
   Serial.println("Adafruit AM2320 Basic Test");
  am2320.begin();

  Serial.println("Web Server started.");
 
  // This is the IP
 // Serial.print(" WebServer IP: ");
  Serial.print("http://");
  Serial.println(WiFi.localIP());


}
 
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 {
      
   
      temper = newT;
      Serial.println(temper);
    
    }
  }



// If disconnected, try to reconnect every 30 seconds.
  if ((WiFi.status() != WL_CONNECTED) && (millis() > wait30)) {
    Serial.println("Trying to reconnect WiFi...");
    WiFi.disconnect();

    WiFi.begin(ssid, password);
    wait30 = millis() + 30000;
  }
   
   
  // Check if a client has connected..
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
   
  Serial.print("New client: ");
  Serial.println(client.remoteIP());
   
  // Wait until the client sends data.
  // Wait until the client sends data.
  
  // Read the information sent by the client.
  String req = client.readStringUntil('\r');
  Serial.println(req);

  // Make the client's request.
       if (req.indexOf("on2") != -1) {digitalWrite(LED2, HIGH); situation = "ON";}
       if (req.indexOf("off2") != -1){digitalWrite(LED2, LOW); situation = "OFF";}
     if (req.indexOf("consultation") != -1){
         if (digitalRead(LED2)){situation = "ON";}
         else {situation = "OFF";}
          }
     
  //////////////////////////////////////////////
  //  WEB PAGE. ////////////////////////////
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  Important.
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("<head><meta charset=utf-8></head>");
  client.println("<body><center><font face='Arial'>");
  client.println("<h1>Temperature Control</h1>");
  client.println("<br><br>");
  client.println("<p>");
  client.println(temper);
  client.println("</p>");
  client.println("<p>");
  client.println("<a href='consultation'><button>상태</button></a>");
  client.println(situation);
  client.println("</p>");
  client.println("<p>");
  client.println("<a href='on2'><button>Click to ON LED2</button></a>");
  client.println("</p>");
  client.println("<p>");
  client.println("<a href='off2'><button>Click to OFF LED2</button></a>"); 
  client.println("</p>");
  
  client.println("</font></center></body></html>");

  Serial.print("Client disconnected: ");
  Serial.println(client.remoteIP());
  client.flush();
  client.stop();
}

 

 

'IOT(Arduino+Inventor)' 카테고리의 다른 글

온라인 온도계  (0) 2023.03.23
ESP8266온도_흙 수분_LED  (1) 2023.03.16
esp8266_LED_On_OFF  (1) 2023.03.13
esp8266_led_On_off  (0) 2023.03.12
온도센서_AM2320_ECP8266_LED_E  (0) 2023.03.07