Fireduino/wifi

来自Firefly wiki
跳转至: 导航搜索

Fireduino -- WiFi

On the Fireduino board integrated with WiFi module,using the AP6181 but actually is Broadcom BCM43362,it is a single-band 2.4GHz supports IEEE802.11b/g/n protocol,specific parameters of SDIO interface WiFi module please refer AP6181 and BCM43362 DataSheet.

Fireduino WiFi 库

Fireduino WiFi library designed to Arduino WiFi library modeled on the design, as far as possible compatible with Arduino WiFi library; Arduino WiFi Shield sample programs to run on Fireduino the case without modification.

Fireduino WiFi API

WiFi class

WiFi Settings, and network settings

begin()

   Description
       Initialize the library and WiFi access WiFi hotspots.
Syntax WiFi.begin(ssid); WiFi.begin(ssid, pass);
Parameter ssid: Access Point pass: Key
Returns WiFi current status

disconnect()

   Description 
       Disconnect the establishment of the current network.
Syntax WiFi.disconnect();
Parameter None
Returns None

config()

   Description 
       Configure the network.
Syntax WiFi.config(ip); WiFi.config(ip, dns); WiFi.config(ip, dns, gateway); WiFi.config(ip, dns, gateway, subnet);
Parameter ip  : IP address of the device dns  : DNS server address gateway : Gateway Address subnet  : Network subnet mask
Returns None

setDNS()

   Description 
       Set the DNS server address.
Syntax WiFi.setDNS(dns_server1) WiFi.setDNS(dns_server1, dns_server2);
Parameter dns_server1: Primary DNS server address dns_server2: Secondary DNS server address
Returns None

SSID()

   Description 
       Get the current network SSID name.
Syntax WiFi.SSID(); WiFi.SSID(wifiAccessPoint);
Parameter wifiAccessPoint : Specify the access point
Returns The current network SSID string

BSSID()

   Description 
       Get access point MAC.
Syntax WiFi.BSSID(bssid);
Parameter bssid : MAC array of 6 bytes
Returns MAC array of 6 bytes

RSSI()

   Description 
       Get in touch with the current access point signal strength.
Syntax WiFi.RSSI(); WiFi.RSSI(wifiAccessPoint);
Parameter wifiAccessPoint : Specify the access point
Returns None

encryptionType()

   Description
       Gets the current type of encryption network.
Syntax WiFi.encryptionType(); WiFi.encryptionType(wifiAccessPoint);
Parameter wifiAccessPoint : Specify the access point
Returns Network encryption type

scanNetworks()

   Description 
       Scan the current access point environment.
Syntax WiFi.scanNetworks();
Parameter None
Returns The number of network discovery

status()

   Description 
       Get connecte status.
Syntax WiFi.status();
Parameter None
Returns Connection Status

macAddress()

   Description 
       Get MAC address of WiFi module.
Syntax WiFi.macAddress(mac);
Parameter mac : MAC array of 6 bytes
Returns MAC array of 6 bytes


IPAddress class

IPAddress class provides network configuration information

localIP()

   Description 
       WiFi network to obtain an IP address.
Syntax WiFi.localIP();
Parameter None
Returns IP address of the WiFi network

subnetMask()

   Description 
       Get WiFi network network subnet mask.
Syntax WiFi.subnet();
Parameter None
Returns Network subnet mask

gatewayIP()

   Description 
      Get Gateway Address WiFi network.
Syntax WiFi.gatewayIP();
Parameter None
Returns WiFi network gateway address

Server class

Service class to create a server and receives a receipt from the client and sends the data to send to the client

WiFiServer()

   Description 
       Create a service, the customer specified port monitor access terminal.
Syntax Server(port);
Parameter port : Listening port
Returns None

begin()

   Description 
       Tell Server start listening.
Syntax server.begin();
Parameter None
Returns None

available()

   Description 
       Being a client access.
Syntax server.available();
Parameter None
Returns A client object, if there is no access to the client, the client returns false objects.

write()

   Description 
       Write data to all the connected clients.
Syntax server.write(data);
Parameter data : Bytes written
Returns The number of bytes written.

print()

   Description 
       Print data to all the connected clients.
Syntax server.print(data) server.print(data, BASE);
Parameter data : Print data BASE : Print Format(BYTE、BIN、OCT、DEC、HEX)
Returns The number of bytes printed

println()

   Description 
       Print data to all connected to the client, followed by a newline.
Syntax server.println(); server.println(data); server.println(data, BASE);
Parameter data : Print data BASE : Print Format(BYTE、BIN、OCT、DEC、HEX)
Returns The number of bytes printed

Client class

Client class to create a client connected to the server, send and receive data.

Client

Client is the base class for all clients, it is not called directly, but call the client class functions are dependent on it.

WiFiClient()

   Description 
       Create a client object to connect into remote IP and port by client.connect ().
Syntax WiFiClient();
Parameter None
Returns Client object

connected()

   Description 
       The connection status of the client.
Syntax client.connected();
Parameter None
Returns Client connection status, true or false

connect()

   Description 
       Connect the remote server.
Syntax client.connect(ip, port); client.connect(URL, port);
Parameter ip  : The client will connect to the IP address URL  : The client will connect to the domain address. For example ( "arduino.cc") port  : The client connects to the remote port number
Returns Success, returns true, otherwise false

write()

   Description 
       Write data to the server.
Syntax client.write(data);
Parameter data : Bytes written
Returns The number of bytes written.

print()

   Description 
       Print data to the server.
Syntax client.print(data); client.print(data, BASE);
Parameter data : Print data BASE : Print format (BYTE, BIN, OCT, DEC, HEX)
Returns The number of bytes printed

println()

   Description 
       Print data to the server, followed by a carriage return and line feed.
Syntax client.println(); client.println(data); client.print(data, BASE);
Parameter data : Print data BASE : Print format (BYTE, BIN, OCT, DEC, HEX)
Returns The number of bytes printed

available()

   Description 
       Returns the number of bytes available to read.
Syntax client.available();
Parameter None
Returns The number of bytes that can be read

read()

   Description 
       Reads the next byte.
Syntax client.read();
Parameter None
Returns The next byte

flush()

   Description 
       Wash buffer.
Syntax client.flush();
Parameter None
Returns None

stop()

   Description 
       Disconnect the server side of the connection.
Syntax client.stop();
Parameter None
Returns None

UDP class

UDP class allows sending and receiving UDP messages.

WiFiUDP

Creating a UDP class instance, you can send a message to receive UDP

begin()

   Description
       UDP library initialization and network settings, start listening on a local port port.
Syntax WiFiUDP.begin(port);
Parameter port: listening on a local port
Returns 1 : success 0 : failure

available()

   Description 
       Get readable data buffer size.
Syntax WiFiUDP.available();
Parameter None
Returns The number of bytes available in the current package

beginPacket()

   Description 
       Start a UDP connection, ready to write data to a remote host.
Syntax WiFiUDP.beginPacket(hostName, port); WiFiUDP.beginPacket(hostIp, port);
Parameter hostName : Remote host domain name hostIp  : Remote host IP address port  : Remote host port
Returns 1 : success 0 : failure

endPacket()

   Description 
       After writing UDP data packet sent to the remote host group.
Syntax WiFiUDP.endPacket(); ;
Parameter None
Returns 1 : Success 0 : Failed

write()

   Description 
       UDP write data to a remote host, you must call it between calls beginPacket () and endPacket(), it calls endPacket () after sending.
Syntax WiFiUDP.write(byte); WiFiUDP.write(buffer, size);;
Parameter byte  : Bytes written buffer : Buffer Pointer size  : Size Length
Returns The length of the size of the write

parsePacket()

   Description 
       Start processing the next available UDP message,and report it's size
Syntax UDP.parsePacket();;
Parameter None
Returns Available UDP packet size

peek()

   Description 
       Peeks data.
Syntax WiFiUDP.peek();
Parameter None
Returns Peeks data, -1 represents no data to see

read()

   Description 
      Reads data from the specified buffer after WiFi UDP.parsePacket () and call when Successful .
Syntax WiFiUDP.read(); WiFiUDP.read(buffer, len);
Parameter buffer : Pointer to buffer data read len: The maximum length
Returns Read data Read data length

flush()

   Description 
       Wash buffer.
Syntax WiFiUDP.flush();
Parameter None
Returns None

stop()

   Description 
       Disconnect and release resources.
Syntax WiFiUDP.stop();
Parameter None
Returns None

remoteIP()

   Description 
       Get the remote host IP address (). After a successful call in WiFi UDP.parsePacket.
Syntax WiFiUDP.remoteIP();
Parameter None
Returns Remote host IP address

remotePort()

   Description 
       Get the remote host port number, WiFi UDP.parsePacket (). After a successful call.
Syntax WiFiUDP.remotePort();
Parameter None
Returns Remote host port number

Firedino WiFi Example

#include <WiFi.h>
char ssid[] = "yourNetwork"; //  your network SSID (name)
char pass[] = "secretPassword";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(23);
boolean alreadyConnected = false; // whether or not the client was connected previously
 
void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // check for the presence of the 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 the firmware");
  }
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  // start the server:
  server.begin();
  // you're connected now, so print out the status:
  printWifiStatus();
}
 
void loop() {
  // wait for a new client:
  WiFiClient client = server.available();
  // when the client sends the first byte, say hello:
  if (client) {
    if (!alreadyConnected) {
      // clead out the input buffer:
      client.flush();
      Serial.println("We have a new client");
      client.println("Hello, client!");
      alreadyConnected = true;
    }
    if (client.available() > 0) {
      // read the bytes incoming from the client:
      char thisChar = client.read();
      // echo the bytes back to the client:
      server.write(thisChar);
      // echo the bytes to the server as well:
      Serial.write(thisChar);
    }
  }
}
 
void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}