|
爱科技、爱创意、爱折腾、爱极致,我们都是技术控
您需要 登录 才可以下载或查看,没有账号?立即注册
x
用arduino ide写一个esp8266的程序。从wifi获取网络时间,东8区。esp8266使用spi协议max7219,4位8*8点阵,每秒钟刷新一下当前时间。按一个按钮显示当前日期,再按一下显示时间 以下是一个基于 Arduino IDE 的 ESP8266 程序,用于获取网络时间,并将其显示在 4 个 8x8 LED 点阵上。同时,按下一个按钮可以显示当前日期,再按一次可以显示时间。 在此代码中,我们首先引用所需的库,然后设置了ESP8266的SPI接口并初始化了Max7219。 `setup()`函数设置了ESP8266的WiFi连接,并使用`getNtpTime()`函数获取当前的网络时间。`loop()`函数用于每秒钟刷新一次时间,同时检查按钮状态以切换日期和时间的显示。 `printDigits()`函数用于将整数转换为两位数并在Max7219上显示。 `setTimeZone()`函数用于设置时区偏移,然后我们使用`getNtpTime()`函数从NTP服务器获取时间,并将其转换为本地时间。 - #include <ESP8266WiFi.h>
- #include <WiFiUdp.h>
- #include <SPI.h>
- #include <LedControl.h>
- #include <Time.h> // time library header
- // Replace with your network credentials
- const char* ssid = "your_SSID";
- const char* password = "your_PASSWORD";
- // NTP Servers:
- static const char ntpServerName[] = "pool.ntp.org";
- const int timeZone = 8; // Beijing Time
- WiFiUDP Udp;
- unsigned int localPort = 8888; // local port to listen for UDP packets
- time_t getNtpTime();
- void digitalClockDisplay();
- void printDigits(int digits);
- // Initialize the MAX7219 display
- const int MAX7219_PIN_CS = 2;
- const int MAX7219_PIN_DIN = 3;
- const int MAX7219_PIN_CLK = 4;
- const int MAX7219_NUM_DEVICES = 4;
- LedControl max7219 = LedControl(MAX7219_PIN_DIN, MAX7219_PIN_CLK, MAX7219_PIN_CS, MAX7219_NUM_DEVICES);
- // Initialize the button
- const int BUTTON_PIN = 5;
- int buttonState = HIGH;
- int lastButtonState = HIGH;
- unsigned long lastDebounceTime = 0;
- unsigned long debounceDelay = 50;
- bool showDate = false;
- void setup() {
- // Initialize serial communication
- Serial.begin(115200);
- // Initialize the MAX7219 display
- max7219.shutdown(0, false);
- max7219.setIntensity(0, 8);
- max7219.clearDisplay(0);
- max7219.shutdown(1, false);
- max7219.setIntensity(1, 8);
- max7219.clearDisplay(1);
- max7219.shutdown(2, false);
- max7219.setIntensity(2, 8);
- max7219.clearDisplay(2);
- max7219.shutdown(3, false);
- max7219.setIntensity(3, 8);
- max7219.clearDisplay(3);
- // Initialize the button
- pinMode(BUTTON_PIN, INPUT);
- // Connect to Wi-Fi network
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- // Initialize the UDP client
- Udp.begin(localPort);
- // Set the time zone
- setTimeZone(timeZone);
- }
- void loop() {
- // Check if the button is pressed
- int reading = digitalRead(BUTTON_PIN);
- if (reading != lastButtonState) {
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > debounceDelay) {
- if (reading != buttonState) {
- buttonState = reading;
- if (buttonState == LOW) {
- showDate = !showDate;
- }
- }
- }
- lastButtonState = reading;
- // Get the current time from the NTP server
- time_t t = getNtpTime();
- if (t != 0) {
- // Convert the time to local time
- t += timeZone * 3600;
- // Display the date or time
- if (showDate) {
- int year = year(t);
- int month = month(t);
- int day = day(t);
- max7219.clearDisplay(0);
- max7219.clearDisplay(1);
- max7219.clearDisplay(2);
- max7219.clearDisplay(3);
- max7219.setChar(0, 0, '2');
- max7219.setChar(0, 1, '0');
- printDigits(year % 100);
- max7219.setChar(0, 4, '/');
- printDigits(month);
- max7219.setChar(0, 6, '/');
- printDigits(day);
- } else {
- int hour = hour(t);
- int minute = minute(t);
- int second = second(t);
- max7219.clearDisplay(0);
- max7219.clearDisplay(1);
- max7219.clearDisplay(2);
- max7219.clearDisplay(3);
- printDigits(hour);
- max7219.setChar(0, 2, ':');
- printDigits(minute);
- max7219.setChar(0, 5, ':');
- printDigits(second);
- }
- }
-
- // Wait for one second
- delay(1000);
- }
- void printDigits(int digits) {
- // utility function for digital clock display: prints preceding colon and leading 0
- max7219.setChar(0, 7, '0' + digits % 10);
- digits /= 10;
- max7219.setChar(0, 6, '0' + digits % 10);
- }
- void setTimeZone(int timeZone) {
- // Set the time zone offset in seconds
- TimeChangeRule myDST = {"DST", Last, Sun, Mar, 2, 60}; // Daylight Saving Time rule
- TimeChangeRule mySTD = {"STD", Last, Sun, Nov, 2, 0}; // Standard Time rule
- Timezone myTZ(myDST, mySTD);
- TimeChangeRule *tcr;
- tcr = &myDST;
- myTZ.setDST(tcr);
- tcr = &mySTD;
- myTZ.setSTD(tcr);
- myTZ.setStartWeekday(Sunday);
- myTZ.setStartWeek(1);
- myTZ.setEndWeekday(Sunday);
- myTZ.setEndWeek(1);
- myTZ.setStartMinute(0);
- myTZ.setEndMinute(0);
- TimeChangeRule dstStart = myTZ.getDSTStartRule();
- TimeChangeRule stdStart = myTZ.getSTDStartRule();
- Timezone::setCache(dstStart, stdStart);
- Timezone::setUtcOffset(timeZone * 3600);
- }
- time_t getNtpTime() {
- // Get the time from the NTP server
- while (Udp.parsePacket() > 0) ; // discard any previously received packets
- Serial.println("Transmit NTP Request");
- byte packetBuffer[48]; // NTP requests are to port 123
- memset(packetBuffer, 0, 48);
- 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
- packetBuffer[12] = 49;
- packetBuffer[13] = 0x4E;
- packetBuffer[14] = 49;
- packetBuffer[15] = 52;
- Udp.beginPacket(ntpServerName, 123); // NTP requests
- Udp.write(packetBuffer, 48); // send an NTP request
- Udp.endPacket();
-
- uint32_t startTime = millis();
- while (millis() - startTime < 1000) {
- int size = Udp.parsePacket();
- if (size >= 48) {
- Udp.read(packetBuffer, 48); // read the packet into the buffer
- uint32_t highWord = word(packetBuffer[40], packetBuffer[41]);
- uint32_t lowWord = word(packetBuffer[42], packetBuffer[43]);
- uint32_t secsSince1900 = highWord << 16 | lowWord;
- const uint32_t seventyYears = 2208988800UL;
- uint32_t epoch = secsSince1900 - seventyYears;
- Serial.print("Received NTP Response: ");
- Serial.println(epoch);
- return epoch;
- }
- }
- Serial.println("No NTP Response :-(");
- return 0; // return 0 if unable to get the time
- }
复制代码
结论:能理解语义,写的程序还算能用,需要稍微的修改一下。
|
|