Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
kpower
Published © GPL3+

ESP8266 Clock Module Development Board

This development board is packed full of features enabled by multiple IC’s and hardware. A powerful package worth investigating

IntermediateFull instructions provided3 hours651
ESP8266 Clock Module Development Board

Things used in this project

Hardware components

18650 Li Battery
×1
ESP8266 Clock Module Development Board
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

ESP8266 RTC Connection

ESP OLED Connection

Code

Simple Blink Code

Arduino
Make the LED Blink
/*
  ESP8266 Clock Module Blink
  Blink the LED on the ESP8266 module

  The built in LED on the ESP8266 module is connected to GPIO2
  If wanted, connect a 220 Ohm resistor and LED between IO2 and GND
  Both will blink
*/

void setup() {
  pinMode(2, OUTPUT);     // Initialize pin 2 which is GPIO2 as an output and built in LED
  Serial.begin(115200);
}

// The loop function runs over and over again forever
void loop() {
  digitalWrite(2, LOW);   // Turn the LED off
  // low is off on the ESP8266-12
  delay(1000);                      // Wait for a second
  digitalWrite(2, HIGH);            // Turn the LED on by making the voltage HIGH
  Serial.println("Next");           // Print out via Serial to Serial Monitor (to test functionality)
  delay(2000);                      // Wait for two seconds (to distinguish between low and high)
}

OLED Demonstration

Arduino
Write a message to OLED
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
  Serial.begin(115200);
  Wire.begin(5,4);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  display.println("Hello, world!");
  display.setCursor(0, 20);
  display.println("Today is Friday");
  display.setCursor(0, 30);
  display.println("Happy Weekend");
  display.setCursor(0, 40);
  display.println("Tomorrow is Saturday");
  display.setCursor(0, 50);
  display.println("Peace");
  display.display(); 
}

void loop() {
  
}

ESP8266 Server and Access Point

Arduino
Show ESP8266 wireless capability
#include <ESP8266WiFi.h>        // Include the Wi-Fi library

#include <ESP8266WebServer.h>

const char *ssid = "ESP8266Network"; // The name of the Wi-Fi network that will be created
const char *password = "1234567890";   // The password required to connect to it, leave blank for an open network
ESP8266WebServer server(80);

void setup() {
  delay(1000);
  Serial.begin(115200);
  delay(10);
  Serial.println('\n');

  WiFi.softAP(ssid, password); // Start the access point

  Serial.print("Access Point \"");
  Serial.print(ssid);
  Serial.println("\" started");

  Serial.print("IP address:\t");
  Serial.println(WiFi.softAPIP()); // Send the IP address of the ESP8266 to the computer
  
  server.on("/", handleRoot);
  server.begin();
  Serial.println("HTTP server started");
  
}

void loop() {
  server.handleClient();  
}

void handleRoot() {
  server.send(200, "text/html", "<h1>You are connected</h1>");
}

PCF8563 Real Time Clock

Arduino
Demonstration of RTC
// Date and time functions using a PCF8563 RTC connected via I2C and Wire lib
#include "RTClib.h"

RTC_PCF8563 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {
  Serial.begin(115200);
  Wire.begin(0,2);

#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  if (! rtc.begin(&Wire)) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC is NOT initialized, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
    //
    // Note: allow 2 seconds after inserting battery or applying external power
    // without battery before calling adjust(). This gives the PCF8523's
    // crystal oscillator time to stabilize. If you call adjust() very quickly
    // after the RTC is powered, lostPower() may still return true.
  }

  // When time needs to be re-set on a previously configured device, the
  // following line sets the RTC to the date & time this sketch was compiled
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));

  // When the RTC was stopped and stays connected to the battery, it has
  // to be restarted by clearing the STOP bit. Let's do this to ensure
  // the RTC is running.
  rtc.start();
}

void loop () {
    DateTime now = rtc.now();

    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");

    // calculate a date which is 7 days, 12 hours and 30 seconds into the future
    DateTime future (now + TimeSpan(7,12,30,6));

    Serial.print(" now + 7d + 12h + 30m + 6s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();

    Serial.println();
    delay(8000);
}

Final Demonstration

Arduino
Final code to display multiple capabilities
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
#include <ESP8266WiFi.h>
#include <EasyNTPClient.h>
#include <WiFiUdp.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Edit lines 13 and 14 with name and password of specific wireless network
const char *ssid     = "XXXXXXX";  // Name of the wireless network ESP8266 will connect with
const char *password = "XXXXXXX";  // Password for wireless network
DateTime now;   //Variable to hold current time in UNIX seconds
WiFiUDP udp;

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Declaration for the RTC IC
RTC_PCF8563 rtc;
// Declaration for NTP Client
EasyNTPClient ntpClient(udp, "pool.ntp.org", -18000); // EST = -18000 seconds; Edit for desired time zone

void setup() {
  Serial.begin(115200);

  // Connect ESP8266 to wireless network of choice
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  // Begin OLED communication
  Wire.begin(5,4);  // OLED SDA = GPIO5 SCL = GPIO4
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  
  // Get current time from NTP server
  now = ntpClient.getUnixTime() + 1;  // Get current time from ntp server
  Wire.begin(0,2);  // RTC SDA = 0 SCL = 2
  rtc.begin();
  rtc.start();
  rtc.adjust(now);  // load current time into PCF8563 RTC
  WiFi.disconnect(); // Disconnect ESP8266 from wireless network; not needed anymore

}

void loop() {
  Wire.begin(5,4);  //OLED SDA = GPIO5 SCL = GPIO4

  delay(1000); // Effectively update time display every second
  display.clearDisplay();

  // Write out date and time to OLED display
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(10, 0);
  display.print(now.year());
  display.print('/');
  display.print(now.month());
  display.print('/');
  display.println(now.day());
  display.setCursor(14,30);
  display.print(now.hour());
  display.print(':');
  if (now.minute() < 10){  // Add a padding 0 if minutes in single digits
    display.print('0');
    display.print(now.minute());
  }
  else {
    display.print(now.minute());
  }
  display.print(':');
  if (now.second() < 10){  // Add a padding 0 if seconds in single digits
    display.print('0');
    display.println(now.second());
  }
  else {
    display.println(now.second());
  }
  
  display.display();

  Wire.begin(0,2);  // RTC SDA = 0 SCL = 2

  now = rtc.now();  // get latest time from PCF8563 RTC
}

Credits

kpower
19 projects • 6 followers
Qualified Electrical Engineer with experience in software and hardware development

Comments