zurielseven
Member
HARDWARE:
I've started work on a Teensy3-based project that interfaces with a W5100 Ethernet Shield by using a Teensy Arduino Shield Adapter. The crystal (DigiKey 300-1002-ND) was soldered to the Teensy, the adapter board was properly assembled, and I've soldered the Teensy+crystal to the adapter. When I stack the Ethernet board atop and run the following code, adapted from the TimeNTP example, the network lights are active, but the Teensy/adapter no longer responds to reprogramming/reboot requests nor does the COM port show up for any IDE (Arduino, UECIDE, etc) or serial terminal program. My current issues are twofold:
1) Does anyone have any ideas about best next steps to take to enable programming the Teensy while its stacked with the Ethernet shield?
2) Also, although the crystal is installed and the button battery is attached and charged, the Teensy does not seem to be keeping time when unplugged...
SOFTWARE:
Note: The Teensy/adapter without Ethernet Shield works properly for Serial time update examples.
I've started work on a Teensy3-based project that interfaces with a W5100 Ethernet Shield by using a Teensy Arduino Shield Adapter. The crystal (DigiKey 300-1002-ND) was soldered to the Teensy, the adapter board was properly assembled, and I've soldered the Teensy+crystal to the adapter. When I stack the Ethernet board atop and run the following code, adapted from the TimeNTP example, the network lights are active, but the Teensy/adapter no longer responds to reprogramming/reboot requests nor does the COM port show up for any IDE (Arduino, UECIDE, etc) or serial terminal program. My current issues are twofold:
1) Does anyone have any ideas about best next steps to take to enable programming the Teensy while its stacked with the Ethernet shield?
2) Also, although the crystal is installed and the button battery is attached and charged, the Teensy does not seem to be keeping time when unplugged...
SOFTWARE:
Note: The Teensy/adapter without Ethernet Shield works properly for Serial time update examples.
Code:
/*
* Time_NTP.pde
* Example showing time sync to NTP time source
*
* This sketch uses the Ethernet library
*/
#include <TimeLib.h>
#include <Ethernet.h>
#include <SPI.h>
int eflag=0;
int leddelay=1000;
int led=13;
byte mac[] = { 0x04, 0xE9, 0xE5, 0x02, 0x57, 0x3A };
// NTP Servers:
IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov
// Client IP address
IPAddress ip(192, 168, 0, 150);
//const int timeZone = 1; // Central European Time
const int timeZone = -5; // Eastern Standard Time (USA)
//const int timeZone = -4; // Eastern Daylight Time (USA)
//const int timeZone = -8; // Pacific Standard Time (USA)
//const int timeZone = -7; // Pacific Daylight Time (USA)
EthernetUDP Udp;
unsigned int localPort = 8888; // local port to listen for UDP packets
void setup()
{
pinMode(4, INPUT_PULLUP); // Tried this gem which was recommended somewhere else on the forum
pinMode(10, INPUT_PULLUP); // since this sketch is not (currently) making use of the SD card
Serial.begin(9600);
delay(250);
Serial.println("TimeNTP Example");
if (Ethernet.begin(mac) == 0) Ethernet.begin(mac, ip);
Serial.println("[46]: Ethernet information set.");
Serial.print("[48]: IP address assigned is ");
Serial.println(Ethernet.localIP());
Udp.begin(localPort);
Serial.println("[51]: waiting for sync");
setSyncProvider(getNtpTime);
pinMode(led, OUTPUT);
}
time_t prevDisplay = 0; // when the digital clock was displayed
void loop()
{
if (timeStatus() != timeNotSet) {
if (now() != prevDisplay) { //update the display only if time has changed
prevDisplay = now();
digitalClockDisplay();
}
}
}
void digitalClockDisplay(){
// digital clock display of the time
// modified by ZurielSeven to show
// the timestamp in MySQL DATETIME format
// this will come in handy in the cloud!!
Serial.print(year());
Serial.print("-");
if (month()<10) Serial.print("0");
Serial.print(month());
Serial.print("-");
if (day()<10) Serial.print("0");
Serial.print(day());
Serial.print(" ");
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits){
// utility for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
/*-------- NTP code ----------*/
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
time_t getNtpTime()
{
while (Udp.parsePacket() > 0) ; // discard any previously received packets
Serial.println("Transmit NTP Request");
sendNTPpacket(timeServer);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
int size = Udp.parsePacket();
if (size >= NTP_PACKET_SIZE) {
Serial.println("Receive NTP Response");
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
Serial.println("No NTP Response :-(");
return 0; // return 0 if unable to get the time
}
// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}