malhar Dhavale
Published

ESP 32 Based Cyber Clock. (My first Project)

This is a Sci fi ESP 32 based clock with features like auto time setting, volt meter, Temperature and Humidity Measurement.

IntermediateWork in progress511
ESP 32 Based Cyber Clock. (My first Project)

Things used in this project

Hardware components

SH1106 1.3' I2C OLED
×1
ESP 32 DevKit V1
×1
DHT 11
×1
CD42 Charging Module
×1
VoltMeter 0.28'
×1
18650 battery Holder
×1
18650 Cell
×1
DPDT Slide Switch
×1
M3 8mm nut bolt
×1

Software apps and online services

Robu 3D printing Service
Robu CNC laser cutting Service

Hand tools and fabrication machines

Portable 7w Soldering Iron

Story

Read more

Custom parts and enclosures

Left Support

DC Plate

Key

Front Panel

Casing

Layer 1

Plate 1

Plate 2

Plate 3

Plate 4

Right Support

Schematics

Circuit Diagram

Cyber Clock Decals premade

Cyber Clock Decals Template

Code

Cyber Clock code

C/C++
Code for the ESP 32 interfacing the OLED and the DHT 11.
#include <Wire.h> 
#include <U8g2lib.h> // Need this to make displays do things and stuff.
#include <WiFi.h>
#include "time.h"
#include "esp_sntp.h"
#include <DFRobot_DHT11.h>
DFRobot_DHT11 DHT;
U8G2_SH1106_128X64_NONAME_F_HW_I2C DISP(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);                                                                              

#define SDA 21  //SDA set to pin 21
#define SCL 22  //SCL set to pin 22
#define DHT11_PIN 2 //DHT11 set to pin 10

const char *ssid = "Wifi_ssid"; //Insert the ssid here
const char *password = "*Wifi_password";//Insert the password here

const char *ntpServer1 = "pool.ntp.org";
const char *ntpServer2 = "time.nist.gov";
const long gmtOffset_sec = (3600*5)+(30*60);
const int daylightOffset_sec = 0;

const char *time_zone = "CET-1CEST,M3.5.0,M10.5.0/3";  // TimeZone rule for Europe/Rome including daylight adjustment rules (optional)



void setup() {
  Serial.begin(115200);
 
  // First step is to configure WiFi STA and connect in order to get the current time and date.
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);

  /**
   * NTP server address could be acquired via DHCP,
   *
   * NOTE: This call should be made BEFORE esp32 acquires IP address via DHCP,
   * otherwise SNTP option 42 would be rejected by default.
   * NOTE: configTime() function call if made AFTER DHCP-client run
   * will OVERRIDE acquired NTP server address
   */
  esp_sntp_servermode_dhcp(1);  // (optional)

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
   DISP.clearBuffer();
     DISP.setFont(u8g2_font_unifont_tf );
    DISP.setCursor(2, 42);
    DISP.print("CONNECTED");
    DISP.sendBuffer();
  Serial.println(" CONNECTED");

  // set notification call-back function
  sntp_set_time_sync_notification_cb(timeavailable);

  /**
   * This will set configured ntp servers and constant TimeZone/daylightOffset
   * should be OK if your time zone does not need to adjust daylightOffset twice a year,
   * in such a case time adjustment won't be handled automagically.
   */
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);

  /**
   * A more convenient approach to handle TimeZones with daylightOffset
   * would be to specify a environment variable with TimeZone definition including daylight adjustmnet rules.
   * A list of rules for your zone could be obtained from https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TZ.h
   */
  //configTzTime(time_zone, ntpServer1, ntpServer2);
  // Setting the pins that control the displays to all outputs.
  pinMode(SDA, OUTPUT);  
  pinMode(SCL, OUTPUT);


  // Set I2C bus speed on Arduino and both displays. This may not be necessary
  // but I put it in anyway - one less thing to troubleshoot if things don't work.
  Wire.setClock(100000);
  DISP.setBusClock(100000);

  // Some devices say they're 0x78/0x7A, but others may say they're 0x3C/0x3D. It's
  // like that because the address is 7 bits but some places (u8g2 included) pad it
  // out to 8 bits. Adding a 0 on the end multiplies the value by 2.
  // Most of the small OLED displays out there seem to be set to 0x3C(0x78). Some
  // can be changed, but not all of them.
  // See https://www.arduino.cc/en/Reference/Wire for details.
  DISP.setI2CAddress(0x78);
  
  // Fire up the displays and get them ready to accept data.
                            
  DISP.begin();
  //DISP.clearBuffer(); 

}
void printLocalTime() {
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    DISP.clearBuffer();
     DISP.setFont(u8g2_font_unifont_tf );
    DISP.setCursor(1, 35);
    DISP.print("Time Unavailable");
    DISP.sendBuffer();
    Serial.println("No time available (yet)");
    return;
  }
   DISP.clearBuffer();  // Let's put some writing on DISPA. Make sure the display buffer is clear before writing or drawing anything.
    DISP.setFont(u8g2_font_logisoso20_tf ); // you can change font throughout the program, too. Check https://github.com/olikraus/u8g2/wiki for others, there are LOTS.
    DISP.setCursor(2, 22); // Move the starting position of what will be written/drawn next tox and y coordinate.
    DISP.print(&timeinfo, "%H:%M:%S");
    DISP.setFont(u8g2_font_unifont_tf );
    DISP.setCursor(2, 42);
    DISP.print(&timeinfo, "%A");
    DISP.setFont(u8g2_font_unifont_tf );
    DISP.setCursor(2, 62);
    DISP.print(&timeinfo, "%B %d");
     DISP.setFont(u8g2_font_logisoso16_tf ); 
    DISP.setCursor(75, 46); 
    DISP.print("|");
    DISP.setFont(u8g2_font_logisoso16_tf ); 
    DISP.setCursor(75,62 ); 
    DISP.print("|");
     DISP.setFont(u8g2_font_unifont_tf );
    DISP.setCursor(87,42);
    DISP.print(DHT.temperature);
     DISP.setFont(u8g2_font_unifont_tf );
    DISP.setCursor(104,42);
    DISP.print("C");
    DISP.setFont(u8g2_font_unifont_tf );
    DISP.setCursor(87, 62);
     DISP.print(DHT.humidity);
      DISP.setFont(u8g2_font_unifont_tf );
    DISP.setCursor(104, 62);
     DISP.print("%");
    DISP.sendBuffer();
  Serial.println(&timeinfo, "%A");
  Serial.println(&timeinfo, "%B %d");
  Serial.println(&timeinfo, "%H:%M:%S");
//  %B %d %H:%M:%S"); 
}

// Callback function (gets called when time adjusts via NTP)
void timeavailable(struct timeval *t) {
  Serial.println("Got time adjustment from NTP!");
  printLocalTime();
}
void loop() {

    printLocalTime();
    DHT.read(DHT11_PIN);
Serial.println(DHT.humidity);
Serial.println(DHT.temperature);
  delay(1000);
    // it will take some time to sync time :)
}

Credits

malhar Dhavale
1 project • 1 follower
Contact
Thanks to TechWithRita.

Comments

Please log in or sign up to comment.