원천 소스
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 |