远程控制亮灭,并在屏幕上显示灯的状态,并且加入了显示时间的功能
#include <Wire.h> // 使用 I2C 的库
#include <Adafruit_GFX.h> //Adafruit 库写入显示器
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include<WebServer.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);
// 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>\
</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);
digitalWrite(ledPin, LOW);
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"){
digitalWrite(ledPin, LOW);
}else if(state == "on"){
digitalWrite(ledPin, HIGH);
}
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=digitalRead(ledPin);
Serial.println(val);
printLocalTime();
display.setTextSize(2);// 设置文本大小
display.setTextColor(WHITE);// 设置文本颜色
display.setCursor(10, 0);//设置显示坐标
if(val==1){
display.println("Light ON"); }
else if(val==0){
display.println("Light OFF"); }
display.display(); // 屏幕上实际显示文本
delay(1000);
}
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);
}