Hello. I'm a new Arduino user and this is my first post on this forum. This post is going to be a bit more concise than it was originally going to be because I am rewriting it from scratch after losing all of my work. Apparently, Command + 2 finger swipe on Firefox on my MacBook is equivalent to pressing the back button, which is equivalent to taking nearly an hour's worth of your life and throwing it into the %#!$ing garbage... Anyway...
I'm using an Arduino UNO R2 with a Wiznet W5100-compliant ethernet shield and IDE 1.0.1.
I'm building an application (picture below) for my hackerspace that announces that we're open or closed via a tweet with a time stamp. Originally I script-kittied together two sketches that could do half of the job independently: when pressing a pushbutton, one script sends a tweet that we're opened or closed and lights up a green LED if we're open and a red LED if we're closed. The other sketch accesses the internet to get the current time, converts it to 12 hour time in my time zone and displays it in the serial monitor. Separately, both scripts work like champs. The problem comes when I try to consolidate the two together.
I verify the script (posted below) and it passes. I upload it successfully to the Arduino ( < 18kb). But then nothing happens. No error codes, nothing displayed in the serial monitor (not even the "start" print statement from the beginning of the loop() function), no tweet is sent when I press the button, no LED lights up.
What do you think? Thank you for any input you can give me.
Jeremy
Xerocraft Hackerspace
Tucson, AZ
xerocraft.org
The Sketch:
#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
// 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("not-for-you-silly");
// 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
}
void loop()
{
Serial.println("start");
btn_val = digitalRead(BUTTON); // read input value and store it fresh
// check if there was a transition
if ((btn_val == HIGH) && (btn_oldval == LOW)) {
if (shop_status == 0) { // then that means we're now OPEN
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
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);
}
btn_oldval = btn_val; // btn_val is now old so store it
}
void sendTweet(char* msg, String stringTime) { // change to boolean
Ethernet.begin(mac); // using DHCP for autoomatic IP address configuration.
Serial.begin(9600);
// start Ethernet and UDP
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
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.");
}
}
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).
if (hour24 < 0)
hour24 += 24;
if (hour24 == 0)
hour = 12;
else if (hour24 <= 12)
hour = hour24;
else
hour = hour24 - 12;
int minute = (epoch % 3600) / 60;
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 {
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 {
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 {
strTime = String(hour);
String strMin = String(minute);
strTime += (":" + strMin + "pm");
return 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)
{
// 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();
}
The Setup: