Files
jupyter_micropython_kernel/micropythondemo.ipynb
2019-01-17 16:29:54 +00:00

528 lines
12 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No serial connected\n",
" %serialconnect to connect\n",
" %lsmagic to list commands"
]
}
],
"source": [
"asdasd"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Closing old serial Serial<id=0x7fb45c4e72e8, open=True>(port='/dev/ttyUSB0', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=0.5, xonxoff=False, rtscts=False, dsrdtr=False)\n",
"Connecting to Serial (/dev/ttyUSB1, 115200)\n",
"\n",
" ** Serial connected **\n",
"\n",
"Serial<id=0x7fb45e527320, open=True>(port='/dev/ttyUSB1', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=0.5, xonxoff=False, rtscts=False, dsrdtr=False)\n",
"Attempting to use a port that is not open<class 'serial.serialutil.SerialException'>raw REPL; CTRL-B to exit\n",
">"
]
}
],
"source": [
"%serialconnect"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Traceback (most recent call last):\n",
" File \"<stdin>\", line 1, in <module>\n",
"NameError: name 'i' is not defined\n"
]
}
],
"source": [
"print(i)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"9\n",
"\n",
"\n",
"*** Sending x03\n",
"\n",
"Traceback (most recent call last):\n",
" File \"<stdin>\", line 7, in <module>\n",
" File \"<stdin>\", line 6, in k\n",
"KeyboardInterrupt: \n"
]
}
],
"source": [
"# slow function to test streaming and interrupts\n",
"import time\n",
"def k(n):\n",
" for i in range(n):\n",
" print(9**i)\n",
" time.sleep(2)\n",
"k(19)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"serial.write 1 bytes"
]
}
],
"source": [
"%writebytes b'\\x0a'"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"__main__ http_client_ssl sys urandom\n",
"_boot http_server time ure\n",
"_onewire http_server_ssl uasyncio/__init__ urequests\n",
"_webrepl inisetup uasyncio/core urllib/urequest\n",
"apa102 json ubinascii uselect\n",
"array lwip ucollections usocket\n",
"btree machine uctypes ussl\n",
"builtins math uerrno ustruct\n",
"dht micropython uhashlib utime\n",
"ds18x20 neopixel uheapq utimeq\n",
"errno network uio uzlib\n",
"esp ntptime ujson webrepl\n",
"example_pub_button onewire umqtt/robust webrepl_setup\n",
"example_sub_led os umqtt/simple websocket\n",
"flashbdev port_diag uos websocket_helper\n",
"framebuf select upip\n",
"gc socket upip_utarfile\n",
"http_client ssd1306 upysh\n",
"Plus any modules on the filesystem\n"
]
}
],
"source": [
"help(\"modules\")"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"object <module 'umachine'> is of type module\n",
" __name__ -- umachine\n",
" mem8 -- <8-bit memory>\n",
" mem16 -- <16-bit memory>\n",
" mem32 -- <32-bit memory>\n",
" freq -- <function>\n",
" reset -- <function>\n",
" unique_id -- <function>\n",
" idle -- <function>\n",
" disable_irq -- <function>\n",
" enable_irq -- <function>\n",
" time_pulse_us -- <function>\n",
" Timer -- <class 'Timer'>\n",
" Pin -- <class 'Pin'>\n",
" Signal -- <class 'Signal'>\n",
" TouchPad -- <class 'TouchPad'>\n",
" ADC -- <class 'ADC'>\n",
" DAC -- <class 'DAC'>\n",
" I2C -- <class 'I2C'>\n",
" PWM -- <class 'PWM'>\n",
" SPI -- <class 'SoftSPI'>\n",
" UART -- <class 'UART'>\n"
]
}
],
"source": [
"help(machine)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"*** [Input/output error]\n",
"\n"
]
}
],
"source": [
"%readbytes"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"%serialconnect [/dev/ttyUSB0] [115200]\n",
"%suppressendcode doesn't send x04 or wait to read after sending the cell\n",
" (assists for debugging using %writebytes and %readbytes)\n",
"%writebytes does serial.write() on a string b'binary_stuff' \n",
"%readbytes does serial.read_all()\n",
"%rebootdevice reboots device\n",
"%sendtofile name.py uploads subsequent text to file\n"
]
}
],
"source": [
"%lsmagic"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 154,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import uasyncio"
]
},
{
"cell_type": "code",
"execution_count": 155,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"object <module 'uasyncio'> is of type module\n",
" _socket -- <module 'lwip'>\n",
" StopLoop -- <class 'StopLoop'>\n",
" EventLoop -- <class 'EventLoop'>\n",
" start_server -- <generator>\n",
" IORead -- <class 'IORead'>\n",
" sleep -- <generator>\n",
" IOWriteDone -- <class 'IOWriteDone'>\n",
" ensure_future -- <function ensure_future at 0x3fff29e0>\n",
" set_debug -- <function set_debug at 0x3fff20e0>\n",
" sleep_ms -- <SleepMs object at 3fff29a0>\n",
" IOReadDone -- <class 'IOReadDone'>\n",
" SysCall -- <class 'SysCall'>\n",
" SleepMs -- <class 'SleepMs'>\n",
" core -- <module 'uasyncio.core'>\n",
" coroutine -- <function coroutine at 0x3fff29c0>\n",
" utimeq -- <module 'utimeq'>\n",
" type_gen -- <class 'generator'>\n",
" SysCall1 -- <class 'SysCall1'>\n",
" StreamWriter -- <class 'StreamWriter'>\n",
" select -- <module 'uselect'>\n",
" DEBUG -- 0\n",
" __name__ -- uasyncio\n",
" __path__ -- uasyncio\n",
" uasyncio -- <module 'uasyncio'>\n",
" StreamReader -- <class 'StreamReader'>\n",
" IOWrite -- <class 'IOWrite'>\n",
" log -- None\n",
" get_event_loop -- <function get_event_loop at 0x3fff2840>\n",
" time -- <module 'utime'>\n",
" PollEventLoop -- <class 'PollEventLoop'>\n",
" Task -- <function Task at 0x3ffef580>\n",
" open_connection -- <generator>\n",
" uerrno -- <module 'uerrno'>\n"
]
}
],
"source": [
"help(uasyncio)"
]
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Connect through wifi to 192.168.0.142\n"
]
}
],
"source": [
"# do sudo arp-scan 192.168.0.0/24 | grep 8a\n",
"# where the 8a is from the wifi hotspot name\n",
"# ---> comes up with si.ifconfig()[0]\n",
"ipnumber = si.ifconfig()[0]\n",
"print(\"Connect through wifi to\", ipnumber)\n",
"# now go to the top and make that webserver request again"
]
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"serial.write 1 bytes to /dev/ttyUSB3 at baudrate 115200"
]
}
],
"source": [
"%writebytes b\"\\x02\""
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Traceback (most recent call last):\n",
" File \"<stdin>\", line 1\n",
"SyntaxError: invalid syntax\n"
]
}
],
"source": [
"%disconnect"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%rebootdevice"
]
},
{
"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",
"10\n",
"11\n",
"12\n",
"13\n",
"14\n",
"15\n",
"16\n",
"17\n",
".18\n",
"\n",
"\n",
"*** Sending x03\n",
"\n",
"Traceback (most recent call last):\n",
" File \"<stdin>\", line 11, in <module>\n",
" File \"uasyncio/core.py\", line 64, in run_forever\n",
" File \"uasyncio/__init__.py\", line 58, in wait\n",
"KeyboardInterrupt: \n"
]
}
],
"source": [
"import uasyncio as asyncio\n",
"loop = asyncio.get_event_loop()\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.create_task(bar()) # Schedule ASAP\n",
"loop.run_forever()\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<bound_method>\n",
"19\n",
"20\n",
"21\n",
"22\n",
"23\n",
"24\n",
"25\n",
"26\n",
".27\n",
"28\n",
"29\n",
"30\n",
"31\n",
"32\n",
"33\n",
".34\n",
"35\n",
"36\n",
"37\n",
"38\n",
"39\n",
"40\n",
"41\n",
".42\n",
"43\n",
"44\n",
"45\n",
"46\n",
"47\n",
"48\n",
".49\n",
"50\n",
"51\n",
"52\n",
"53\n",
"54\n",
"\n",
"\n",
"*** Sending x03\n",
"\n",
"Traceback (most recent call last):\n",
" File \"<stdin>\", line 2, in <module>\n",
" File \"uasyncio/core.py\", line 64, in run_forever\n",
" File \"uasyncio/__init__.py\", line 58, in wait\n",
"KeyboardInterrupt: \n"
]
}
],
"source": [
"print(loop.run_forever)\n",
"loop.run_forever()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['__init__', 'remove_reader', 'wait', '__qualname__', 'add_writer', 'remove_writer', 'add_reader', '__module__', 'q', 'poller', 'objmap']\n"
]
}
],
"source": [
"print(dir(loop))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"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": 2
}