data send and receive from server


hey guys, please me out. have been trying send data server in computer. using keypad enter password , send wamp server via wifi shield. password must start 4 or 5. somehow doesnt work. both php file in server oki. here sketch


#include <spi.h>
#include <wifi.h>
#include<wire.h>
#include <liquidcrystal_i2c.h>
//#include <servo.h>
#include <keypad.h>
//servo myservo;  // create servo object control servo
// initialize library numbers of interface pins
liquidcrystal_i2c lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, positive);

#define length 7


char data[length]; // 6 number of chars can hold + null char = 7


byte data_count = 0, master_count = 0;
bool pass_is_good;
char customkey;

//pachi nikalne
boolean startread = false; // reading?
int stringpos = 0; // string index counter
char instring[8]; // string incoming serial data
//int pos = 0; // servo
//int currentposition;

const byte rows = 4; //four rows
const byte cols = 4; //three columns
char keys[rows][cols] = {
  {'1','4','7','*'},
  {'2','5','8','0'},
  {'3','6','9','#'},
  {'a','b','c','d'}
  // original
  //{'1','2','3','a'},
  //{'4','5','6','b'},
  //{'7','8','9','c'},
  //{'*','0','#','d'}
};
byte rowpins[rows] = {5, 4, 3, 2};//connect row pinouts of keypad  {9, 8, 7, 6}; original
byte colpins[cols] =   {9, 8, 7, 6};//connect column pinouts of keypad {5, 4, 3, 2}; original
char key[10];
char key1[10];



keypad keypad = keypad( makekeymap(keys), rowpins, colpins, rows, cols );
char ssid[] = "our_network"; //  network ssid (name)
char pass[] = "larsson123";    // network password (use wpa, or use key wep)
//int keyindex = 0;            // network key index number (needed wep)

int status = wl_idle_status;
// if don't want use dns (and reduce sketch size)
// use numeric ip instead of name server:
ipaddress server(192,168,0,3);  // numeric ip google (no dns)
//char server[] = "localhost";    // name address google (using dns)

// initialize ethernet client library
// ip address , port of server
// want connect (port 80 default http):
wificlient client;



void setup()
{
  // myservo.attach(9);

    //initialize serial , wait port open:
      lcd.begin(16, 2);
  // print message lcd.
  lcd.clear();
serial.begin(115200);
 while (!serial)
  {
    ; // wait serial port connect. needed native usb port only
  }

  // check presence of shield:
  if (wifi.status() == wl_no_shield)
  {
    serial.println("wifi shield not present");
    // don't continue:
    while (true);
  }
  string fv = wifi.firmwareversion();
  if (fv != "1.1.0")
  {
    serial.println("please upgrade firmware");
  }
 

  // attempt connect wifi network:
  while (status != wl_connected)
 {
    serial.print("attempting connect ssid: ");
    serial.println(ssid);
    // connect wpa/wpa2 network. change line if using open or wep network:
    status = wifi.begin(ssid, pass);

    // wait 10 seconds connection:
    delay(1000);
  }
  serial.println("connected wifi");
 // printwifistatus();


  // if connection, report via serial:


}

void loop()
{

   lcd.setcursor(0,0);
  lcd.print("enter password");

  customkey = keypad.getkey();
  if (customkey) // makes sure key pressed, equal (customkey != no_key)
  {
    data[data_count] = customkey; // store char data array
    lcd.setcursor(data_count,1); // move cursor show each new char
    lcd.print(data[data_count]); // print char @ said cursor
    data_count++; // increment data array 1 store new char, keep track of number of chars entered
  }

   if(data_count == 1 && data[0]!='4')
   // if array index equal number of expected chars, compare data master
  {
    if(data[0]!='5')
    {
    lcd.clear();
    lcd.setcursor(0, 0);
    lcd.print("wrong entry");

   

    delay(1000);// added 1 second delay make sure password shown on screen before gets cleared.
    lcd.clear();
  cleardata();
    }
  }

  if(data_count == length-2 && data[0]=='4') // if array index equal number of expected chars, compare data master
  {
    lcd.clear();
    lcd.setcursor(0, 0);
    lcd.print("verifying...");
    client_borrow(data);
    client_borrow_verify();
    delay (5000);
    lcd.clear();
   
    cleardata();
   
   
  }

   if(data_count == length-1 && data[0]=='5') // if array index equal number of expected chars, compare data master
  {
    lcd.clear();
    lcd.setcursor(0, 0);
    lcd.print("password park");


   
    delay(1000);// added 1 second delay make sure password shown on screen before gets cleared.
   
   
 }
 }
 
void client_borrow(char data[])
{
 

 serial.println("\nstarting connection server...");
   if(client.connect(server,80))
    {
    serial.print(data);
    serial.println("connected...");
    client.print("get /bicycle/passwordcheckborrow.php?");
    client.print("pass=");
    client.print(data);
    client.println(" http/1.1");
    client.print("host: ");
    client.println(server);
    client.println("connection: close");
    client.println();
    client.println();
    serial.println("data sent");
    delay(1000);
    }
  else
  {
    serial.println("connection failed");
  }
   client.stop();

}
void client_borrow_verify()
{
    serial.println("\nstarting connection server disp...");
  // if connection, report via serial:
  if (client.connect(server, 80))
  {
    serial.println("connected server");
    // make http request:
    client.println("get /bicycle/passwordverifyborrow.php http/1.1");
     client.print("host: ");
    client.println(server);
    client.println("connection: close");
    client.println();
    serial.println("oki");
  }
 stringpos = 0;
  memset( &instring, 0, 32 ); //clear instring memory
while(true)
{
    if (client.available())
    {
        serial.println("verifying");
      char c = client.read();
      serial.println(c);

      if (c == '+' )
      { //'<' our begining character
        startread = true; //ready start reading part
     
       
      }else if(startread)
      {

        if(c != '|')
        { //'>' our ending character
          instring[stringpos] = c;
          stringpos ++;
        }
        else
        {
          //got need here! can disconnect now
          startread = false;
          serial.write(instring);
          lcd.clear();
          lcd.setcursor(0,0);
          lcd.print(instring);
          delay(5000);
           client.stop();
          client.flush();
         serial.println("disconnecting.");
         

         }
         }
    }
}
}

void cleardata()
{
  while(data_count !=0)
  {   // can used array size,
    data[data_count--] = 0; //clear array new data
  }
}

edit post , put code tags around code!

usually read servers answer before closing connection.


Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > data send and receive from server


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts