2018年12月2日 星期日
ESP32 LED Control WEbserver
//ESP32 Board
#include <WiFi.h>
// Replace with your network credentials
const char* ssid = "alex9ufo";
const char* password = "alex9981";
WiFiServer server(80);
const int led = 2 ; // the number of the LED pin
// Client variables
char linebuf[80];
int charcount=0;
void setup()
{
// initialize the LED as an output:
pinMode(led, OUTPUT);
//Initialize serial and wait for port to open:
Serial.begin(115200);
while(!Serial) {
}
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// attempt to connect to Wifi network:
while(WiFi.status() != WL_CONNECTED)
{
// Connect to WPA/WPA2 network.
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop()
{
// listen for incoming clients
WiFiClient client = server.available();
if (client)
{
Serial.println("New client");
memset(linebuf,0,sizeof(linebuf));
charcount=0;
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
Serial.write(c);
//read char by char HTTP request
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
if (c == '\n' && currentLineIsBlank)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println();
client.println("<!DOCTYPE HTML><html><head>");
client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head>");
client.println("<h1>ESP32 - Web Server example</h1>");
client.println("<h1><p>LED <a href=\"on\"><button><h1>ON<h1></button></a> ");
client.println(" <a href=\"off\"><button><h1>OFF<h1></button></a></p></h1> ");
client.println("</html>");
break;
}
if (c == '\n')
{
// you're starting a new line
currentLineIsBlank = true;
if (strstr(linebuf,"GET /on") > 0)
{
Serial.println("LED ON");
digitalWrite(led, HIGH);
}
else if (strstr(linebuf,"GET /off") > 0)
{
Serial.println("LED OFF");
digitalWrite(led, LOW);
}
// you're starting a new line
currentLineIsBlank = true;
memset(linebuf,0,sizeof(linebuf));
charcount=0;
}
else if (c != '\r')
{
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
訂閱:
張貼留言 (Atom)
Messaging API作為替代方案
LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...
-
python pip 不是内部或外部命令 -- 解決方法 要安裝 Pyqt5 1. 首先,開啟命令提示字元。 2. 輸入 pip3 install pyqt5 好像不能執行 ! ! 錯誤顯示 : ‘ pip3 ’ 不是內部或外部命令、可執行的程式或批...
-
課程講義 下載 11/20 1) PPT 下載 + 程式下載 http://www.mediafire.com/file/cru4py7e8pptfda/106%E5%8B%A4%E7%9B%8A2-1.rar 11/27 2) PPT 下載...
-
• 認 識 PreFix、InFix、PostFix PreFix(前序式):* + 1 2 + 3 4 InFix(中序式): (1+2)*(3+4) PostFix(後序式):1 2 + 3 4 + * 後 序式的運算 例如: 運算時由 後序式的...
沒有留言:
張貼留言