用Arduino IDE实现无线控制ESP8266-01上的led亮灭

昨天刚收到了ESP8266-01 01S WIFI模块,测试一下产品是好是坏,顺便写这一篇,内容简单。
在这里插入图片描述

准备的工作

点进去下载ESP8266 SDK 因为用的Arduino IDE ,下载好后直接双击运行.exe文件即可,我用的是8266_package_2.6.3。
然后打开Arduino IDE 选择开发板NodeMCU 1.0(ESP-12E Module):
在这里插入图片描述

打开示例
在这里插入图片描述
找不到的就直接拷下面代码也可以

Arduino IDE代码

注意修改wifi名称与密码,LED_BUILTIN就是这个wifi模块上的LED灯,注意波特率设置为115200

#include <ESP8266WiFi.h>
#ifndef STASSID
#define STASSID "你的wifi名称"
#define STAPSK  "你的wifi密码"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;
WiFiServer server(80);

void setup() {
  Serial.begin(115200);

  // prepare LED
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, 0);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print(F("Connecting to "));
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

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

  server.begin();
  Serial.println(F("Server started"));

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  Serial.println(F("new client"));

  client.setTimeout(5000); // default is 1000

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(F("request: "));
  Serial.println(req);

  // Match the request
  int val;
  if (req.indexOf(F("/gpio/0")) != -1) {        ///gpio/0的话就让灯灭
    val = 0;
  } else if (req.indexOf(F("/gpio/1")) != -1) { ///gpio/1的话就让灯亮
    val = 1;
  } else {
    Serial.println(F("invalid request"));
    val = digitalRead(LED_BUILTIN);
  }

  // Set LED according to the request
  digitalWrite(LED_BUILTIN, val);

  // read/ignore the rest of the request
  // do not client.flush(): it is for output only, see below
  while (client.available()) {
    // byte by byte is not very efficient
    client.read();
  }

  // Send the response to the client
  // it is OK for multiple small client.print/write,
  // because nagle algorithm will group them into one single packet
  client.print(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now "));
  client.print((val) ? F("high") : F("low"));
  client.print(F("<br><br>Click <a href='http://"));
  client.print(WiFi.localIP());
  client.print(F("/gpio/1'>here</a> to switch LED GPIO on, or <a href='http://"));
  client.print(WiFi.localIP());
  client.print(F("/gpio/0'>here</a> to switch LED GPIO off.</html>"));

  // The client will actually be *flushed* then disconnected
  // when the function returns and 'client' object is destroyed (out-of-scope)
  // flush = ensure written data are received by the other side
  Serial.println(F("Disconnecting from client"));
}

上传运行:
运行成功后,打开串口监视器,注意右下角设置波特率为115200,一开始可能看不到东西,这时候按下wifi模块上面的RST复位按键,此时应该能看到下面的界面:

在这里插入图片描述
然后打开浏览器,输入出现的地址,我的是192.168.3.11
在这里插入图片描述
可以看到下面的 here 就可以控制led的亮灭
或者自己在那一串地址后面 加入 /gpio/1 或0 试试

  • 3
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
好的,以下是基于 Arduino IDE 的 ESP32-CAM 点灯科技和蓝牙无线监控的代码示例: ``` #include <BluetoothSerial.h> #include <Wire.h> #include <Arduino.h> // 定义 LED 灯的引脚 const int ledPin = 4; // 定义蓝牙串口对象 BluetoothSerial SerialBT; void setup() { // 初始化串口和 LED 灯 Serial.begin(9600); pinMode(ledPin, OUTPUT); // 初始化蓝牙串口 SerialBT.begin("ESP32-CAM Bluetooth"); Serial.println("ESP32-CAM Bluetooth 开始工作"); } void loop() { // 检测蓝牙串口是否有数据可读 if (SerialBT.available()) { // 读取蓝牙串口的数据 char data = SerialBT.read(); // 根据读取到的数据来控制 LED 灯的开关 switch (data) { case '0': digitalWrite(ledPin, LOW); Serial.println("LED 灯已关闭"); break; case '1': digitalWrite(ledPin, HIGH); Serial.println("LED 灯已打开"); break; default: break; } } } ``` 在上述代码中,我们首先定义了 LED 灯的引脚为 4 号引脚,然后初始化了蓝牙串口对象 SerialBT。在 setup 函数中,我们初始化了串口和 LED 灯,并且开启了蓝牙串口。在 loop 函数中,我们不断检测蓝牙串口是否有数据可读,如果有数据可读,则通过读取到的数据来控制 LED 灯的开关。其中,当读取到 '0' 时,表示关闭 LED 灯;当读取到 '1' 时,表示打开 LED 灯。 需要注意的是,我们使用了 BluetoothSerial 库来实现蓝牙串口通信,因此需要先在 Arduino IDE 中安装该库。同时,由于 ESP32-CAM 板子上没有 USB 转串口芯片,因此我们需要通过 FTDI 等 USB 转串口模块将 ESP32-CAM 与电脑连接起来,以便进行代码烧录和调试。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值