0

I want to send IP address with which my raspberry pi is connected to other server.

This is my guess.

  1. turn on raspberry pi (without monitor)

  2. find wifi automatically... try until success at connection

  3. if success, find wifi IP address

  4. send that IP address to other server by http.

  5. run my flask server.

In this process, how can I make raspberry pi to do [2,3] ?

Advice please !

6
  • 1
    Why do you WANT to find the IP address? I manage to run my local network without knowing the address of anything on it! You should ask your real question - what you want to do.
    – Milliways
    Commented Nov 9, 2018 at 10:40
  • 1
    @Milliways - You are right it might not be the best way of achieving the actual goal. However it's still a problem which could be solvable - The RPi is a learning tool after all. However - and here's the 'but' - the question isn't actually RPi specific. No idea what a 'flask' server is either...
    – Charemer
    Commented Nov 9, 2018 at 12:10
  • 1
    2. "Find WiFi automatically" ... this needs to be clarified. Are you looking for the unit to connect to the first available unencrypted WiFi connection? Have you entered in a particular network's credentials and want to know when the connection has been made? Is there a list of credentials that will be tried for known networks? ... 3 "Find WiFi IP address" ... this needs clarification as well... Are you asking, "I want the Pi to send its IP address to a known server." or "I want a separate network host to discover the Pi's IP address once it acquires one." Commented Nov 10, 2018 at 2:42
  • 1
    @Charemer : "flask" is a python web server... Project webpage Commented Nov 10, 2018 at 2:44
  • @RubberStamp 2. "Find WiFi automatically" => I want my RPi connect to specific wifi device. I will attach pocket wifi to RPi. If I turn on RPi, I want it to find that pocket wifi automatically, and enter password which I have saved into RPi already. And connection success ! ==> Is this possible ? Please give me some advices.
    – RPF
    Commented Nov 10, 2018 at 5:09

2 Answers 2

2

Wifi connections on a Raspberry Pi are made by wpa_supplicant. It is configured in /etc/wpa_supplicant/wpa_supplicant.conf. You can connect to a given wlan network as described in Setting WiFi up via the command line. This will be done automatically as soon as you get within reach of a wifi access point. If you want to connect to any open access point regardless of its SSID, you can use this network block in /etc/wpa_supplicant/wpa_supplicant.conf:

network={
        key_mgmt=NONE
}

You can also try to use peer to peer wifi connections. You may find some hints at unprotected ad-hoc wifi conncetion.

When you are automatically connected by wifi you can extract the ip address from the link information. To give you an idea how this works here is a simple bash script:

rpi ~$ cat get_ip_addr.sh
#!/bin/bash
# wait 30 sec if interface gets online
/lib/systemd/systemd-networkd-wait-online --interface=$1 --timeout=30 --quiet
if [ $? -ne 0 ]; then
    exit 1
else
    /sbin/ip -4 -br addr show $1 | /bin/grep -Po "\d+\.\d+\.\d+\.\d+"
fi

rpi ~$ chmod +x get_ip_addr.sh
rpi ~$ ./get_ip_addr.sh wlan0
192.168.4.37

rpi ~$ ./get_ip_addr.sh xxx
Event loop failed: Connection timed out
rpi ~$
2
  • Your script requires the interface name to be known before the address can be extracted. ... ip -4 a |grep inet.*wl |cut -d " " -f6 |cut -d \/ -f1 should list all IPv4 addressed wireless interfaces which use either the wlan[0-9] naming convention or the so-called "predictable" naming convention. Commented Nov 13, 2018 at 15:40
  • @RubberStamp Of course, you are right. As written its only a simple script easy to understand to show the idea. It can be modified as you did to fit specific needs. The OP may better use a python script but I was to lazy to show it also ;-)
    – Ingo
    Commented Nov 13, 2018 at 16:21
2

You should really ask a single question at a time.

Your steps 1-3 will happen automatically (if the Pi is correctly configured See How to set up networking/WiFi )

If you just want to know the IP Address your Pi is using use hostname -I.

You COULD go through the process of sending the IP address somewhere - but you need to specify the format and destination to get an answer.

OR you could just connect to your server by name raspberrypi.local instead of IP address. (NOTE raspberrypi is the default hostname, and can/should be changed).

If you REALLY want to know the IP address you can discover it by many means;

arp raspberrypi.local on most networks (arp raspberrypi may work on some)

e.g.

arp archpi.local
archpi.local (10.1.1.20) -- no entry

or

getent hosts archpi.local | awk '{ print $1 }'
10.1.1.20

or

ping -c 1 archpi.local
PING archpi.local (10.1.1.20): 56 data bytes
64 bytes from 10.1.1.20: icmp_seq=0 ttl=64 time=4.607 ms

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.