The following example shows to how develop an application to communicate with your RooWifi :: Roomba Wi-Fi Remote based in a Python script, using GTK for your Linux preferred distribution.
This example is done using Ubuntu Desktop 12.04 (32bits). Download the file below this post, edit it and start Roomba Wi-Fi Remote. You’ll see that the script doesn’t contain much code as it has been explained in another post. The Transparent Mode just need a connection to the TCP/IP port 9001 and can start receiving and sending data as you send commands through the serial port of your desktop.
We know that an API written in Python or a RooWifi class you worked perfect, nowadays we are developing it such as C# and other languages like java (soon). This is only another little application that is just a guide in how to connect to the device. Developers must edit it to customize it to their needs and then just run it.
After this introduction we can start this example for RooWifi WiFi Remote for Roomba
If you download the file at the end of this article, you will see that it is a compressed file with two important scripts: RoombaWifiRemote and RoombaWifiRemote.py:
This bash script just launches the python application from your Ubuntu Nautilus
#!/bin/bash python RoombaWifiRemote.py
The file .py contains the necessary code to launch the User Interface, allowing the user to insert the IP address of Roomba Remote LAN and communicate with the robot. This script will emulate your computer or laptop as the top panel of the iRobot Roomba allowing the user command the different buttons of the robot.
The libraries used:
import socket #Para trabajar a nivel de socket tcp/ip import pygtk #Para trabajar con Python y GUI Gtk pygtk.require('2.0') import gtk import operator import time #Working with time functions like time.sleep(x) import string #Working with strings (Update of the battery status in our basic GUI)
Here you can see the code:
def ConnectWithRoomba(self, widget, data=None): print("Connect Button Pressed") try: self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.remote_ip = (self.TextIP.get_text()) self.s.connect((self.remote_ip , self.port)) self.s.send(chr(FULL_MODE_COMMAND)) time.sleep(1) self.s.send(chr(LED_COMMAND)) self.s.send(chr(25)) self.s.send(chr(255)) self.s.send(chr(128)) #Battery Status request self.s.send(chr(SENSOR_COMMAND)) self.s.send(chr(SENSOR_PACKET_0)) time.sleep(1) data = self.s.recv(NUM_BYTES_PACKET_0) aux1 = 100*(ord(data)*256+ord(data)) aux2 = ord(data)*256+ord(data) self.label.set_label ("Battery: " + str(aux1/aux2) + "%") widget.set_sensitive(False) self.button_Disconnection.set_sensitive(True) self.button_Clean.set_sensitive(True) self.button_Spot.set_sensitive(True) self.button_Dock.set_sensitive(True) self.button_Refresh.set_sensitive(True) except: self.label.set_label ("CAN'T CONNECT")
The workflow of the code above is as follows: first, connect to the robot. This function is just used when we start the connection with the button “Connection”. This function will fail in case the entered IP or HOST introduced by the user is an incorrect one.
Once the connection is established between the terminal and the robot, a command is sent giving total control of the robot to the user. The red and blue LEDs are turned on in order to check the connection has been performed successfully. Finally, all the sensors that contain information about the battery state are read.
The next functions contain the commands Clean, Thoroughly Clean or Return to Base:
#Clean Button def CleanRoomba(self, widget, data=None): self.s.send(chr(CLEAN_COMMAND)) #Spot Button def SpotRoomba(self, widget, data=None): self.s.send(chr(SPOT_COMMAND)) #Dock Button def DockRoomba(self, widget, data=None): self.s.send(chr(DOCK_COMMAND))
Now the function who closes the TCP/IP Socket:
#Disconnect from Roomba Wifi Remote def DisconnectWithRoomba(self, widget, data=None): widget.set_sensitive(False) self.button_Connection.set_sensitive(True) self.button_Clean.set_sensitive(False) self.button_Spot.set_sensitive(False) self.button_Dock.set_sensitive(False) self.button_Refresh.set_sensitive(False) print("Disconnect Button Pressed") self.s.close()
Finally here is the function to refresh the battery state of the iRobot Roomba:
#Battery Status Refresh def RefreshRoomba(self, widget, data=None): self.s.send(chr(SENSOR_COMMAND)) self.s.send(chr(SENSOR_PACKET_0)) time.sleep(1) data = self.s.recv(NUM_BYTES_PACKET_0) aux1 = 100*(ord(data)*256+ord(data)) aux2 = ord(data)*256+ord(data) self.label.set_label ("Battery: " + str(aux1/aux2) + "%")
To run the script from your Ubuntu system, be aware that you must have installed the following downloadable packages from the Ubuntu repository: