diff --git a/.vscode/arduino.json b/.vscode/arduino.json new file mode 100644 index 0000000..11dee6d --- /dev/null +++ b/.vscode/arduino.json @@ -0,0 +1,5 @@ +{ + "board": "esp8266:esp8266:nodemcuv2", + "configuration": "xtal=80,vt=flash,exception=legacy,ssl=all,eesz=4M2M,led=2,ip=lm2f,dbg=Disabled,lvl=None____,wipe=none,baud=115200", + "sketch": "Network_clock_fans_monitor.ino" +} \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..716569a --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,22 @@ +{ + "configurations": [ + { + "name": "Win32", + "includePath": [ + "C:\\Users\\杨宝祥\\AppData\\Local\\Arduino15\\packages\\esp8266\\tools\\**", + "C:\\Users\\杨宝祥\\Documents\\Arduino\\libraries\\**", + "C:\\Program Files (x86)\\Arduino\\libraries\\**", + "C:\\Users\\杨宝祥\\AppData\\Local\\Arduino15\\packages\\esp8266\\hardware\\esp8266\\2.6.3\\**", + "C:\\Users\\杨宝祥\\Documents\\Arduino\\libraries" + ], + "forcedInclude": [], + "defines": [ + "USBCON" + ], + "intelliSenseMode": "windows-msvc-x64", + "cStandard": "gnu17", + "cppStandard": "c++17" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..ded6629 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "vsciot-vscode.vscode-arduino" + ] +} \ No newline at end of file diff --git a/ESP8266_Network_Clock_12864SSD1306/ESP8266_Network_Clock_12864SSD1306.ino b/ESP8266_Network_Clock_12864SSD1306/ESP8266_Network_Clock_12864SSD1306.ino deleted file mode 100644 index a690413..0000000 --- a/ESP8266_Network_Clock_12864SSD1306/ESP8266_Network_Clock_12864SSD1306.ino +++ /dev/null @@ -1,261 +0,0 @@ -/* 作者:flyAkari 会飞的阿卡林 bilibili UID:751219 - * 本代码适用于ESP8266 NodeMCU + 12864显示屏 - * 7pin SPI引脚,正面看,从左到右依次为GND、VCC、D0、D1、RES、DC、CS - * ESP8266 --- OLED - * 3V --- VCC - * G --- GND - * D7 --- D1 - * D5 --- D0 - * D2orD8--- CS - * D1 --- DC - * RST --- RES - * 4pin IIC引脚,正面看,从左到右依次为GND、VCC、SCL、SDA - * ESP8266 --- OLED - * 3.3V --- VCC - * G (GND) --- GND - * D1(GPIO5)--- SCL - * D2(GPIO4)--- SDA - */ -#include -#include -#include -#include -#include -#include - -//若屏幕使用SH1106,只需把SSD1306改为SH1106即可 -//U8G2_SSD1306_128X64_NONAME_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/4, /* dc=*/5, /* reset=*/3); -U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); -//U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 4, /* data=*/ 5); //D-duino - -DYWiFiConfig wificonfig; -ESP8266WebServer webserver(80); -#define DEF_WIFI_SSID "D1" -#define DEF_WIWI_PASSWORD "01234567890" -#define AP_NAME "flyAkari" //dev -void wificb(int c) -{ - Serial.print("=-=-=-=-"); - Serial.println(c); -} - -static const char ntpServerName[] = "ntp1.aliyun.com"; //NTP服务器,阿里云 -const int timeZone = 8; //时区,北京时间为+8 - -WiFiUDP Udp; -unsigned int localPort = 8888; // 用于侦听UDP数据包的本地端口 - -time_t getNtpTime(); -void sendNTPpacket(IPAddress& address); -void oledClockDisplay(); -void sendCommand(int command, int value); -void initdisplay(); - -boolean isNTPConnected = false; - -const unsigned char xing[] U8X8_PROGMEM = { - 0x00, 0x00, 0xF8, 0x0F, 0x08, 0x08, 0xF8, 0x0F, 0x08, 0x08, 0xF8, 0x0F, 0x80, 0x00, 0x88, 0x00, - 0xF8, 0x1F, 0x84, 0x00, 0x82, 0x00, 0xF8, 0x0F, 0x80, 0x00, 0x80, 0x00, 0xFE, 0x3F, 0x00, 0x00 -}; /*星*/ -const unsigned char liu[] U8X8_PROGMEM = { - 0x40, 0x00, 0x80, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, - 0x20, 0x02, 0x20, 0x04, 0x10, 0x08, 0x10, 0x10, 0x08, 0x10, 0x04, 0x20, 0x02, 0x20, 0x00, 0x00 -}; /*六*/ - - -void setup() -{ - Serial.begin(115200); - while (!Serial) - continue; - Serial.println("NTP Clock oled version v1.1"); - Serial.println("Designed by flyAkari"); - initdisplay(); - u8g2.clearBuffer(); - u8g2.setFont(u8g2_font_unifont_t_chinese2); - u8g2.setCursor(0, 14); - u8g2.print("Waiting for WiFi"); - u8g2.setCursor(0, 30); - u8g2.print("connection..."); - u8g2.setCursor(0, 47); - u8g2.print("flyAkari"); - u8g2.setCursor(0, 64); - u8g2.print("192.168.4.1"); - u8g2.sendBuffer(); - Serial.println("OLED Ready"); - - Serial.print("Connecting WiFi..."); - wificonfig.begin(&webserver, "/"); - DYWIFICONFIG_STRUCT defaultConfig = wificonfig.createConfig(); - strcpy(defaultConfig.SSID, DEF_WIFI_SSID); - strcpy(defaultConfig.SSID_PASSWORD, DEF_WIWI_PASSWORD); - strcpy(defaultConfig.HOSTNAME, AP_NAME); - strcpy(defaultConfig.APNAME, AP_NAME); - wificonfig.setDefaultConfig(defaultConfig); - wificonfig.enableAP(); - while (WiFi.status() != WL_CONNECTED) - { - wificonfig.handle(); //若不需要Web后台,可以注释掉此行 - //Serial.println("Waiting for Connection..."); - } - Serial.println(""); - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); - Serial.println("Starting UDP"); - Udp.begin(localPort); - Serial.print("Local port: "); - Serial.println(Udp.localPort()); - Serial.println("waiting for sync"); - setSyncProvider(getNtpTime); - setSyncInterval(30); //每300秒同步一次时间 - isNTPConnected = true; -} - -time_t prevDisplay = 0; //当时钟已经显示 - -void loop() -{ - if (timeStatus() != timeNotSet) - { - if (now() != prevDisplay) - { //时间改变时更新显示 - prevDisplay = now(); - oledClockDisplay(); - } - } - wificonfig.handle(); //若不需要Web后台,可以注释掉此行 -} - -void initdisplay() -{ - u8g2.begin(); - u8g2.enableUTF8Print(); -} - -void oledClockDisplay() -{ - int years, months, days, hours, minutes, seconds, weekdays; - years = year(); - months = month(); - days = day(); - hours = hour(); - minutes = minute(); - seconds = second(); - weekdays = weekday(); - Serial.printf("%d/%d/%d %d:%d:%d Weekday:%d\n", years, months, days, hours, minutes, seconds, weekdays); - u8g2.clearBuffer(); - u8g2.setFont(u8g2_font_unifont_t_chinese2); - u8g2.setCursor(0, 14); - if (isNTPConnected) - u8g2.print("当前时间 (UTC+8)"); - else - u8g2.print("无网络!"); //如果上次对时失败,则会显示无网络 - String currentTime = ""; - if (hours < 10) - currentTime += 0; - currentTime += hours; - currentTime += ":"; - if (minutes < 10) - currentTime += 0; - currentTime += minutes; - currentTime += ":"; - if (seconds < 10) - currentTime += 0; - currentTime += seconds; - String currentDay = ""; - currentDay += years; - currentDay += "/"; - if (months < 10) - currentDay += 0; - currentDay += months; - currentDay += "/"; - if (days < 10) - currentDay += 0; - currentDay += days; - - u8g2.setFont(u8g2_font_logisoso24_tr); - u8g2.setCursor(0, 44); - u8g2.print(currentTime); - u8g2.setCursor(0, 61); - u8g2.setFont(u8g2_font_unifont_t_chinese2); - u8g2.print(currentDay); - u8g2.drawXBM(80, 48, 16, 16, xing); - u8g2.setCursor(95, 62); - u8g2.print("期"); - if (weekdays == 1) - u8g2.print("日"); - else if (weekdays == 2) - u8g2.print("一"); - else if (weekdays == 3) - u8g2.print("二"); - else if (weekdays == 4) - u8g2.print("三"); - else if (weekdays == 5) - u8g2.print("四"); - else if (weekdays == 6) - u8g2.print("五"); - else if (weekdays == 7) - u8g2.drawXBM(111, 49, 16, 16, liu); - u8g2.sendBuffer(); -} - -/*-------- NTP 代码 ----------*/ - -const int NTP_PACKET_SIZE = 48; // NTP时间在消息的前48个字节里 -byte packetBuffer[NTP_PACKET_SIZE]; // 输入输出包的缓冲区 - -time_t getNtpTime() -{ - IPAddress ntpServerIP; // NTP服务器的地址 - - while (Udp.parsePacket() > 0); // 丢弃以前接收的任何数据包 - Serial.println("Transmit NTP Request"); - // 从池中获取随机服务器 - WiFi.hostByName(ntpServerName, ntpServerIP); - Serial.print(ntpServerName); - Serial.print(": "); - Serial.println(ntpServerIP); - sendNTPpacket(ntpServerIP); - uint32_t beginWait = millis(); - while (millis() - beginWait < 1500) - { - int size = Udp.parsePacket(); - if (size >= NTP_PACKET_SIZE) - { - Serial.println("Receive NTP Response"); - isNTPConnected = true; - Udp.read(packetBuffer, NTP_PACKET_SIZE); // 将数据包读取到缓冲区 - unsigned long secsSince1900; - // 将从位置40开始的四个字节转换为长整型,只取前32位整数部分 - secsSince1900 = (unsigned long)packetBuffer[40] << 24; - secsSince1900 |= (unsigned long)packetBuffer[41] << 16; - secsSince1900 |= (unsigned long)packetBuffer[42] << 8; - secsSince1900 |= (unsigned long)packetBuffer[43]; - Serial.println(secsSince1900); - Serial.println(secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR); - return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR; - } - } - Serial.println("No NTP Response :-("); //无NTP响应 - isNTPConnected = false; - return 0; //如果未得到时间则返回0 -} - -// 向给定地址的时间服务器发送NTP请求 -void sendNTPpacket(IPAddress& address) -{ - memset(packetBuffer, 0, NTP_PACKET_SIZE); - packetBuffer[0] = 0b11100011; // LI, Version, Mode - packetBuffer[1] = 0; // Stratum, or type of clock - packetBuffer[2] = 6; // Polling Interval - packetBuffer[3] = 0xEC; // Peer Clock Precision - // 8 bytes of zero for Root Delay & Root Dispersion - packetBuffer[12] = 49; - packetBuffer[13] = 0x4E; - packetBuffer[14] = 49; - packetBuffer[15] = 52; - Udp.beginPacket(address, 123); //NTP需要使用的UDP端口号为123 - Udp.write(packetBuffer, NTP_PACKET_SIZE); - Udp.endPacket(); -} diff --git a/ESP8266_Network_Clock_12864SSD1306/I2C_ESP8266_Network_Clock_12864SSD1306v1.1.ino.d1.bin b/ESP8266_Network_Clock_12864SSD1306/I2C_ESP8266_Network_Clock_12864SSD1306v1.1.ino.d1.bin deleted file mode 100644 index 0f43a47..0000000 Binary files a/ESP8266_Network_Clock_12864SSD1306/I2C_ESP8266_Network_Clock_12864SSD1306v1.1.ino.d1.bin and /dev/null differ diff --git a/ESP8266_Network_Clock_12864SSD1306/SPI_ESP8266_Network_Clock_12864SSD1306v1.1.ino.d1.bin b/ESP8266_Network_Clock_12864SSD1306/SPI_ESP8266_Network_Clock_12864SSD1306v1.1.ino.d1.bin deleted file mode 100644 index 1b94980..0000000 Binary files a/ESP8266_Network_Clock_12864SSD1306/SPI_ESP8266_Network_Clock_12864SSD1306v1.1.ino.d1.bin and /dev/null differ diff --git a/ESP8266_Network_Clock_7SEGMAX7219/ESP8266_Network_Clock_7SEGMAX7219.ino b/ESP8266_Network_Clock_7SEGMAX7219/ESP8266_Network_Clock_7SEGMAX7219.ino deleted file mode 100644 index c6381ec..0000000 --- a/ESP8266_Network_Clock_7SEGMAX7219/ESP8266_Network_Clock_7SEGMAX7219.ino +++ /dev/null @@ -1,175 +0,0 @@ -/********************************************************************** - * 项目:8位数码管显示时间的NTP时钟 - * 硬件:适用于NodeMCU ESP8266 + MAX7219 - * 功能:连接WiFi后获取时间并在8位数码管上显示 - * 作者:flyAkari 会飞的阿卡林 bilibili UID:751219 - * 日期:2019/04/23 - **********************************************************************/ -#include -#include -#include -#include -//---------------修改此处""内的信息--------------------------------------------- -const char ssid[] = "WiFi_SSID"; //WiFi名 -const char pass[] = "WiFi_Password"; //WiFi密码 -static const char ntpServerName[] = "ntp.sjtu.edu.cn"; //NTP服务器,上海交通大学 -const int timeZone = 8; //时区,北京时间为+8 -//----------------------------------------------------------------------------- -WiFiUDP Udp; -unsigned int localPort = 8888; // 用于侦听UDP数据包的本地端口 -const int slaveSelect = 5; -const int scanLimit = 7; - -time_t getNtpTime(); -void sendNTPpacket(IPAddress &address); -void digitalClockDisplay(); -void sendCommand(int command, int value); -void initdisplay(); - -void setup() -{ - Serial.begin(9600); - while (!Serial) - continue; - Serial.println("NTP Clock version v1.0"); - - SPI.begin(); - pinMode(slaveSelect, OUTPUT); - digitalWrite(slaveSelect, LOW); - sendCommand(12, 1); //Shutdown,open - sendCommand(15, 0); //DisplayTest,no - sendCommand(10, 15); //Intensity,15(max) - sendCommand(11, scanLimit); //ScanLimit,8-1=7 - sendCommand(9, 255); //DecodeMode,Code B decode for digits 7-0 - digitalWrite(slaveSelect, HIGH); - initdisplay(); - Serial.println("LED Ready"); - - Serial.print("Connecting WiFi..."); - WiFi.mode(WIFI_STA); - WiFi.begin(ssid, pass); - while (WiFi.status() != WL_CONNECTED){ - delay(500); - Serial.print("."); - } - Serial.println(""); - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); - Serial.println("Starting UDP"); - Udp.begin(localPort); - Serial.print("Local port: "); - Serial.println(Udp.localPort()); - Serial.println("waiting for sync"); - setSyncProvider(getNtpTime); - setSyncInterval(300); //每300秒同步一次时间 -} - -time_t prevDisplay = 0; // 当时钟已经显示 - -void loop() -{ - if (timeStatus() != timeNotSet){ - if (now() != prevDisplay){ //时间改变时更新显示 - prevDisplay = now(); - digitalClockDisplay(); - } - } -} - -void sendCommand(int command, int value) -{ - digitalWrite(slaveSelect, LOW); - SPI.transfer(command); - SPI.transfer(value); - digitalWrite(slaveSelect, HIGH); -} - -void initdisplay() -{ - sendCommand(8, 0xa); - sendCommand(7, 0xa); - sendCommand(6, 0xa); - sendCommand(5, 0xa); - sendCommand(4, 0xa); - sendCommand(3, 0xa); - sendCommand(2, 0xa); - sendCommand(1, 0xa); -} - -void digitalClockDisplay() -{ - int years, months, days, hours, minutes, seconds, weekdays; - years = year(); - months = month(); - days = day(); - hours = hour(); - minutes = minute(); - seconds = second(); - weekdays = weekday(); - Serial.printf("%d/%d/%d %d:%d:%d Weekday:%d\n", years, months, days, hours, minutes, seconds, weekdays); - sendCommand(8, hours / 10); - sendCommand(7, hours % 10); - sendCommand(6, 0xa); - sendCommand(5, minutes / 10); - sendCommand(4, minutes % 10); - sendCommand(3, 0xa); - sendCommand(2, seconds / 10); - sendCommand(1, seconds % 10); -} - -/*-------- NTP 代码 ----------*/ - -const int NTP_PACKET_SIZE = 48; // NTP时间在消息的前48个字节里 -byte packetBuffer[NTP_PACKET_SIZE]; // 输入输出包的缓冲区 - -time_t getNtpTime() -{ - IPAddress ntpServerIP; // NTP服务器的地址 - - while (Udp.parsePacket() > 0); // 丢弃以前接收的任何数据包 - Serial.println("Transmit NTP Request"); - // 从池中获取随机服务器 - WiFi.hostByName(ntpServerName, ntpServerIP); - Serial.print(ntpServerName); - Serial.print(": "); - Serial.println(ntpServerIP); - sendNTPpacket(ntpServerIP); - uint32_t beginWait = millis(); - while (millis() - beginWait < 1500){ - int size = Udp.parsePacket(); - if (size >= NTP_PACKET_SIZE){ - Serial.println("Receive NTP Response"); - Udp.read(packetBuffer, NTP_PACKET_SIZE); // 将数据包读取到缓冲区 - unsigned long secsSince1900; - // 将从位置40开始的四个字节转换为长整型,只取前32位整数部分 - secsSince1900 = (unsigned long)packetBuffer[40] << 24; - secsSince1900 |= (unsigned long)packetBuffer[41] << 16; - secsSince1900 |= (unsigned long)packetBuffer[42] << 8; - secsSince1900 |= (unsigned long)packetBuffer[43]; - Serial.println(secsSince1900); - Serial.println(secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR); - return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR; - } - } - Serial.println("No NTP Response :-("); //无NTP响应 - return 0; //如果未得到时间则返回0 -} - -// 向给定地址的时间服务器发送NTP请求 -void sendNTPpacket(IPAddress &address) -{ - memset(packetBuffer, 0, NTP_PACKET_SIZE); - packetBuffer[0] = 0b11100011; // LI, Version, Mode - packetBuffer[1] = 0; // Stratum, or type of clock - packetBuffer[2] = 6; // Polling Interval - packetBuffer[3] = 0xEC; // Peer Clock Precision - // 8 bytes of zero for Root Delay & Root Dispersion - packetBuffer[12] = 49; - packetBuffer[13] = 0x4E; - packetBuffer[14] = 49; - packetBuffer[15] = 52; - Udp.beginPacket(address, 123); //NTP需要使用的UDP端口号为123 - Udp.write(packetBuffer, NTP_PACKET_SIZE); - Udp.endPacket(); -} diff --git a/Network_clock_fans_monitor.ino b/Network_clock_fans_monitor.ino new file mode 100644 index 0000000..afd6c83 --- /dev/null +++ b/Network_clock_fans_monitor.ino @@ -0,0 +1,401 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +//------------------修改此处""内的信息-------------------// +char m = 1; //0为24小时,1为上午,2为下午;若需开启12小时制,则填1或2均可 +static const char ntpServerName[] = "time.windows.com"; //NTP服务器,微软 +const int timeZone = 8; //时区,北京时间为+8 +static int scrnoff = 23; //屏幕晚上关闭的时间,24小时制,不想关填24 +static int scrnon = 8; //屏幕早上开启时间,24小时制,不想关填0 +String biliuid = "163844189"; //bilibili UID +//------------------------------------------------------// + +DYWiFiConfig wificonfig; +ESP8266WebServer webserver(80); +#define DEF_WIFI_SSID "D1" +#define DEF_WIWI_PASSWORD "01234567890" +#define AP_NAME "flyAkari" //dev +void wificb(int c) +{ + Serial.print("=-=-=-=-"); + Serial.println(c); +} +//const int rs = 19, en = 20, d4 = 16, d5 = 7, d6 = 6, d7 = 5; +//LiquidCrystal lcd(rs, en, d4, d5, d6, d7); +LiquidCrystal_I2C lcd(0x27, 16, 2); + +WiFiUDP Udp; +unsigned int localPort = 8888; // 用于侦听UDP数据包的本地端口 + +const unsigned long HTTP_TIMEOUT = 5000; +WiFiClient client; +HTTPClient http; +String response; +int follower = 0; +const int slaveSelect = 5; +const int scanLimit = 7; + +time_t getNtpTime(); +void sendNTPpacket(IPAddress &address); +void oledClockDisplay(); +void sendCommand(int command, int value); +void initdisplay(); + +boolean isNTPConnected = false; + +void setup() +{ + Serial.begin(115200); + while (!Serial) + continue; + Serial.println("NTP Clock oled version v1.1"); + //Serial.println("Designed by flyAkari"); + //lcd.begin(16, 2); + lcd.init(); + initdisplay(); + + lcd.setCursor(0, 0); + lcd.print("Connect to Wifi"); + lcd.setCursor(0, 1); + lcd.print("flyAkari"); + Serial.println("Screen Ready"); + + Serial.print("Connecting WiFi..."); + wificonfig.begin(&webserver, "/"); + DYWIFICONFIG_STRUCT defaultConfig = wificonfig.createConfig(); + strcpy(defaultConfig.SSID, DEF_WIFI_SSID); + strcpy(defaultConfig.SSID_PASSWORD, DEF_WIWI_PASSWORD); + strcpy(defaultConfig.HOSTNAME, AP_NAME); + strcpy(defaultConfig.APNAME, AP_NAME); + wificonfig.setDefaultConfig(defaultConfig); + wificonfig.enableAP(); + while (WiFi.status() != WL_CONNECTED) + { + wificonfig.handle(); //若不需要Web后台,可以注释掉此行 + //Serial.println("Waiting for Connection..."); + } + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + Serial.println("Starting UDP"); + Udp.begin(localPort); + Serial.print("Local port: "); + Serial.println(Udp.localPort()); + Serial.println("waiting for sync"); + setSyncProvider(getNtpTime); + setSyncInterval(300); //每300秒同步一次时间 + isNTPConnected = true; + lcd.clear(); +} + +time_t prevDisplay = 0; //当时钟已经显示 + +void loop() +{ + if (WiFi.status() == WL_CONNECTED) + { + if (getJson()) + { + if (parseJson(response)) + { + displayNumber(follower); + } + } + } + else + { + Serial.println("[WiFi] Waiting to reconnect..."); + errorCode(0x1); + } + //delay(1000); + + if (timeStatus() != timeNotSet) + { + if (now() != prevDisplay) + { //时间改变时更新显示 + prevDisplay = now(); + oledClockDisplay(); + } + } + wificonfig.handle(); //若不需要Web后台,可以注释掉此行 +} + +void errorCode(byte errorcode) +{ + lcd.setCursor(9, 1); + lcd.print("--E"); + lcd.print(errorcode); + lcd.print("---"); +} + +void initdisplay() +{ + //lcd.clear(); + lcd.backlight(); + lcd.setCursor(0, 0); + lcd.print("----------------"); + lcd.setCursor(0, 1); + lcd.print("----------------"); + delay(500); + lcd.clear(); +} + +void lcd_NightMode() +{ + lcd.setBacklight(0); +} + +bool energysaving(int hours) +{ + if (hours >= scrnoff || hours < scrnon) + { + return true; + } + return false; +} + +void oledClockDisplay() +{ + int years, months, days, hours, minutes, seconds, weekdays; + years = year(); + months = month(); + days = day(); + hours = hour(); + minutes = minute(); + seconds = second(); + weekdays = weekday(); + Serial.printf("%d/%d/%d %d:%d:%d Weekday:%d\n", years, months, days, hours, minutes, seconds, weekdays); + Serial.println(""); + if (energysaving(hours)) + { + lcd_NightMode(); + //Serial.println("Screen is in the energy saving mode. "); + //return; + } + else + lcd.setBacklight(1); + + /*if (isNTPConnected) + { + u8g2.print("当前时间 (UTC+8)"); + } + else + { + u8g2.print("无网络!"); + }*/ + //如果上次对时失败,则会显示无网络 + + String currentTime = ""; + if (m != 0) + { + if (hours < 12) + m = 1; + else + m = 2; + } + if (hours < 10) + { + currentTime += 0; + } + currentTime += hours; + currentTime += ":"; + if (minutes < 10) + { + currentTime += 0; + } + currentTime += minutes; + currentTime += ":"; + if (seconds < 10) + { + currentTime += 0; + } + currentTime += seconds; + /*if (m != 0) + { + if (m == 2) + { + currentTime += "P"; + } + else + { + currentTime += "A"; + } + }*/ + String currentDay = ""; + currentDay += years; + currentDay += "/"; + if (months < 10) + { + currentDay += 0; + } + currentDay += months; + currentDay += "/"; + if (days < 10) + { + currentDay += 0; + } + currentDay += days; + + lcd.setCursor(0, 1); + lcd.print(currentTime); + lcd.setCursor(0, 0); + lcd.print(currentDay); + + lcd.setCursor(11, 0); + if (weekdays == 1) + lcd.print("SU"); + else if (weekdays == 2) + lcd.print("MO"); + else if (weekdays == 3) + lcd.print("TU"); + else if (weekdays == 4) + lcd.print("WE"); + else if (weekdays == 5) + lcd.print("TH"); + else if (weekdays == 6) + lcd.print("FR"); + else if (weekdays == 7) + lcd.print("SA"); +} +/* +1970-01-01 FR AM +00:00:00 8848488 +*/ + +bool getJson() +{ + bool r = false; + http.setTimeout(HTTP_TIMEOUT); + http.begin("http://api.bilibili.com/x/relation/stat?vmid=" + biliuid); + int httpCode = http.GET(); + if (httpCode > 0) + { + if (httpCode == HTTP_CODE_OK) + { + response = http.getString(); + //Serial.println(response); + r = true; + } + } + else + { + Serial.printf("[HTTP] GET JSON failed, error: %s\n", http.errorToString(httpCode).c_str()); + errorCode(0x2); + r = false; + } + http.end(); + return r; +} + +bool parseJson(String json) +{ + const size_t capacity = JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(5) + 70; + DynamicJsonDocument doc(capacity); + deserializeJson(doc, json); + + int code = doc["code"]; + const char *message = doc["message"]; + + if (code != 0) + { + Serial.print("[API]Code:"); + Serial.print(code); + Serial.print(" Message:"); + Serial.println(message); + errorCode(0x3); + return false; + } + + JsonObject data = doc["data"]; + unsigned long data_mid = data["mid"]; + int data_follower = data["follower"]; + if (data_mid == 0) + { + Serial.println("[JSON] FORMAT ERROR"); + errorCode(0x4); + return false; + } + Serial.print("UID: "); + Serial.print(data_mid); + Serial.print(" follower: "); + Serial.println(data_follower); + + follower = data_follower; + return true; +} + +void displayNumber(int number) //display number in the middle +{ + if (number < 0 || number > 9999999) + return; + lcd.setCursor(9, 1); + lcd.print(number); +} + +/*-------- NTP 代码 ----------*/ + +const int NTP_PACKET_SIZE = 48; // NTP时间在消息的前48个字节里 +byte packetBuffer[NTP_PACKET_SIZE]; // 输入输出包的缓冲区 + +time_t getNtpTime() +{ + IPAddress ntpServerIP; // NTP服务器的地址 + + while (Udp.parsePacket() > 0) + ; // 丢弃以前接收的任何数据包 + Serial.println("Transmit NTP Request"); + // 从池中获取随机服务器 + WiFi.hostByName(ntpServerName, ntpServerIP); + Serial.print(ntpServerName); + Serial.print(": "); + Serial.println(ntpServerIP); + sendNTPpacket(ntpServerIP); + uint32_t beginWait = millis(); + while (millis() - beginWait < 1500) + { + int size = Udp.parsePacket(); + if (size >= NTP_PACKET_SIZE) + { + Serial.println("Receive NTP Response"); + isNTPConnected = true; + Udp.read(packetBuffer, NTP_PACKET_SIZE); // 将数据包读取到缓冲区 + unsigned long secsSince1900; + // 将从位置40开始的四个字节转换为长整型,只取前32位整数部分 + secsSince1900 = (unsigned long)packetBuffer[40] << 24; + secsSince1900 |= (unsigned long)packetBuffer[41] << 16; + secsSince1900 |= (unsigned long)packetBuffer[42] << 8; + secsSince1900 |= (unsigned long)packetBuffer[43]; + Serial.println(secsSince1900); + Serial.println(secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR); + return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR; + } + } + Serial.println("No NTP Response :-("); //无NTP响应 + isNTPConnected = false; + return 0; //如果未得到时间则返回0 +} + +// 向给定地址的时间服务器发送NTP请求 +void sendNTPpacket(IPAddress &address) +{ + memset(packetBuffer, 0, NTP_PACKET_SIZE); + packetBuffer[0] = 0b11100011; // LI, Version, Mode + packetBuffer[1] = 0; // Stratum, or type of clock + packetBuffer[2] = 6; // Polling Interval + packetBuffer[3] = 0xEC; // Peer Clock Precision + // 8 bytes of zero for Root Delay & Root Dispersion + packetBuffer[12] = 49; + packetBuffer[13] = 0x4E; + packetBuffer[14] = 49; + packetBuffer[15] = 52; + Udp.beginPacket(address, 123); //NTP需要使用的UDP端口号为123 + Udp.write(packetBuffer, NTP_PACKET_SIZE); + Udp.endPacket(); +} diff --git a/README.md b/README.md index 610e643..6048d97 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,28 @@ -# ESP8266_Network_Clock -Use 7-Seg LED, 1602 LCD, 12864 display, LED Dot Matrix and ESP8266 NodeMCU to make a network clock -Demo: https://www.bilibili.com/video/av50338779 - -Recommend -ESP8266:2.6.1 -Arduino IDE:1.8.10 -Arduino Time Library: https://github.com/PaulStoffregen/Time -Arduino Timezone Library: https://github.com/JChristensen/Timezone -网盘:http://t.cn/AisQmbyf 提取:27mx +# Network clock and number of your followers monitor + +可以在看时间的同时看你有多少粉丝。 + +## How to use 如何使用 + +将软件刷写进已经连接好的硬件中,连接 wifi `flyAkari` ,打开网址 `192.168.4.1` ,连上一个可用的wifi即可。 + +注意:你的手机可能会比较排斥公共的wifi。 + +按流量计费的用户请注意:这个东西不会消耗很多流量。 + +## How to conncet wire 如何连线 + +| esp8266 | 转接板 | +| :-----: | :----: | +| D1 | SCL | +| D2 | SDA | + +## 啊,这么简单啊,...那你能帮帮我吗? + +![Then can you help help me? ](helpme.gif) + +## 接下来的功能 + +- [x] 实现时间 +- [x] 实现粉丝解析 +- [ ] 实现播放量的解析 \ No newline at end of file diff --git a/helpme.gif b/helpme.gif new file mode 100644 index 0000000..854dbf0 Binary files /dev/null and b/helpme.gif differ