ilteris kaplan blog

Archive of blog posts since 2005

April 5, 2008

Wiki

Xport Sending ID through PHP File

#wiki

Xport Sending ID through PHP File


// Sending values to the server according to two N.O. momentary pushbuttons.

// Code is based on Tom Igoe's book.

//declare inputs 

int btn1  = 10;
int btn2  = 11;


// modes
#define disconnected  0
#define connecting    1
#define sendData      2
#define endConnection 3



byte switchState    = 0;
byte done           = 0; 
//state which helps to do a while loop inside of connection  case/switch statement
int status          = 0;
//our status while we are in the case/switch statement
byte alreadyPressed = 0;
//flag to check if we pressed the button before.
//to avoid multiple presses.
byte inByte = -1;
//our serial data holder.
byte k = 0;


void setup() {
  Serial.begin(9600);
//  Serial.println("Hello World");

  pinMode(btn1, INPUT);
  pinMode(btn2, INPUT);

}


void loop() {
  switchState = checkPressed();
  
  byte pass = switchState;
  // var that holds the information which button is pressed.
  if       (switchState == 1 && alreadyPressed == 0) {
   // Serial.println("btn1 called");
    dumpValue(pass); // we are passing which value to send to database.
    
  } 
  else if  (switchState == 2 && alreadyPressed == 0) {
   // Serial.println("btn2 called");
    dumpValue(pass);
  } 
  else {
  }
}




void dumpValue(byte var_) {
  // flags
  done           = 0;
  alreadyPressed = 1;
  
  while(done == 0) {

    switch(status) {
    case disconnected:
    //  Serial.println("I am in disconnected case");  
   
      deviceConnect();
      break;
     
    case connecting:
   //   Serial.println("I am in connecting case");
      serverConnect(); 
      break;
    case sendData:
     
      httpRequest(var_);
      break;
    case endConnection:
      // make all states to the default ones so we 
      // can call them once we need them again.
      status         = 0;
      // setting the status to disconnected to prepare next time when the 
      // button is pressed starts from the beginning.
      done           = 1;
      // done equals true meaning we completed our cycle to send information.
      alreadyPressed = 0;
      // setting the alreadyPressed to false since we can press the buttons again.
        k = 0;
     
      break;
    }
 }

}

void deviceConnect() {
  //   fill in your server's numerical address below:
 if(k==0) { // make sure not to call twice.
  Serial.println("C128.122.253.189/80"); 
  k++;
}
  status = connecting;
  
}

void serverConnect() {


   // byte inByte = Serial.read();
   // Serial.print(inByte);
   if(Serial.available() > 0) {
     inByte = Serial.read();
  
    if (inByte == 'C') {  // 'C' in ascii
      status = sendData;
    }
    else {
      // if you got anything other than a C, try again:
      status = disconnected;
    }
   }

  }



void httpRequest(byte var_) {
  Serial.print("GET /~ik501/data/die.php?var1=");
  Serial.print(var_, DEC);
  Serial.println(" HTTP/1.1");
  Serial.println("HOST: itp.nyu.edu");
  Serial.println(); 
  status = endConnection;
}



byte checkPressed () {
  byte _btn1 = digitalRead(btn1);
  byte _btn2 = digitalRead(btn2);

  if(_btn1 == 1) { 
    return 1; 
  }
  if(_btn2 == 1) { 
    return 2; 
  }
  else {
  } 
}



Continue Reading

Back to Archive