Liberate Your Robot With Ad-Hoc Networking

Don’t let your robot be trapped by it’s WiFi network… go with Robot Ad-Hoc Networking!

Ad-Hoc Wireless Connection
Ad-Hoc Wireless Connection

Robot Ad-Hoc Networking is easy to configure and means that you, your robot, and other pre-configured devices like a laptop or tablet, can go anywhere and not need a WiFi (router / internet) connection. This configuration allows you to enter a new location and operate the robot without access to any local networks.

The Scenario

When going “into-the-field”, robot operators want:

a static IP address (for the robot)

Ad-Hoc wireless network

DHCP server (for connecting clients to the robot, eg. laptop or tablet)

When back at the office, operators want:

dynamic IP address

regular wireless network

disable DHCP server

The robot should automatically configure itself on boot up, based on what WiFi signals it finds.

The Solution

The file ‘rc.local’ calls ‘/home/ubuntu/scripts/go_dhcp_ad_hoc.sh’. This script checks to see if we got an IP address for WiFi (‘wlan0’). If yes, do nothing, otherwise configure Robot Ad-Hoc Networking and the DHCP server.

To use this method, install and configure:

  • DHCP server
  • ‘go_dhcp_ad_hoc.sh’ script
  • call script from rc.local

On boot, system will try to connect to the WiFi, if it fails then it will create the Ad-Hoc network and launch the DHCP server.

Install and Run

Note: please backup your robot software before making any modifications

1. Install required modules (run these commands on the robot):

$ sudo apt-get update

$ sudo apt install iw

$ sudo apt-get install isc-dhcp-server

2. Edit ‘/etc/dhcp/dhcpd.conf’, add the following to the end of the file:

subnet 192.168.0.0 netmask 255.255.255.0 {
     range 192.168.0.10 192.168.0.40;
     option broadcast-address 191.168.0.255;
     default-lease-time 600;
     max-lease-time 7200;
     authoritative;
 }

3. Create DHCP init scripts:

$ sudo cp /etc/default/isc-dhcp-server /etc/default/isc-dhcp-server.orig

$ sudo cp /etc/default/isc-dhcp-server /etc/default/isc-dhcp-server.wlan

$ sudo vi /etc/default/isc-dhcp-server.wlan

Change the last line of ‘etc/default/isc-dhcp-server.wlan’ to:

INTERFACES="wlan0"

4. Install script (see below):

$ vi /home/ubuntu/scripts/go_dhcp_ad_hoc.sh

  (copy the content from below)

$ sudo chmod 777 /home/ubuntu/scripts/go_dhcp_ad_hoc.sh

5. Edit ‘/etc/rc.local’ to call the script, by adding the following to the end of the file:

sleep 2
sudo /home/ubuntu/scripts/go_dhcp_ad_hoc.sh

You’re done!

Connecting to the Ad-Hoc Network

If the WiFi specified in /etc/network/interfaces’ is not found, robot will auto-configure Ad-Hoc network and present the Wifi SSID:

  MiniTurtyAdHoc

Connect your laptop or tablet using the Ad-Hoc WiFi connection, and then use the robot in the normal manner. The IP adress of the robot in Ad-Hoc mode is:

  192.168.0.1

To disable this functionality (prevent boot into Ad-Hoc mode), simply comment out (using ‘#’) the line in the ‘rc.local’ file.

The ‘go_dhcp_ad_hoc.sh’ Script

$ cat ~/scripts/go_dhcp_ad_hoc.sh:

#!/bin/sh
if ! wpa_cli -i wlan0 status | grep ip_address
then
  echo 'Not got IP address, setting up Ad-Hoc'
  if pstree | grep wpa_supplicant
  then
    sudo killall wpa_supplicant
  fi
  if pstree | grep avahi-daemon
  then
    sudo killall avahi-daemon
  fi
  if pstree | grep dhclient
  then
    sudo killall dhclient
  fi
  sleep 1

  sudo ifconfig wlan0 down
  sudo iwconfig wlan0 mode Ad-Hoc
  sudo iwconfig wlan0 essid MiniTurtyAdHoc
  sudo ifconfig wlan0 192.168.0.1
  sudo ifconfig wlan0 up

  sleep 1
  sudo cp /etc/default/isc-dhcp-server.wlan /etc/default/isc-dhcp-server
  sudo systemctl restart isc-dhcp-server
  sudo cp /etc/default/isc-dhcp-server.orig /etc/default/isc-dhcp-server
fi

Need more help?… Ask a question in the comments below!

Leave a Reply

Your email address will not be published. Required fields are marked *