TA的每日心情 | 奋斗 2016-4-18 10:19 |
---|
签到天数: 1 天 连续签到: 1 天 [LV.1]初来乍到
|
本帖最后由 zhenlinuaa 于 2016-4-22 22:27 编辑
闲来无事,用小e做了个电子表,时间从网络自动获取,运行界面如下图所示:
源代码:- #include <ESP8266WiFi.h>
- #include <time.h>
- #include <Wire.h>
- #include "SSD1306.h"
- const char* ssid = "xxx"; //wifi name
- const char* password = "xxxxx"; //wifi password
- // Initialize the oled display for address 0x3c
- //I2C-ADD=0X3C, sda-pin=2 and sdc-pin=14
- SSD1306 oled(0x3c, 2, 14);
- void setup()
- {
- Serial.begin(115200);
- Serial.setDebugOutput(true);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- Serial.println("\nConnecting to WiFi");
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print(".");
- delay(1000);
- }
- configTime(8 * 3600, 0, "pool.ntp.org", "time.nist.gov");
- Serial.println("\nWaiting for time");
- while (!time(nullptr))
- {
- Serial.print(".");
- delay(1000);
- }
- Serial.println("");
- oled.init();
- oled.flipScreenVertically();
- }
- void loop()
- {
- time_t now = time(nullptr);
- String currenttiem = ctime(&now);
- Serial.println(currenttiem);
- String hms = currenttiem.substring(11, 19);
- String date = currenttiem.substring(0, 11)+currenttiem.substring(20);
- oled.setFont(ArialMT_Plain_16);
- oled.setTextAlignment(TEXT_ALIGN_LEFT);
- oled.drawString(0, 0, date);
- oled.setFont(ArialMT_Plain_24);
- oled.setTextAlignment(TEXT_ALIGN_LEFT);
- oled.drawString(0, 30, hms);
- oled.display();
- oled.clear();
- delay(1000);
- }
复制代码 |
|