Sie sind auf Seite 1von 40

ROBOTC PROGRAMMING

10. WiFi CommunicationHeaders and User Defined Functions


d-r. Ramona Markoska, assist. prof.
TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

Topic of discusion in this chapter


WiFi sensor Hardware, Software Interconnection Initialization WEP and WPA network connectivity WiFi Heders and functions TCP server- send, receive Examples

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

10.0.Wifi sensor for Lego Mindstorms NXT


A Wifi sensor for the LEGO MINDSTORMS NXT. Communicate with robots, computers, and the internet. The Dexter Industries Wifi sensor for Lego Mindstorms NXT is a sensor designed to allow your NXT to connect to the internet via a local Wifi network. The sensor operates with standard 802.11 b/g/n access and can be configured to run on the vast majority of home networks. The Dexter Industries Wifi Sensor connects to the NXT via the high speed communications Port 4. The wifi sensor is powered by an external 9V battery. The Wifi Sensor comes with a full range of security options and is able to connect with WPA2-PSK, WPA, and WEP security networks.

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

10.1.WiFi Sensor- Hardware


Wifi Module: This is where wifi signals are processed and firmware is stored. Wifi Antenna: This is where the Wifi sensor sends and receives data. This area should be kept clear of metal obstructions. Power Switch: Turn this on and off to turn the sensor on and off. Power LED: This LED will light when the power is turned on. Lego Mounts: These are provided to integrate into LEGO Hardware. Battery Clip: This is where a 9V battery is connected. Battery Adapter: This is where you connect the 9V battery to power the sensor.

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

10.2.WiFi Sensor- Hardware, interconnection

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

10.3.WiFi Sensor- Start Up & Inicialization


Battery: Place a fresh 9V battery in the battery clip on the back of the
sensor, and attach the battery connector. Push the Power Switch towards the Female NXT Adapter. The bright blue LED should turn on.

Connection: Connect the sensor to Port 4 of the NXT. The sensor uses
the high-speed RS-485 line to connect with the NXT. (The sensor can only be connected to Port 4 and will not work on Ports 1-3.) Wifi networks typically come either encrypted or unencrypted. Encrypted wifi networks can be WEP *) or WPA *). In all of these examples make sure that your debug stream window is open. The window dialog between the NXT and the Wifi sensor will show up on RobotCs debug stream.

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

10.3.WiFi Sensor- Inicialization(1)


Setup the High Speed Link: Here we setup Port 4 for serial operations. We initialize the port to 9600 baud.The sensor ships with a baud rate preset of 9600, but you may want to increase the speed and then forget that you did this. The given routine scans Port4 for the proper baud rate and connects at that baud rate. Turn the Echo Off: This is the first thing we configure on the Wifi sensor. The wifi sensor defaults to echo, meaning that what you type in echos back. To turn this off, we must use the command ATE0. Software Flow Control: Next we set the software flow control. This again is a parameter we need to set to be able to communicate with the Wifi sensor. Verbose: Turn verbose on. The sensor can respond in a verbose manner, with a sentence describing the problem or response. (This is called verbose mode.) The sensor can also respond with a number code, making the communications faster.
TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

10.3.WiFi Sensor- Inicialization(2)


Infrastructure Mode: You can operate the sensor in infrastructure mode or not.
To connect directly to another wifi module (such as a phone or computer) infrastructure mode should be off. To connect to a network, such as your home wifi network, infrastructure should be on. Since were connecting to either WPA or WEP networks, we will leave our network on.

Set DHCP *) : DHCP stands for Dynamic Host Configuration Protocol. By

initiating this, you will automatically obtain an IP after connecting to a network that is connected to the internet. Disassociate: (Optional). In case our sensor has already connected to a network, we will want to make sure it is disconnected. Keep Alive: ( Optional). After connecting to a network, the wifi sensor will disconnect after around 2 minutes if no communication occurs. (In our example, we extend the keep alive time to 10 minutes.)

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

*). . Note !
Wired
is a security algorithm for IEEE 802.11 wireless networks, recognizable by the key of 10 or 26 hexadecimal digits . As a part of the original 802.11 standard , WEP is widely in use and is often the first security choice presented to users by router configuration tools.

Equivalent

Privacy (WEP)

Wi-Fi Protected Access (WPA) and Wi-Fi Protected Access II (WPA2) are two
security protocols and security certification programs developed by the Wi-Fi Alliance to secure wireless computer networks. The Alliance defined these in response to serious weaknesses researchers had found in the previous system, WEP (Wired Equivalent Privacy).

Dynamic Host Configuration Protocol (DHCP) is a network protocol that is used


to configure network devices so that they can communicate on an IP network. A DHCP client uses the DHCP protocol to acquire configuration information, such as an IP address, default route and one or more DNS server addresses from a DHCP server. The DHCP client then uses this information to configure its host. Once the configuration process is complete, the host is able to communicate on the internet

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

*). . Note !
Pre-Shared Key or PSK is a shared secret which was previously shared between the
two parties using some secure channel before it needs to be used. To build a key from shared secret, the key derivation function should be used. Such systems almost always use symmetric key cryptographic algorithms. The term PSK is used in WiFi encryption such as WEP or WPA, where both the wireless access points (AP) and all clients share the same key. The basic service set (BSS) is the basic building block of an 802.11 wireless LAN. In infrastructure mode, a single access point (AP) together with all associated stations (STAs) is called a BSS . Extended service set- (ESS) is a set of one or more interconnected BSSs and integrated local area networks that appear as a single BSS to the logical link control layer at any station associated with one of those BSSs; Service Set IDentification (SSID)- Each ESS is identified by a service set identifier (SSID); for an BSS, the SSID is chosen by the client device that starts the network, and broadcasting of the SSID is performed in a pseudo-random order by all devices that are members of the network. The maximum length of the SSID is currently 32 bytes long.

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

10.4.Wifi Sensor- connecting to a WEP


Set the WEP Key: The WEP key is like a password. We will set the key and the Wifi sensor will save it to temporary memory. Associate with the Network: WEP networks use an SSID. When you call this command, you will specify the SSID you are connecting to. The WEP key will be called on automatically behind the scenes. Like the WPA network, you may need to try multiple times to connect. Done. Again, you should see an IP number flash across the Debug Stream.

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

10.4.1.Setup for Wifi Sensor, and Associate with a WEP Network


. Description: This script will connect the DI Wifi sensor to a WEP
network. After connecting the NXT will query the network status and print it out on the debugging console. Output is in the debugging console of RobotC. ( ! turn on the debugging console !). You must replace the values of SSID and WEP_KEY for your system.

. Note that.. !
Code files (with a .c extension) are not the only files commonly seen in programs. The other type of file is called a header file, sometimes known as an include file. Header files almost always have a .h extension. The purpose of a header file is to hold declarations for other files to use.

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

. Codes customization, includes, headers,


(Wifi->WEP)
turn on the debugging console

replace the values of SSID and WEP_KEY (when use it for own purpose)

header files hold declarations for other files to use


TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

.Code -main ()- Wifi sensor connecting to a WEP

Wifi Startup function with description of activity for each defined and used function, included in header DIWIFI_connect_WEP

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

. Code- file DIWIFI-Connect_WEP.H


Wifi sensor is not a part of standard Lego mindstorm kit. According that, there is not category of functions for wifi sensors included in NXT Function library. All created and prepared wifi functions are included in especially created files , named headers, and with extenension .h. Some parts of header file DIWIFI-Connect_WEP.C, are given :

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

. Receive()- file DIWIFI-Connect_WEP.H

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

. echo_all_input_off()- file DIWIFI-Connect_WEP.H

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

. Software Flow control- file DIWIFI-Connect_WEP.H

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

. config_wifi () DIWIFI-Connect_WEP.H

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

. wifi_auth_mode()- file DIWIFI-Connect_WEP.H

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

. Set_wep_key- file DIWIFI-Connect_WEP.H

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

10.5.Wifi Sensor- connecting to a WPA network


Set the PSK: WPA networks use Pre Shared Keys *) for encryption and authentication. The wifi module will calculate this from your SSID and Network key. In our example, these are specified as character arrays SSID and wpa_psk. Calculating the key can take a few seconds. Connect to the Network: The wifi sensor now has your PSK in memory and all you need to do is direct it to connect to the network (connect to an SSID *) service set identifier ,your wifi network name). This is done in our examples as connect_to_ssid. Notice that the command we structured will attempt to connect until receives a positive connection. With many networks, you may need to try multiple times to connect. Done. You should see an IP Number flash across the Debug console. Youre now on the internet!

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

10.5.1.Setup for Wifi Sensor, and Associate with a WPA Network


. Description: This scrip will connect the DI Wifi sensor to a WPA
network. Output is in the debugging console of RobotC.Be sure to turn on the debugging console. You must replace the values of SSID and WPA_PAK

. Note that.. !
Code files (with a .c extension) are not the only files commonly seen in programs. The other type of file is called a header file, sometimes known as an include file. Header files almost always have a .h extension. The purpose of a header file is to hold declarations for other files to use.

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

.Code- Wifi sensor connecting to a WPA


turn on the debugging console

replace the values of SSID and WPA_PSK (when use it for own purpose)

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

.Code- main()- Wifi sensor connecting to a WPA

Wifi Startup function with description of activity for each defined and used function , included in header DIWIFI_connect_WP A

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

. Code- file DIWIFI-Connect_WPA.H


Wifi sensor is not a part of standard Lego mindstorm kit. According that, there is not category of functions for wifi sensors included in NXT Function library. All created and prepared wifi functions are included in especially created files , named headers, and with extenension .h. Some parts of header file DIWIFI-Connect_WPA.C, are given :

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

. scanBaudRate()- DIWIFI-Connect_WPA.H

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

. eco_all_input_off ()- DIWIFI-Connect_WPA.H

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

. Software_flow_ctrl()- DIWIFI-Connect_WPA.H

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

. setDHCP ()- DIWIFI-Connect_WPA.H

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

. Receive_connect ()- DIWIFI-Connect_WPA.H

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

10.6. WIFI Sensor: Communicating with Computers

Using Wifi Sensor you can send and receive data from a computer or other internet-enabled device. For example, you can send information or commands to a robot, or gather data from the robot. TCP connection: TCP is a two-way communication protocol that operates over the internet. The WIFI sensor can serve as both a server (handling multiple connections coming in) and a client (connecting to one server).

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

10.6.1.Sending and Receiving Information on the Server


In the TCP protocol, messages are surrounded by tags that indicate when the information starts and stops. The message starts with the tag <ESC>S<CID> where : ESC is the escape character, S is . . . well, S, and.. <CID> is the CID the TCP server is working on. A message ends with <ESC>E. For example, a message that says Hello on CID number 1 would look like this:<ESC>S0 Hello <ESC>E Incoming data into the server is also surrounded by these two tags. When we start our server, we wait for something to connect to the server we just set up. Then we parse or listen for the <ESC>S We can now connect to NXT using a terminal program such as Putty or Teraterm. Dexter Industry use Teraterm (Teraterm is free for download here http://hp.vector.co.jp/authors/VA002416/teraterm.html). Then we type in the IP the NXT got when it connected to the wifi network, and specify Port 20.
TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

10.6.2.Wifi Sensor -TCP Server Example


. Description:This program will setup a TCP server on the Dexter Industries Wifi
Server.You can connect to it with a Terminal emulator such as Telnet or Tera Term.Simply connect to the IP on Port 20 (after connecting to a wifi network)After connecting with a terminal, everything you type will be shown onthe screen of the NXT and a musical noise will play after each character.For more information on the Dexter Industries WIFI sensor.

. Note that.. ! Code files (with a .c extension) are not the only files commonly seen in programs. The other type of file is called a header file, sometimes known as an include file. Header files almost always have a .h extension. The purpose of a header file is to hold declarations for other files to us

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

Wifi Sensor -TCP Server- creation of header file


1. Prepare all functions and paste them together in new file

2. Choose them a name, and save using extension .h

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

. Code- Wifi Sensor, TCP Server Example

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

. Code-some specific functions, DIWIFI_TCP.h

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

. Code-some specific functions, DIWIFI_TCP.h

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

Discusion, Questions ?

Comparison: WiFi Bluetooth ? Multy Robot communication ? Human- Robot communication web or mobile oriented ? Other sensors from Dexter Industry ?

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

References
www.dexterindustres.com http://iel.ucdavis.edu/projects/chmindstorms/ Lego Mindstorm, Help, http://www.robotc.net/teachingmindstorms http://www.cplusplus.com/doc/tutorial/functions/ http://www.tutorialspoint.com/cplusplus/cpp_functions.htm www.robotsquare.com

TEMPUS IV Project: 158644 -JPCR Development of Regional Interdisciplinary Mechatronic Studies DRIMS

ROBOTC PROGRAMMING

Das könnte Ihnen auch gefallen