massa
Published © MIT

Brick or Clock ?

It looks like an ordinary brick, but it's a smart clock that tells you the exact time.

BeginnerFull instructions provided1 hour121
Brick or Clock ?

Things used in this project

Hardware components

STAMPS3 
M5Stack STAMPS3 
×1
M5Stack Hall Effect Unit
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Schematics

WiFi NTP Clock

Code

WiFi NTP Clock

Arduino
#include <TM1628.h>
#include <TM16xxMatrix16.h>
#include <FastLED.h>

#include <WiFi.h>
#include "time.h"
#include "esp_sntp.h"

#define DATA_PIN G1 // data pin No. for LED1 and LED2
#define CLK_PIN G3 // clock pin No. for LED1 and LED2
#define STB_PIN G7 // strobe pin No. for LED1

#define DATA_PIN2 G1 // data pin No. for LED1 and LED2 
#define CLK_PIN2 G3 // clock pin No. for LED1 and LED2
#define STB_PIN2 G5 // strobe pin No. for LED2
#define HALL G9 // hole sensor1 pin No.
#define HALL2 G13 // hole sensor2 pin No.

#define MATRIX_NUMCOLUMNS 7
#define MATRIX_NUMROWS 10
#define INTENSITY 7

const char* ssid       = "********"; // your WiFi SSID
const char* password   = "********"; // your WiFi password

const char* ntpServer1 = "ntp.nict.jp";
const char* ntpServer2 = "time.google.com";
const char* ntpServer3 = "ntp.jst.mfeed.ad.jp";
const long  gmtOffset_sec = 9 * 3600;
const int   daylightOffset_sec = 0;

int hour = 0;
int minute = 0;
int second = 0;
bool second_out = 0;
int pre_hour = 0;
bool timeset = 0; // 

unsigned long now;
unsigned long starttime = 0;
unsigned long now2;
unsigned long starttime2 = 0;

TM1628 module(DATA_PIN, CLK_PIN, STB_PIN);
TM16xxMatrix16 matrix(&module, MATRIX_NUMCOLUMNS, MATRIX_NUMROWS);
TM1628 module2(DATA_PIN2, CLK_PIN2, STB_PIN2);
TM16xxMatrix16 matrix2(&module2, MATRIX_NUMCOLUMNS, MATRIX_NUMROWS);

uint16_t allnumber[60][10];
// 107 dot font
const uint16_t numbers[13][7] = {
  {0b00110, 0b01001, 0b01001, 0b01001, 0b01001, 0b01001, 0b00110}, // 0
  {0b00010, 0b00110, 0b00010, 0b00010, 0b00010, 0b00010, 0b00111}, // 1
  {0b00110, 0b01001, 0b00001, 0b00110, 0b01000, 0b01000, 0b01111}, // 2
  {0b00110, 0b01001, 0b00001, 0b00110, 0b00001, 0b01001, 0b00110}, // 3
  {0b00001, 0b00011, 0b00101, 0b01001, 0b01111, 0b00001, 0b00001}, // 4
  {0b01111, 0b01000, 0b01110, 0b00001, 0b00001, 0b01001, 0b00110}, // 5
  {0b00110, 0b01001, 0b01000, 0b01110, 0b01001, 0b01001, 0b00110}, // 6
  {0b01111, 0b00001, 0b00010, 0b00100, 0b00100, 0b00100, 0b00100}, // 7
  {0b00110, 0b01001, 0b01001, 0b00110, 0b01001, 0b01001, 0b00110}, // 8
  {0b00110, 0b01001, 0b01001, 0b00111, 0b00001, 0b01001, 0b00110}, // 9
  {0b00110, 0b01001, 0b01001, 0b00110, 0b00010, 0b00010, 0b00010}, // 10
  {0b00110, 0b01001, 0b01001, 0b00110, 0b00010, 0b00100, 0b00100}, // 11
  {0b00110, 0b01001, 0b01001, 0b00110, 0b00100, 0b00100, 0b00100}  // 12
};

void displayNumber(int num) { //
  matrix.setAll(false); 
  int numh = num / 10;
  int numl = num % 10;

  module.setupDisplay(true, INTENSITY); // set default intensity
  module2.setupDisplay(true, INTENSITY); // set default intensity

      for (int y1 = 0; y1 < 7; y1++) {
      allnumber[num][y1]=numbers[numh][y1] << 5 | numbers[numl][y1] ;    
      }  
  for (int y = 0; y < 7; y++) {
    for (int x = 0; x < 10; x++) {
      if (allnumber[num][y] & (1 << (9 - x))) {
        matrix.setPixel(y, x, true);
      }
    }
  }
}

void displayNumber2(int num) { 
  int numh = num / 10;
  int numl = num % 10;

  module.setupDisplay(true, INTENSITY); // set default intensity
  module2.setupDisplay(true, INTENSITY); // set default intensity

  for (int y1 = 0; y1 < 7; y1++) {
    allnumber[num][y1]=numbers[numh][y1] << 5 | numbers[numl][y1] ;    
  }  
  for (int y = 0; y < 7; y++) {
    for (int x = 0; x < 10; x++) {
      if (allnumber[num][y] & (1 << (9 - x))) {
      matrix2.setPixel(y, x, true);
      }
    }
  }
}

void displayClear2() {
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  hour = timeinfo.tm_hour;
  minute = timeinfo.tm_min;
  second = timeinfo.tm_sec;
    
  for (int y = 0; y < 7; y++) {
    for (int x = 0; x < 10; x++) {
      matrix.setPixel(y, x, false);

    if (pre_hour != hour ){
       matrix2.setPixel(y, x, false); 
      }
      delay(10);
    }
  }
}

void printLocalTime()
{
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
  hour = timeinfo.tm_hour;
  minute = timeinfo.tm_min;
  second = timeinfo.tm_sec;
 
  displayNumber2(hour);
  displayNumber(minute);  

}

void printLocalTime2()
{
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
  hour = timeinfo.tm_hour;
  minute = timeinfo.tm_min;
  second = timeinfo.tm_sec;

  matrix2.setAll(false);
  displayNumber2(minute);
  displayNumber(second);
}


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

void get_ntp()
{
    //connect to WiFi
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" CONNECTED");
  
  // init and get the time
  sntp_set_time_sync_notification_cb( timeavailable );
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2, ntpServer3);
  printLocalTime();

  //disconnect WiFi as it's no longer needed
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);

}

void setup()
{
  pinMode(HALL, INPUT); //Set the pins to which the Hall sensor is connected to the input mode.
  pinMode(HALL2, INPUT); //Set the pins to which the Hall sensor2 is connected to the input mode.
  Serial.begin(9600);
  
  get_ntp();

  Serial.println("Redy");

}

void loop()
{
  if (bool status = !digitalRead(HALL)){
    now2 = millis();
    if(now2 - starttime2 > 1000){
      printLocalTime2();
      starttime2 = now2;
      second_out = 0;
    }
  }
  else if (bool status = !digitalRead(HALL2)){
    if (second_out == 0){
      matrix.setAll(false); 
      matrix2.setAll(false); 
      printLocalTime();
      second_out = 1;
    }
    now = millis();
    if(now - starttime > 60000){
      displayClear2();
      printLocalTime();
      starttime = now;
    }
  }
  else {
    matrix.setAll(false);   
    matrix2.setAll(false);
    second_out = 0;
  }
}

Credits

massa
1 project • 0 followers

Comments