Raspberry Pi ad-hoc networking

柔情痞子 提交于 2019-12-29 18:03:47

问题


I want to try some networking projects with Raspberry Pis, and I need to just send packets between a pair of pis. I would be happy as a first step just being able to ping between to Raspberry Pis in ad-hoc mode. I have not successfully done this despite looking at several tutorials and examples online.

I have 2x Raspberry Pis with the Debian Wheezy OS installed. I am using the following USB adapter which I installed firmware for on both pis and tested that they work by connected them in managed mode to a router: Bus 001 Device 004: ID 050d:945a Belkin Components F7D1101 v1 Basic Wireless Adapter [Realtek RTL8188SU]

Here are is some printouts about the networking information:

/etc/network/interfaces at each pi:


pi1@raspberrypi ~ $ cat /etc/network/interfaces 
auto lo
iface lo inet loopback
iface eth0 inet dhcp
auto wlan0
iface wlan0 inet manual
    address 192.168.2.1
    netmask 255.255.255.0
    wireless-channel 4
    wireless-essid pi-ad-hoc
    wireless-mode ad-hoc
pi2@raspberrypi ~ $ cat /etc/network/interfaces 
auto lo
iface lo inet loopback
iface eth0 inet dhcp
auto wlan0
iface wlan0 inet manual
    address 192.168.2.2
    netmask 255.255.255.0
    wireless-channel 4
    wireless-essid pi-ad-hoc
    wireless-mode ad-hoc

ifconfig at each pi:


pi1@raspberrypi ~ $ ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr ec:1a:59:46:8e:5a  
          inet addr:192.168.2.1  Bcast:192.168.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:26 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
pi2@raspberrypi ~ $ ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr ec:1a:59:46:59:0a  
          inet addr:192.168.2.2  Bcast:192.168.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:6 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

iwconfig at each pi:


pi1@raspberrypi ~ $ iwconfig wlan0
wlan0     IEEE 802.11bg  ESSID:"pi-ad-hoc"  Nickname:"rtl_wifi"
          Mode:Ad-Hoc  Cell: 02:11:87:FA:4A:02   Bit Rate:54 Mb/s
Sensitivity:0/0
Retry:off RTS thr:off Fragment thr:off Power Management:off Link Quality:0 Signal level:0 Noise level:0 Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0 Tx excessive retries:0 Invalid misc:0 Missed beacon:0 pi2@raspberrypi ~ $ iwconfig wlan0 wlan0 IEEE 802.11bg ESSID:"pi-ad-hoc" Nickname:"rtl_wifi" Mode:Ad-Hoc Cell: 02:11:87:C4:F2:01 Bit Rate:54 Mb/s
Sensitivity:0/0
Retry:off RTS thr:off Fragment thr:off Power Management:off Link Quality:0 Signal level:0 Noise level:0 Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0 Tx excessive retries:0 Invalid misc:0 Missed beacon:0

route at one pi (identical on other pi):


pi1@raspberrypi ~ $ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
192.168.1.0     *               255.255.255.0   U     0      0        0 eth0
192.168.2.0     *               255.255.255.0   U     0      0        0 wlan0

iwlist scan at each pi:


pi1@raspberrypi ~ $ sudo iwlist wlan0 scan
wlan0     Scan completed :
          Cell 01 - Address: 02:11:87:FA:4A:02
                    ESSID:"pi-ad-hoc"
                    Protocol:IEEE 802.11bg
                    Mode:Ad-Hoc
                    Frequency:2.427 GHz (Channel 4)
                    Encryption key:off
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
                              9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
                              48 Mb/s; 54 Mb/s
                    Quality:0  Signal level:0  Noise level:0
          Cell 04 - Address: 02:11:87:C4:F2:01
                    ESSID:"pi-ad-hoc"
                    Protocol:IEEE 802.11bg
                    Mode:Ad-Hoc
                    Frequency:2.427 GHz (Channel 4)
                    Encryption key:off
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
                              9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
                              48 Mb/s; 54 Mb/s
                    Signal level=100/100
pi2@raspberrypi ~ $ sudo iwlist wlan0 scan wlan0 Scan completed : Cell 01 - Address: 02:11:87:C4:F2:01 ESSID:"pi-ad-hoc" Protocol:IEEE 802.11bg Mode:Ad-Hoc Frequency:2.427 GHz (Channel 4) Encryption key:off Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s 9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s 48 Mb/s; 54 Mb/s Quality:0 Signal level:0 Noise level:0 Cell 02 - Address: 02:11:87:FA:4A:02 ESSID:"pi-ad-hoc" Protocol:IEEE 802.11bg Mode:Ad-Hoc Frequency:2.427 GHz (Channel 4) Encryption key:off Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s 9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s 48 Mb/s; 54 Mb/s Signal level=100/100

Ping does not work, and no networking seems to work between them. From iwconfig, you can see that they each have a different "Cell" address (not the same Cell as in the iwlist scan) which is the pseudo-base station ID that is used to define an ad-hoc network (my best understanding from what I've read). Also, from the iwlist, each pi can see their own plus the other pi's ad-hoc network. I assume they need to select the same Cell id to communicate, and I'm unsure how to get them to do this automatically. I tried statically forcing these to be the same with the following command at each pi which did not change the cell id and therefore did not work:

sudo iwconfig wlan0 ap (some address)

I also tried a solution which uses ap_scan=2 in the wpa_supplicant config which did not seem to help.

Anyone have any idea what I've done wrong?

Thanks, Andy.


回答1:


After some searching I found that the Belkin USB adapter I was using apparently didn't have ad-hoc mode support with the linux drivers. I bought some other wireless USB adapaters that worked great "Edimax EW-7811Un 150 Mbps Wireless 11n Nano Size USB Adapter". They are cheaper, smaller, and they worked in ad-hoc mode without even needing to worry drivers. The details I put for troubleshooting can be used as a guide if you are also wanting to do ad-hoc raspberry PI projects.




回答2:


This works for me in /etc/network/interfaces:

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
wireless-essid "MYPINET"
wireless-channel 3
wireless-mode ad-hoc
wireless-ap 11:5F:02:38:5C:45
address 192.168.10.1
netmask 255.255.255.0

The essid, channel and ap can be any valid value (same on all your Pi's). Make sure to assign different addresses on the same subnet to your different Pi's and you should be fine. The ap defines the cell ID that was mentioned above. FWIW I am using a TP-link WN725N. It has the RTL8188CUS chipset and works fine out of the box even though dmesg indicates the Pi is treating it as an RTL8192.




回答3:


I was unsuccessful with any adapter using the RTL8188CUS chipset. Luckily, I had a number of Ralink RT5370 dongles (from this kit) that support the nl80211 interface and ad-hoc mode.

My solution involves using wpa_supplicant and is configured with 2 files. Ensure that the nl80211 driver is installed:

sudo apt-get install libnl1

Next, create the following wpa_supplicant configuration file called /etc/wpa_supplicant-adhoc.conf on each Pi:

ctrl_interface=DIR=/run/wpa_supplicant GROUP=netdev
update_config=1
ap_scan=2

network={
  ssid="pihoc_wpa"
  mode=1
  frequency=2462
  proto=WPA
  key_mgmt=WPA-NONE
  pairwise=NONE
  group=TKIP
  psk="password"
} 

where you can choose the ssid, frequency (look here for valid values), and psk. Make sure that you are part of the user group net-dev using the command

getent group netdev

and if not, you can add yourself using

sudo usermod -a -G netdev userName

Next, add the following block to the /etc/network/interfaces file on each Pi:

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
   address 10.10.2.1
   netmask 255.255.255.0
pre-up wpa_supplicant -B -D nl80211 -i wlan0 -c /etc/wpa_supplicant-adhoc.conf

where each Pi has a different address field beginning with 10.10.2.. Also, if your RT5370 adapter is using an interface other than wlan0 (e.g. wlan1, wlan2, etc.), be sure to use that interface name instead.

At this point, the Pis should automatically join the network upon being rebooted. Test the connection by pinging or using ssh, for example run the following from the agent with IP address 10.10.2.1:

ssh 10.10.2.2

to access the agent with IP address 10.10.2.2.

The steps listed here are adapted from this Arch Linux wiki article and this Raspberry Pi forum discussion.



来源:https://stackoverflow.com/questions/15423325/raspberry-pi-ad-hoc-networking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!