ESP32远程控制小灯亮灭,oled屏显示亮度值

一:简单的远程控制小灯亮灭,并在屏幕上显示亮度等级 

#include <Wire.h> // 使用 I2C 的库
#include <Adafruit_GFX.h> //Adafruit 库写入显示器
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <WebServer.h>
#include <Arduino.h>
#include <analogWrite.h>

const int ledPin = 2;
const char *ssid = "nova6";    //你的网络名称
const char *password = "zdx123456"; //你的网络密码 
#define SCREEN_WIDTH 128 // 使用的是 128×64 OLED 显示屏
#define SCREEN_HEIGHT 64 // 
WebServer server(80);
int val = 0;

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
//  I2C 通信协议
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);// (-1) 参数表示您的 OLED 显示器没有 RESET 引脚
 
void testscrolltext(void); //函数声明
 
const char *ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 8 * 3600;
const int daylightOffset_sec = 0;


void handleRoot(){
  String HTML = "<!DOCTYPE html>\
  <html>\
  <head><meta charset='utf-8'></head>\
  <body>\
  鼠标点击控制LED!\
  <script>var xhttp = new XMLHttpRequest();\
          function sw(arg){\
            xhttp.open('GET', '/sw?Led=' + arg,true );\
            xhttp.send()}\
  </script>\
  <button onmousedown=sw('on')>开灯</button>\
  <button onmousedown=sw('off')>关灯</button>\
  <button onmousedown=sw('1')>1</button>\
  <button onmousedown=sw('2')>2</button>\
  <button onmousedown=sw('3')>3</button>\
  <button onmousedown=sw('4')>4</button>\
  </body>\
  </html>";
  server.send(200,"text/html",HTML);
  
}

void printLocalTime()
{
    struct tm timeinfo;
    if (!getLocalTime(&timeinfo))
    {
        Serial.println("Failed to obtain time");
        return;
    }
    Serial.println(&timeinfo, "%F %T %A"); // 格式化输出
    display.clearDisplay();// 清除显示
    display.setTextSize(2);// 设置文本大小
    display.setTextColor(WHITE);// 设置文本颜色
    display.setCursor(0, 30);//设置显示坐标
    // Display static text
    display.println(&timeinfo, "%F %T");//
}
void setup() {
  Serial.begin(115200);//115200 的波特率初始化串行监视器以进行调试
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");
    }
  Serial.println("WiFi connected!");
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))  //0x3d 0x3c  0x78 0x7A // Address 0x3D for 128x64
   { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  pinMode(ledPin, OUTPUT);
  //analogWrite(ledPin, 10);
  delay(2000);
  display.clearDisplay();// 清除显示
  Serial.print("\nIP地址: ");
  Serial.println(WiFi.localIP());
  //创建服务器,如果访问IP地址的根目录。然后就返回一段html字符串
  server.on("/", handleRoot);
  server.on("/sw",ledSwitch);
  server.on("/c", [](){server.send(200,"text/html","hello");});
  server.onNotFound([](){server.send(200,"text/html;charset=utf-8","没有找到页面!");});
  server.begin();


//  display.clearDisplay();// 清除显示
//  display.setTextSize(3);// 设置文本大小
//  display.setTextColor(WHITE);// 设置文本颜色
//  display.setCursor(0, 30);//设置显示坐标
//  // Display static text
//  display.println("naiva");//
//    display.setTextColor(WHITE);// 设置文本颜色
//  display.setCursor(20, 0);//设置显示坐标
//  display.println("haha");// 
//  display.display(); // 屏幕上实际显示文本
   configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
   printLocalTime();
}
 
//void configTime(long gmtOffset_sec, int daylightOffset_sec, const char* server1, const char* server2 = nullptr, const char* server3 = nullptr) 
int i = 0; 


void ledSwitch(){
  String state = server.arg("Led");
  if(state == "off"){
    analogWrite(ledPin, 0);
    val = 0;
  }else if(state == "on"){
    analogWrite(ledPin, 250);
    val = 4;
  }else if(state == "1"){
    analogWrite(ledPin, 20);
    val = 1;
  }else if(state == "2"){
    analogWrite(ledPin, 100);
    val = 2;
  }else if(state == "3"){
    analogWrite(ledPin, 180);
    val = 3;
  }else if(state == "4"){
    analogWrite(ledPin, 250);
    val = 4;
  }
  server.send(200, "text/html", "LED IS <b>" + state + "</b>.");
}
void loop() {
//  //testscrolltext();
//  display.clearDisplay();
//  display.setTextSize(1); // 
//  display.setTextColor(WHITE);
//  display.setCursor(10, 0);
//  display.println(F("naiva"));
//  display.display();      //
//  delay(100);
//  display.startscrollright(0x00, 0x0F);//  从左到右滚动文本
//  delay(4000);
//  display.stopscroll();// 停止滚动
//  delay(1000);
//  display.startscrollleft(0x00, 0x0F);//  从右到左滚动文本
//  delay(4000);
//  display.stopscroll();
//  delay(1000);

  server.handleClient();
  //int val=analogRead(ledPin);
  Serial.println(val);
  printLocalTime();
  display.setTextSize(2);// 设置文本大小
  display.setTextColor(WHITE);// 设置文本颜色
  display.setCursor(0, 0);//设置显示坐标
  if(val>0){
  display.println("Light ON"); }
  else if(val==0){
    display.println("Light OFF"); }
  display.setTextSize(2);// 设置文本大小
  display.setTextColor(WHITE);// 设置文本颜色
  display.setCursor(109, 0);//设置显示坐标
  display.println(val);
  display.display(); // 屏幕上实际显示文本
  delay(20);
}
void testscrolltext(void) {
  display.clearDisplay();
 
  display.setTextSize(2); // Draw 2X-scale text
  display.setTextColor(WHITE);
  display.setCursor(10, 0);
  display.println(F("NAIVA415"));
  display.display();      // Show initial text
  delay(100);
 
  // Scroll in various directions, pausing in-between:
  display.startscrollright(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);
  display.startscrollleft(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);
  display.startscrolldiagright(0x00, 0x07);
  delay(2000);
  display.startscrolldiagleft(0x00, 0x07);
  delay(2000);
  display.stopscroll();
  delay(1000);
}
 
 

二:加入时间管理

通过获取系统时间,对小灯亮灭进行了定时。实现到时间自动亮灭 

#include <Wire.h> // 使用 I2C 的库
#include <Adafruit_GFX.h> //Adafruit 库写入显示器
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <WebServer.h>
#include <Arduino.h>
#include <analogWrite.h>

const int ledPin = 2;
const char *ssid = "nova6";    //你的网络名称
const char *password = "zdx123456"; //你的网络密码 
#define SCREEN_WIDTH 128 // 使用的是 128×64 OLED 显示屏
#define SCREEN_HEIGHT 64 // 
WebServer server(80);
int val = 0;

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
//  I2C 通信协议
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);// (-1) 参数表示您的 OLED 显示器没有 RESET 引脚
 
void testscrolltext(void); //函数声明
 
const char *ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 8 * 3600;
const int daylightOffset_sec = 0;
struct tm timeinfo;
struct tm now;
void handleRoot(){
  String HTML = "<!DOCTYPE html>\
  <html>\
  <head><meta charset='utf-8'></head>\
  <body>\
  鼠标点击控制LED!\
  <script>var xhttp = new XMLHttpRequest();\
          function sw(arg){\
            xhttp.open('GET', '/sw?Led=' + arg,true );\
            xhttp.send()}\
  </script>\
  <button onmousedown=sw('on')>开灯</button>\
  <button onmousedown=sw('off')>关灯</button>\
  <button onmousedown=sw('1')>1</button>\
  <button onmousedown=sw('2')>2</button>\
  <button onmousedown=sw('3')>3</button>\
  <button onmousedown=sw('4')>4</button>\
  </body>\
  </html>";
  server.send(200,"text/html",HTML);
  
}

void printLocalTime()
{

    if (!getLocalTime(&timeinfo))
    {
        Serial.println("Failed to obtain time");
        return;
    }
    Serial.println(&timeinfo, "%F %T %A"); // 格式化输出
    display.clearDisplay();// 清除显示
    display.setTextSize(2);// 设置文本大小
    display.setTextColor(WHITE);// 设置文本颜色
    display.setCursor(0, 30);//设置显示坐标
    // Display static text
    display.println(&timeinfo, "%F %T");//
    if(timeinfo.tm_sec>20 && timeinfo.tm_sec<40){
      if(val==0){
        analogWrite(ledPin, 0);
      }else if(val==1){
        analogWrite(ledPin, 50);
      }else if(val==2){
        analogWrite(ledPin, 100);
      }else if(val==3){
        analogWrite(ledPin, 180);
      }else if(val==4){
        analogWrite(ledPin, 250);
      }
    }
    else{
      analogWrite(ledPin, 0);
    }
    Serial.println(&timeinfo); // 格式化输出
}
void setup() {
  Serial.begin(115200);//115200 的波特率初始化串行监视器以进行调试
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");
    }
  Serial.println("WiFi connected!");
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))  //0x3d 0x3c  0x78 0x7A // Address 0x3D for 128x64
   { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  pinMode(ledPin, OUTPUT);
  //analogWrite(ledPin, 10);
  delay(2000);
  display.clearDisplay();// 清除显示
  Serial.print("\nIP地址: ");
  Serial.println(WiFi.localIP());
  //创建服务器,如果访问IP地址的根目录。然后就返回一段html字符串
  server.on("/", handleRoot);
  server.on("/sw",ledSwitch);
  server.on("/c", [](){server.send(200,"text/html","hello");});
  server.onNotFound([](){server.send(200,"text/html;charset=utf-8","没有找到页面!");});
  server.begin();


//  display.clearDisplay();// 清除显示
//  display.setTextSize(3);// 设置文本大小
//  display.setTextColor(WHITE);// 设置文本颜色
//  display.setCursor(0, 30);//设置显示坐标
//  // Display static text
//  display.println("naiva");//
//    display.setTextColor(WHITE);// 设置文本颜色
//  display.setCursor(20, 0);//设置显示坐标
//  display.println("haha");// 
//  display.display(); // 屏幕上实际显示文本
   configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
   printLocalTime();
}
 
//void configTime(long gmtOffset_sec, int daylightOffset_sec, const char* server1, const char* server2 = nullptr, const char* server3 = nullptr) 
int i = 0; 


void ledSwitch(){
  String state = server.arg("Led");
  if(state == "off"){
    analogWrite(ledPin, 0);
    val = 0;
  }else if(state == "on"){
    analogWrite(ledPin, 250);
    val = 4;
  }else if(state == "1"){
    analogWrite(ledPin, 20);
    val = 1;
  }else if(state == "2"){
    analogWrite(ledPin, 100);
    val = 2;
  }else if(state == "3"){
    analogWrite(ledPin, 180);
    val = 3;
  }else if(state == "4"){
    analogWrite(ledPin, 250);
    val = 4;
  }
  server.send(200, "text/html", "LED IS <b>" + state + "</b>.");
}
void loop() {
//  //testscrolltext();
//  display.clearDisplay();
//  display.setTextSize(1); // 
//  display.setTextColor(WHITE);
//  display.setCursor(10, 0);
//  display.println(F("naiva"));
//  display.display();      //
//  delay(100);
//  display.startscrollright(0x00, 0x0F);//  从左到右滚动文本
//  delay(4000);
//  display.stopscroll();// 停止滚动
//  delay(1000);
//  display.startscrollleft(0x00, 0x0F);//  从右到左滚动文本
//  delay(4000);
//  display.stopscroll();
//  delay(1000);

  server.handleClient();
  //int val=analogRead(ledPin);
  Serial.println(val);
  printLocalTime();
  display.setTextSize(2);// 设置文本大小
  display.setTextColor(WHITE);// 设置文本颜色
  display.setCursor(0, 0);//设置显示坐标
  if(val>0){
  display.println("Light ON"); }
  else if(val==0){
    display.println("Light OFF"); }
  display.setTextSize(2);// 设置文本大小
  display.setTextColor(WHITE);// 设置文本颜色
  display.setCursor(109, 0);//设置显示坐标
  display.println(val);
  display.display(); // 屏幕上实际显示文本
  delay(20);
}
void testscrolltext(void) {
  display.clearDisplay();
 
  display.setTextSize(2); // Draw 2X-scale text
  display.setTextColor(WHITE);
  display.setCursor(10, 0);
  display.println(F("NAIVA415"));
  display.display();      // Show initial text
  delay(100);
 
  // Scroll in various directions, pausing in-between:
  display.startscrollright(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);
  display.startscrollleft(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);
  display.startscrolldiagright(0x00, 0x07);
  delay(2000);
  display.startscrolldiagleft(0x00, 0x07);
  delay(2000);
  display.stopscroll();
  delay(1000);
}
 
 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: esp32是一款功能强大的Wi-Fi和蓝牙模块,可以用来实现对LED的控制。通过与Wi-Fi网络连接,我们可以远程控制ESP32上的LED灯。 首先,我们需要连接ESP32与LED灯。ESP32开发板上有多个引脚,我们可以选择其中一个GPIO引脚将其连接到LED的正极,而将LED的负极连接到ESP32的地线(GND)。接下来,我们需要在编程环境中进行相关设置。 使用ESP32的开发环境如Arduino IDE或MicroPython,我们可以通过编写程序来实现对LED的控制。首先,我们需要导入相关库文件,如WiFi库和GPIO库。然后,我们需要定义LED所连接的GPIO引脚,并设置为输出模式。 接下来,我们需要连接到Wi-Fi网络。我们可以通过编写代码配置Wi-Fi的名称和密码,在ESP32上进行连接。一旦成功连接到Wi-Fi网络,我们就可以使用ESP32的IP地址来进行通信。 现在,我们可以通过发送特定的指令来控制LED。我们可以编写代码,当接收到特定指令时,ESP32会将所连接的GPIO引脚设置为高电平或低电平,来打开或关闭LED。 此外,我们还可以扩展功能,比如通过手机上的App或Web页面来控制LED的开关。我们可以编写代码,使ESP32作为一个Web服务器,在浏览器上打开ESP32的IP地址,并通过互动界面来实现远程控制LED。 总之,ESP32是一款强大的Wi-Fi和蓝牙模块,可以用来实现对LED的远程控制。通过编写程序,我们可以将ESP32与Wi-Fi网络连接,并设置GPIO引脚来控制LED的开关。希望这些信息能对你有所帮助。 ### 回答2: ESP32是一款高度集成的微控制器,支持Wi-Fi连接。它可用于控制LED灯,实现智能家居和物联网应用。 要控制LED灯,首先需要连接ESP32和LED灯。通过使用ESP32的GPIO引脚,将其连接到LED灯的正极和负极。然后,编写代码以控制ESP32上的GPIO引脚。 在ESP32中,我们可以使用Arduino IDE或其他支持ESP32开发的集成开发环境来编写代码。首先,要使用Wi-Fi功能,我们需要设置Wi-Fi连接。我们需要提供Wi-Fi的SSID和密码以连接到无线网络。 一旦ESP32连接到Wi-Fi网络,我们可以编写代码来控制LED灯。在ESP32上,我们可以使用DigitalWrite函数将GPIO引脚设置为高电平或低电平以打开或关闭LED灯。 例如,如果我们将LED灯连接到ESP32的GPIO引脚5上,我们可以使用以下代码来控制LED灯: ``` #include <WiFi.h> const char* ssid = "WiFi网络的SSID"; const char* password = "WiFi网络的密码"; const int ledPin = 5; void setup() { pinMode(ledPin, OUTPUT); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("正在连接到WiFi..."); } Serial.println("WiFi连接成功!"); Serial.print("IP地址:"); Serial.println(WiFi.localIP()); } void loop() { digitalWrite(ledPin, HIGH); // 打开LED灯 delay(1000); // 延时1秒 digitalWrite(ledPin, LOW); // 关闭LED灯 delay(1000); // 延时1秒 } ``` 以上代码设置GPIO引脚5为输出,并将其设置为高电平或低电平来打开或关闭LED灯。在循环中,我们持续循环开关LED灯的状态,并使用1秒的延时。 通过上传这段代码到ESP32,它将连接到Wi-Fi网络并控制LED灯的开关状态。你可以使用手机或电脑通过Wi-Fi连接到ESP32,并从远程控制LED灯的状态。 这是使用ESP32和Wi-Fi控制LED灯的简单介绍。你可以根据需要进一步开发和优化代码,实现更复杂的功能,如通过手机应用程序控制LED灯的颜色和亮度。 ### 回答3: ESP32是一款功能强大的微控制器,具有内置WiFi模块,可以用于控制LED灯。 对于ESP32控制LED,首先需要将ESP32与LED灯连接起来。可以通过将LED的正极连接到ESP32的数字输出引脚,如GPIO2,并将LED的负极连接到ESP32的地线GND。 在代码方面,首先需要包含"WiFi.h"和"esp_wifi.h"这两个头文件,以便使用ESP32WiFi功能。然后,需要编写WiFi连接函数,将ESP32连接到相应的WiFi网络。连接成功后,可以使用WiFi客户端API发送请求或接收命令,以控制LED灯的开关状态。 在LED控制方面,可以使用digitalWrite函数来设置GPIO引脚的输出状态。例如,可以使用digitalWrite(GPIO2, HIGH)来点亮LED灯,使用digitalWrite(GPIO2, LOW)来关闭LED灯。 为了使程序能够实时响应外部命令,可以将控制部分放入一个循环中,并使用delay函数来设置一定的延迟时间,以便程序可以持续运行。 除了基本的开关控制外,还可以通过PWM(脉宽调制)来实现灯光亮度的调节。可以使用analogWrite函数来设置PWM输出的占空比,从而控制LED的亮度。 总的来说,ESP32是一款功能强大的微控制器,内置WiFi模块使其可以轻松控制LED灯。通过适当的代码编写和电路连接,我们可以实现对LED灯开关状态以及亮度的控制。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值