Как связать два raspberry pi через wifi direct

Обновлено: 02.05.2024

I want to connect two devices using Wifi-Direct. One is a RaspBerry PI 3 (with Raspbian Jessie), the other is an Android smartphone. Raspberry as server and Android as client.

I decided to use wpa_supplicant in Raspbian and the WifiDirect example app for Android:

My wpa_supplicant.conf contains:

I am following this steps:

1) In the raspberry pi:

2) Then in the Android app, I can see the raspberry as a peer but I get a connection failure. I try with the settings app (Wifi => Advanced => Wifi Direct) and I can see the raspberry as a peer too but the diference is that when I try to make the connection, the application request me a PIN code and I am able to make the connection if I introduce the pin returned by p2p_pin any in the raspberry.

Im trying to understand this behavior:

Why is requesting me a PIN? This is the reason why I am not able to make the connection through the WifiDirect example app? Im missing something here?

Я хочу подключиться с телефона Android к RPI 3 через Wi-Fi direct.

Я хочу сделать это через Wi-Fi direct, потому что RPI будет контроллером для одного или нескольких передвижных устройств, которые будут использоваться как в зданиях, так и на природе.

В зданиях его можно было просто подключить к существующему Wi-Fi, но в природе нет Wi-Fi. Следовательно, RPI должен был быть своей собственной точкой доступа, но тогда телефон, управляющий им, не мог быть одновременно подключен к Wi-Fi здания. Кроме того, это может осложниться с несколькими RPI, один должен быть AP, а другие должны будут подключиться к нему.

Было бы здорово, если бы я мог просто включить RPI, не делая никаких настроек каждый раз, когда меняются их настройки, и подключаться к ним независимо от того, где он используется.

Так что с Wi-Fi direct есть требование: как я могу это сделать? Может ли кто-нибудь предоставить мне пример (или ссылку на пример)? В принципе, любой намек, который приближает меня к цели, поможет.

что у меня так далеко

Затем я установил wpa_supplicant, используя "pacman -S wpa_supplicant"

Затем я создал для него небольшую конфигурацию в /etc/wpa_supplicant/p2p.conf, которая содержит:

тогда я начал использовать wpa_supplicant wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/p2p.conf

Затем я проверил прямое использование Wi-Fi, wpa_cli а затем p2p_find нашел мой телефон. Поэтому я считаю, что с точки зрения водителя все хорошо.

Мне просто нужно выяснить, как использовать wpa_supplicant для регистрации серверной программы, работающей на моем PI, и как найти и подключиться к ней с Android.

I want to connect from an android phone to a RPI 3 over Wi-Fi direct.

I want to do it over Wi-Fi direct because the RPI will be a controller for one or more movable devices that will be used in buildings as well as out in the nature.

In buildings, it could just connect to the existing Wi-Fi, but in the nature there is no Wi-Fi. Therefore, the RPI would need to be its own AP, but then the phone controlling it could not be connected to the building's Wi-Fi at the same time. Also, it would get complicated with multiple RPIs, one would need to be the AP and the others would need to connect to it.

It would be cool if I just could power up the RPIs, withouth doing any configuration every time their setup changes, and connect to it regardless from where it's used.

So with Wi-FI direct being a requirement: How can I do this? Can someone provide me with an example (or a Link to an example)? Basically any hint that brings me closer to the target will help.

what i have so far

Then I've installed wpa_supplicant using "pacman -S wpa_supplicant"

Then I've created a small configuration for it under "/etc/wpa_supplicant/p2p.conf" that contains:

then I've started wpa_supplicant using wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/p2p.conf

Then I've tested Wi-Fi direct using wpa_cli and then p2p_find which found my phone. So I assum driver-wise, everything's good.

I just need to figure out how to use wpa_supplicant to register a server-program running on my PI, and how find & connect to it from android.

So I have a fresh pi zero w up and running raspbian stretch lite.

How can I enable Wifi direct/p2p where the Pi is GO and where it has a static ip i can connect to, and install/configure a DHCP server on the pi that when a client device connects to the pi over WiFi direct it get assigned an ip address?

Any idea how to do it? has anybody managed to get a similar scenario working? where to start or what resources I could use, its not my first attempt I have been scratching my head around this a lot for the past couple of weeks, and its driving me crazy, it's definitely not an easy task, I messed a lot with the previous installed OS trying to get it working so I had to re-flash a new image.

As stated in my answer to your similar but less specific question dhcpcd.conf setting static ip not working! I'm just looking at WiFi Direct connections. So could you please paste your /etc/wpa_supplicant/wpa_supplicant.conf into your question?

I added the contents of my wpa_supplicant.conf, as I have messed up my previous attempt by running lots of commands, so I wanted to start on a clean installation, also the pi had already other stuff that aren't needed anymore so i thought it's better to start from scratch anyways.

1 Answer 1

UPDATE:
I have made a more detailed answer at Connect Android smartphone with Wi-Fi Direct to a Raspberry Pi. Please look there. This answer here isn't updated anymore.

We want to have a DHCP server running. WiFi Direct is organized in groups and every group has one group owner (GO). Only the group owner is allowed to run a DHCP server because we have to ensure that only one DHCP server is present in the group. The group owner can change and it seems that there are protocol datagrams to negotiate DHCP. I have seen a hint about this at Wi-Fi Direct vs Ad-hoc mode, section 6) Frame exchange sequence:

After that we can exchange data frames in the group, but usually the DHCP protocol will be used first to provide the client with an IP address.

But I haven't found further information how it works so for this setup I will set the RasPi to the group owner. To ensure that a device is always negotiated to a group owner we use the option p2p_go_intent=[0..15] . 0 means the RasPi becomes a client, 15 means the RasPi becomes a group owner. 7 means a chance of 50 % to become a group owner.

I prefer to use systemd-networkd because it has all in one and is able to manage dynamically changing interfaces. WiFi Direct groups are represented by virtual wifi interfaces with increasing numbers, e.g. p2p-wlan0-0 , p2p-wlan0-1 and so on.

Just follow to Use systemd-networkd for general networking. You can use section "♦ Quick Step". Then come back here.

To configure wpa_supplicant create this file with your settings for country= and device_name= . By specification, the device name should always start with DIRECT-. You can just copy and paste this in one block to your command line beginning with cat and including EOF (delimiter EOF will not get part of the file):

To give the group interface a static ip address and enable a DHCP server on it, create this file:

To manage devices with wpa_cli we have to specify the control interface wpa_cli -ip2p-dev-wlan0 . In wpa_supplicant.conf we have specified where to find the names of the control interfaces:

Then on the RasPi I start finding devices:

Now I go to the WiFi Direct page on my smartphone
System settings -> Networks -> WiFi -> tick bar in upper right corner -> Advanced WiFi -> Wi-Fi Direct

Wi-Fi Direct page

Now I could look with wpa_cli -ip2p-dev-wlan0 p2p_peers what mac addresses of found devices are available. Then I have to show the details for every mac address with wpa_cli -ip2p-dev-wlan0 p2p_peer what name it has to find my smartphone. I do it with this one liner, for example:

and find a2:91:69:b2:91:a9 for my LG Leon 4G LTE . Now I only work with this mac address (p2p_dev_addr) and connect to it:

Note that we use pbc (push button control) and go_intent=15 to make the RasPi the group owner (see above).

On the smartphone tick SEARCH

In the popped up window I just confirm the connection by ticking ACCEPT (push button).

Wi-Fi Direct invitation

Under Advanced I find the ip address of the device, e.g. 192.168.4.102:

Wi-Fi Direct Advanced

and just ping it from the RasPi to confirm that the smartphone is connected.

To finish the connection we need the group identifier. You can find it with:

I have a pi running the latest release of raspbian and it's connected to the Internet using a wireless USB dongle. What I would like to do, is to share the pi's wifi connection so that any computer connected to the pi using a LAN cable would be able to receive the Internet. I had a look around the Internet but I can't seem to find anything of relevance. I'm familiar with this process on Windows and Mac OS X, but doing this on the pi has just got me stumped.

EDIT: I don't know whether this helps anyone but I am connected to the Internet on my pi via wlan0, but I would like to share that Internet connection via eth0.

Bridging your WiFi and Ethernet connections on the Pi and plugging it into a router would be simpler. Is this possible for your application?

@tlhIngan unfortunatly not, you see I don't have access to any Ethernet sockets and I need to boot my laptop from PXE, which has to be done by Ethernet. Also I want to learn more about Linux, and I thought building this project would give me a bit more confidence with Linux. I thought this would be an easier solution as my router is all the way across my house :/ Anyway, thanks for your reply.

I found this tutorial video exactly what you need: youtu.be/IAa4tI4JrgI The Raspberry PI shares internet it gets from wifi to Ethernet port.

@tlhIngan - Can you elaborate? Would creating a bridge require a router too? I want my network audio receiver to share my Pis WiFi over Ethernet and be on the same subnet and DHCP as my main router.

2 Answers 2

For Raspbian Jessie

Updated for Raspbian Stretch

Above configuration won't work in newer version of Raspbian. So, I have created a script for this which make it possible in less pain.

Connect to WiFi network using this guide.

Download the script from here. Place it at /home/pi/

Open up /etc/xdg/lxsession/LXDE-pi/autostart file

Add the last line :

Make sure you have given full path to the file. And you're done. Now reboot to see the changes

Читайте также: