1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

Hacking Roomba - Tod E.Kurt Part 9 pps

30 286 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 30
Dung lượng 571,96 KB

Nội dung

222 Part III — More Complex Interfacing Lantronix XPort Lantronix was one of the first companies to produce an embedded device server for serial devices, thus enabling those devices to be on the Internet. Their Cobox Micro was a module very similar in look to the SitePlayer, but included an Ethernet jack. The XPort is a miniatur- ization of the Micro. The entire device server fits inside of a slightly elongated Ethernet jack. Figure 11-14 shows what an XPort looks like not connected to anything. It is tiny. It’s hard to believe there’s really a computer in there. F IGURE 11-14: Lantronix XPort While the SitePlayer is very much aimed toward the hobbyist and includes such hacker-friendly things as standard breadboard pin spacing and 5V tolerant inputs, the XPort is aimed at the professional device integrator. It has 3.3V inputs and a high-density spacing. These two factors make it a little harder for the typical hacker to use. The XPort evaluation board, shown in Figure 11-15, converts it to a more hacker-friendly format. It still requires 5VDC power, so a small power supply to use the Roomba’s battery is needed. Notice how much more complex it seems compared to the SitePlayer Telnet System box. This is partially because it is an evalua- tion board and so has extra parts to let engineers properly evaluate the device, but also because integrating the XPort just takes more infrastructure when dealing with the 5 VDC and RS-232 world of most hackers. 223 Chapter 11 — Connecting Roomba to the Internet F IGURE 11-15: Lantronix XPort evaluation board If you’re designing a new system (which will thus likely run on 3.3V) and it needs to be small, the XPort is a great product. For hackers, or if you’re adding to an existing 5V system, the SitePlayer or the Cobox Micro is better. Many prefer the SitePlayer because it has a slightly more modern configuration style. Configuring the XPort In its default configuration, the XPort responds on a single IP address and on a number of different ports: Ⅲ Port 10001: Serial-to-Ethernet gateway Ⅲ Port 9999: Text-based configuration Ⅲ Port 80: Web-based configuration 224 Part III — More Complex Interfacing Unlike the SitePlayer, which uses the cross-platform ZeroConf/Bonjour/Rendezvous protocol to help you auto-discover it, the XPort assumes you will use their Windows-based DeviceInstaller. If you use Windows, go ahead and use that. If you don’t have Windows, you can still configure the XPort using the networking debugging techniques mentioned in the “Debugging Network Devices” sidebar earlier in this chapter. The XPort will ask your network’s DHCP server for an IP address so that you can connect to it. One method of finding its IP address is to do one nmap network scan before plugging the XPort in and then another nmap network scan after plugging it in. The extra IP address that appears is the XPort. Somehow the slightly annoying Java applet that is the XPort’s web interface manages to be both simpler and more confusing than the SitePlayer (see Figure 11-16). Thankfully, the only parameters that you need to change are at the very top. Change the serial parameters to be Roomba compatible (57600 bps 8N1). First, make sure Serial Protocol is set to RS232. Next, set the Speed setting to 57600, the bits per second speed the Roomba expects. Then, set Character Size to 8, for 8-bit bytes, and the Parity to None. Finally, change the Flow Control setting to 2, which means no flow control to the XPort. Leave all other settings alone. Click Update Settings to complete the changes. You could also use the simpler Telnet configuration interface to do the same thing. The Lantronix WiMicro Wi-Fi module in Chapter 12 is con- figured almost exactly the same as the XPort and you’ll use Telnet to configure it. F IGURE 11-16: XPort web interface 225 Chapter 11 — Connecting Roomba to the Internet There is no username and password combination on either the web interface or the Telnet interface. You can enable a password on the Telnet configuration interface or disable the Telnet interface entirely. This doesn’t affect the web interface, which has no password protec- tion, but you can also disable that interface too. Be sure to read the XPort documentation thoroughly about configuration, especially if you’re using Windows. Using the XPort When the XPort is configured, using it is just like using the SitePlayer Telenet. The only differ- ence is the Telnet port. SitePlayer Telnet uses the standard port 23, while XPort uses port 10001. In lieu of creating a custom circuit board for the XPort, the evaluation board is small enough to be mounted on the top of Roomba. To supply power, either bring out the 5V from the power supply in the serial tether and attach it to the 5V input of the evaluation board or build a small 5V power supply. Going Further with XPort Lantronix did a smart thing in making their line of embedded device server products similar to each other. If you choose not to use the XPort, the Micro or Mini modules might be appropri- ate for you. If you want Wi-Fi connectivity instead of Ethernet, the WiPort or WiMicro mod- ules are replacements for their Ethernet cousins. The WiPort will be covered in detail in the next chapter. Modifying RoombaComm for the Net You now have Roomba on the Net, but all the code you’ve created thus far has been designed for the serial port. In Java, as in most modern languages, dealing with serial ports or Ethernet ports is fairly similar. The RoombaComm library uses that fact to create a new subclass of the RoombaComm base class that knows how to deal with the TCP telnet port that both the SitePlayer Telnet and XPort produce. This new subclass is called RoombaCommTCPClient, and most of it is shown in Listing 11-1. Listing 11-1: RoombaCommTCPClient package roombacomm.net; import roombacomm.*; public class RoombaCommTCPClient extends RoombaComm { String host = null; Continued 226 Part III — More Complex Interfacing Listing 11-1 Continued int port = -1; Socket socket; InputStream input; OutputStream output; public RoombaCommTCPClient() { super(); } // portid is “host:port” public boolean connect(String portid) { String s[] = portid.split(“:”); if( s.length < 2 ) { logmsg(“bad portid “+portid); return false; } host = s[0]; try { port = Integer.parseInt(s[1]); } catch( Exception e ) { logmsg(“bad port “+e); return false; } logmsg(“connecting to ‘“+host+”:”+port+”’”); try { socket = new Socket(host, port); input = socket.getInputStream(); output = socket.getOutputStream(); } catch( Exception e ) { logmsg(“connect: “+e); return false; } } public boolean send(int b) { // will also cover char try { output.write(b & 0xff); // for good measure output.flush(); } catch (Exception e) { e.printStackTrace(); return false; } return false; } // other methods to implement RoomabComm } 227 Chapter 11 — Connecting Roomba to the Internet You should note two key differences in RoombaCommTCPClient. First, notice that the String argument to connect() goes from being a serial port name to being a host:port combination. The host is the IP address of the Ethernet-to-serial device and the port is either port 23 (for SitePlayer) or port 10001 (for XPort). The second thing to note is that Java uses the exact same objects (InputStream and OutputStream) to represent reading and writing data over a network as it does for communi- cating over a serial line. This means that most of the code like send() can be almost exactly the same. For network devices, the Socket object provides InputStreams and OutputStreams; for serial ports, the SerialPort object does. The updateSensors() and associated code to read data back from Roomba aren’t shown, but they are largely the same. Unlike SerialPort, which runs a separate thread and provides an EventListener interface, Java’s Socket doesn’t. So a standard EventListener-like thread is created to periodically look for input. When information arrives, the EventListener buffers it and calls an internal event method to deal with the data, just like RoombaCommSerial .serialEvent(). All of the example programs in this book thus far have explicitly created RoombaCommSerial objects. This was done to make things more obvious, but all of the example programs can be quickly changed to use another subclass of RoombaComm. Listing 11-2 shows a version of the familiar SimpleTest example program, very slightly modified to use RoombaCommTCPClient. In fact, the only modification necessary is changing what type of RoombaComm object is instantiated and to remove the serial-specific parameter setting. Similarly, all of the Processing sketches can quickly be modified to use RoombaCommTCPClient instead. Listing 11-2: SimpleTest, for Networked Roombas package roombacomm.net; import roombacomm.*; public class SimpleTest { static boolean debug = false; public static void main(String[] args) { String portnamem = args[0]; if( !roombacomm.connect(portname) ) { System.out.println(“Couldn’t connect to “+portname); System.exit(1); } System.out.println(“Roomba startup on port “+portname); roombacomm.startup(); roombacomm.control(); roombacomm.pause(50); Continued 228 Part III — More Complex Interfacing Listing 11-2 Continued System.out.println(“Checking for Roomba “); if( roombacomm.updateSensors() ) System.out.println(“Roomba found!”); else System.out.println(“No Roomba. :( Is it turned on?”); System.out.println(“Playing some notes”); roombacomm.playNote(72,10); // C roombacomm.pause(200); roombacomm.playNote(79,10); // G roombacomm.pause(200); roombacomm.playNote(76,10); // E roombacomm.pause(200); System.out.println(“Spinning left, then right”); roombacomm.spinLeft(); roombacomm.pause(1000); roombacomm.spinRight(); roombacomm.pause(1000); roombacomm.stop(); // and so on System.out.println(“Disconnecting”); roombacomm.disconnect(); } } Summary Getting a Roomba on your LAN is pretty easy with the right tools. Now anyone on your net- work can access Roomba and run the programs you write for it. No special serial port drivers are needed, just an Internet connection. The Ethernet tether turns out to be a pretty good replacement for the serial tether because dealing with Ethernet, as a user, is just simpler. The Ethernet module and your computer do all the hard work. Ethernet has the added benefit of giving you much longer cable lengths, up to 100 meters (325 feet). For the next chapter, the Ethernet tether can function in a similar support role for the Wi-Fi adapter as the serial tether did for the Bluetooth adapter: providing a known-good interface that is the same in all ways except one is wired and the other wireless. 229 Chapter 11 — Connecting Roomba to the Internet Both the SitePlayer and the XPort are good embedded device servers. For Roomba hacking, the SitePlayer is a bit more appropriate, but the XPort is more useful if you’re trying to add network capability to devices with less available space. For example, if you wanted to put your coffee maker or alarm clock on the Internet, the extra space savings the XPort affords could be critical. Modifying RoombaComm to use a networked version of Roomba was easy. And while modify- ing your existing programs and sketches to use the new, networked RoombaComm is a little clunky, no doubt you have some ideas on how to make it work. Java has some patterns for deal- ing with this situation, and they’re easy to add. Both the SitePlayer and the XPort support a UDP mode instead of the Telnet-like TCP. UDP is connectionless, making you deal with packets of data instead of streams. For most cases, TCP is preferred, but you may like dealing with Roomba (or other networked objects you create) in a packetized fashion. Going Wireless with Wi-Fi I n a few short years wireless Internet connectivity has gone from research project to required computer feature. All new laptops have wireless capa- bility built in and many desktops do, too. USB adapters to add wireless to existing computers can be had for under $20. It seems we hardly know how we ever lived without wireless Internet. And that’s the interesting thing. Being free from a physical cable has changed how we interact with our computers. Laptops are outselling desktops. The dedicated computer nook is giving way to computer use any time, anywhere. You can surf the Net (often for free) in public places like coffee shops, airports, and hotels around the world. Cities are rolling out metro-wide Wi-Fi access for all, partly as a way to seem progressive, but also as a valid and inexpensive way of providing a critical resource to its citizens. The computer is becoming less of a destination and more of a companion. The Net is now the destina- tion, and it must be available for use wherever people want it. Both new cell phones and Skype phones have Wi-Fi built-in, and they are both able to forgo the standard cellular network for an Internet connection. Everything that can is going Wi-Fi. In the previous chapter, you saw how to add an embedded Internet device server to an existing system. Adding Internet connectivity to stationary domestic objects with Ethernet is relatively cheap and simple. Everyone should experiment with putting his or her coffee maker on the Net. The tools and techniques learned for a wired network adapter carry over to a wireless one. For a mobile device like Roomba, it makes less sense because the cable gets in the way. It’s more of a test device and a stepping-stone to Wi-Fi. Like the RS-232 serial tether as a debugging tool for the Bluetooth adapter, a wired version of a network adapter complements a wireless one. This chapter shows how to build the Wi-Fi version of a network adapter. It’s currently much more expensive to add Wi-Fi in a manner similar to the Siteplayer, but having a Wi-Fi Roomba is really cool.  Understand and debug Wi-Fi  Use Lantronix WiMicro with Roomba  Try SitePlayer with a wireless bridge  Build a Wi-Fi Roomba  Control Roomba with a Web page  Control Roomba with PHP chapter in this chapter [...]... the Wi-Fi Roomba adapter, you’ll need the following parts: Ⅲ Lantronix WiMicro, Mouser part number 515-WM11A000 2-0 1 Ⅲ Mini-DIN 8-pin cable, Jameco part number 10604 Ⅲ 7805 +5 VDC voltage regulator, Jameco part number 51262 Ⅲ Two 1µF polarized electrolytic capacitors, Jameco part number 94 160PS Ⅲ Two 8-pin header receptacle, Jameco part number 70754 Ⅲ General-purpose circuit board, Radio Shack part number... 0% packet loss round-trip min/avg/max/stddev = 0.240/1.250/2.304/1.007 ms Chapter 12 — Going Wireless with Wi-Fi WiMicro Telnet Configuration Now that you have the IP address of the WiMicro, telnet to its configuration menu on port 99 99, just like you can with the XPort You should see something like: demo% telnet 1 69. 254.78.1 19 999 9 Trying 1 69. 254.78.1 19 Connected to 1 69. 254.78.1 19 Escape character... media: autoselect demo% ping 1 69. 254.255.255 PING 1 69. 254.255.255 (1 69. 254.255.255): 56 data bytes 64 bytes from 1 69. 254.86. 198 : icmp_seq=0 ttl=255 time=0.240 ms 64 bytes from 1 69. 254.78.1 19: icmp_seq=0 ttl=64 time=2.208 ms (DUP!) 64 bytes from 1 69. 254.86. 198 : icmp_seq=1 ttl=255 time=0.247 ms 64 bytes from 1 69. 254.78.1 19: icmp_seq=1 ttl=64 time=2.304 ms (DUP!) ^C - 1 69. 254.255.255 ping statistics... Listing 1 2-2 : PHP Roomba Control with roombacmd.php . either the web interface or the Telnet interface. You can enable a password on the Telnet configuration interface or disable the Telnet interface entirely. This doesn’t affect the web interface, which. one. For a mobile device like Roomba, it makes less sense because the cable gets in the way. It’s more of a test device and a stepping-stone to Wi-Fi. Like the RS-232 serial tether as a debugging tool. goes over the layout of the WiMicro board. DPAC Airborne and Other Wireless Device Servers There aren’t as many Wi-Fi device servers as there are Ethernet ones. This is unfortunate since Wi-Fi

Ngày đăng: 10/08/2014, 04:21

TỪ KHÓA LIÊN QUAN