Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    How to set a specific IP Address in boot.py

    Scheduled Pinned Locked Moved LoPy
    lopystatic ip
    1 Posts 1 Posters 566 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S Offline
      simonjcarr
      last edited by

      I have added the following code to my boot.py file and it works fine, however my board gets a dynamic IP address from my router DHCP server.

      How can I change the code to set a static IP Address.

      # boop.py
      known_nets = [('***********', '***********')] # change this line to match your WiFi settings
      
      import machine
      import os
      
      uart = machine.UART(0, 115200) # disable these two lines if you don't want serial access
      os.dupterm(uart)
      
      if machine.reset_cause() != machine.SOFT_RESET: # needed to avoid losing connection after a soft reboot
          from network import WLAN
          wl = WLAN()
      	
          # save the default ssid and auth
          original_ssid = wl.ssid()
          original_auth = wl.auth()
      
          wl.mode(WLAN.STA)
      	
          available_nets = wl.scan()
          nets = frozenset([e.ssid for e in available_nets])
      
          known_nets_names = frozenset([e[0] for e in known_nets])
          net_to_use = list(nets & known_nets_names)
      
          try:
              net_to_use = net_to_use[0]
              pwd = dict(known_nets)[net_to_use]
              sec = [e.sec for e in available_nets if e.ssid == net_to_use][0]
              wl.connect(net_to_use, (sec, pwd), timeout=10000)
          except:
              wl.init(mode=WLAN.AP, ssid=original_ssid, auth=original_auth, channel=6, antenna=WLAN.INT_ANT)
      1 Reply Last reply Reply Quote 0

      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

      With your input, this post could be even better 💗

      Register Login
      • 1 / 1
      • First post
        Last post

      Pycom on Twitter

      Pycom