oled 또다른 라이브러리를 넣어봤다.

라이브러리는 Adafruit 에서 만든 것인다.

 

참고 사이트 : https://learn.adafruit.com/monochrome-oled-breakouts/wiring-128x64-oleds

 

먼저 라이브러리를 설치 해야 한다. 라이브러리는 2가지를 설치 해야 한다.

 

Adafruit SSD1306 , Adafruit GFX Library

 

 

 

 

설치 완료후 다시 아두이노 실행하면 된다.

 

소스는 아래와 같다.

 

예제 소스와 동일 하기 때문에 예제 소스를 확인 해도 된다.

 

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

 

 

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

 

#define OLED_PCLK     D5
#define OLED_PDATA    D7
#define OLED_PRESET   D1
#define OLED_PCMD     D4
#define OLED_PCS     D8

 

Adafruit_SSD1306 display(OLED_PDATA, OLED_PCLK, OLED_PCMD, OLED_PRESET, OLED_PCS);   //MOSI, CLK, DC, RESET, CS

 

#define SSD1306_LCDHEIGHT  64
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
  
#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 
static const unsigned char PROGMEM logo16_glcd_bmp[] =
  { B00000000, B11000000,
    B00000001, B11000000,
    B00000001, B11000000,
    B00000011, B11100000,
    B11110011, B11100000,
    B11111110, B11111000,
    B01111110, B11111111,
    B00110011, B10011111,
    B00011111, B11111100,
    B00001101, B01110000,
    B00011011, B10100000,
    B00111111, B11100000,
    B00111111, B11110000,
    B01111100, B11110000,
    B01110000, B01110000,
    B00000000, B00110000 };
  
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

 

void setup() {

  Serial.begin(115200);
  delay(10);
 
  Serial.println("Start..");

 

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC);
  // init done
 
  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  delay(2000);

 

  // Clear the buffer.
  display.clearDisplay();

 

  // draw a single pixel
  display.drawPixel(10, 10, WHITE);
  // Show the display buffer on the hardware.
  // NOTE: You _must_ call display after making any drawing commands
  // to make them visible on the display hardware!
  display.display();
  delay(2000);

  display.clearDisplay();

 

  // draw many lines
  testdrawline();
  display.display();
  delay(2000);
  display.clearDisplay();

 

  // draw rectangles


  testdrawrect();
  display.display();
  delay(2000);
  display.clearDisplay();

 

  // draw multiple rectangles


  testfillrect();
  display.display();
  delay(2000);
  display.clearDisplay();

 

  // draw mulitple circles


  testdrawcircle();
  display.display();
  delay(2000);
  display.clearDisplay();

 

  // draw a white circle, 10 pixel radius


  display.fillCircle(display.width()/2, display.height()/2, 10, WHITE);
  display.display();
  delay(2000);
  display.clearDisplay();

  testdrawroundrect();
  delay(2000);
  display.clearDisplay();

  testfillroundrect();
  delay(2000);
  display.clearDisplay();

  testdrawtriangle();
  delay(2000);
  display.clearDisplay();
  
  testfilltriangle();
  delay(2000);
  display.clearDisplay();

 

  // draw the first ~12 characters in the font


  testdrawchar();
  display.display();
  delay(2000);
  display.clearDisplay();

 

  // draw scrolling text


  testscrolltext();
  delay(2000);
  display.clearDisplay();

 

  // text display tests


  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Hello, world!");
  display.setTextColor(BLACK, WHITE); // 'inverted' text
  display.println(3.141592);
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.print("0x"); display.println(0xDEADBEEF, HEX);
  display.display();
  delay(2000);
  display.clearDisplay();

 

  // miniature bitmap display


  display.drawBitmap(30, 16,  logo16_glcd_bmp, 16, 16, 1);
  display.display();

 

  // invert the display


  display.invertDisplay(true);
  delay(1000);
  display.invertDisplay(false);
  delay(1000);
  display.clearDisplay();

 

  // draw a bitmap icon and 'animate' movement


  testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH);

}

 

void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
  uint8_t icons[NUMFLAKES][3];
 
  // initialize
  for (uint8_t f=0; f< NUMFLAKES; f++) {
    icons[f][XPOS] = random(display.width());
    icons[f][YPOS] = 0;
    icons[f][DELTAY] = random(5) + 1;
   
    Serial.print("x: ");
    Serial.print(icons[f][XPOS], DEC);
    Serial.print(" y: ");
    Serial.print(icons[f][YPOS], DEC);
    Serial.print(" dy: ");
    Serial.println(icons[f][DELTAY], DEC);
  }

  while (1) {
    // draw each icon
    for (uint8_t f=0; f< NUMFLAKES; f++) {
      display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE);
    }
    display.display();
    delay(200);
   
    // then erase it + move it
    for (uint8_t f=0; f< NUMFLAKES; f++) {
      display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK);
      // move it
      icons[f][YPOS] += icons[f][DELTAY];
      // if its gone, reinit
      if (icons[f][YPOS] > display.height()) {
        icons[f][XPOS] = random(display.width());
        icons[f][YPOS] = 0;
        icons[f][DELTAY] = random(5) + 1;
      }
    }
   }
}


void testdrawchar(void) {
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);

  for (uint8_t i=0; i < 168; i++) {
    if (i == '\n') continue;
    display.write(i);
    if ((i > 0) && (i % 21 == 0))
      display.println();
  }   
  display.display();
}

void testdrawcircle(void) {
  for (int16_t i=0; i<display.height(); i+=2) {
    display.drawCircle(display.width()/2, display.height()/2, i, WHITE);
    display.display();
  }
}

void testfillrect(void) {
  uint8_t color = 1;
  for (int16_t i=0; i<display.height()/2; i+=3) {
    // alternate colors
    display.fillRect(i, i, display.width()-i*2, display.height()-i*2, color%2);
    display.display();
    color++;
  }
}

void testdrawtriangle(void) {
  for (int16_t i=0; i<min(display.width(),display.height())/2; i+=5) {
    display.drawTriangle(display.width()/2, display.height()/2-i,
                     display.width()/2-i, display.height()/2+i,
                     display.width()/2+i, display.height()/2+i, WHITE);
    display.display();
  }
}

void testfilltriangle(void) {
  uint8_t color = WHITE;
  for (int16_t i=min(display.width(),display.height())/2; i>0; i-=5) {
    display.fillTriangle(display.width()/2, display.height()/2-i,
                     display.width()/2-i, display.height()/2+i,
                     display.width()/2+i, display.height()/2+i, WHITE);
    if (color == WHITE) color = BLACK;
    else color = WHITE;
    display.display();
  }
}

void testdrawroundrect(void) {
  for (int16_t i=0; i<display.height()/2-2; i+=2) {
    display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, WHITE);
    display.display();
  }
}

void testfillroundrect(void) {
  uint8_t color = WHITE;
  for (int16_t i=0; i<display.height()/2-2; i+=2) {
    display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, color);
    if (color == WHITE) color = BLACK;
    else color = WHITE;
    display.display();
  }
}
  
void testdrawrect(void) {
  for (int16_t i=0; i<display.height()/2; i+=2) {
    display.drawRect(i, i, display.width()-2*i, display.height()-2*i, WHITE);
    display.display();
  }
}

void testdrawline() { 
  for (int16_t i=0; i<display.width(); i+=4) {
    display.drawLine(0, 0, i, display.height()-1, WHITE);
    display.display();
  }
  for (int16_t i=0; i<display.height(); i+=4) {
    display.drawLine(0, 0, display.width()-1, i, WHITE);
    display.display();
  }
  delay(250);
 
  display.clearDisplay();
  for (int16_t i=0; i<display.width(); i+=4) {
    display.drawLine(0, display.height()-1, i, 0, WHITE);
    display.display();
  }
  for (int16_t i=display.height()-1; i>=0; i-=4) {
    display.drawLine(0, display.height()-1, display.width()-1, i, WHITE);
    display.display();
  }
  delay(250);
 
  display.clearDisplay();
  for (int16_t i=display.width()-1; i>=0; i-=4) {
    display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE);
    display.display();
  }
  for (int16_t i=display.height()-1; i>=0; i-=4) {
    display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE);
    display.display();
  }
  delay(250);

  display.clearDisplay();
  for (int16_t i=0; i<display.height(); i+=4) {
    display.drawLine(display.width()-1, 0, 0, i, WHITE);
    display.display();
  }
  for (int16_t i=0; i<display.width(); i+=4) {
    display.drawLine(display.width()-1, 0, i, display.height()-1, WHITE);
    display.display();
  }
  delay(250);
}

void testscrolltext(void) {
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(10,0);
  display.clearDisplay();
  display.println("scroll");
  display.display();
 
  display.startscrollright(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);
  display.startscrollleft(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);   
  display.startscrolldiagright(0x00, 0x07);
  delay(2000);
  display.startscrolldiagleft(0x00, 0x07);
  delay(2000);
  display.stopscroll();
}

 

 

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

 

 

 

 

 

 

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

nodeMCU tft LCD 라이브러리변경(Adafruit ->Ucglib)  (0) 2018.03.26
nodeMCU에 ST7735 Tft Lcd 연결  (0) 2018.03.26
nodeMCU oled 드라이브 U8g2  (0) 2018.03.23
nodeMCU web 따라하기 3  (0) 2018.03.12
nodeMCU Web 따라하기 2  (0) 2018.03.12

OLED 디스프레이를 위해서 구매를 했다.

 

인터넷을 검색해 보니 I2C 의 속도가 느려서 SPI를 추천한다고 해서 SPI 방식으로 구매했다.

 

구매사이트는 인터넷 가장 싼곳으로 당연히 메뉴얼도 존재 하지 않는 사이트...ㅠㅠ

 

일단 아두이노에 라이브러리를 선택 했다.

여러가지 라이브러리중 업그레이드가 가장 많이 된것 같은 U8G2를 선택 했다.

다른사람들 평에는 속도빠르고 버퍼를 작게 먹는 다고 되어 있어서 선택의 폭을 줄일수 있었다.

 

사이트는  : https://github.com/olikraus/u8g2

 

설치는 아래와 같은 방법으로 진행 했다.

 

 

 

 

케이블 연결을 위해 자료 조사를 진행 했다.

 

참고 사이트 :  https://github.com/jandelgado/arduino/wiki/SSD1306-based-OLED-connected-to-Arduino

 

참고로 사진상의 내용과 같이 저항 연결에 따라 I2C, SPI 모드 선택이 가능하다.

 

 

사이트에서 추천한 PIN 연결은 아래와 같았다.

 

 

 

하지만 위와 같이 연결할경우 기존에 사용중이던 소스에서 D0 (LED) 가 겹쳐서 해당 PIN 을 변경 했다.

처음 봤을때는 SPI를 하드웨어로 동작하는걸로 생각했는데 결국은 GPIO 제어가 되어야하는 것으로

보여서 아무생각없이 PIN MAP 을 변경 했다.

 

SSD1306

 

 

nodeMCU

특징

1

GND

 

GND

전원

2

VDD

 

3.3V

전원

3

SCK(D0)

 

D5(GPIO14)

CLOCK

4

SDA(D1)

 

D7(GPIO13)

DATA(MOSI)

5

RES

 

D1(GPIO5)

RESET

6

DC

 

D4(GPIO2)

DATA/COMMAND

7

CS

 

D8(GPIO15)

CHIP SELECT

 

 

연결 회로도는 아래와 같다.

 

 

참고고 OLED 회로도는 아래와 같다.

 

 

 

소스는 예제 소스 기준으로 작성 하였다. (기존 소스에 그대로 사용했다.)

 

좋은 점은  한글 출력이 가능 하다는 점이다.  ( setup에 테스트 코드만 넣어서 해당 코드만 올림)

 

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

 

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

 

#include <Arduino.h>
#include <U8g2lib.h>
//#include <U8x8lib.h>


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

#define OLED_PCLK     D5
#define OLED_PDATA    D7
#define OLED_PRESET   D1
#define OLED_PCMD     D4
#define OLED_PCS     D8

 

//GIPO롤 출력하는 가장 기본 적인 설정

U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ OLED_PCLK, /* data=*/ OLED_PDATA, /* cs=*/ OLED_PCS, /* dc=*/ OLED_PCMD, /* reset=*/ OLED_PRESET);

 

 

// 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);

  
  u8g2.begin();
 
  u8g2.clearBuffer();          // clear the internal memory
  u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
  u8g2.drawStr(0,10,"Hello World!");  // write something to the internal memory
  u8g2.sendBuffer();          // transfer internal memory to the display

  delay(1000);

 

  u8g2.clearBuffer();          // clear the internal memory
  u8g2.setFontDirection(0);
  u8g2.setFont(u8g2_font_inb24_mf);
  u8g2.drawStr(0, 30, "U");
 
  u8g2.setFontDirection(1);
  u8g2.setFont(u8g2_font_inb30_mn);
  u8g2.drawStr(21,8,"8");
     
  u8g2.setFontDirection(0);
  u8g2.setFont(u8g2_font_inb24_mf);
  u8g2.drawStr(51,30,"g");
  u8g2.drawStr(67,30,"\xb2");
 
  u8g2.drawHLine(2, 35, 47);
  u8g2.drawHLine(3, 36, 47);
  u8g2.drawVLine(45, 32, 12);
  u8g2.drawVLine(46, 33, 12);

  u8g2.sendBuffer();

 
  delay(1000);
 
  u8g2.clearBuffer();          // clear the internal memory

  u8g2.enableUTF8Print();    // enable UTF8 support for the Arduino print() function
  u8g2.setFont(u8g2_font_unifont_t_korean1); 
  u8g2.setFontDirection(0);

 

  //u8g2.firstPage();
  u8g2.setCursor(0, 15);
  u8g2.print("Hello World!");
  u8g2.setCursor(0, 35);
  u8g2.print("안녕 세상");    // Korean "Hello World"

  u8g2.setFont(u8g2_font_unifont_t_korean2); 
  u8g2.setFontDirection(0);

  u8g2.setCursor(0, 55);
  u8g2.print("안녕 세상");    // Korean "Hello World"

 

  u8g2.sendBuffer();

 
}

 

 

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

 

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

 

 

출력 화면 예 입니다.

 

   

 

 

 

 

 

 

 

 

 

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

nodeMCU에 ST7735 Tft Lcd 연결  (0) 2018.03.26
nodeMCU oled 드라이브 Adafruit  (0) 2018.03.26
nodeMCU web 따라하기 3  (0) 2018.03.12
nodeMCU Web 따라하기 2  (0) 2018.03.12
nodeMCU web 따라하기  (0) 2018.03.12

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

nodeMCU Web 따라하기 2

 

원천 소스

http://blog.daum.net/ejleep1/332

 

기존소스와 현재 소스 합쳐서 만들었다.

 

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

#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);
    }

    // Read the first line of the request
    String req = client.readStringUntil('\r');
    Serial.println(req);
    client.flush();
 
    // Match the request
    int val1 = 0;
    /*if (req.indexOf("/gpio/0") != -1)
    val = 0;
    else if (req.indexOf("/gpio/1") != -1)
    val = 1;
    else {
    Serial.println("invalid request");
    client.stop();
    return;
    }
*/
    // 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 <body> <h1>GPIO2 is now = ";
    s += (val1)?"high<p>":"low<p>";
    s += "Analog Value is now = ";
    s += (sensorReading);
    s += "</h1><p>";
    //s += "<img src='https://lh4.ggpht.com/yPq0eEBNR4g530UHDfpbtwe-yiAMLVjUcnGCsjW9_K9VniuLr_YbeefSsd3uqYvZWJ8=w300'><p>";
    //s += "<iframe width='560' height='315' src='https://www.youtube.com/embed/ZicP4y-nFg4' frameborder='0' allowfullscreen></iframe>";
    //s += "</html>\n";
    // 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>Data Refresh </button></a><br />");          
    client.println("</html>");
    delay(1);
    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 드라이브 U8g2  (0) 2018.03.23
nodeMCU web 따라하기 3  (0) 2018.03.12
nodeMCU web 따라하기  (0) 2018.03.12
nodeMCU wifi 연결  (0) 2018.03.12
nodeMCU analog input 업데이트  (0) 2018.03.10

원천 소스

https://blog.naver.com/mtinet/220791197571

 

https://github.com/mtinet/ESP8266_12E/blob/master/WiFiWebServer.ino

 

소스를 따라해봤다.

 

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

 

#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 =  "ssid";     // 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.println(WiFi.localIP());

  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);
    }

    // Read the first line of the request
    String req = client.readStringUntil('\r');
    Serial.println(req);
    client.flush();
 
    // Match the request
    int val = 0;
    /*if (req.indexOf("/gpio/0") != -1)
    val = 0;
    else if (req.indexOf("/gpio/1") != -1)
    val = 1;
    else {
    Serial.println("invalid request");
    client.stop();
    return;
    }
*/
    // 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<h1>GPIO2 is now = ";
    s += (val)?"high<p>":"low<p>";
    s += "Analog Value is now = ";
    s += (sensorReading);
    s += "</h1><p>";
    s += "<img src='https://lh4.ggpht.com/yPq0eEBNR4g530UHDfpbtwe-yiAMLVjUcnGCsjW9_K9VniuLr_YbeefSsd3uqYvZWJ8=w300'><p>";
    s += "<iframe width='560' height='315' src='https://www.youtube.com/embed/ZicP4y-nFg4' frameborder='0' allowfullscreen></iframe>";
    s += "</html>\n";
   
    
    // Send the response to the client
    client.print(s);
    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 web 따라하기 3  (0) 2018.03.12
nodeMCU Web 따라하기 2  (0) 2018.03.12
nodeMCU wifi 연결  (0) 2018.03.12
nodeMCU analog input 업데이트  (0) 2018.03.10
nodeMCU 참고 사이드  (0) 2018.03.10

 

#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 =  "ssid";     // replace with your wifi ssid and wpa2 key
const char *pass =  "password";

 

WiFiClient client;

 

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


void flip_test();
void led_test();


// 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());

 

  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");
  }
}

 

 

//-------------------
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 Web 따라하기 2  (0) 2018.03.12
nodeMCU web 따라하기  (0) 2018.03.12
nodeMCU analog input 업데이트  (0) 2018.03.10
nodeMCU 참고 사이드  (0) 2018.03.10
nodeMCU pin MAP 다시보기  (0) 2018.03.09

타이머 ticker 를 2개로 늘리고 ADC 를 살려 봤다.


------------------ 참고소스 ----------------------


#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;



unsigned int prt_index = 0;

unsigned char led_flag = 0;



void flip_test();

void led_test();



// 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);


  flipper1.attach(0.3, flip_test);    //start , ms

  flipper2.attach(1, 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");

  }

}


//-------------------

void flip_test()

{

  int inputA_val = analogRead(ANALOG_IN);

  

  prt_index ++;

  //Serial.printf("%d",prt_index);

  switch (prt_index % 4)

  {

    case 0:

      Serial.printf("1");

      break;


    case 1:

      Serial.printf("2");

      break;


    case 2:

      Serial.printf("3");

      break;


    case 3:

      Serial.printf("4 , A: %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 web 따라하기  (0) 2018.03.12
nodeMCU wifi 연결  (0) 2018.03.12
nodeMCU 참고 사이드  (0) 2018.03.10
nodeMCU pin MAP 다시보기  (0) 2018.03.09
nodeMCU 타이머(Ticker)살리기  (0) 2018.03.09

인터넷 검색중 nodeMCU 에 간단한 회로를 첨부해서

소스와 함께 설명한 사이트를 찾았다.


기초적인 내용을 이해하는데 좋을 듯 하다


https://roboindia.com/tutorials/nodemcu-arduino



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

nodeMCU wifi 연결  (0) 2018.03.12
nodeMCU analog input 업데이트  (0) 2018.03.10
nodeMCU pin MAP 다시보기  (0) 2018.03.09
nodeMCU 타이머(Ticker)살리기  (0) 2018.03.09
아두이노 멀티 장비 설정  (0) 2018.03.07

+ Recent posts