Lots of debugging and experimentation going on!

This commit is contained in:
Chris Liebman 2017-11-11 17:57:05 -08:00
parent 72434b0151
commit d7b938c970
5 changed files with 60 additions and 40 deletions

View File

@ -56,6 +56,10 @@ SoftwareSerial gps(GPS_RX_PIN, GPS_TX_PIN, false, SERIAL_BUFFER_SIZE);
char nmeaBuffer[NMEA_BUFFER_SIZE]; char nmeaBuffer[NMEA_BUFFER_SIZE];
MicroNMEA nmea(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 #ifdef NTP_PACKET_DEBUG
void dumpNTPPacket(NTPPacket* ntp) void dumpNTPPacket(NTPPacket* ntp)
{ {
@ -142,18 +146,6 @@ void oneSecondInterrupt()
#endif #endif
} }
void waitForEdge(int edge)
{
while (digitalRead(SYNC_PIN) == edge)
{
delay(0);
}
while (digitalRead(SYNC_PIN) != edge)
{
delay(0);
}
}
void getNTPTime(NTPTime *time) void getNTPTime(NTPTime *time)
{ {
time->seconds = toNTP(seconds); time->seconds = toNTP(seconds);
@ -197,6 +189,7 @@ void recievePacket(AsyncUDPPacket aup)
void recievePacket() void recievePacket()
#endif #endif
{ {
static NTPPacket ntp;
NTPTime recv_time; NTPTime recv_time;
getNTPTime(&recv_time); getNTPTime(&recv_time);
#if defined(USE_ASYNC_UDP) #if defined(USE_ASYNC_UDP)
@ -219,7 +212,6 @@ void recievePacket()
return; return;
} }
NTPPacket ntp;
#if defined(USE_ASYNC_UDP) #if defined(USE_ASYNC_UDP)
memcpy(&ntp, aup.data(), sizeof(ntp)); memcpy(&ntp, aup.data(), sizeof(ntp));
#else #else
@ -278,11 +270,6 @@ void recievePacket()
void badChecksum(MicroNMEA& mn) void badChecksum(MicroNMEA& mn)
{ {
const char* s = mn.getSentence(); 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); dbprintf("badChecksum: (length:%d 1stbyte:%02x) '%s'\n", strlen(s), s[0], s);
} }
@ -296,7 +283,7 @@ void unknownSentence(MicroNMEA& mn)
} }
sentence_unknown = true; sentence_unknown = true;
dbprintf("unknownSentence: %s\n", mn.getSentence()); dbprintf("unknownSentence: %s\n", sentence);
} }
void resetGPS() void resetGPS()
@ -346,7 +333,7 @@ void processGPS()
{ {
if (nmea.isValid() && nmea.getYear() > 2000) if (nmea.isValid() && nmea.getYear() > 2000)
{ {
struct tm tm; static struct tm tm;
tm.tm_year = nmea.getYear() - 1900; tm.tm_year = nmea.getYear() - 1900;
tm.tm_mon = nmea.getMonth() - 1; tm.tm_mon = nmea.getMonth() - 1;
tm.tm_mday = nmea.getDay(); tm.tm_mday = nmea.getDay();
@ -382,7 +369,7 @@ void processGPS()
void sendSentence(const char* sentence) void sendSentence(const char* sentence)
{ {
char cksum[3]; static char cksum[3];
MicroNMEA::generateChecksum(sentence, cksum); MicroNMEA::generateChecksum(sentence, cksum);
cksum[2] = '\0'; cksum[2] = '\0';
dbprintf("sendSentence: '%s*%s'\n", sentence, cksum); dbprintf("sendSentence: '%s*%s'\n", sentence, cksum);
@ -411,13 +398,21 @@ void setup()
micros_history_index = 0; micros_history_index = 0;
#endif #endif
#if !defined(USE_NO_WIFI)
WiFiManager wifi; WiFiManager wifi;
//wifi.setDebugOutput(false); //wifi.setDebugOutput(false);
String ssid = "SynchroClock" + String(ESP.getChipId()); String ssid = "SynchroClock" + String(ESP.getChipId());
wifi.autoConnect(ssid.c_str(), NULL); wifi.autoConnect(ssid.c_str(), NULL);
#endif
Wire.begin(); #if defined(USE_OLED_DISPLAY)
Wire.setClockStretchLimit(1500); if (!display.init())
{
dbprintln("display.init() failed!");
}
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
#endif
gps.begin(9600); gps.begin(9600);
nmea.setBadChecksumHandler(&badChecksum); nmea.setBadChecksumHandler(&badChecksum);
@ -432,7 +427,7 @@ void setup()
#if defined(USE_ASYNC_UDP) #if defined(USE_ASYNC_UDP)
while(!udp.listen(NTP_PORT)) { while(!udp.listen(NTP_PORT)) {
#else #else
while(!udp.begin(NTP_PORT)) { while(!udp.begin(NTP_PORT)) {
#endif #endif
dbprintf("setup: failed to listen on port %d! Will retry in a bit...\n", NTP_PORT); dbprintf("setup: failed to listen on port %d! Will retry in a bit...\n", NTP_PORT);
delay(1000); delay(1000);
@ -483,12 +478,25 @@ void loop()
stdev = sqrt(stdev / micros_history_count); stdev = sqrt(stdev / micros_history_count);
dbprintf("mean:%f stdev:%f ", mean, stdev); dbprintf("mean:%f stdev:%f ", mean, stdev);
#endif #endif
double disp = us2s(max(abs(MICROS_PER_SEC-max_micros), abs(MICROS_PER_SEC-min_micros))); 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");
dispersion = (uint32_t)(disp * 65536.0); 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; 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);
} }

View File

@ -20,18 +20,25 @@
* Author: liebman * Author: liebman
*/ */
//#define USE_ASYNC_UDP #define USE_ASYNC_UDP
#define USE_OLED_DISPLAY
//#define USE_NO_WIFI
#ifndef _ESPNTPServer_H_ #ifndef _ESPNTPServer_H_
#define _ESPNTPServer_H_ #define _ESPNTPServer_H_
#include "Arduino.h" #include "Arduino.h"
#include "Ticker.h" #include "Ticker.h"
#include "Wire.h" #if !defined(USE_NO_WIFI)
#include "WiFiManager.h" #include "WiFiManager.h"
#endif
#include "SoftwareSerial.h" #include "SoftwareSerial.h"
#include "MicroNMEA.h" #include "MicroNMEA.h"
#include <time.h> #include <time.h>
#if defined(USE_OLED_DISPLAY)
#include "SSD1306Wire.h"
#endif
#if defined(USE_ASYNC_UDP) #if defined(USE_ASYNC_UDP)
#include "ESPAsyncUDP.h" #include "ESPAsyncUDP.h"
#else #else
@ -53,7 +60,7 @@
#define WARMUP_SECONDS 2 #define WARMUP_SECONDS 2
#define PRECISION_COUNT 10000 #define PRECISION_COUNT 10000
#define MICROS_PER_SEC 1000000 #define MICROS_PER_SEC 1000000
#define SERIAL_BUFFER_SIZE 256 #define SERIAL_BUFFER_SIZE 128
#define NMEA_BUFFER_SIZE 128 #define NMEA_BUFFER_SIZE 128
#define VALIDITY_CHECK_MS 1100 #define VALIDITY_CHECK_MS 1100
//#define MICROS_HISTORY_SIZE 1000 //#define MICROS_HISTORY_SIZE 1000
@ -112,7 +119,7 @@ typedef struct ntp_packet
#define toNTP(t) ((uint32_t)t+SEVENTY_YEARS) #define toNTP(t) ((uint32_t)t+SEVENTY_YEARS)
// simple versions - we don't worry about side effects // simple versions - we don't worry about side effects
#define max(a, b) ((a) < (b) ? (b) : (a)) #define MAX(a, b) ((a) < (b) ? (b) : (a))
#define min(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif /* _ESPNTPServer_H_ */ #endif /* _ESPNTPServer_H_ */

View File

@ -76,7 +76,9 @@ void Logger::println(const char* message)
snprintf(_buffer, LOGGER_BUFFER_SIZE-1, "%s\n", message); snprintf(_buffer, LOGGER_BUFFER_SIZE-1, "%s\n", message);
Serial.print(_buffer); Serial.print(_buffer);
Serial.flush(); Serial.flush();
#if defined(USE_NETWORK)
send(_buffer); send(_buffer);
#endif
} }
extern int vsnprintf(char* buffer, size_t size, const char* fmt, va_list args); 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.print(_buffer);
Serial.flush(); Serial.flush();
#if defined(USE_NETWORK)
send(_buffer); send(_buffer);
#endif
} }
void Logger::flush() void Logger::flush()
@ -105,9 +109,9 @@ void Logger::flush()
#endif #endif
} }
#if defined(USE_NETWORK)
void Logger::send(const char* message) void Logger::send(const char* message)
{ {
#if defined(USE_NETWORK)
// if we are not configured for TCP then just return // if we are not configured for TCP then just return
if (_host == NULL) if (_host == NULL)
{ {
@ -153,7 +157,7 @@ void Logger::send(const char* message)
dbprintln("Logger::log failed to send packet!"); dbprintln("Logger::log failed to send packet!");
} }
#endif #endif
#endif
} }
#endif
Logger logger; Logger logger;

View File

@ -27,7 +27,7 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#define USE_TCP #define USE_TCP
#define USE_NETWORK //#define USE_NETWORK
#ifdef USE_NETWORK #ifdef USE_NETWORK
#ifdef USE_TCP #ifdef USE_TCP
@ -63,8 +63,9 @@ private:
uint16_t _port; uint16_t _port;
uint16_t _failed; uint16_t _failed;
char _buffer[LOGGER_BUFFER_SIZE]; char _buffer[LOGGER_BUFFER_SIZE];
#ifdef USE_NETWORK
void send(const char* message); void send(const char* message);
#endif
}; };
extern Logger logger; extern Logger logger;

View File

@ -1,6 +1,6 @@
# ESPNTPServer # ESPNTPServer
GPS fueled ESP8266 based NTP Server 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 GPS: Adafruit Untimate GPS
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.
Work in progress....