Ok, I messed around with the code some more and I'm able to send the "We're open!" tweet with a time stamp. But for some reason when I try to send the "We're closed!" tweet, it gets hung up in the sendNTPpacket() method at the Udp.endPacket(); statement. What do you think?
My new code:
#include <SPI.h> // needed in Arduino 0019 or later
#include <Ethernet.h>
#include <Twitter.h>
#include <EthernetUdp.h>
// The includion of EthernetDNS is not needed in Arduino IDE 1.0 or later.
// Please uncomment below in Arduino IDE 0022 or earlier.
//#include <EthernetDNS.h>
#define LED_red 5 // the pin for the red LED
#define LED_grn 6 // the pin for the green LED
#define BUTTON 7 // input pin of the pushbutton
int btn_val = 0; // stores the state of the input pin
// int btn_oldval = 0; // stores the previous value of "btn_val"
int shop_status = 0; // 0 = We're currently CLOSED
// 1 = We're currently OPEN
// int shop_oldstatus = 0; // stores the previous value of "shop_status"
// 0 = green LED off and red LED on. "We're CLOSED"
// 1 = green LED on and red LED off "We're OPEN"
// Ethernet Shield Settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// If you don't specify the IP address, DHCP is used(only in Arduino 1.0 or later).
// byte ip[] = { 192, 168, 2, 250 };
unsigned int localPort = 8888; // local port to listen for UDP packets
IPAddress timeServer(192, 43, 244, 18); // time.nist.gov NTP server
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("839253360-JuNm5OmdkZXyKRLIRMlOovOy94q1mlXut5Ya5p0o");
// Messages to post
char msgOpen[] = "We're open!";
char msgClosed[] = "We're closed!";
String strTime;
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);
// Ethernet.begin(mac);
// Udp.begin(localPort);
}
void loop()
{
Serial.println("Ready!");
delay(1000);
btn_val = digitalRead(BUTTON); // read input value and store it fresh
// check if there was a transition
if ((btn_val == HIGH)) {
Ethernet.begin(mac);
Udp.begin(localPort);
if (shop_status == 0) { // then that means we're now OPEN
Serial.print("shop_status: ");
Serial.println(shop_status);
Serial.println("");
getTime();
sendTweet(msgOpen, 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
Serial.print("shop_status: ");
Serial.println(shop_status);
Serial.println("");
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
}
}
String getTime() {
int hour = 0;
int timezone = -7; // AZ timezone is UTC-7.
sendNTPpacket(timeServer); // send an NTP packet to a time server
// 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");
*/
}
}
}
}
void sendTweet(char* msg, String stringTime) { // change to boolean
// Ethernet.begin(mac); // using DHCP for autoomatic IP address configuration.
// start Ethernet and UDP
if (Ethernet.begin(mac) == 0) { //move this
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;) // BAD!
;
}
// Udp.begin(localPort);
char msgChar[30];
String strMsg = String(msg);
strMsg += String(stringTime);
strMsg.toCharArray(msgChar,30);
Serial.println("connecting ...");
Serial.println(msgChar);
if (twitter.post(msgChar)) {
// Specify &Serial to output received response to Serial.
// If no output is required, you can just omit the argument, e.g.
// int status = twitter.wait();
int status = twitter.wait(&Serial);
if (status == 200) {
Serial.println("OK.");
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println("connection failed.");
}
}
// send an NTP request to the time server at the given address
unsigned long 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(); // Sketch hangs up here.
}