dhcp hostname doesn´t work on self compiled master branch (solved)

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Sledge
Posts: 11
Joined: Thu Jan 19, 2017 5:56 pm

dhcp hostname doesn´t work on self compiled master branch (solved)

Post by Sledge » Sun Nov 26, 2017 7:19 pm

Hi,

if i flash the 1.9.3 binary the following code runs flawless using the config(dhcp_hostname="hostname")

Code: Select all

from time import sleep_ms
import machine
from Config import Config
Config1 = Config()
def do_connect():
    import network
    ap = network.WLAN(network.AP_IF) 
    ap.active(False)
    wlan = network.WLAN(network.STA_IF)
    wlan.config(dhcp_hostname=Config1.GetValue('DeviceName'))
    wlan.active(False)
    sleep_ms(2000)
    wlan.active(True)
    sleep_ms(100)
    SSID = Config1.GetValue('SSID')
    PASSWORD = Config1.GetValue('PW')
    nets = wlan.scan()
    bytessid = bytes(SSID, 'utf-8')
    rssi = ''
    print('searching for access points...')
    for net in nets:
        if net[0] == bytessid:
            print('Access point %s found. RSSI %s' %(net[1], net[3]))
            if not rssi:
                rssi, BSSID = net[3], net[1]
            if net[3] > rssi:
                rssi, BSSID = net[3], net[1]
    print('Connect to strongest ap %s from net %s with rssi %s and password %s' %(BSSID, SSID, rssi, PASSWORD))
    wlan.connect(SSID, PASSWORD, bssid=BSSID)
    print('0', end= ' ')
    i=0
    while not wlan.isconnected():
        i+=1
        print(i, end= ' ')
        if i==61:
            print('wlan connection error, restart')
            machine.reset()
        sleep_ms(1000)
    print(' ')
    print('Wlan connection succeeded')
    print(wlan.ifconfig())

do_connect()
gc.collect()
sleep_ms(10000)
import app.py
But on a self compiled firmware the following error occurs:

Code: Select all

Traceback (most recent call last):
  File "main.py", line 43, in <module>
  File "main.py", line 10, in do_connect
OSError: can't set STA config
MicroPython v1.9.3-52-gf59c6b4 on 2017-11-26; ESP module with ESP8266
Did i miss something or is the master branch not the source of esp8266-20171101-v1.9.3.bin
Last edited by Sledge on Mon Nov 27, 2017 9:50 pm, edited 1 time in total.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: dhcp hostname doesn´t work on self compiled master branch

Post by Roberthh » Sun Nov 26, 2017 8:33 pm

Did you try to set the value for dhcp_hostname="name" to a fixed string instead of getting it by a call to Config1.GetValue('DeviceName'), just to sort out the reason for the error?

Sledge
Posts: 11
Joined: Thu Jan 19, 2017 5:56 pm

Re: dhcp hostname doesn´t work on self compiled master branch

Post by Sledge » Sun Nov 26, 2017 9:53 pm

Yes i did also try with a constant name, it is running fine on pre compiled 1.9.3 binary. But same code doesn´t work with self compiled code from master.

I´m a bit unsure if the master has the same code as the pre compiled 1.9.3 version. On the mp download page you can only get the source of 1.9.2 for download. http://micropython.org/resources/source ... 9.2.tar.xz

Sledge
Posts: 11
Joined: Thu Jan 19, 2017 5:56 pm

Re: dhcp hostname doesn´t work on self compiled master branch

Post by Sledge » Sun Nov 26, 2017 11:35 pm

Hmm ok strange thing, i flashed the pre compiled image back and testet the code again and it gave me the same error. I had to connect to my network first without changing hostname. After that the code did work again and changed the hostname. Can this be a bug or is it my fault?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: dhcp hostname doesn´t work on self compiled master branch

Post by pythoncoder » Mon Nov 27, 2017 6:44 am

When you erase flash and flash a build the ESP8266 starts in a different state compared to a restart of a module which has previously been connected: the ESP8266 remembers the last connection and initially reconnects to that AP. So after the line

Code: Select all

wlan = network.WLAN(network.STA_IF)
on an initial connection wlan.isconnected() will return False. On subsequent reboots or power cycles it will return True because it will initially connect to the last network it saw. So from the evidence of your results I would say that the line

Code: Select all

wlan.config(dhcp_hostname=Config1.GetValue('DeviceName'))
only works in the case where the device is connected to a network (I've never actually tried setting dhcp_hostname so to some extent I'm flying blind here).
Peter Hinch
Index to my micropython libraries.

Sledge
Posts: 11
Joined: Thu Jan 19, 2017 5:56 pm

Re: dhcp hostname doesn´t work on self compiled master branch

Post by Sledge » Mon Nov 27, 2017 6:05 pm

Thanks for clarifying pythoncoder. Do you think it is a problem to always run the do_connect() in my code or is it better to first check if a connection could be "automatically" established?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: dhcp hostname doesn´t work on self compiled master branch (solved)

Post by pythoncoder » Tue Nov 28, 2017 5:35 am

I think this rather depends on your application. If it's connected automatically is it worth seeking a better signal? Only you can answer this.

There is the problem of flash wear to consider. The ESP8266 remembers its last connection by storing it in flash, a type of hardware which supports a finite number of write operations. So each time you connect to a different network a write occurs. The quality of flash chips varies; some are specified to 10K writes or more so the problem may be theoretical.

I'd be interested to know the purpose of setting the dhcp_hostname parameter when it has already connected to a network. I'm no network guru, but by the time wlan.isconnected() has returned True I would have thought DHCP would have done its stuff and supplied an IP.
Peter Hinch
Index to my micropython libraries.

Sledge
Posts: 11
Joined: Thu Jan 19, 2017 5:56 pm

Re: dhcp hostname doesn´t work on self compiled master branch (solved)

Post by Sledge » Wed Nov 29, 2017 10:01 pm

I do not understand that too. As far as i found out you can set the dhcp hostname only if the esp had a connection to a wlan. But after a reconnect it won´t use it for a known wlan. You have to connect to another wlan and then back. My approach is, to first connect to an access point on my mobile, set the hostname and then connect to my home wlan. Its a bit strange but works for me. Better than 100 iot devices in your home network with cryptic hostnames :)

Post Reply