復制一個我從網上下載的程序給你看一下
/*
ESP8266的SoftAP+Web Server配網功能 開源帶庫(帶NTP數碼管時鐘)
*/
#include <Wire.h>
#include <ESP8266WebServer.h>
#include <TM1650.h>
#include "Public.h"
#include "WifiConfig.h"
#include "NTP.h"
/*#define SEG_A 0b00000001
#define SEG_B 0b00000010
#define SEG_C 0b00000100
#define SEG_D 0b00001000
#define SEG_E 0b00010000
#define SEG_F 0b00100000
#define SEG_G 0b01000000*/
ESP8266WebServer web(80); //WEB服務器,SoftAP默認地址為:http://192.168.4.1
#define TM_SCL 5 //GPIO5接TM1637 SCL
#define TM_SDA 13 //GPIO4接TM1637 SDA
TM1650 smg;
//定義特殊字符
// A
// ---
// F | | B
// -G-
// E | | C
// ---
// D
const uint8_t SEG_boot[] =
{ //boot
SEG_F|SEG_E|SEG_G|SEG_C|SEG_D, SEG_G|SEG_C|SEG_D|SEG_E, SEG_G|SEG_C|SEG_D|SEG_E, SEG_G|SEG_C
};
const uint8_t SEG_Sn[] =
{ //-Sn-
SEG_G, SEG_A|SEG_F|SEG_G|SEG_C|SEG_D, SEG_E|SEG_G|SEG_C, SEG_G
};
const uint8_t SEG_AP[] =
{ //-AP-
SEG_G, SEG_A|SEG_B|SEG_C|SEG_E|SEG_F|SEG_G, SEG_A|SEG_B|SEG_E|SEG_F|SEG_G, SEG_G
};
void setup()
{
//初始化調試串口
DebugBegin(74880); //74880與bootloader的一致,啟動時不會出現亂碼
DebugPrintln(); //打印回車換行
//打印軟件版本信息
const char BuildDate[]= __DATE__ ;
const char BuildTime[]= __TIME__ ;
DebugPrintf("App (Build %s %s) be starting...\n",BuildTime,BuildDate);
//打印系統及芯片信息
FlashMode_t ideMode = ESP.getFlashChipMode();
DebugPrint("Arduino Core For ESP8266 Version: "); DebugPrintln(ESP.getCoreVersion());
//DebugPrintln(ESP.getFullVersion());
DebugPrint("Reset reason: "); DebugPrintln(ESP.getResetReason());
//DebugPrint("Rest info: "); DebugPrintln(ESP.getResetInfo());
DEBUG_OUTPUT.printf("Chip id: %08X\n", ESP.getChipId());
DEBUG_OUTPUT.printf("Flash real id: %08X\n", ESP.getFlashChipId());
DEBUG_OUTPUT.printf("Flash real size: %u KBytes\n", ESP.getFlashChipRealSize()/1024);
DEBUG_OUTPUT.printf("IDE config Flash size: %u KBytes\n", ESP.getFlashChipSize()/1024);
DEBUG_OUTPUT.printf("IDE config Flash frequency: %u MHz\n", ESP.getFlashChipSpeed()/1000000);
DEBUG_OUTPUT.printf("Flash ide mode: %s\n", (ideMode == FM_QIO ? "QIO" : ideMode == FM_QOUT ? "QOUT" : ideMode == FM_DIO ? "DIO" : ideMode == FM_DOUT ? "DOUT" : "UNKNOWN"));
//初始化ESP的看門狗
ESP.wdtEnable(8000); //使能軟件看門狗的觸發間隔MS
ESP.wdtFeed(); //喂狗
Wire.begin( TM_SDA, TM_SCL );
smg.Init();
//時鐘數碼管顯示boot
smg.SetBrightness( 1 ); //設置亮度
smg.WriteNum( SEG_boot ); //顯示boot
//初始化配網功能,SoftAP模式下配網:http://192.168.4.1/WifiConfig;程序升級:http://192.168.4.1/update
WifiConfigInit(&web);
//主頁跳轉,默認跳轉到配網頁面
web.on("/", []()
{
web.sendHeader("Location", "/WifiConfig");
web.send(302, F("text/plain"), "");
});
//啟動WebServer
web.begin();
delay(100);
web.client().setNoDelay(true);
DebugPrintln("HTTP server started.");
}
bool Init = true;
void loop()
{
ESP.wdtFeed(); //喂狗
WifiConfigTask(); //Wifi配網程序任務
web.handleClient(); //WEB服務處理數據
if(WifiConfigState()==Wifi_AP_OK)
{ //配網模式下
smg.SetBrightness( 7 ); //設置亮度
smg.WriteNum( SEG_AP ); //顯示-AP-
}
if(WifiConfigState()==Wifi_STA_OK)
{ //聯網模式下
if(Init)
{
Init=false;
//在數碼管上顯示IP地址最后一段,方便瀏覽器輸入地址
IPAddress myip=WiFi.localIP();
uint8_t fourth_octet=myip[3];
uint8_t seg[4];
seg[0]=SEG_A|SEG_B|SEG_E|SEG_F|SEG_G; //P
seg[1]=d.encodeDigit(fourth_octet / 100);
seg[2]=d.encodeDigit(fourth_octet % 100 / 10);
seg[3]=d.encodeDigit(fourth_octet % 10);
smg.SetBrightness( 1 ); //設置亮度
smg.WriteNum( seg ); //顯示IP地址最后一段
unsigned long showTime = millis();
NtpInit(3600); //初始化NTP功能,每1小時對時一次
while((millis() - showTime) < 2000) delay(100); //保證IP可以顯示2s
}
else
{
task();
}
}
//打印當前網絡狀態
if(WifiConfigState()==Wifi_NoLink)DebugPrintln("Wifi config state is Wifi_NoLink");
if(WifiConfigState()==Wifi_STABegin)DebugPrintln("Wifi config state is Wifi_STABegin");
if(WifiConfigState()==Wifi_STALinking)DebugPrintln("Wifi config state is Wifi_STALinking");
//if(WifiConfigState()==Wifi_STA_OK)DebugPrintln("Wifi config state is Wifi_STA_OK");
if(WifiConfigState()==Wifi_APBegin)DebugPrintln("Wifi config state is Wifi_APBegin");
//if(WifiConfigState()==Wifi_AP_OK)DebugPrintln("Wifi config state is Wifi_AP_OK");
}
time_t prevDisplay = 0;
void task()
{
if(timeStatus() == timeNotSet)
{ //時間更新失敗
smg.SetBrightness( 1 ); //設置亮度
smg.WriteNum( SEG_Sn ); //顯示-Sn-
}
else
{
//更新時間顯示(時間轉換為一個千位數,冒號閃爍,保留前導零)
d.showNumberDecEx(hour()*100+minute(), (millis()%1000<500)?0b01000000:0, true);
if(now() != prevDisplay)
{
prevDisplay = now();
//這里放只有在時間發生變化時才執行的程序(每秒執行一次)
DebugPrintf("%.4u-%.2u-%.2u %.2u:%.2u:%.2u\n", year(), month(), day(), hour(), minute(), second());
//自動亮度調節
if(hour()>6 && hour()<22)
{ //白天
smg.SetBrightness( 7 ); //設置亮度
}
else
{ //晚上
smg.SetBrightness( 1 ); //設置亮度
}
}
}
}
|