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 |