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

该代码示例展示了一个使用Arduino和WebServer库实现的远程控制LED灯亮度的系统,通过网页进行开关和亮度调节。同时,结合时间管理功能,可以根据系统时间自动调整LED灯的亮灭状态。系统还配备了一个128x64的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
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值