How do I find the Yun IP address in a Python script

I’m working on a WiFi controlled robot using the Yun. I have a working proto-type using the Mega 2560 with a WiFi shield, but I want to port this to the Yun because its much compact than a Mega with a WiFi shield.

I have a PC application that sends data over Wifi to the robot. The robot needs to read the data and change servos or I/O pins based on the values.

I’ve seen several Python scripts that open a socket to read data. In all the examples I found, the IP address was hard coded in the script. I don’t want to have to edit the Python script every time my IP address changes. There is a pretty-wifi-status.lua script that works with an example sketch, but its in lua, not Python.

This script works on my PC, but errors on the Yun.

import socket
hostname = socket.gethostname()
address = socket.gethostbyname("%s.local" % hostname)
addr = address
print addr

The error I get is:

File “getIP.py”, line 3, in
address = socket.gethostbyname("%s.local" % hostname)
socket.gaierror: [Errno -2] Name or service not known

Can anyone tell me what I am doing wrong or another way to get the WiFi IP address of the Yun from within Python?

nano getip.py
#!/usr/bin/python
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('arduino.cc', 0))
address=s.getsockname()[0]
print address
chmod 755 getip.py
./getip.py

Thanks!

That worked. From what I understand, it opens a connection to arduino.cc and then returns the IP address of that connection. So this code only works if the Yun can connect to the Internet.

I changed it to connect to 8.8.8.8 instead of arduino.cc and it returns the IP much faster.

My intent for the Yun is to replace a Mega 2560 with a Wifi shield in a Wifi controlled robot. If I don't have an Internet connection, this won't work. The Yun is far smaller than a Mega with a Wifi shield. Its also less expensive and takes less power. Those are very desirable attributes for a small battery operated robot.

One last thing. Should I close that socket at the end of the script?

One last thing. Should I close that socket at the end of the script?

definitely not hurt.

Plan B:

s.connect(('arduino.cc', 0))
s.connect(('192.168.0.1', 0))

'192.168.0.1' is router's address.

Plan C:

Setup Yun use static IP address:

http://forum.arduino.cc/index.php?topic=227693.msg1650457#msg1650457

Plan D:

ARP Binding/IP & MAC Binding at Router:

a lot of new routers support ARP Binding, now Binding arduino Yun MAC address with fix ip address.

Plan E:

Keep Yun remain at AP (Access Point) mode, computer or smartphone connect with it. The Yun ip address is '192.168.240.1'.

A static IP would be nice, but since the intent for this project is a tiny WiFi controller robot, it won't always be in the same place and have access to the same network.

When using it at my home, I'd prefer the Yun connect to my home network. That way my other devices don't have to change their settings or lose their Internet connection to connect to the Yun.

When I'm away from home, I would consider setting up the Yun as an AP and then connect to the Yun network from my laptop. That is simpler than trying to configure the Yun for all different networks.

Is there an easy way to programmatically change the Yun WiFi settings? I could possible use a digital I/O pin as a jumper to select two configurations. I would run a jumper wire from either ground of 5V to an I/O pin. I could have a sketch or script check the state of that I/O pin and set the WiFi to either a client on my home network or an AP when away from home.

Emailguy:

Is there an easy way to programmatically change the Yun WiFi settings?