哈喽各位小夥伴,不知不觉系列文章已经进行一半了(感觉要解锁成就了),不知道各位在看完前几篇後,对Esp32s、程序写法有没有更深的认识,如果没有的话,可以继续的看下去,因为後面会有更多应用,可以看到更多不同的程序码,回归正题,今天要讲解用Esp32s的AP mode控制继电器(Relay),那我们就直接介绍元件。
图片取自:使用者绘制
上面红圈所圈选的就是本次会使用到的继电器(Relay),那继电器的功能常被拿来当作安全保护或控制的功用,为甚麽说这两点呢,因为继电器可以利用较小电流控制较大电流的零件,像是控制灯泡、马达...等等物品,那这可以解释利用小电流控制大电流的零件,那没解释到如何做到安全保护的功用对吧,因为这讲解到继电器的构造,继电器内部像是个Y,当继电器接受到电流时,因继电器内部有电磁铁会将内部的簧片拉至常开(NO,Normally Open),但如果没接收到电内部簧片就固定於至常闭(NC,Normally Close),那我们可以针对继电器的这功能做出,当继电器接收到电流就跳掉,用这点做一个安全保护的功能。
Esp32s GND -> St01 GND
Esp32s Vcc(5V) -> ST01(G) +
Esp32s 23 or 22 -> ST01(G) io
(io这边可选择用Esp32s的23或22脚都可以,因为程序码写的是两个按钮,但如果选择22那操作时就只能按22的按钮,反之23也相同)
#include <WiFi.h>
// Replace with your network credentials
const char* ssidAP = "E32_Relay_AP";
const char* passwordAP = "12345678";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String output0State = "off";
String output2State = "off";
// Assign output variables to GPIO pins
const int output0 = 23;
const int output2 = 22;
void setup() {
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(output0, OUTPUT);
pinMode(output2, OUTPUT);
// Set outputs to LOW
digitalWrite(output0, LOW);
digitalWrite(output2, LOW);
// Connect to Wi-Fi network with SSID and password
Serial.print("Setting AP (Access Point)…");
// Remove the password parameter, if you want the AP (Access Point) to be open
WiFi.softAP(ssidAP, passwordAP);
delay(500);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /23/on") >= 0) {
Serial.println("GPIO 23 on");
output0State = "on";
digitalWrite(output0, HIGH);
} else if (header.indexOf("GET /23/off") >= 0) {
Serial.println("GPIO 23 off");
output0State = "off";
digitalWrite(output0, LOW);
} else if (header.indexOf("GET /22/on") >= 0) {
Serial.println("GPIO 22 on");
output2State = "on";
digitalWrite(output2, HIGH);
} else if (header.indexOf("GET /22/off") >= 0) {
Serial.println("GPIO 22 off");
output2State = "off";
digitalWrite(output2, LOW);
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #555555;}</style></head>");
// Web Page Heading
client.println("<body><h1>ESP32 Web Server</h1>");
// Display current state, and ON/OFF buttons for GPIO 23
client.println("<p>GPIO 23 - State " + output0State + "</p>");
// If the output0State is off, it displays the ON button
if (output0State=="off") {
client.println("<p><a href=\"/23/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/23/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 22
client.println("<p>GPIO 22 - State " + output2State + "</p>");
// If the output2State is off, it displays the ON button
if (output2State=="off") {
client.println("<p><a href=\"/22/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/22/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
跟之前操作LED AP那篇相同,一样是连上WiFi然後在浏览器上打192.168.4.1,就可连至Esp32s所开启得网页中罗,然後就可以看自己接的脚位是23或22去点击,就可以控制Relay的开启予关闭
图片取自:使用者拍摄
你会发现,你会讶异,怎麽跟LED AP长一样,哎呀你猜对了,就是一样的写法。
好了,今天先到这里!没想到吧,LED AP的写法在这边也可做运用,那可以做运用的原因是因为继电器也只有分做开启(ON)与关闭(OFF),那LED也是相同的状况,相信我後面还有一篇也是这样,那就是STA,但下面一篇就是开始讲解loop里面的程序啦,所以请不要觉得我在划水(虽然真的很像),那我们就下一篇见啦~
>>: [13th-铁人赛]Day 10:Modern CSS 超详细新手攻略 - background
教材网址 https://coding104.blogspot.com/2021/06/java-f...
今天会是单纯的自我对话,没有任何程序 0x1 回想 Laravel 开发过程 Laravel 对我来...
我有一群变态朋友,我们总称变态五怪人(?),现在扣掉我就变成变态四乾妈了~ 拖到现在乾妈们终於来了...
今天再举一个for回圈的例子,找出1~100的偶数。 Ans:从一开始所以一开始int i=1,然後...
物件导向的继承(inheritance)特性 继承 为物件导向程序设计的特性之一,子类别 (subc...