【Ardiuno】使用ESP32单片机创建web服务通过网页控制小灯开关的实验(图文)

经过实验测试ESP32单片机的网络连接还是很方便的,这里小飞鱼按照程序实例的代码亲自实验一下使用Esp32生成的网页服务来实现远程无线控制小灯开关功能,这样真的是离物联网开发越来越近了,哈哈!

连接好开发板和电路,将小灯正极连接在5号针脚上,接好负极。

程序代码:

复制代码
#include <WiFi.h>

const char* ssid     = "XIAOFEIYU";
const char* password = "XIAOFEIYU666";

WiFiServer server(80);

void setup()
{
    Serial.begin(9600);
    pinMode(5, OUTPUT);      // set the LED pin mode
    delay(10);

    // We start by connecting to a WiFi network
    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected.");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    
    server.begin();

    //==========================================================================
    pinMode(2, OUTPUT);
    digitalWrite(2, HIGH);
    delay(500);
    digitalWrite(2, LOW);
    delay(500); 

    digitalWrite(2, HIGH);
    delay(500);
    digitalWrite(2, LOW);
    delay(500); 

    digitalWrite(2, HIGH);
    delay(2500);
    digitalWrite(2, LOW);
}


void loop(){
  WiFiClient client = server.available();   

  if (client) {                            
    Serial.println("New Client.");           
    String currentLine = "";                
    while (client.connected()) {            
      if (client.available()) {             
        char c = client.read();            
        
        if (c == '\n') {                    
          if (currentLine.length() == 0) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 5 on.<br>");
            client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 5 off.<br>");

            client.println();            
            break;
          } else {    
            currentLine = "";
          }
        } else if (c != '\r') {  
          currentLine += c;      
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          Serial.println("GET /H");
          digitalWrite(5, HIGH);               // GET /H  开灯
        }
        if (currentLine.endsWith("GET /L")) {
          Serial.println("GET /L");
          digitalWrite(5, LOW);                // GET /L  关灯
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}

先打开串口监视器,然后编译程序上传到开发板,从串口服务器上查看到开发板的ip:192.168.199.207。

打开浏览器,输入网址可以查看到一个web页面

点击上面的一条记录上的链接,可以看到小灯被点亮了。

点击上面的一条记录上的链接,可以看到小灯熄灭了。

突然发现使用无线网络控制小灯居然可以这么神奇,各位博友有什么好的创意可以互相交流。

相关推荐
FreakStudio4 小时前
WIZnet 开源方案分享:W55MH32 开发板 + 小智 AI实现以太网版本的小智机器人
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
科芯创展4 小时前
XZ61N,N-channel open drain输出电压检测芯片
单片机·嵌入式硬件
迷茫、Peanut9 小时前
51调时钟(2)
单片机
吠品9 小时前
Python字典键获取的四种实用方法
stm32·单片机·嵌入式硬件
ye1501277745510 小时前
220V转12V|18V1A非隔离电源芯片WT5110
单片机·嵌入式硬件·其他·硬件工程
追兮兮11 小时前
MCUQuickStart:一键生成 STM32 / GD32 的 Keil、CMake、RTOS 工程
stm32·单片机·freertos·gd32·gcc·工程模版
小殷学长12 小时前
【单片机毕业设计25-基于stm32的导盲眼镜设计】
stm32·单片机·课程设计
辰哥单片机设计12 小时前
STM32智能病房呼叫系统
stm32·单片机·嵌入式硬件
想你依然心痛13 小时前
TensorFlow Lite Micro:在MCU上运行神经网络推理——量化、算子支持与内存优化
单片机·神经网络·tensorflow