LED 제어 하고 상태 확인 가능하도록 수정 했다.

 

원본 소스

 

http://wglab.tk/221175139758

 

 

-------------------------- 소스 ----------------------------

#include <ESP8266WiFi.h>
#include <Ticker.h>

#define LED     D0    //LED PIN define
#define KEY_IN  D3    //Flash key pin define
#define ANALOG_IN A0  //analog in define


Ticker flipper1;
Ticker flipper2;


const char *ssid =  "WIFI";     // replace with your wifi ssid and wpa2 key
const char *pass =  "----";

WiFiClient client;

unsigned int prt_index = 0;
unsigned char led_flag = 0;


void flip_test();
void led_test();

WiFiServer server(80);

// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(115200);
  delay(10);
 
  Serial.println("Start..");

  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED, OUTPUT);
  pinMode(KEY_IN, INPUT);   // == pinMode(KEY_IN, INPUT);

  Serial.println("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, pass);
 
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");

  Serial.print("MAC: ");
  Serial.println(WiFi.macAddress());

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
 
  flipper1.attach(0.3, flip_test);    //start , ms
  //flipper2.attach(0.5, led_test);       //start , ms

  //flipper.detach(); //stop
}

// the loop function runs over and over again forever
void loop() {

  int buttonState = digitalRead(KEY_IN);
  if (buttonState == 0)
  {
    Serial.printf("button key input !! \n");
  }

  WiFiClient client = server.available();
  if (client)
  {
    Serial.println("new client");
    while(!client.available()){
    delay(1);
    }

    //요청을 읽는다.
    String request = client.readStringUntil('\r');
    Serial.println(request);
    client.flush();
 
    //사용자가 접속한 URL읽어서 판별
    if (request.indexOf("/ON") > 0) { //1이면
      Serial.println("LED ON");
      digitalWrite(LED, LOW);
    }
    if (request.indexOf("/OFF") > 0) { //0이면
      Serial.println("LED OFF");
      digitalWrite(LED, HIGH);
    }

    // Match the request
    int val1 = digitalRead(LED);;

    // Prepare the response
    int sensorReading = analogRead(ANALOG_IN);
    String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n ";
    s += "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">";     //한글출력
    s += "<body>";   
    s += "<h1>LED is now = ";
    s += (val1)?"OFF<p>":"ON<p>";
    s += "Analog Value is now = ";
    s += (sensorReading);
    s += "</h1><p>";
   
    // Send the response to the client
    client.print(s);

    //-----
    client.println("<svg height='210' width='300'>");
    int val = analogRead(A0)/5;
    int y1 = 200-val;
    for(int i=1;i<300;i++){
      // 아나로그입력 생성(0-1023):1023=3.3V
      int x1 = (i-1);
      int x2 = i;
      int val = analogRead(A0)/5;
      int y2 = 200-val;
      client.println("<line x1='");
 
      client.println(x1);
      client.println("' y1='");
      client.println(y1);
      client.println("' x2='"); 
      client.println(x2);
      client.println("' y2='");
      client.println(y2);
      client.println("'");
      client.println("' style='stroke:rgb(255,0,0);stroke-width:2' />");
      client.println("Sorry, your browser does not support inline SVG.");
      y1 = y2;
    }

    client.println("</svg>");
    client.println("  </body>");
   
    client.println("<br><br>");   
    client.println("<a href=\"/ON\"\"><button>LED 점등 </button></a>");
    client.println("<a href=\"/OFF\"\"><button>LED 소등 </button></a><br />");
   
    client.println("<br><br>");
    client.println("<a href=\"/reflsh\"\"><button>Data Refresh </button></a><br />");          
    client.println("</html>");
    delay(1);
     
    Serial.println("Client disonnected");
  }

}

//-------------------
void flip_test()
{
  int inputA_val = analogRead(ANALOG_IN);
 
  prt_index ++;
  //Serial.printf("%d",prt_index);
  switch (prt_index % 4)
  {
    case 0:
      Serial.printf("^");
      break;

    case 1:
      Serial.printf("-");
      break;

    case 2:
      Serial.printf("^");
      break;

    case 3:
      Serial.printf("; , B: %d \n", inputA_val);
      break;
  }
}

//--------------------
void led_test()
{
  if (led_flag)
  {
    led_flag = 0;
    digitalWrite(LED, LOW);    // turn the LED off by making the voltage LOW
  }
  else
  {
    led_flag = 1;
    digitalWrite(LED, HIGH);   // turn the LED on (HIGH is the voltage level)
  }
}

-------------------------------------------------------------

 

--------------------------- 출력 ---------------------------

 

 

-------------------------------------------------------------

 

 

 

'공부 > arduino' 카테고리의 다른 글

nodeMCU oled 드라이브 Adafruit  (0) 2018.03.26
nodeMCU oled 드라이브 U8g2  (0) 2018.03.23
nodeMCU Web 따라하기 2  (0) 2018.03.12
nodeMCU web 따라하기  (0) 2018.03.12
nodeMCU wifi 연결  (0) 2018.03.12

+ Recent posts