Skip to content

Commit

Permalink
Merge pull request #26 from kevinkk525/improvement
Browse files Browse the repository at this point in the history
added timeout option, small client change
  • Loading branch information
peterhinch committed Feb 19, 2019
2 parents 0b83133 + d35e6c7 commit 5afa7b1
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 29 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ SERVER = '192.168.0.41' # Server IP address.
SSID = 'my_ssid'
PW = 'WiFi_password'
PORT = 8123
TIMEOUT = 2000
```

The ESP8266 can store WiFi credentials in flash memory. If desired, ESP8266
Expand Down Expand Up @@ -361,7 +362,8 @@ class App:
def __init__(self, loop, verbose):
self.cl = client.Client(loop, local.MY_ID, local.SERVER,
local.PORT, local.SSID, local.PW,
conn_cb=self.state, verbose=verbose)
local.TIMEOUT, conn_cb=self.state,
verbose=verbose)
loop.create_task(self.start(loop))

async def start(self, loop):
Expand Down
19 changes: 8 additions & 11 deletions client.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,6 @@ async def _run(self, loop):
if e.args[0] in (errno.ECONNABORTED, errno.ECONNRESET, errno.ECONNREFUSED):
if init:
await self.bad_server()
self._sock.close()
await asyncio.sleep(1) # prevents spamming "WIFI OK" if verbose and server down
continue # possibly temporary server outage as not first connect
else:
self._sock.setblocking(False)
# Start reading before server can send: can't send until it
Expand Down Expand Up @@ -293,14 +290,14 @@ async def _run(self, loop):
if self._concb is not None:
# apps might need to know if they lost connection to the server
launch(self._concb, False, *self._concbargs)
finally:
init = False
self._close() # Close socket but not wdt
s.disconnect()
self._feed(0)
await asyncio.sleep_ms(self._to * 2) # Ensure server detects outage
while s.isconnected():
await asyncio.sleep(1)
finally:
init = False
self._close() # Close socket but not wdt
s.disconnect()
self._feed(0)
await asyncio.sleep_ms(self._to * 2) # Ensure server detects outage
while s.isconnected():
await asyncio.sleep(1)

async def _reader(self): # Entry point is after a (re) connect.
c = self.connects # Count successful connects
Expand Down
3 changes: 2 additions & 1 deletion examples/c_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class App(client.Client):
def __init__(self, loop, verbose):
self.verbose = verbose
self.cl = client.Client(loop, local.MY_ID, local.SERVER, local.PORT, local.SSID, local.PW,
conn_cb=self.constate, verbose=verbose, led=led, wdog=False)
local.TIMEOUT, conn_cb=self.constate, verbose=verbose,
led=led, wdog=False)
loop.create_task(self.start(loop))

async def start(self, loop):
Expand Down
1 change: 1 addition & 0 deletions examples/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
SSID = 'MY_WIFI_SSID'
PW = 'PASSWORD'
PORT = 8123
TIMEOUT = 2000
4 changes: 2 additions & 2 deletions examples/s_app_cp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import ujson as json

from micropython_iot import server
from .local import PORT
from .local import PORT, TIMEOUT


class App:
Expand Down Expand Up @@ -64,7 +64,7 @@ def run():
clients = {'1', '2', '3', '4'}
apps = [App(loop, n) for n in clients] # Accept 4 clients with ID's 1-4
try:
loop.run_until_complete(server.run(loop, clients, True, port=PORT))
loop.run_until_complete(server.run(loop, clients, True, port=PORT, timeout=TIMEOUT))
except KeyboardInterrupt:
print('Interrupted')
finally:
Expand Down
2 changes: 1 addition & 1 deletion pb_link/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Use empty string ('') in SSID and PW to only connect to the WLAN which the
# ESP8266 already "knows".
# If Port or Timeout are changed, these values must be passed to server.run()
config = [8123, '192.168.0.42', 1500, 10, 'MY_WIFI_SSID', 'PASSWORD']
config = [8123, '192.168.0.42', 2000, 10, 'MY_WIFI_SSID', 'PASSWORD']

try:
from pyb import I2C # Only pyb supports slave mode
Expand Down
3 changes: 2 additions & 1 deletion pb_link/s_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from micropython_iot import server

PORT = 8123
TIMEOUT = 2000


class App:
Expand Down Expand Up @@ -65,7 +66,7 @@ def run():
clients = {'1', '2', '3', '4'}
apps = [App(loop, n) for n in clients] # Accept 4 clients with ID's 1-4
try:
loop.run_until_complete(server.run(loop, clients, True, port=PORT))
loop.run_until_complete(server.run(loop, clients, True, port=PORT, timeout=TIMEOUT))
except KeyboardInterrupt:
print('Interrupted')
finally:
Expand Down
2 changes: 1 addition & 1 deletion qos/c_qos.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, loop, verbose):
led = Pin(2, Pin.OUT, value=1) # Optional LED
self.cl = client.Client(loop, local.MY_ID, local.SERVER,
local.PORT, local.SSID, local.PW,
verbose=verbose, led=led)
local.TIMEOUT, verbose=verbose, led=led)
self.tx_msg_id = 0
self.dupes = 0 # Incoming dupe count
self.missing = 0
Expand Down
2 changes: 1 addition & 1 deletion qos/c_qos_fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self, loop, verbose):
self.verbose = verbose
self.cl = client.Client(loop, local.MY_ID, local.SERVER,
local.PORT, local.SSID, local.PW,
verbose=verbose, led=led)
local.TIMEOUT, verbose=verbose, led=led)
self.tx_msg_id = 0
self.dupes = 0 # Incoming dupe count
self.missing = 0
Expand Down
1 change: 1 addition & 0 deletions qos/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
SSID = 'MY_WIFI_SSID'
PW = 'PASSWORD'
PORT = 8123
TIMEOUT = 2000
4 changes: 2 additions & 2 deletions qos/s_qos_cp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
except ImportError:
import ujson as json
from micropython_iot import server
from .local import PORT
from .local import PORT, TIMEOUT


class App:
Expand Down Expand Up @@ -80,7 +80,7 @@ def run():
loop = asyncio.get_event_loop()
app = App(loop, 'qos')
try:
loop.run_until_complete(server.run(loop, {'qos'}, True, port=PORT))
loop.run_until_complete(server.run(loop, {'qos'}, True, port=PORT, timeout=TIMEOUT))
except KeyboardInterrupt:
print('Interrupted')
finally:
Expand Down
9 changes: 5 additions & 4 deletions qos/s_qos_fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
except ImportError:
import ujson as json
from micropython_iot import server
from .local import PORT
from .local import PORT, TIMEOUT


class App:
def __init__(self, loop, client_id):
Expand Down Expand Up @@ -73,8 +74,8 @@ async def writer(self, loop):
self.tx_msg_id += 1
count += 1
await self.conn # Only launch write if link is up
#while not self.conn():
#await asyncio.sleep(0.05)
# while not self.conn():
# await asyncio.sleep(0.05)
print('Sent {} to remote {}\n'.format(data, self.client_id))
loop.create_task(self.conn.write(json.dumps(data), wait=False))
await asyncio.sleep(3.95)
Expand All @@ -84,7 +85,7 @@ def run():
loop = asyncio.get_event_loop()
app = App(loop, 'qos')
try:
loop.run_until_complete(server.run(loop, {'qos'}, True, port=PORT))
loop.run_until_complete(server.run(loop, {'qos'}, True, port=PORT, timeout=TIMEOUT))
except KeyboardInterrupt:
print('Interrupted')
finally:
Expand Down
3 changes: 2 additions & 1 deletion remote/c_comms_rx.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def __init__(self, loop, verbose):
self.verbose = verbose
self.led = Pin(2, Pin.OUT, value=1) # LED for received data
self.cl = client.Client(loop, 'rx', local.SERVER, local.PORT,
local.SSID, local.PW, verbose=verbose)
local.SSID, local.PW, local.TIMEOUT,
verbose=verbose)
loop.create_task(self.start(loop))

async def start(self, loop):
Expand Down
3 changes: 2 additions & 1 deletion remote/c_comms_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def __init__(self, loop, verbose):
self.switch.open_func(lambda: self.must_send.set())
self.must_send = Event()
self.cl = client.Client(loop, 'tx', local.SERVER, local.PORT,
local.SSID, local.PW, verbose=verbose, led=led)
local.SSID, local.PW, timeout=local.TIMEOUT,
verbose=verbose, led=led)
loop.create_task(self.start(loop))

async def start(self, loop):
Expand Down
1 change: 1 addition & 0 deletions remote/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
SSID = 'MY_WIFI_SSID'
PW = 'PASSWORD'
PORT = 8123
TIMEOUT = 2000
4 changes: 2 additions & 2 deletions remote/s_comms_cp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from micropython_iot import server

from .local import PORT
from .local import PORT, TIMEOUT


class App:
Expand Down Expand Up @@ -73,7 +73,7 @@ def run():
loop = asyncio.get_event_loop()
apps = [App(loop, name) for name in clients] # Accept 2 clients
try:
loop.run_until_complete(server.run(loop, clients, False, port=PORT))
loop.run_until_complete(server.run(loop, clients, False, port=PORT, timeout=TIMEOUT))
except KeyboardInterrupt:
print('Interrupted')
finally:
Expand Down

0 comments on commit 5afa7b1

Please sign in to comment.