Static IP Possibility

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
yeyeto2788
Posts: 28
Joined: Wed Mar 30, 2016 4:09 pm

Static IP Possibility

Post by yeyeto2788 » Thu Sep 08, 2016 6:51 pm

Hello all,

I'm building a Temperature and Humidity server that I want to monitor from work but I'm not able to set a static or fixed IP.

Is there a possibility to set Fixed IP on MicroPython for ESP8266? I have found some info but only for the WiPy.

Ohh I almost forgot, I'm running Version 1.8.3 :)

Thanks!

User avatar
deshipu
Posts: 1365
Joined: Thu May 28, 2015 5:54 pm

Re: Static IP Possibility

Post by deshipu » Thu Sep 08, 2016 8:11 pm

What did you try already and how it didn't work?

User avatar
deshipu
Posts: 1365
Joined: Thu May 28, 2015 5:54 pm

Re: Static IP Possibility

Post by deshipu » Thu Sep 08, 2016 8:16 pm

Looking at the code, you should be able to set the address with network.ifconfig, passing it a tuple of four values: the address, the network mask, the gateway address and the dns server address.

yeyeto2788
Posts: 28
Joined: Wed Mar 30, 2016 4:09 pm

Re: Static IP Possibility

Post by yeyeto2788 » Fri Sep 09, 2016 5:40 am

[quote="deshipu"]Looking at the code, you should be able to set the address with network.ifconfig, passing it a tuple of four values: the address, the network mask, the gateway address and the dns server address.[/quote]

You meant something like this:
[code]network.ifconfig ('192.168.1.30', '255.255.255.0', '10.1.1.7', '8.8.8.8')[/code]
Last edited by yeyeto2788 on Wed Sep 14, 2016 11:05 am, edited 1 time in total.

User avatar
deshipu
Posts: 1365
Joined: Thu May 28, 2015 5:54 pm

Re: Static IP Possibility

Post by deshipu » Fri Sep 09, 2016 8:01 am

Well, the gateway IP usually has to be in the same subnetwork as your normal IP for it to function properly. Otherwise, how it didn't work?

yeyeto2788
Posts: 28
Joined: Wed Mar 30, 2016 4:09 pm

Re: Static IP Possibility

Post by yeyeto2788 » Wed Sep 14, 2016 11:05 am

Hello, sorry for the late reply
deshipu wrote:Well, the gateway IP usually has to be in the same subnetwork as your normal IP for it to function properly. Otherwise, how it didn't work?

This is my code to setup the Static IP but it seems not to be working :(

Code: Select all

import network
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.ifconfig('xxx.xxx.xxx.xxx','255.255.255.0','192.168.1.1','xxx.xxx.xxx.xxx')
sta_if.connect('SSID','Password')


This is the error I get when I input the data

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 9, in <module>
TypeError: function expected at most 2 arguments, got 5
It seems to be expecting a str instead of a tuple or a list.

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

Re: Static IP Possibility

Post by Roberthh » Wed Sep 14, 2016 11:11 am

You have to put the parameters in a tuple:

Code: Select all

sta_if.ifconfig(('xxx.xxx.xxx.xxx','255.255.255.0','192.168.1.1','xxx.xxx.xxx.xxx'))
Like shown in the docs.

yeyeto2788
Posts: 28
Joined: Wed Mar 30, 2016 4:09 pm

Re: Static IP Possibility

Post by yeyeto2788 » Wed Sep 14, 2016 11:23 am

Code: Select all

import network
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.ifconfig(('xxx.xxx.xxx.xxx','255.255.255.0','192.168.1.1','xxx.xxx.xxx.xxx'))
sta_if.connect('SSID','Password')
It actually worked!!! I re-read the documentation http://docs.micropython.org/en/latest/e ... twork.html and it works :D :D :D :D

The problem was that I had to do it this way:

Code: Select all

network.ifconfig(('xxx.xxx.xxx.xxx','255.255.255.0','192.168.1.1','xxx.xxx.xxx.xxx'))
Note the double parenthesis on the code.

Why is this? maybe is a silly question but I don't get it. :(

fdufnews
Posts: 29
Joined: Mon Jul 25, 2016 11:31 am

Re: Static IP Possibility

Post by fdufnews » Wed Sep 14, 2016 11:44 am

As said in the documentation,
To set the above values, pass a 4-tuple with the required information.
So

Code: Select all

('xxx.xxx.xxx.xxx','255.255.255.0','192.168.1.1','xxx.xxx.xxx.xxx')
is a 4-tuple.
Maybe you should look here http://www.tutorialspoint.com/python/python_tuples.htm

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

Re: Static IP Possibility

Post by pythoncoder » Thu Sep 15, 2016 8:05 am

Code: Select all

a = (1,2,3)
creates a 3-tuple (a tuple with 3 elements). If we have a function which expects a 3-tuple as an argument we can call it with either of:

Code: Select all

func(a)
func((6,7,8))
Peter Hinch

Post Reply