grimanis's blog

Just another Blogs.sch.gr site

Απλό ψηφιακό ρολόι με wemos και oled οθόνη.

Νοέ 202125

2021 12 04 18.11.04

Υλικά:

1 wemos d1 r1 mini (lolin)

1 OLED Display ssd1306 128×64 I2C

1 παλιό φορτιστή κινητού

Καλώδια και ένα κουτάκι για σασί.

Αρχείο για εκτύπωση κουτιού σε 3d printer.  box

Πρόγραμμα για το wemos

// Libraries needed:
// Time.h & TimeLib.h: https://github.com/PaulStoffregen/Time
// Timezone.h: https://github.com/JChristensen/Timezone
// SSD1306.h & SSD1306Wire.h: https://github.com/squix78/esp8266-oled-ssd1306
// NTPClient.h: https://github.com/arduino-libraries/NTPClient
// ESP8266WiFi.h & WifiUDP.h: https://github.com/ekstrand/ESP8266wifi
//

#include <ESP8266WiFi.h>
#include <WifiUDP.h>
#include <String.h>
#include <Wire.h>
//#include <SSD1306.h>
//#include <SSD1306Wire.h>
#include <NTPClient.h>
#include <Time.h>
#include <TimeLib.h>
#include <Timezone.h>
#include <TM1637Display.h>

const int CLK = D6; //Set the CLK pin connection to the display
const int DIO = D5; //Set the DIO pin connection to the display

TM1637Display display(CLK, DIO); //set up the 4-Digit Display.

// Define NTP properties
#define NTP_OFFSET 60 * 60 // In seconds
#define NTP_INTERVAL 60 * 1000 // In miliseconds
#define NTP_ADDRESS “ca.pool.ntp.org” // change this to whatever pool is closest (see ntp.org)

// Set up the NTP UDP client
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);

const char* ssid = “********”; // insert your own ssid
const char* password = “********”; // and password
String date;
String t;
const char * days[] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”} ;
const char * months[] = {“Jan”, “Feb”, “Mar”, “Apr”, “May”, “June”, “July”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”} ;
const char * ampm[] = {“AM”, “PM”} ;
int k = 0;
int timer=0;

void setup ()
{
Serial.begin(115200); // most ESP-01’s use 115200 but this could vary
timeClient.begin(); // Start the NTP UDP client

// Connect to wifi
Serial.println(“”);
Serial.print(“Connecting to “);
Serial.print(ssid);
//display.drawString(0, 10, “Connecting to Wifi…”);
// display.display();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.print(“Connected to WiFi at “);
Serial.print(WiFi.localIP());
Serial.println(“”);
// display.drawString(0, 24, “Connected.”);
// display.display();
display.setBrightness(0x0f); //set the diplay to maximum brightness
delay(1000);
}

void loop()
{
if(k==0){
if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status
{
date = “”; // clear the variables
t = “”;

// update the NTP client and get the UNIX UTC timestamp
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime();

// convert received time stamp to time_t object
time_t local, utc;
utc = epochTime;

// Then convert the UTC UNIX timestamp to local time
TimeChangeRule usEDT = {“EDT”, Second, Sun, Mar, 2, +120}; //UTC + 2 hours – change this as needed
TimeChangeRule usEST = {“EST”, First, Sun, Nov, 2, +60}; //UTC + 1 hours – change this as needed
Timezone usEastern(usEDT, usEST);
local = usEastern.toLocal(utc);

// now format the Time variables into strings with proper names for month, day etc
date += days[weekday(local)-1];
date += “, “;
date += months[month(local)-1];
date += ” “;
date += day(local);
date += “, “;
date += year(local);

// format the time to 12-hour format with AM/PM and no seconds
t += hourFormat12(local);
t += “:”;
if(minute(local) < 10) // add a zero if minute is under 10
t += “0”;
t += minute(local);
t += ” “;
t += ampm[isPM(local)];

int HH=hourFormat12(local)*100; //display on $digit 7segment
int MM=minute(local);
timer=HH+MM;

// Display the date and time
Serial.println(“”);
Serial.print(“Local date: “);
Serial.print(date);
Serial.println(“”);
Serial.print(“Local time: “);
Serial.print(t);

}
else // attempt to connect to wifi again if disconnected
{

WiFi.begin(ssid, password);

delay(1000);
}

}
k++;
if(k==9)k=0;

// Print 1234 with the center colon:
display.showNumberDecEx(timer, 0b11100000, false, 4, 0);
delay(500);
display.showNumberDecEx(timer);
delay(500);

delay(10); //Send a request to update every 10 sec (= 10,000 ms)
}

Αφήστε μια απάντηση

Η ηλ. διεύθυνση σας δεν δημοσιεύεται. Τα υποχρεωτικά πεδία σημειώνονται με *



Αλλαγή μεγέθους γραμματοσειράς
Αντίθεση
Μετάβαση σε γραμμή εργαλείων