• Increase font size
  • Default font size
  • Decrease font size
  • default color
  • cyan color
  • red color

Arduino.TW樂園

Member Area
WiShield應用-利用Socket方式互相對連!
站長Blog文章單元 - 無線連接(XBee, Bluetooth, RFID, IR)
作者是 ken   
週五, 11 十二月 2009 02:40

 

使用方式:

1. 下載附件檔案之後解壓縮,請先將原先的WiShield資料夾砍掉,再將資料夾內的WiShield覆蓋原來的WiShield。

2. 打開SocketApp_Server.pde並且寫入server端,Client端也是找到資料夾內的SocketApp_Client.pde檔案並且寫入Arduino

3. 硬體必須是ATMEGA 328(因為大小都超過12K)

4. 先啟動SERVER端硬體,並等待Server建立完成(紅燈會亮起)

5. 當Client端連到時候自己本身的紅燈也會亮起來


Server端程式碼

#include <WiShield.h>

 

#define WIRELESS_MODE_INFRA 1

 

#define WIRELESS_MODE_ADHOC 2

 

// Wireless configuration parameters ----------------------------------------

 

unsigned char local_ip[] = {192,168,168,88}; // IP address of WiShield

 

unsigned char gateway_ip[] = {192,168,168,1}; // router or gateway IP address


unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
prog_char ssid[] PROGMEM = {"KEN_WiFi"};  // max 32 bytes
unsigned char security_type = 0; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"12345678"}; // max 64 characters
// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 
  0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};

// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_ADHOC;
unsigned char ssid_len;
unsigned char security_passphrase_len;
//---------------------------------------------------------------------------
//====== myServer =======
Server server(2001); //Port
boolean s = false;
Client client_a;
int a1 = 168;
void setup(){
  Serial.begin(9600);
  Serial.println("Starting WiFi..."); 
  WiFi.begin(local_ip, gateway_ip, subnet_mask);
  Serial.println("WiFi started....");
  server.begin();
  Serial.println("Server ready....");
}

void loop(){
  if(!client_a.connected()) {
     server.available(&client_a);
     Serial.println("Connected to a client.");
     client_a.println("Hello from Arduino client A!");
  } else {
    while(client_a.available()) {
     // s = true;
       char c = (char)client_a.read();
       Serial.println("===I AM SERVER====");
       Serial.println(c);
    }
  }
 //Send data
 if(s==true){
     client_a.print(255,BYTE);  //<--data
  }
}

 

Server端程式碼下載

Client 端程式碼:

#include <WiShield.h>
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
// Wireless configuration parameters ----------------------------------------
byte local_ip[]    = {192,168,168,188}; // IP address of WiShield
byte gateway_ip[]  = {192,168,168,1}; // router or gateway IP address
byte subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
prog_char ssid[] PROGMEM    = {"KEN_WiFi"};  // max 32 bytes
unsigned char security_type = 0; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"big_secret"}; // max 64 characters
// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 
  0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};

// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_ADHOC;
unsigned char ssid_len;
unsigned char security_passphrase_len;
//---------------------------------------------------------------------------
//====== myClient =======
byte server_ip[] = {192,168,168,88};  //Serve IP
Client client(server_ip, 2001);
int a = 0;
void connect_and_login()
{
  // trying to connect to issho server and login
  if(client.connect()) {
    Serial.println("Client connected.");     
    Serial.println("sending command");
    client.println("/login KEN SERVER!");
    Serial.println("ready."); 
  }else{
    Serial.println("Client could NOT connect.");
  }
}

void setup()
{
  Serial.begin(9600); 
  WiFi.begin(local_ip, gateway_ip, subnet_mask);
  connect_and_login();
}
void loop()
{
  if(!client.connected()) {
     connect_and_login();    
     Serial.println("Oh No!");
  } else { 
    Serial.println("===I AM CLIENT AND SEND MESG====");
    client.println(a);
    a++;
     //while(client.available()) {
     //   char c = (char)client.read();
     //   Serial.print(c);
     // }
     delay(500);
  }
}

Client端程式碼下載

Download:

 附件下載:ServerToClient.zip

 參考網站:

 


blog comments powered by Disqus
最近更新在 週日, 11 七月 2010 00:08