Skip to content

Commit

Permalink
asyncio examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sasilva1998 committed Nov 25, 2019
1 parent d742110 commit f237ae7
Show file tree
Hide file tree
Showing 2 changed files with 293 additions and 0 deletions.
5 changes: 5 additions & 0 deletions asyncio/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Asyncio Examples

Here you will find some Jupyter Notebooks where almost every `asyncio` oriented method is tested.

Based on: [MicroPython Asyncio](https://github.com/peterhinch/micropython-async/blob/master/TUTORIAL.md)
288 changes: 288 additions & 0 deletions asyncio/asyncio_pruebas.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pruebas de asyncio\n",
"\n",
"Tutorial de asyncio seguido del siguiente link: https://github.com/peterhinch/micropython-async/blob/master/TUTORIAL.md\n",
"Para instalar asyncio se debe correr los siguientes codigos en el micro, pero primero es necesario conectarse a internet."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[34mConnecting to --port=/dev/ttyUSB1 --baud=115200 \u001b[0m\n",
"\u001b[34mReady.\n",
"\u001b[0m"
]
}
],
"source": [
"%serialconnect --port=/dev/ttyUSB1"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import network\n",
"sta_if = network.WLAN(network.STA_IF)\n",
"if not sta_if.isconnected():\n",
" sta_if.active(True)\n",
" sta_if.connect('Cc2.4', 'cortocircuito123')\n",
" while not sta_if.isconnected():\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ya conectados a internet, corremos lo siguiente para instalar las librerias."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Installing to: /lib/\n",
".Warning: micropython.org SSL certificate is not validated\n",
"Installing micropython-uasyncio 2.0 from https://micropython.org/pi/uasyncio/uasyncio-2.0.tar.gz\n",
".Installing micropython-uasyncio.core 2.0 from https://micropython.org/pi/uasyncio.core/uasyncio.core-2.0.tar.gz\n",
"Installing to: /lib/\n",
".Installing micropython-uasyncio.synchro 0.1.1 from https://micropython.org/pi/uasyncio.synchro/uasyncio.synchro-0.1.1.tar.gz\n",
".Installing micropython-uasyncio.core 2.0 from https://micropython.org/pi/uasyncio.core/uasyncio.core-2.0.tar.gz\n",
"Installing to: /lib/\n",
"Installing micropython-uasyncio.queues 0.1.2 from https://micropython.org/pi/uasyncio.queues/uasyncio.queues-0.1.2.tar.gz\n",
".Installing micropython-collections.deque 0.1.3 from https://micropython.org/pi/collections.deque/collections.deque-0.1.3.tar.gz\n",
".Installing micropython-uasyncio.core 2.0 from https://micropython.org/pi/uasyncio.core/uasyncio.core-2.0.tar.gz\n",
"."
]
}
],
"source": [
"import upip\n",
"upip.install('micropython-uasyncio')\n",
"upip.install('micropython-uasyncio.synchro')\n",
"upip.install('micropython-uasyncio.queues')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"El siguiente ejemplo corre indefinidamente por lo que se lo debe parar interrumpiendo el kernel."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
".4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"\u001b[34m\n",
"\n",
"*** Sending Ctrl-C\n",
"\n",
"\u001b[0m"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Traceback (most recent call last):\n",
" File \"<stdin>\", line 11, in <module>\n",
" File \"/lib/uasyncio/core.py\", line 173, in run_forever\n",
" File \"/lib/uasyncio/__init__.py\", line 69, in wait\n",
"KeyboardInterrupt: \n"
]
}
],
"source": [
"import uasyncio as asyncio\n",
"async def bar():\n",
" count = 0\n",
" while True:\n",
" count += 1\n",
" print(count)\n",
" await asyncio.sleep(1) # Pause 1s\n",
"\n",
"loop = asyncio.get_event_loop()\n",
"loop.create_task(bar()) # Schedule ASAP\n",
"loop.run_forever()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ahora con mas de una cuenta"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"-1\n",
"2\n",
"-2\n",
"3\n",
"-3\n",
"4\n",
"5\n",
"-4\n",
"6\n",
"-5\n",
"7\n",
"8\n",
"-6\n",
"9\n",
"-7\n",
"10\n",
"11\n",
"-8\n",
".12\n",
"-9\n",
"13\n",
"14\n",
"-10\n",
"15\n",
"-11\n",
"16\n",
"17\n",
"-12\n",
"18\n",
"-13\n",
"19\n",
"20\n",
"-14\n",
"21\n",
".-15\n",
"22\n",
"23\n",
"-16\n",
"24\n",
"\u001b[34m\n",
"\n",
"*** Sending Ctrl-C\n",
"\n",
"\u001b[0m"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Traceback (most recent call last):\n",
" File \"<stdin>\", line 21, in <module>\n",
" File \"/lib/uasyncio/core.py\", line 173, in run_forever\n",
" File \"/lib/uasyncio/__init__.py\", line 69, in wait\n",
"KeyboardInterrupt: \n"
]
}
],
"source": [
"import uasyncio as asyncio\n",
"\n",
"\n",
"async def bar():\n",
" count = 0\n",
" while True:\n",
" count += 1\n",
" print(count)\n",
" await asyncio.sleep(1) # Pause 1s\n",
" \n",
"async def foo():\n",
" count = 0\n",
" while True:\n",
" count += -1\n",
" print(count)\n",
" await asyncio.sleep(1.5) # Pause 1s\n",
"\n",
"loop = asyncio.get_event_loop()\n",
"loop.create_task(bar()) # Schedule ASAP\n",
"loop.create_task(foo())\n",
"loop.run_forever()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[34mConnecting to --port=/dev/ttyUSB1 --baud=115200 \u001b[0m\n",
"MicroPython v1.11 on 2019-05-29; ESP32 module with ESP32\n",
"Type \"help()\" for more information.\n",
">>>[reboot detected 0]repl is in normal command mode\n",
"[\\r\\x03\\x03] b'\\r\\n>>> '\n",
"[\\r\\x01] b'\\r\\n>>> \\r\\nraw REPL; CTRL-B to exit\\r\\n>' \u001b[34mReady.\n",
"\u001b[0m"
]
}
],
"source": [
"%serialconnect --port=/dev/ttyUSB1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "MicroPython - USB",
"language": "micropython",
"name": "micropython"
},
"language_info": {
"codemirror_mode": "python",
"file_extension": ".py",
"mimetype": "text/python",
"name": "micropython"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

0 comments on commit f237ae7

Please sign in to comment.