Thank you for the information about moving away from the String class. I will do that.
I did what PeterH suggested and got a little farther in the execution. Now it looks like I'm getting stuck at the "Udp.beginPacket(address, 123);" statement in sendNTPpacket().
Here's what the serial monitor says:
start
btn_val: 0
start
btn_val: 1 <<here's where i pressed the button and changed the value
shop_status: 0
getTime() begins!
hour: 0
timezone: -7
About to access timeServer: 192.43.244.18
sendNTPpacket() begins!
memset set!
adding to packetBuffer!
packetBuffer set!
Amended code:
void setup()
{
pinMode(LED_red, OUTPUT); // tell Arduino LED_red is an output
pinMode(LED_grn, OUTPUT); // tell Arduino LED_grn is an output
pinMode(BUTTON, INPUT); // tell Arduino BUTTON is an input
Serial.begin(9600);
}
void loop()
{
Serial.println("start");
delay(1000);
btn_val = digitalRead(BUTTON); // read input value and store it fresh
Serial.print("btn_val: ");
Serial.println(btn_val);
// check if there was a transition
if ((btn_val == HIGH) && (btn_oldval == LOW)) {
if (shop_status == 0) { // then that means we're now OPEN
Serial.print("shop_status: ");
Serial.println(shop_status);
getTime();
Serial.print("getTime(): ");
Serial.println(getTime());
sendTweet(msgOpen, strTime);
Serial.print("msgOpen: ");
Serial.println(msgOpen);
Serial.print("strTime: ");
Serial.println(strTime);
digitalWrite(LED_grn, HIGH); // Turn green LED on
digitalWrite(LED_red, LOW); // Turn red LED off
} else { // or else shop_status == 1, which means that we're now CLOSED
getTime();
sendTweet(msgClosed, strTime);
digitalWrite(LED_grn, LOW); // Turn green LED off
digitalWrite(LED_red, HIGH); // Turn red LED on
}
shop_status = 1 - shop_status;
delay(60000); // Change to 60000
}
btn_oldval = btn_val; // btn_val is now old so store it
}
String getTime() {
Serial.println("getTime() begins!");
int hour = 0;
int timezone = -7; // AZ timezone is UTC-7.
Serial.print("hour: ");
Serial.println(hour);
Serial.print("timezone: ");
Serial.println(timezone);
Serial.print("About to access timeServer: ");
Serial.println(timeServer);
// Udp.begin(localPort);
sendNTPpacket(timeServer); // send an NTP packet to a time server
Serial.println("Hello?");
Serial.print("timeserver accessed: ");
Serial.println(timeServer);
// wait to see if a reply is available
delay(1000);
if ( Udp.parsePacket() ) {
// We've received a packet, read the data from it
Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer
//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears;
int hour24 = ((epoch % 86400L) / 3600) + timezone; // print the hour (86400 equals secs per day).
Serial.print("hour24: ");
Serial.println(hour24);
if (hour24 < 0)
hour24 += 24;
if (hour24 == 0)
hour = 12;
else if (hour24 <= 12)
hour = hour24;
else
hour = hour24 - 12;
Serial.print("hour: ");
Serial.println(hour);
int minute = (epoch % 3600) / 60;
Serial.print("minute: ");
Serial.println(minute);
if (minute < 10) {
if (hour24 < 12) {
strTime = String(hour);
String strMin = String(minute);
strTime += (":0" + strMin + "am");
return strTime;
// Serial.println(strTime);
/*
Serial.print(hour);
Serial.print(":0");
Serial.print(minute);
Serial.print("am");
*/
} else { // else hour24 > 12
strTime = String(hour);
String strMin = String(minute);
strTime += (":0" + strMin + "pm");
return strTime;
// Serial.println(strTime);
/*
Serial.print(hour);
Serial.print(":0");
Serial.print(minute);
Serial.print("pm");
*/
}
} else { // else minute > 10
if (hour24 < 12) {
strTime = String(hour);
String strMin = String(minute);
strTime += (":" + strMin + "am");
return strTime;
// Serial.println(strTime);
/*
Serial.print(hour);
Serial.print(':');
Serial.print(minute);
Serial.print("am");
*/
} else { // else hour24 > 12
strTime = String(hour);
String strMin = String(minute);
strTime += (":" + strMin + "pm");
return strTime;
Serial.print("strTime: ");
Serial.println(strTime);
/*
Serial.print(hour);
Serial.print(':');
Serial.print(minute);
Serial.print("pm");
*/
}
}
}
}
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address)
{
Serial.println("sendNTPpacket() begins!");
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
Serial.println("memset set!");
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
Serial.println("adding to packetBuffer!");
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;
Serial.println("packetBuffer set!");
// 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
Serial.println("beginPacket set!");
Udp.write(packetBuffer,NTP_PACKET_SIZE);
Serial.println("write set!");
Serial.print("NTP_PACKET_SIZE: ");
Serial.println(NTP_PACKET_SIZE);
Udp.endPacket();
}