Can connect in REPL but not with main.py
-
Hello together,
I can connect to the WiFi network and send data to my server while only using the REPL in VScode and the pymakr plugin. Connecting to the WiFi network is not possible for me when I try to do it in the main.py. I just can't connect to the WiFi network.
I'm using an older version of VScode, because with newer versions pymakr doesn't work...
VScode: 1.51.1
Pymakr 1.1.18>>> os.uname( (sysname='WiPy', nodename='WiPy', release='1.20.2.r6', version='v1.11-c5a0a97 on 2021-10-28', machine='WiPy with ESP32', pybytes='1.7.1')
The lines I put in the REPL line by line:
import network import time import urequests # https://github.com/micropython/micropython-lib/blob/master/urequests/ wlan = network.WLAN(mode=network.WLAN.STA) wlan.connect('networkname', auth = (wlan.WPA2, 'password'), timeout=5000) wlan.isconnected() print(wlan.ifconfig()) dataRec = """{"Number1":11,"Number2":2021}""" print(dataRec) dataRec = eval(dataRec) res = urequests.post("https://website.com/test.php", json=dataRec) res.close() print(res.status_code) res.status_code - 200
I looked at different examples on the official tutorial and here how to connect to the WiFi network, but the suggestions were not successful for me.
import network import time import urequests # https://github.com/micropython/micropython-lib/blob/master/urequests/ import machine # create WiFi station object try: wlan = network.WLAN(mode=network.WLAN.STA) print("[WiFi] network interface client object created") except: print("[WiFi] network interface client object (except). Reset board ...") machine.reset() try: wlan.connect('networkname', auth = (wlan.WPA2, 'password')) print('Connecting to: ' + 'networkname') while not wlan.isconnected(): time.sleep_ms(100) print('#',end='') print('WLAN connection succeeded!') except: print('Could not connect to '+ 'networkname' +', resetting.') machine.reset()
I just gets stuck in the while loop:
See https://docs.pycom.io/smart for details [WiFi] network interface client object created Connecting to: networkname #################################################################
I want to use the WiPy because of the TLS1.2 which comes with it. I don't know what the issue is and if there is another micropython based microcontroller board being shipped with TLS1.2 I'm happy to switch.
Do you have an idea, what the issue is rob? @robert-hh :)
Kind regards
-
@robert-hh okay, makes sense regarding the documentation. thanks for the information
-
@SciWax said in Can connect in REPL but not with main.py:
Does 2 stand for selecting the external antenna?
2 stands for MAN_ANT: taking the value from P12 to select the antenna mode.
-
@robert-hh Unfortunately during my project, I have no access to the access point and can't reboot it.
-
@robert-hh I've checked the value of the output of
wlan.antenna()
It was 2 without touching it
with
wlan.antenna(wlan.INT_ANT)
it becomes 0
>>> wlan.antenna() 0
If I upload the code with setting the wlan.antenna to internal nothing changes really:
while not wlan.isconnected()
The code only works every second time, when I reset the board. During the first time attempt, where I just power it (Connecting the expansion board with the usb port), it gets stuck in the while loop, second time when using the reset button the WiPy gets the Wifi connection immediately. When I continue using the reset button again, it is getting stuck again, using it again, it connects immediately. Hence every second time it works. It is a really weird behaviour.
I don't know, if not being aware of the board starting with wlan.antenna() being 2 did something funny with the hardware. Does 2 stand for selecting the external antenna? Maybe that broke the WiPy a little, because this behaviour is so strange. It can't be my code I think.
When I have access to my other expansion boards, I will try another one and a fresh WiPy with setting the wlan.antenna to internal. Then Ive checked everything on a hardware level.
-
@SciWax If I have WiFi problems it usually also helps to reboot the Access Point.
-
@SciWax You can either put it to a definitive state or use the
antanna=WLAN.INT_ANT
option in the WLAN constructor. You can as well callwlan.antenna()
to see the actual value or callwlan.antenna(WLAN.INT_ANT)
to set it to internal antenna.
-
@robert-hh No, I didn't touch the Pin. I was thinking about it too, but all the tutorials talk about how you only need to touch this Pin, if you want to use an external antenna. Should I pull P12 low just in case? I'm using the internal Antenna.
-
@SciWax That is still strange. What is the level of P12, the antenna selection pin? Did you change something with it? The default is MAN_ANT, which is the level you force P12 to be. See https://docs.pycom.io/firmwareapi/pycom/network/wlan/
-
@robert-hh Thank you. Good to know. I can work around the issue I have with the WiPys, because I just use them as a board to easily send data to my server. I just connect them to my other microcontroller board.
-
@SciWax About TLS: See https://docs.micropython.org/en/latest/library/ssl.html
-
@robert-hh Yeah, I've looked in the forums and tried many examples from here. Also this one. I have to check, if "sleeps" also help here in your example code. I'm using the pycom expansion board and connect it to my notebook. I also tried two different WiPys and behaviour was the same.
-
@SciWax There was some discussion in the MicroPython site that for TLS you might have to add your own certificates. Should be in the documentation. Different to Pycom, MicroPython.org does not run any web services.
-
@robert-hh Thank you for the advise and fast answer. I really appreciate it. What I really liked about Pycom and their boards was the SSL/TLS support, so I could use it to talk with TLS secured websites out of the box.
So if I would get one ESP32-S3 board and use micropython, I could immediately send a post request to a https website using TLS for web security reasons? I do not need to add a ssl certificate to an ESP32-S3 board myself, right? It will just work out of the box like for the pycom boards?
Kind regards
-
@SciWax About Another board with TLS. The mainstream MicroPython supports ESP32 boards and TLS 1.2. The Generic ESP32 firmware with or without SPIRAM support should work on WiPy boards. Only the "Pxx" pin names are not supported.
-
That is strange. Your code is similar to what I use regularly.
def do_connect(): import network import time wlan = network.WLAN(mode=network.WLAN.STA) # create station interface if not wlan.isconnected(): # check if the station is connected to an AP wlan.connect(ssid=WIFI_SSID, auth=(network.WLAN.WPA2, WIFI_PASSWD)) for _ in range(100): if wlan.isconnected(): # check if the station is connected to an AP break print('.', end='') time.sleep_ms(200) else: print("Connect attempt timed out\n") return print('\nnetwork config:', wlan.ifconfig()) do_connect()
-
It seems like putting more "sleeps" in the code helped:
# create WiFi station object try: wlan = network.WLAN(mode=network.WLAN.STA) time.sleep(1) print("[WiFi] network interface client object created") except: print("[WiFi] network interface client object (except). Reset board ...") machine.reset() try: wlan.connect('networkname', auth = (wlan.WPA2, 'password')) time.sleep(1) print('Connecting to: ' + 'networkname') while not wlan.isconnected(): time.sleep_ms(200) print('#',end='') print('WLAN connection succeeded!') except: print('Could not connect to '+ 'networkname' +', resetting.') machine.reset()
Still, it doesn't always work. When I reset the WiPy with the Reset-Button, sometimes there is an immediate connection and sometimes not.