.ESP-8266 공부


외부에서 ESP모듈에 접속하여

ESP 모듈에 부착된 LED/RELAY를

제어하는 방법.


프로그램의 주요 구성요소 

#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
#include "mainPage.h"            // 필요한 경우 html 코드 추가
ESP8266WebServer server(80);    // 서버 포트 80
//=======================================================================
void handleRoot(){
  Serial.println("You called root page");
  String s = MAIN_page;                   //Read HTML contents
  server.send(200, "text/html", s);     //Send web page
}
void handleForm() { ..... }
void handleADC(){ ..... }
void handleWebRequests(){ ...... }
//=======================================================================
void setup() {
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  server.on("/", handleRoot);         // which routine ...
  server.on("/form", handleForm);     //as Per  <a href="form">
  server.on("/ledOn", handleLEDon); //as Per  <a href="ledOn">,
  server.begin();                     // start server
}
//=======================================================================
void loop() {
  server.handleClient();
}
//=======================================================================


연습용 구성 : ESP-01, DHT11, OLED LCD 0.967", PC2303 USB2Serial


ESP-01 프로그램용 PL2303

아래 사이트에 게시된 코드 ...

IOT based home automation project

https://circuits4you.com/2016/05/19/iot-based-home-automation-project/

/*
 * Copyright (c) 2015, circuits4you.com
 * All rights reserved.
/* Create a WiFi access point and provide a web server on it. */

#include 
#include  
#include 

#include "mainPage.h"

const int Load1=16;
const int Load2=14;
const int Load3=12;
const int Load4=13;

/* Set these to your desired credentials. */
const char *ssid = "HomeServer";
const char *password = "homeautomation";

ESP8266WebServer server(80);
String L1Status,L2Status,L3Status,L4Status,Temperature;
//=======================================================================
//                    handles main page 192.168.4.1
//=======================================================================
/* Just a little test message.  Go to http://192.168.4.1 in a web browser
 * connected to this access point to see it.
 */
void handleRoot() {
  String s = MAIN_page;    
  s.replace("@@L1@@", L1Status);
  s.replace("@@L2@@", L2Status);
  s.replace("@@L3@@", L3Status);
  s.replace("@@L4@@", L4Status);
  s.replace("@@TEMP@@", Temperature);
  server.send(200, "text/html", s);    
}

//=======================================================================
//                    Handle Set Date/Time Settings
//=======================================================================
void handleForm() {
  String t_state = server.arg("submit");
  
  Temperature = String(analogRead(A0)/10);   //Do calibration here
  
//Change Load-1 State as per request
  if(t_state=="ON1")
  {
    L1Status="ON";    
    digitalWrite(Load1, HIGH);       //Load1 Turned on
  }
  
  if(t_state=="OFF1")
  {
    L1Status="OFF";    
    digitalWrite(Load1, LOW);      //Load1 Turned off  
  }
//Change Load-2 State as per request
  if(t_state=="ON2")
  {
    L2Status="ON";    
    digitalWrite(Load2, HIGH);       //Load1 Turned on
  }
  
  if(t_state=="OFF2")
  {
    L2Status="OFF";    
    digitalWrite(Load2, LOW);      //Load1 Turned off  
  }
//Change Load-3 State as per request
  if(t_state=="ON3")
  {
    L3Status="ON";    
    digitalWrite(Load3, HIGH);       //Load1 Turned on
  }
  
  if(t_state=="OFF3")
  {
    L3Status="OFF";    
    digitalWrite(Load3, LOW);      //Load1 Turned off  
  }
//Change Load-4 State as per request
  if(t_state=="ON4")
  {
    L4Status="ON";    
    digitalWrite(Load4, HIGH);       //Load1 Turned on
  }
  
  if(t_state=="OFF4")
  {
    L4Status="OFF";    
    digitalWrite(Load4, LOW);      //Load1 Turned off  
  }

  server.sendHeader("Location", "/");
  server.send(302, "text/plain", "Updated-- Press Back Button");  //This Line Keeps It on Same Page
   
  delay(500);
}
//=======================================================================
//                    Power on setup
//=======================================================================

void setup() {
  delay(1000);
  /* You can remove the password parameter if you want the AP to be open. */
  WiFi.softAP(ssid, password);

  IPAddress myIP = WiFi.softAPIP();
  server.on("/", handleRoot);
  server.on("/form", handleForm);
  server.begin();
  pinMode(Load1, OUTPUT);
  pinMode(Load2, OUTPUT);
  pinMode(Load3, OUTPUT);
  pinMode(Load4, OUTPUT);  
}

//=======================================================================
//                    Main Program Loop
//=======================================================================
void loop() {
  server.handleClient();
}
//=======================================================================



참고사이트

ESP8266 Arduino WiFi Web Server LED on off control


ESP8266 (NodeMCU) ADC analog value on dial gauge

https://circuits4you.com/2018/02/03/esp8266-nodemcu-adc-analog-value-on-dial-gauge/


Reprogram Sonoff Smart Switch with Web Server




#include 
#include 
#include 
#include 

MDNSResponder mdns;

const char* ssid = "YOUR_SSID";                      // Replace with your network credentials
const char* password = "YOUR_PASSWORD";
ESP8266WebServer server(80);
String webPage = "";

int gpio13Led = 13;
int gpio12Relay = 12;

void setup(void){
  webPage += "

SONOFF Web Server

 

"; pinMode(gpio13Led, OUTPUT); // preparing GPIOs digitalWrite(gpio13Led, HIGH); pinMode(gpio12Relay, OUTPUT); digitalWrite(gpio12Relay, HIGH); Serial.begin(115200); delay(5000); WiFi.begin(ssid, password); Serial.println(""); while (WiFi.status() != WL_CONNECTED) { // Wait for connection delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); if (mdns.begin("esp8266", WiFi.localIP())) { Serial.println("MDNS responder started"); } server.on("/", [](){ server.send(200, "text/html", webPage); }); server.on("/on", [](){ server.send(200, "text/html", webPage); digitalWrite(gpio13Led, LOW); digitalWrite(gpio12Relay, HIGH); delay(1000); }); server.on("/off", [](){ server.send(200, "text/html", webPage); digitalWrite(gpio13Led, HIGH); digitalWrite(gpio12Relay, LOW); delay(1000); }); server.begin(); Serial.println("HTTP server started"); } void loop(void){ server.handleClient(); }

'IoT_ESP8266' 카테고리의 다른 글

ESP-01 : 서버 LED ON/OFF - 고정 IP  (0) 2019.01.01
ESP-01 : 서버로 LED ON/OFF  (0) 2019.01.01
ESP8266 - ESP-01 : 온도및 습도 모니터링 OLED/Web  (0) 2018.12.30
ESP-01 with OLED  (1) 2018.12.30
Arduino IDE 이용 ESP8266 웹서버  (0) 2018.08.14
블로그 이미지

DIYworld

,