From d7b938c970627bb6b3ca04290e691e87858cb388 Mon Sep 17 00:00:00 2001 From: Chris Liebman Date: Sat, 11 Nov 2017 17:57:05 -0800 Subject: [PATCH] Lots of debugging and experimentation going on! --- ESPNTPServer.cpp | 66 +++++++++++++++++++++++++++--------------------- ESPNTPServer.h | 17 +++++++++---- Logger.cpp | 8 ++++-- Logger.h | 5 ++-- README.md | 4 +-- 5 files changed, 60 insertions(+), 40 deletions(-) diff --git a/ESPNTPServer.cpp b/ESPNTPServer.cpp index 9709f41..3743781 100644 --- a/ESPNTPServer.cpp +++ b/ESPNTPServer.cpp @@ -56,6 +56,10 @@ SoftwareSerial gps(GPS_RX_PIN, GPS_TX_PIN, false, SERIAL_BUFFER_SIZE); char nmeaBuffer[NMEA_BUFFER_SIZE]; MicroNMEA nmea(nmeaBuffer, NMEA_BUFFER_SIZE); +#if defined(USE_OLED_DISPLAY) +SSD1306Wire display(0x3c, SDA, SCL); +#endif + #ifdef NTP_PACKET_DEBUG void dumpNTPPacket(NTPPacket* ntp) { @@ -142,18 +146,6 @@ void oneSecondInterrupt() #endif } -void waitForEdge(int edge) -{ - while (digitalRead(SYNC_PIN) == edge) - { - delay(0); - } - while (digitalRead(SYNC_PIN) != edge) - { - delay(0); - } -} - void getNTPTime(NTPTime *time) { time->seconds = toNTP(seconds); @@ -197,6 +189,7 @@ void recievePacket(AsyncUDPPacket aup) void recievePacket() #endif { + static NTPPacket ntp; NTPTime recv_time; getNTPTime(&recv_time); #if defined(USE_ASYNC_UDP) @@ -219,7 +212,6 @@ void recievePacket() return; } - NTPPacket ntp; #if defined(USE_ASYNC_UDP) memcpy(&ntp, aup.data(), sizeof(ntp)); #else @@ -278,11 +270,6 @@ void recievePacket() void badChecksum(MicroNMEA& mn) { const char* s = mn.getSentence(); - if (s == NULL || s[0] == '\0') - { - //dbprintf("badChecksum: NULL sentence!!!!"); - return; - } dbprintf("badChecksum: (length:%d 1stbyte:%02x) '%s'\n", strlen(s), s[0], s); } @@ -296,7 +283,7 @@ void unknownSentence(MicroNMEA& mn) } sentence_unknown = true; - dbprintf("unknownSentence: %s\n", mn.getSentence()); + dbprintf("unknownSentence: %s\n", sentence); } void resetGPS() @@ -346,7 +333,7 @@ void processGPS() { if (nmea.isValid() && nmea.getYear() > 2000) { - struct tm tm; + static struct tm tm; tm.tm_year = nmea.getYear() - 1900; tm.tm_mon = nmea.getMonth() - 1; tm.tm_mday = nmea.getDay(); @@ -382,7 +369,7 @@ void processGPS() void sendSentence(const char* sentence) { - char cksum[3]; + static char cksum[3]; MicroNMEA::generateChecksum(sentence, cksum); cksum[2] = '\0'; dbprintf("sendSentence: '%s*%s'\n", sentence, cksum); @@ -411,13 +398,21 @@ void setup() micros_history_index = 0; #endif +#if !defined(USE_NO_WIFI) WiFiManager wifi; //wifi.setDebugOutput(false); String ssid = "SynchroClock" + String(ESP.getChipId()); wifi.autoConnect(ssid.c_str(), NULL); +#endif - Wire.begin(); - Wire.setClockStretchLimit(1500); +#if defined(USE_OLED_DISPLAY) + if (!display.init()) + { + dbprintln("display.init() failed!"); + } + display.flipScreenVertically(); + display.setFont(ArialMT_Plain_10); +#endif gps.begin(9600); nmea.setBadChecksumHandler(&badChecksum); @@ -432,7 +427,7 @@ void setup() #if defined(USE_ASYNC_UDP) while(!udp.listen(NTP_PORT)) { #else - while(!udp.begin(NTP_PORT)) { + while(!udp.begin(NTP_PORT)) { #endif dbprintf("setup: failed to listen on port %d! Will retry in a bit...\n", NTP_PORT); delay(1000); @@ -483,12 +478,25 @@ void loop() stdev = sqrt(stdev / micros_history_count); dbprintf("mean:%f stdev:%f ", mean, stdev); #endif - double disp = us2s(max(abs(MICROS_PER_SEC-max_micros), abs(MICROS_PER_SEC-min_micros))); - dbprintf("min:%f max:%f jitter:%f dispersion:%f valid_count:%lu valid:%s\n", - us2s(min_micros), us2s(max_micros), us2s(max_micros-min_micros), disp, - valid_count, valid?"true":"false"); + double disp = us2s(MAX(abs(MICROS_PER_SEC-max_micros), abs(MICROS_PER_SEC-min_micros))); dispersion = (uint32_t)(disp * 65536.0); + dbprintf("min:%lu max:%lu jitter:%lu valid_count:%lu valid:%s\n", + min_micros, max_micros, max_micros-min_micros, + valid_count, valid?"true":"false"); } last_seconds = seconds; - //delay(1); + + // + // Update the display + // + display.clear(); + display.setTextAlignment(TEXT_ALIGN_LEFT); + display.setFont(ArialMT_Plain_10); + const char* s = ctime(&last_seconds); + display.drawString(0, 0, WiFi.localIP().toString()); + display.drawString(0, 10, s); + // write the buffer to the display + display.display(); + + delay(1); } diff --git a/ESPNTPServer.h b/ESPNTPServer.h index a8e16b1..5017554 100644 --- a/ESPNTPServer.h +++ b/ESPNTPServer.h @@ -20,18 +20,25 @@ * Author: liebman */ -//#define USE_ASYNC_UDP +#define USE_ASYNC_UDP +#define USE_OLED_DISPLAY +//#define USE_NO_WIFI #ifndef _ESPNTPServer_H_ #define _ESPNTPServer_H_ #include "Arduino.h" #include "Ticker.h" -#include "Wire.h" +#if !defined(USE_NO_WIFI) #include "WiFiManager.h" +#endif #include "SoftwareSerial.h" #include "MicroNMEA.h" #include +#if defined(USE_OLED_DISPLAY) +#include "SSD1306Wire.h" +#endif + #if defined(USE_ASYNC_UDP) #include "ESPAsyncUDP.h" #else @@ -53,7 +60,7 @@ #define WARMUP_SECONDS 2 #define PRECISION_COUNT 10000 #define MICROS_PER_SEC 1000000 -#define SERIAL_BUFFER_SIZE 256 +#define SERIAL_BUFFER_SIZE 128 #define NMEA_BUFFER_SIZE 128 #define VALIDITY_CHECK_MS 1100 //#define MICROS_HISTORY_SIZE 1000 @@ -112,7 +119,7 @@ typedef struct ntp_packet #define toNTP(t) ((uint32_t)t+SEVENTY_YEARS) // simple versions - we don't worry about side effects -#define max(a, b) ((a) < (b) ? (b) : (a)) -#define min(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) < (b) ? (b) : (a)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) #endif /* _ESPNTPServer_H_ */ diff --git a/Logger.cpp b/Logger.cpp index 41cabd0..32dabfc 100644 --- a/Logger.cpp +++ b/Logger.cpp @@ -76,7 +76,9 @@ void Logger::println(const char* message) snprintf(_buffer, LOGGER_BUFFER_SIZE-1, "%s\n", message); Serial.print(_buffer); Serial.flush(); +#if defined(USE_NETWORK) send(_buffer); +#endif } extern int vsnprintf(char* buffer, size_t size, const char* fmt, va_list args); @@ -91,7 +93,9 @@ void Logger::printf(const char* fmt, ...) Serial.print(_buffer); Serial.flush(); +#if defined(USE_NETWORK) send(_buffer); +#endif } void Logger::flush() @@ -105,9 +109,9 @@ void Logger::flush() #endif } +#if defined(USE_NETWORK) void Logger::send(const char* message) { -#if defined(USE_NETWORK) // if we are not configured for TCP then just return if (_host == NULL) { @@ -153,7 +157,7 @@ void Logger::send(const char* message) dbprintln("Logger::log failed to send packet!"); } #endif -#endif } +#endif Logger logger; diff --git a/Logger.h b/Logger.h index 1d42e7b..708ecf0 100644 --- a/Logger.h +++ b/Logger.h @@ -27,7 +27,7 @@ #include #define USE_TCP -#define USE_NETWORK +//#define USE_NETWORK #ifdef USE_NETWORK #ifdef USE_TCP @@ -63,8 +63,9 @@ private: uint16_t _port; uint16_t _failed; char _buffer[LOGGER_BUFFER_SIZE]; - +#ifdef USE_NETWORK void send(const char* message); +#endif }; extern Logger logger; diff --git a/README.md b/README.md index 0d04cd6..b3cd67b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # ESPNTPServer GPS fueled ESP8266 based NTP Server -Don't have a GPS unit yet so I'm using a DS3231 as a PPS source to workout -some of the software. I'm actually using one of my [AnalogClock](https://github.com/liebman/AnalogClock) boards as it has the DS3231 1hz routed to an ESP8266 GPIO pin. +GPS: Adafruit Untimate GPS +Work in progress....