Cov txheej txheem:

Tsis Txhob Nkim Koj Lub Sijhawm: Siv NTP!: 8 Kauj Ruam
Tsis Txhob Nkim Koj Lub Sijhawm: Siv NTP!: 8 Kauj Ruam

Video: Tsis Txhob Nkim Koj Lub Sijhawm: Siv NTP!: 8 Kauj Ruam

Video: Tsis Txhob Nkim Koj Lub Sijhawm: Siv NTP!: 8 Kauj Ruam
Video: lub sij hawm tsis tos koj,,, cia li siv koj lub suab los hu nkauj qhuas Vajtswv ntuj 2024, Kaum ib hlis
Anonim
Image
Image

Ua koj lub sijhawm suav! Nov yog cov ncauj lus sib tham nquag ntawm kuv cov neeg siv khoom, thiab yog vim li cas? Vim yog qhov yooj yim thiab qhov tsis tseem ceeb uas thaum koj ua cov ntaub ntawv teev npe, thiab rau txhua yam cuam tshuam nrog kev siv tshuab, koj xav tau lub sijhawm raug! Thiab muaj ntau txoj hauv kev los ntsuas sijhawm: moos nrog roj teeb, RTC (Lub Sijhawm Tiag), ntawm lwm tus. Tab sis qhov kuv xav qhia koj hnub no (uas qhia hnub tim, hnub ntawm lub lim tiam, thiab sijhawm) yog NTP (Network Time Protocol), uas yog online. Hauv cov vis dis aus no, peb yuav khaws cov ntaub ntawv hnub tim thiab sijhawm online, thiab qhia cov ntaub ntawv ntawm cov zaub.

Kauj Ruam 1: Qhia tawm

Los ua ke
Los ua ke

Kauj ruam 2: Sib dhos

Kauj Ruam 3: Sib dhos - Rooj

Sib dhos - Rooj
Sib dhos - Rooj

Kauj Ruam 4: Adafruit GFX Library

Adafruit GFX Lub Tsev Qiv Ntawv
Adafruit GFX Lub Tsev Qiv Ntawv

Hauv Arduino IDE, mus rau Sketch-> Xws li Tsev Kawm Ntawv-> Tswj Tsev Qiv Ntawv …

Nruab Adafruit GFX Library

Kauj Ruam 5: Adafruit ST7735 Lub Tsev Qiv Ntawv

Adafruit ST7735 Lub Tsev Qiv Ntawv
Adafruit ST7735 Lub Tsev Qiv Ntawv

Hauv Arduino IDE, mus rau Sketch-> Xws li Tsev Kawm Ntawv-> Tswj Tsev Qiv Ntawv …

Nruab Adafruit ST7735

Kauj Ruam 6: NTPClient Library

NTPClient Library
NTPClient Library

Mus rau https://github.com/taranais/NTPClient thiab rub taranais-hloov NTPClient tsev qiv ntawv.

Unzip thiab muab cov ntawv tais ceev tseg rau hauv C: / Users / Documents / Arduino / libraries

Kauj ruam 7: ESP32_NTP.ino

Suav nrog thiab txhais

#include #include // Biblioteca NTPClient modificada #include // Socket UDP #include // Display #include // Display #include // Pinos do display #define DISPLAY_DC 12 // A0 #define DISPLAY_CS 13 // CS #define DISPLAY_MOSI 14 // SDA #define DISPLAY_CLK 27 // SCK #define DISPLAY_RST 0 // Fuso Horário, tsis muaj caso horário de verão de Brasília int timeZone = -2; // Struct com os dados do dia e hora struct Date {int dayOfWeek; koj dag; hli muaj; int xyoo; int teev; feeb feeb; int vib nas this; }; // Socket UDP que a lib utiliza para recuperar dados sobre o horário WiFiUDP udp; // Objeto responsável por recuperar dados sobre horário NTPClient ntpClient (udp, // socket udp "0.br.pool.ntp.org", // URL do servwer NTP timeZone*3600, // Deslocamento do horário em relacão ao GMT 0 60000); // Intervalo entre verificações online // Nomes dos dias da semana char* dayOfWeekNames = {"Hnub Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; // Objeto responsável pelo display Adafruit_ST7735 zaub = Adafruit_ST7735 (DISPLAY_CS, DISPLAY_DC, DISPLAY_MOSI, DISPLAY_CLK, DISPLAY_RST);

Teeb

teeb tsa tsis muaj dab tsi () {Serial.begin (115200); setupDisplay (); txuasWiFi (); setupNTP (); // Cria uma nova tarefa no core 0 xTaskCreatePinnedToCore (wifiConnectionTask, // Função que será executada "wifiConnectionTask", // Nome da tarefa 10000, // Tamanho da memória disponível (em WORDs) NULL, // N vamo vamos passro nen parameter, // prioridade NULL, // Não precisamos de referência para a tarefa 0); // Tsis txhob ua core}

Teeb NTNT

tsis muaj dabtsis teeb tsa NTP () {// Inicializa o tus neeg siv NTP ntpClient.begin (); // Espera pelo primeiro hloov tshiab online Serial.println ("Tos rau thawj qhov hloov tshiab"); thaum (! ntpClient.update ()) {Serial.print ("."); ntpClient.forceUpdate (); ncua (500); } Serial.println (); Serial.println ("Thawj Qhov Hloov Tshiab Ua tiav"); }

WifiConnectionTask

// Tarefa que verifica se conexão caiu e tenta reconectarvoid wifiConnectionTask (void* param) {thaum (tseeb) {// Se WiFi tsis tau txuas nrog yog (WiFi.status ()! = WL_CONNECTED) {// Manda conectar connectWiFi (); } // Ncua de 100 zuam vTaskDelay (100); }}

ConnectWiFi

void connectWiFi () {Serial.println ("Txuas"); // Troque pelo nome e senha da sua rede WiFi WiFi.begin ("SSID", "12345678"); // Espera enquanto não estiver conectado thaum (WiFi.status ()! = WL_CONNECTED) {Serial.print ("."); ncua (500); } Serial.println (); Serial.print ("Txuas nrog"); Serial.println (WiFi. SSID ()); }

Teeb Teeb

void setupDisplay () {display.initR (INITR_BLACKTAB); // Inicializa o tso saib display.setRotation (3); // Rotaciona display.setTextSize (2); // Tamanho ua texto display.fillScreen (ST77XX_BLACK); // Preenche com a cor preta display.setCursor (0, 0); // Coloca o cursor no começo display.setTextColor (ST77XX_WHITE, ST77XX_BLACK); // Texto branco com fundo preto display.setTextWrap (tsis tseeb); // Tsis siv neeg lub tshuab hluav taws xob}

Loop

void loop () {// Recupera os dados sobre a data e horário Date date = getDate (); // Rov pib dua tus cursor display.setCursor (0, 0); // Exibe os dados no display display.printf (" %s / n / n %s / n / n %02d/ %02d/ %d / n / n %02d: %02d: %02d", WiFi. SSID ().c_str (), dayOfWeekNames [date.dayOfWeek], date.day, date.month, date.year, date.hours, date.minutes, date.seconds); ncua (100); }

GetDate

Date getDate () {// Recupera os dados de data e horário usando o client NTP char* strDate = (char*) ntpClient.getFormattedDate (). C_str (); // Passa os dados da string para a struct Hnub tim; sscanf (strDate, "%d-%d-%dT%d:%d:%dZ", & date.year, & date.month, & date.day, & date.hours, & date.minutes, & date.seconds); // Dia da semana de 0 a 6, sendo 0 o domingo date.dayOfWeek = ntpClient.getDay (); hnub rov tuaj; }

Kauj ruam 8: Cov ntaub ntawv

Rub tawm cov ntawv

INO

PDF

Pom zoo: