oled显示网络时间, wifi链接网络
#include "time.h"
String week[8] = {
"Sun", "Mon", "Tues", "Wednes", "Thur", "Fri", "Sat"
};
void printLocalTime(Adafruit_SSD1306 &display) {
struct tm timeinfo;
if(!getLocalTime( & timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %Y-%m-%d %H:%M:%S");
char display_str[30] = {0};
sprintf(display_str,"%04d-%02d-%02d %02d:%02d:%02d %01d",
timeinfo.tm_year+1900,
timeinfo.tm_mon+1,
timeinfo.tm_mday,
timeinfo.tm_hour,
timeinfo.tm_min,
timeinfo.tm_sec, timeinfo.tm_wday);
display.clearDisplay();
display.setCursor(0, 0);
display.println(display_str);
display.display();
}
const char * ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 28800;
const int daylightOffset_sec = 0;
void ntp_get_date_init() {
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
}
#include <Arduino.h>
#include <WiFi.h>
#include <Adafruit_SSD1306.h>
#include "ntp_get_date.h"
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire);
const char* id="Wifi账号";
const char* psw="wifi密码";
void setup() {
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
display.display();
delay(500);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setRotation(0);
display.clearDisplay();
WiFi.begin(id,psw);
while(WiFi.status()!=WL_CONNECTED){
delay(500);
Serial.println("connection...");
}
display.setCursor(0, 0);
display.println("wifi connect to Wifi007 success");
display.display();
Serial.println("wifi connect to Wifi007 success");
ntp_get_date_init();
}
void loop() {
printLocalTime(display);
delay(500);
}