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 !

RPF
  • 117
  • 2
  • 9

2 Answers2

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 ~$
Ingo
  • 42,961
  • 20
  • 87
  • 207
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
Milliways
  • 62,573
  • 32
  • 113
  • 225