Talk:OpenDuino
From syn2cat - HackerSpace.lu
(Difference between revisions)
(fixed stalling bug) |
|||
Line 30: | Line 30: | ||
Serial.begin(9600); | Serial.begin(9600); | ||
+ | lights(); | ||
// connect once on launch | // connect once on launch | ||
dialup(); | dialup(); | ||
Line 53: | Line 54: | ||
} //*/ | } //*/ | ||
client.stop(); | client.stop(); | ||
+ | } | ||
+ | |||
+ | void lights() { | ||
+ | if( open ) { | ||
+ | digitalWrite(7,HIGH); | ||
+ | digitalWrite(5,LOW); | ||
+ | } else { | ||
+ | digitalWrite(7,LOW); | ||
+ | digitalWrite(5,HIGH); | ||
+ | } | ||
} | } | ||
Line 60: | Line 71: | ||
time = millis(); | time = millis(); | ||
+ | // read the switch state | ||
if(digitalRead(0) == 0) { | if(digitalRead(0) == 0) { | ||
− | if( time - trigger_oldtime > 1000 ) { | + | if( time - trigger_oldtime > 1000 || time - trigger_oldtime < 0 ) { |
trigger_oldtime = time; | trigger_oldtime = time; | ||
Serial.println("Changing state."); | Serial.println("Changing state."); | ||
Line 70: | Line 82: | ||
} | } | ||
− | + | // set the lights | |
− | + | lights(); | |
− | + | ||
− | + | // send regular updates | |
− | + | if( time-oldtime > 300000 || time - oldtime < 0 ) { | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | if( time-oldtime > 300000 ) { | + | |
oldtime = time; | oldtime = time; | ||
dialup(); | dialup(); |
Revision as of 13:20, 25 September 2009
temporary storage for source code of arduino part:
#include <Ethernet.h> byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; byte ip[] = { 192, 168, 1, 99 }; byte gateway[] = { 192 ,168 ,1 ,254 }; byte subnet[] = { 255, 255, 255, 0 }; boolean open = false; //byte server_ip[] = {74, 125, 127, 100}; //google byte server_ip[] = { 80, 90, 47, 170 }; //hackerspace.lu //byte server_ip[] = { 192, 168, 1, 65 }; char request[300] = "GET /OpenDuino.php?status=0 HTTP/1.0"; //char request[300] = "GET /index.html HTTP/1.0"; unsigned long time = 0; unsigned long oldtime = 0; unsigned long trigger_oldtime = 0; //Server server(1337); void setup() { Ethernet.begin(mac, ip, gateway, subnet); //server.begin(); Serial.begin(9600); lights(); // connect once on launch dialup(); oldtime = millis(); } void dialup() { Client client(server_ip,80); Serial.println("Connecting..."); Serial.println(request); if( client.connect() ){ client.println(request); client.println(); } else { Serial.println("Connection failed."); } //*/ if(client.available()) { char c = client.read(); Serial.println(c); } //*/ client.stop(); } void lights() { if( open ) { digitalWrite(7,HIGH); digitalWrite(5,LOW); } else { digitalWrite(7,LOW); digitalWrite(5,HIGH); } } void loop() { time = millis(); // read the switch state if(digitalRead(0) == 0) { if( time - trigger_oldtime > 1000 || time - trigger_oldtime < 0 ) { trigger_oldtime = time; Serial.println("Changing state."); open = !open; request[26] = (int)open+48; dialup(); } } // set the lights lights(); // send regular updates if( time-oldtime > 300000 || time - oldtime < 0 ) { oldtime = time; dialup(); } }