I'm using ESP8266-01 with micropython (1.9.3). My code creates a Poller object . The Poller is created on a socket from which I'm trying to poll for new connections.
The problem is that the result of poll() is not clear.
Following is the snippet of interest:
# Create server socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
# TODO : If both the wifi and sta are operating simultaneously, then bind only to WiFi
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(1)
print("Just before registering poller")
poller = select.poll()
poller.register(s, select.POLLIN)
# Get the current time since epoch
startTimeEpoch = utime.time()
while True:
print("Just before polling")
all_events = poller.poll(200) # time in milliseconds
print("Just after polling!")
print(all_events)
read_avl = 0
for (obj, all_event) in all_events:
read_avl = 1
if read_avl == 1:
print("Just before TRY!")
try:
... # Recv code, etc
And this is the response:
No file wifi.conf found!
Starting web server
#7 ets_task(4020edc0, 29, 3fff95f0, 10)
Just before registering poller
Just before polling
Just after polling!
[]
Traceback (most recent call last):
File "<stdin>", line 21, in <module>
File "<stdin>", line 4, in start_web_server
File "<stdin>", line 128, in web_server
TypeError: function takes 1 positional arguments but 0 were given
>>>
As could be seen, when there's nothing connecting to the socket, poller.poll(200) returns something which when printed prints an empty array []. I've tried also the following, but to no avail:
print(all_events)
if all_events:
print("Just before TRY!")
try:
... # Recv code, etc
What would be the correct way to write the above code, so that it works on ESP8266 with MicroPython?
Also, I tried to rewrite the code using poller.ipoll(...) as so:
while True:
print("Just before polling")
# all_events = poller.poll(200) # time in milliseconds
all_events = poller.ipoll(200)
print("Just after polling!")
print(all_events)
read_avl = 0
# for (obj, all_event) in all_events:
for f, ev, data in all_events:
read_avl = 1
if read_avl == 1:
print("Just before TRY!")
try:
... recv, etc goes here..
This gives the following:
No file wifi.conf found!
Starting web server
#7 ets_task(4020edc0, 29, 3fff95f0, 10)
Just before registering poller
Just before polling
Just after polling!
<poll>
Traceback (most recent call last):
File "<stdin>", line 21, in <module>
File "<stdin>", line 4, in start_web_server
File "<stdin>", line 128, in web_server
TypeError: function takes 1 positional arguments but 0 were given
>>>
Again, the return object could not be assigned correctly in
for f, ev, data in all_events:
How to restructure/rewrite code to work with poll() or ipoll()
What does micropython Poller.poll() return [ESP8266]
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: What does micropython Poller.poll() return [ESP8266]
You can see some example code here.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.