/* Arduino --> ThingTweet via Ethernet The ThingTweet sketch is designed for the Arduino + Ethernet Shield. This sketch updates a Twitter status via the ThingTweet App (http://community.thingspeak.com/documentation/apps/thingtweet/) using HTTP POST. ThingTweet is a Twitter proxy web application that handles the OAuth. Getting Started with ThingSpeak and ThingTweet: * Sign Up for a New User Account for ThingSpeak - https://www.thingspeak.com/users/new * Link your Twitter account to the ThingTweet App - Apps / ThingTweet * Enter the ThingTweet API Key in this sketch under "ThingSpeak Settings" Arduino Requirements: * Arduino with Ethernet Shield or Arduino Ethernet * Arduino 1.0 IDE * Twitter account linked to your ThingSpeak account Network Requirements: * Ethernet port on Router * DHCP enabled on Router * Unique MAC Address for Arduino Created: October 17, 2011 by Hans Scharler (http://www.iamshadowlord.com) Updated: December 7, 2012 by Hans Scharler (http://www.iamshadowlord.com) Additional Credits: Example sketches from Arduino team, Ethernet by Adrian McEwen */ #include #include #include // Local Network Settings byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network unsigned int localPort = 8888; // local port to listen for UDP packets // ThingSpeak Settings char thingSpeakAddress[] = "api.thingspeak.com"; String thingtweetAPIKey = "21I4R9UJQZJSC60Y"; // Variable Setup long lastConnectionTime = 0; boolean lastConnected = false; int failedCounter = 0; // NTP stuff //IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov NTP server IPAddress timeServer(193,170,62,252); // ana austrian one // IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov NTP server // IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.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; // Initialize Arduino Ethernet Client EthernetClient client; // Specific variables // Status int hsopen =-1; int ethernetstatus = -1; //debug options boolean debug = true; const int ropen=5; const int rwait=6; const int rclosed=7; const int runknown=8; const int eok=A0; const int efail=A1; const int eunknown=A2; const int topen = 2; const int tclose = 3; // Status LED pins void setup() { pinMode(ropen, OUTPUT); pinMode(rwait, OUTPUT); pinMode(rclosed, OUTPUT); pinMode(runknown, OUTPUT); pinMode(eok, OUTPUT); pinMode(efail, OUTPUT); pinMode(eunknown, OUTPUT); // Taster pinMode(topen, INPUT); pinMode(tclose, INPUT); digitalWrite(topen,HIGH); digitalWrite(tclose,HIGH); // Start Serial for debugging on the Serial Monitor if(debug){ Serial.begin(9600); } // Start Ethernet on Arduino setEth(-1); startEthernet(); delay(1000); setRoom(hsopen); // Update Twitter via ThingTweet } void loop() { if((digitalRead(topen)==LOW)&&(hsopen!=1)){ setRoom(2); if(updateTwitterStatus("Hackerspace @ Freies Theater: OPENING at " + read_time())==0){ hsopen=1; } else { hsopen=-1; } delay(3000); setRoom(hsopen); } if((digitalRead(tclose)==LOW)&&(hsopen!=0)){ setRoom(2); if(updateTwitterStatus("Hackerspace @ Freies Theater: CLOSING at " + read_time())==0){ hsopen=0; } else { hsopen=-1; } delay(3000); setRoom(hsopen); } // Print Update Response to Serial Monitor if(client.available()) { char c = client.read(); if(debug){ Serial.print(c); } } // Disconnect from ThingSpeak if (!client.connected() && lastConnected) { if(debug){ Serial.println("...disconnected"); } client.stop(); } // Check if Arduino Ethernet needs to be restarted if (failedCounter > 3 ) {startEthernet();} lastConnected = client.connected(); } int updateTwitterStatus(String tsData) { if (client.connect(thingSpeakAddress, 80)) { // Create HTTP POST Data tsData = "api_key="+thingtweetAPIKey+"&status="+tsData; client.print("POST /apps/thingtweet/1/statuses/update HTTP/1.1\n"); client.print("Host: api.thingspeak.com\n"); client.print("Connection: close\n"); client.print("Content-Type: application/x-www-form-urlencoded\n"); client.print("Content-Length: "); client.print(tsData.length()); client.print("\n\n"); client.print(tsData); lastConnectionTime = millis(); if (client.connected()) { if(debug){ Serial.println("Connecting to ThingSpeak..."); Serial.println(); } failedCounter = 0; return 0; } else { failedCounter++; if(debug){ Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")"); Serial.println(); } return -1; } } else { failedCounter++; if(debug){ Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")"); Serial.println(); } lastConnectionTime = millis(); return -1; } } void startEthernet() { client.stop(); if(debug){ Serial.println("Connecting Arduino to network..."); Serial.println(); } setEth(-1); delay(1000); // Connect to network amd obtain an IP address using DHCP if (Ethernet.begin(mac) == 0) { if(debug){ Serial.println("DHCP Failed, reset Arduino to try again"); Serial.println(); } setEth(0); } else { if(debug){ Serial.println("Arduino connected to network using DHCP"); Serial.println(); } Udp.begin(localPort); setEth(1); } delay(1000); } void setEth(int statuss){ if(debug){ Serial.println("Setting eth "); Serial.println(statuss); } digitalWrite(eok,((statuss==1)?HIGH:LOW)); digitalWrite(efail,((statuss==0)?HIGH:LOW)); digitalWrite(eunknown,((statuss==-1)?HIGH:LOW)); } void setRoom(int statuss){ if(debug){ Serial.println("Setting room "); Serial.println(statuss); } digitalWrite(ropen,((statuss==1)?HIGH:LOW)); digitalWrite(rwait,((statuss==2)?HIGH:LOW)); digitalWrite(rclosed,((statuss==0)?HIGH:LOW)); digitalWrite(runknown,((statuss==-1)?HIGH:LOW)); } //NTP Functions: Read the Time from NTP Server String read_time(){ String time= ""; Serial.println("readTime"); sendNTPpacket(timeServer); // send an NTP packet to a time server // wait to see if a reply is available for(int i = 50; i>0;i--){ if(debug){ Serial.print("iteration "); Serial.println(i); } delay(50); 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; if(debug){ Serial.print("Seconds since Jan 1 1900 = " ); Serial.println(secsSince1900); // now convert NTP time into everyday time: Serial.print("Unix time = "); } // Unix time starts on Jan 1 1970. In seconds, that's 2208988800: const unsigned long seventyYears = 2208988800UL; // subtract seventy years: unsigned long epoch = secsSince1900 - seventyYears; // print Unix time: if(debug){ Serial.println(epoch); } //time +="The UTC time is "; // UTC is the time at Greenwich Meridian (GMT) time +=((epoch + 3600) % 86400L) / 3600; // print the hour (86400 equals secs per day) time +=':'; if ( ((epoch % 3600) / 60) < 10 ) { // In the first 10 minutes of each hour, we'll want a leading '0' time +="0"; } time +=((epoch % 3600) / 60); // print the minute (3600 equals secs per minute) time +=':'; if ( (epoch % 60) < 10 ) { // In the first 10 seconds of each minute, we'll want a leading '0' time +="0"; } time+=epoch %60; if(debug){Serial.println(time);} return(time); } } } // 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(); }