NTP and GPS for the Nixie Clock V1

Joerg
Posts: 1
Joined: Thu Feb 17, 2022 7:51 am

NTP and GPS for the Nixie Clock V1

Post by Joerg »

I replaced the PIC with an ESP32 (NodeMCU) to use NTP and GPS.

Here ist the Code:

// NTP or GPS for the Nixie Clock V1
// Replace the PIC with an ESP32
// V1.0 by Joerg Ochs

#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <TinyGPSPlus.h>
#include <HardwareSerial.h>

const char *ssid = "xxxx";
const char *password = "xxxx";

const long GPS_TimeOffset = 1;
const long utcOffsetInSeconds = GPS_TimeOffset * 3600;

const int stime = 1;
const int pwmfre = 65000;
const long interval = 1000;

const int PWM_PIN = 15; // PIC 9
const int A_PIN = 32; //PIC 18
const int B_PIN = 33; //PIC 13
const int C_PIN = 18; //PIC 6
const int D_PIN = 19; //PIC 11
const int e_PIN = 12; //PIC 17
const int z_PIN = 13; //PIC 1
const int h_PIN = 26; //PIC 3
const int t_PIN = 27; //PIC 8
const int d_PIN = 14; //PIC 2
const int RX_PIN = 16; //GPS TX
const int TX_PIN = 17; //GPS RX
//VIN PIC 14
//Gnd PIC 5
//GND GPS GND
//3V3 GPS VCC
byte tSource;
int div_seg;
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);

TinyGPSPlus gps;
HardwareSerial SerialGPS(1);

//==========================================================================
bool NTP(void){
int WLAN_flag = 1;

WiFi.begin(ssid, password);
Serial.print("starte WLAN ");
while(WiFi.status()!=WL_CONNECTED) {
Serial.print(".");
delay(500);
if (WiFi.status() == WL_CONNECT_FAILED)
{WLAN_flag = 0;
exit;}}

if (WLAN_flag == 1){
Serial.println("");
Serial.print("IP Addresse: ");
Serial.println(WiFi.localIP());
Serial.println("WLAN Verbindung steht");
timeClient.begin();
Serial.println("TimeClient Verbindung steht");
return(true);}
else{
Serial.println("");
Serial.print("WLAN Verbindung fehlgeschlagen");
return(false);}
}

bool GPS(void){
SerialGPS.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN);
delay(5000);
while (SerialGPS.available() > 0)
if (gps.encode(SerialGPS.read()))
{ if(gps.location.isValid() == 1)
return(true);
else
return(false);
}
}

int Time_Source(void){

if (NTP() == true){
Serial.println("ZeitQuelle ist NTP");
tSource = 1;
exit;}
else if (GPS() == true){
Serial.println("ZeitQuelle ist GPS");
tSource = 2;
exit;}
else{
Serial.println("keine Zeitquelle erkannt");
tSource = 0;
exit;}
}

void PWM(void){
ledcSetup(0, pwmfre, 8);
ledcAttachPin(15, 0);
ledcWrite(0, 128);
}

void ziffer(int n){

switch (n){
case 0:
{digitalWrite(A_PIN, LOW);
digitalWrite(B_PIN, LOW);
digitalWrite(C_PIN, LOW);
digitalWrite(D_PIN, LOW);
break;}

case 1:
{digitalWrite(A_PIN, HIGH);
digitalWrite(B_PIN, LOW);
digitalWrite(C_PIN, LOW);
digitalWrite(D_PIN, LOW);
break;}

case 2:
{digitalWrite(A_PIN, LOW);
digitalWrite(B_PIN, LOW);
digitalWrite(C_PIN, LOW);
digitalWrite(D_PIN, HIGH);
break;}

case 3:
{digitalWrite(A_PIN, HIGH);
digitalWrite(B_PIN, LOW);
digitalWrite(C_PIN, HIGH);
digitalWrite(D_PIN, LOW);
break;}

case 4:
{digitalWrite(A_PIN, LOW);
digitalWrite(B_PIN, LOW);
digitalWrite(C_PIN, HIGH);
digitalWrite(D_PIN, LOW);
break;}

case 5:
{digitalWrite(A_PIN, LOW);
digitalWrite(B_PIN, HIGH);
digitalWrite(C_PIN, HIGH);
digitalWrite(D_PIN, LOW);
break;}

case 6:
{digitalWrite(A_PIN, HIGH);
digitalWrite(B_PIN, LOW);
digitalWrite(C_PIN, LOW);
digitalWrite(D_PIN, HIGH);
break;}

case 7:
{digitalWrite(A_PIN, HIGH);
digitalWrite(B_PIN, HIGH);
digitalWrite(C_PIN, HIGH);
digitalWrite(D_PIN, LOW);
break;}

case 8:
{digitalWrite(A_PIN, HIGH);
digitalWrite(B_PIN, HIGH);
digitalWrite(C_PIN, LOW);
digitalWrite(D_PIN, LOW);
break;}

case 9:
{digitalWrite(A_PIN, LOW);
digitalWrite(B_PIN, HIGH);
digitalWrite(C_PIN, LOW);
digitalWrite(D_PIN, LOW);
break;}
}

delay (stime);

digitalWrite(A_PIN, LOW);
digitalWrite(B_PIN, LOW);
digitalWrite(C_PIN, LOW);
digitalWrite(D_PIN, LOW);
}

//==========================================================================

void setup() {

pinMode(PWM_PIN, OUTPUT);
pinMode(A_PIN, OUTPUT);
pinMode(B_PIN, OUTPUT);
pinMode(C_PIN, OUTPUT);
pinMode(D_PIN, OUTPUT);
pinMode(e_PIN, OUTPUT);
pinMode(z_PIN, OUTPUT);
pinMode(h_PIN, OUTPUT);
pinMode(t_PIN, OUTPUT);
pinMode(d_PIN, OUTPUT);

Serial.begin(115200);

Time_Source();
PWM();

}

//==========================================================================

void loop() {
int s_einer;
int s_zehner;
int m_einer;
int m_zehner;
int h_einer;
int h_zehner;

switch (tSource){
case 1:
timeClient.update();
s_einer = (timeClient.getSeconds()) % 10;
s_zehner = (timeClient.getSeconds() / 10) % 10;

m_einer = (timeClient.getMinutes()) % 10;
m_zehner = (timeClient.getMinutes() / 10) % 10;

h_einer = (timeClient.getHours()) % 10;
h_zehner = (timeClient.getHours() / 10) % 10;
break;

case 2:
while (SerialGPS.available() > 0)
if (gps.encode(SerialGPS.read()));

s_einer = (gps.time.second()) % 10;
s_zehner = (gps.time.second() / 10) % 10;

m_einer = (gps.time.minute()) % 10;
m_zehner = (gps.time.minute() / 10) % 10;

h_einer = (gps.time.hour()+GPS_TimeOffset) % 10;
h_zehner = (((gps.time.hour()+GPS_TimeOffset)) / 10) % 10;
break;

default:
m_einer = 8;
m_zehner = 8;

h_einer = 8;
h_zehner = 8;
break;
}


digitalWrite(e_PIN, HIGH);
ziffer(m_einer);
digitalWrite(e_PIN, LOW);
digitalWrite(z_PIN, HIGH);
ziffer(m_zehner);
digitalWrite(z_PIN, LOW);
digitalWrite(h_PIN, HIGH);
ziffer(h_einer);
digitalWrite(h_PIN, LOW);
digitalWrite(t_PIN, HIGH);
ziffer(h_zehner);
digitalWrite(t_PIN, LOW);

currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (div_seg == LOW)
div_seg = HIGH;
else
div_seg = LOW;
}

digitalWrite(d_PIN, HIGH);
ziffer(div_seg);
digitalWrite(d_PIN, LOW);
}
Attachments
IMG_0097.jpeg
IMG_0097.jpeg (122.52 KiB) Viewed 9105 times
IMG_0096.jpeg
IMG_0096.jpeg (94.61 KiB) Viewed 9105 times