Power & Source of Big Ideas

Nanohat oled / bakebit python script

Moderators: chensy, FATechsupport

Hi all

Sorry for the noob question but my python knowledge is near 0 so I am struggling to figure this out.

I have a nanopi neo with the nanoled hat that loads the bakebit_nanohat_oled.py script but it keeps displaying the loopback address (127.0.0.1)

I believe my the issue lies in the following section

def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except:
IP = '127.0.0.1'
finally:
s.close()
return IP


if I put an ip address in the section s.connect(('10.255.255.255', 1)) that is valid on the subnet the nanopi receives it ip address in (ie 12.1.1.1, which does not actually exist) then it displays the ip address that was assigned to by DHCP.

Any ideas to resolve this would be appreciated.
Andy
I have just taken receipt of my first NanoPi NEO2 and I have the same problem-I used a mesh network with its own DHCP server, which in turn sits "behind" a OpenWRT flashed router (also running its own separate DHCP server-so yes,I'm double NAT'ing in some cases).
If I plug the Nano into the mesh network, I get the 127.0.0.1 address on the screen-but I successfully lease an IP. If I plug the Nano directly into the OpenWRT router, I get a valid IP-and it shows up on the screen. I can only assume the mesh network doesn't do the DHCP transaction as quickly as the OpenWRT-and therefor defaults to using the loopback (l0) address-even though I have a valid IP!
Did you get a fix for this?-I was thinking what script pulls the info for the OLED screen, if that can be delayed (even with a simple sleep command), this might work... :-)

cheers
cabs
So I found a fix:

Code: Select all

def get_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        # doesn't even have to be reachable
        s.connect(('8.8.8.8', 1))
        IP = s.getsockname()[0]


(changed the 10.255.255.255 to 8.8.8.8-seems to do the trick!)
Hopefully this helps someone!

Cheers
cabs

Who is online

In total there are 59 users online :: 0 registered, 0 hidden and 59 guests (based on users active over the past 5 minutes)
Most users ever online was 5185 on Wed Jan 22, 2020 1:44 pm

Users browsing this forum: No registered users and 59 guests