专注电子技术学习与研究
当前位置:单片机教程网 >> Arduino >> 浏览文章

如何使用Arduino 制作 串口(UART)转WiFi的服务器

作者:huqin   来源:本站原创   点击数:  更新时间:2014年04月02日   【字体:

1. 所需要硬件
A. Netgear WiFi Router
B. Arduino USB board
C. Arduino WiFi Shield


 

 

2. 所需要软件

下载
http://www.51hei.com/f/WiShield.zip

放到
arduino-0018\hardware\libraries\WiShield 下.

 

3. CuteDigi 串口(UART)转WiFi Sketch

(以下代码:)

#include <WiShield.h>

#define WIRELESS_MODE_INFRA   1
#define WIRELESS_MODE_ADHOC   2

// Wireless configuration parameters ----------------------------------------
byte local_ip[]    = {10,0,0,3};   // IP address of WiShield
byte gateway_ip[]  = {10,0,0,1};   // router or gateway IP address
byte subnet_mask[] = {255,255,255,0};   // subnet mask for the local network
prog_char ssid[] PROGMEM    = {"cutedigi"};      // 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_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;
//---------------------------------------------------------------------------


Server server(5000);
Client client;

unsigned long last_serial;

void setup()
{
  Serial.begin(9600);

  WiFi.begin(local_ip, gateway_ip, subnet_mask);
  server.begin();
  Serial.begin(9600);
 
}


void loop()
{


if(!client.connected()) {
     server.available(&client);
  } else {
 
   

     if (Serial.available() > 0)
     {
       client.print((char)Serial.read());
       last_serial=millis();
     }
     else
     {
       if ( millis()-last_serial > 10 )
         client.sendnow();
     }
 
     while(client.available()) {
       char c = (char)client.read();
       Serial.print(c);
    }
 
  }
 


}

测试:
我们需要下载一个串口监视器:http://www.51hei.com/f/LinkSprite-NEC-serial-v1.0.rar

TCP/IP调试器


 

 

关闭窗口

相关文章