From 35438eb87fed6f46b502686f466ae4e32e7eca3d Mon Sep 17 00:00:00 2001 From: Romedius Weiss Date: Wed, 13 Feb 2013 03:59:53 +0100 Subject: [PATCH] debugging the direct version... not usable yet... --- ITS_Open_direct/ITS_Open_direct.ino | 289 ++++++++-------------------- 1 file changed, 75 insertions(+), 214 deletions(-) diff --git a/ITS_Open_direct/ITS_Open_direct.ino b/ITS_Open_direct/ITS_Open_direct.ino index 678f443..d697452 100644 --- a/ITS_Open_direct/ITS_Open_direct.ino +++ b/ITS_Open_direct/ITS_Open_direct.ino @@ -26,12 +26,12 @@ Example sketches from Arduino team, Ethernet by Adrian McEwen #include #include -#include +//#include #include "APIKey.h" -char thingSpeakAddress[] = "api.thingspeak.com"; +char ITSAddress[] = "it-syndikat.org"; // Local Network Settings byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network @@ -53,9 +53,6 @@ const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the 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; @@ -139,7 +136,11 @@ void loop() break; } case 1:{ - readStatus = readThingspeakResponse(); + readStatus = readServerReturn(); + if(debug){ + Serial.print("got it ... status: "); + Serial.println(readStatus); + } break; } case 2:{ @@ -148,116 +149,31 @@ void loop() } } - - // 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; - } -} int readButtons(){ int ret=0; if((digitalRead(topen)==LOW)&&(hsopen!=1)){ + startEthernet(); setRoom(2); - if(updateTwitterStatus("Opening" + tmsg + read_time())==0){ - hsopen=1; - ret=1; - } - else - { - hsopen=-1; - } - delay(3000); - setRoom(hsopen); - } - if((digitalRead(tclose)==LOW)&&(hsopen!=0)){ - setRoom(2); - if(updateTwitterStatus("Closing" + tmsg + read_time())==0){ - hsopen=0; - ret=1; - } - else - { - hsopen=-1; - } - delay(3000); + TriggerServerUpdate(true); + hsopen=1; + ret=1; setRoom(hsopen); } - return ret; -} - -int readThingspeakResponse(){ -// Print Update Response to Serial Monitor - if(client.available()) - { - char c = client.read(); - if(debug){ - Serial.print(c); - } + if((digitalRead(tclose)==LOW)&&(hsopen!=0)){ + startEthernet(); + setRoom(2); + TriggerServerUpdate(false); + hsopen=0; + ret=1; + setRoom(hsopen); } - - // Disconnect from ThingSpeak - if (!client.connected() && lastConnected) - { - if(debug){ - Serial.println("...disconnected"); - } - client.stop(); - return 0; - } - return 1; + return ret; } void startEthernet() @@ -285,10 +201,8 @@ void startEthernet() Serial.println("Arduino connected to network using DHCP"); Serial.println(); } - Udp.begin(localPort); setEth(1); - } - + } delay(1000); } @@ -304,7 +218,7 @@ void setEth(int statuss){ void setRoom(int statuss){ if(debug){ - Serial.println("Setting room "); + Serial.print("Setting room "); Serial.println(statuss); } digitalWrite(ropen,((statuss==1)?HIGH:LOW)); @@ -313,113 +227,41 @@ void setRoom(int statuss){ digitalWrite(runknown,((statuss==-1)?HIGH:LOW)); } -//NTP Functions: Read the Time from NTP Server -String read_time(){ - String time= ""; - if(debug){ - 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(); -} - -/** -* http://arduino.cc/en/Tutorial/TwitterClient -* -* -*/ void TriggerServerReq() { // attempt to connect, and wait a millisecond: - if(debug){Serial.println("connecting to server...");} + if(debug){Serial.println("connecting to server... Status req");} if (client.connect(serverName, 80)) { if(debug){Serial.println("making HTTP request...");} // make HTTP GET request to server: client.println("GET /status-s.php HTTP/1.1"); client.println("HOST: it-syndikat.org"); client.println(); + }else{ + if(debug){Serial.println("Not connected...");} } // note the time of this connect attempt: readStatus=2; } +void TriggerServerUpdate(boolean stat) { + // attempt to connect, and wait a millisecond: + if(debug){Serial.println("connecting to server... Update Req");} + if (client.connect(serverName, 80)) { + if(debug){Serial.println("making HTTP request...");} + // make HTTP GET request to server: + String s =(stat?"true":"false"); + client.println("GET /update.php?open=" + s + " HTTP/1.1"); + client.println("HOST: it-syndikat.org"); + client.println(); + }else{ + if(debug){Serial.println("Not connected...");} + } + // note the time of this connect attempt: + readStatus=1; +} + +//clean this mess up int readServerStatus() { if (client.connected()) { if (client.available()) { @@ -433,13 +275,7 @@ int readServerStatus() { if (inChar == '\n') { currentLine = ""; } - // if the current line ends with , it will - // be followed by the tweet: - if ( currentLine.endsWith("")) { - // tweet is beginning. Clear the tweet string: - readingTweet = true; - tweet = ""; - } + // if you're currently reading the bytes of a tweet, // add them to the tweet String: if (readingTweet) { @@ -450,22 +286,47 @@ int readServerStatus() { // if you got a "<" character, // you've reached the end of the tweet: readingTweet = false; - if(debug){Serial.println("Message:");} if(debug){Serial.println(tweet);} - if(debug){Serial.println(tweet.startsWith("Opening", 1));} - if(debug){Serial.println(tweet.startsWith("Closing", 1));} - if(tweet.startsWith("Opening", 1)){hsopen=1;} - else if(tweet.startsWith("Closing", 1)){hsopen=0;} - else {hsopen=-1;} + if(debug){Serial.println(tweet.startsWith("true", 0));} + if(debug){Serial.println(tweet.startsWith("false", 0));} + if(tweet.startsWith("true", 0)){hsopen=1;} + else {if(tweet.startsWith("false", 0)){hsopen=0;} + else {hsopen=-1;}} setRoom(hsopen); // close the connection to the server: client.stop(); return 0; } } + // if the current line ends with , it will + // be followed by the tweet: + if ( currentLine.endsWith("")) { + // tweet is beginning. Clear the tweet string: + readingTweet = true; + tweet = ""; + } } } return 2; } + +//clean this mess up +int readServerReturn() { + //if(debug){Serial.println("readServerReturn ... ");} + if (client.connected()) { + if (client.available()) { + char c = client.read(); + Serial.print(c); + return 1; + } + return 1; + }else{ + Serial.println(); + Serial.println("disconnecting."); + client.stop(); + return 0; + } +} +