一:简单的远程控制小灯亮灭,并在屏幕上显示亮度等级
#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