Files
jupyter_micropython_kernel/micropython_webserve.ipynb

940 lines
22 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Connecting to Serial (/dev/ttyUSB1, 115200)\n",
"\n",
" ** Serial connected **\n",
"\n",
"Serial<id=0x7f125c7fe470, open=True>(port='/dev/ttyUSB1', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=0.5, xonxoff=False, rtscts=False, dsrdtr=False)\n",
"\n",
">>> \n",
">>> \n",
">>> \n",
">>> \n",
"raw REPL; CTRL-B to exit\n",
">"
]
}
],
"source": [
"%serialconnect"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"ipnumber = \"192.168.4.1\" # to be rewritten when using network.WLAN\n",
"ipnumber = \"192.168.0.29\"\n",
"ipnumber = \"0.0.0.0\""
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[0;32mI (862703) modsocket: Initializing\u001b[0m\r\n"
]
}
],
"source": [
"# The code necessary for accepting and serving a connection into its hotspot\n",
"# (you have to totally reset device to recover a connection from being in use)\n",
"import socket\n",
"\n",
"s = socket.socket()\n",
"s.bind((ipnumber, 80))\n",
"s.listen(5) # 5 connections"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"s.settimeout(120)\n",
"cl, addr = s.accept() # hangs for 2 minutes or until there is a connection\n",
"# Now connect to the webpage http://192.168.4.1/gggffffggg"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[b'GET /ddddd HTTP/1.1\\r\\n']\n",
"[b'Host: 192.168.4.1\\r\\n']\n",
"[b'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0\\r\\n']\n",
"[b'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\\r\\n']\n",
"[b'Accept-Language: en-GB,en;q=0.5\\r\\n']\n",
"[b'Accept-Encoding: gzip, deflate\\r\\n']\n",
"[b'Connection: keep-alive\\r\\n']\n",
"[b'Upgrade-Insecure-Requests: 1\\r\\n']\n",
"[b'\\r\\n']\n"
]
}
],
"source": [
"fcl = cl.makefile('rwb', 0)\n",
"while True:\n",
" k = fcl.readline()\n",
" print([k])\n",
" if not k.strip():\n",
" break\n"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"cl.sendall(\"HTTP/1.0 200 OK\\r\\n\")\n",
"cl.sendall(\"Content-Type: text/plain\\r\\n\\r\\n\")\n",
"cl.sendall(\"zingggfff\")\n",
"cl.close()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%serialconnect"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Traceback (most recent call last):\n",
" File \"<stdin>\", line 1, in <module>\n",
"NameError: name 'si' is not defined\n"
]
}
],
"source": [
"print(si.scan())\n"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"object <WLAN> is of type WLAN\n",
" active -- <function>\n",
" connect -- <function>\n",
" disconnect -- <function>\n",
" status -- <function>\n",
" scan -- <function>\n",
" isconnected -- <function>\n",
" config -- <function>\n",
" ifconfig -- <function>\n"
]
}
],
"source": [
"help(si)"
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"MicroPython v1.9.2-273-g56f18602 on 2017-09-25; ESP32 module with ESP32\n",
"Type \"help()\" for more information.\n",
">>> \n",
">>> \n",
"PYB: soft reboot\n",
"OSError: [Errno 2] ENOENT\n",
"MicroPython v1.9.2-273-g56f18602 on 2017-09-25; ESP32 module with ESP32\n",
"Type \"help()\" for more information.\n",
">>> \n",
">>> \n",
">>> \n",
">>> \n",
">>> \n",
">>> \n",
"raw REPL; CTRL-B to exit\n",
">"
]
}
],
"source": [
"%rebootdevice"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[0;32mI (21515796) wifi: event 1\u001b[0m\n",
"strongest DoESWifi signal b'DoESBackup'\n",
"Connect through wifi to 192.168.0.29\n"
]
}
],
"source": [
"# If you want to connect through \n",
"import network\n",
"si = network.WLAN(network.STA_IF)\n",
"si.active(True)\n",
"#assert not si.isconnected()\n",
"\n",
"doeswifiname = max((sc for sc in si.scan() if sc[0][:4] == b'DoES'), key=lambda X: X[3])[0] # X[3]=RSSI (received signal strength)\n",
"\n",
"print(\"strongest DoESWifi signal\", doeswifiname)\n",
"si.connect(doeswifiname, \"decafbad00\")\n",
"\n",
"ipnumber = si.ifconfig()[0]\n",
"print(\"Connect through wifi to\", ipnumber)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[0;32mI (1550016) wifi: event 1\u001b[0m\n",
"(b'Section 31', b'\\x00\\x0c\\xf6\\x82\\xa4\\xd4', 13, -60, 3, False)\n",
"(b'IOISIS', b'\\x18\\xd0q\\x0f.\\xea', 6, -63, 3, False)\n",
"(b'PromoTrader', b'\\x18\\xd0q\\x0e\\xff\\x1e', 1, -66, 3, False)\n",
"(b'DoESBackup', b'\\x18\\xd0q\\x0f.\\x12', 6, -72, 3, False)\n",
"(b'DIRECT-FD-HP ENVY 4520 series', b'\\xf40\\xb9\\xb8\\xc5\\xfe', 6, -72, 3, False)\n",
"(b'TNCAPBCA9E3', b'\\xc4\\xea\\x1d\\xbc\\xa9\\xe3', 11, -79, 4, False)\n",
"(b'DIRECT-45-HP OfficeJet 7510', b'dQ\\x06\\xf6H\\xd1', 6, -80, 3, False)\n",
"(b'304', b'\\x18\\xd0q\\n\\xfb\\x9a', 6, -82, 3, False)\n",
"(b'MercantServices', b'\\x18\\xd0q\\n\\xfb\\xd8', 1, -83, 3, False)\n",
"(b'DoESLiverpool', b'\\xd0\\x03K\\xd5u\\x96', 11, -83, 3, False)\n",
"(b'DIRECT-liM2070 Series', b'2\\xcd\\xa7\\xa7\\xe8\\t', 11, -83, 3, False)\n",
"(b'SophieGreen', b'\\x18\\xd0q\\x0f+.', 6, -87, 3, False)\n",
"(b'LiverSketchClub', b'\\x00\\xd0\\xcb\\xf51N', 6, -87, 3, False)\n",
"(b'MerseyEmpleymentLaw', b'\\x18\\xd0q\\x0f-@', 1, -91, 3, False)\n"
]
}
],
"source": [
"for x in si.scan():\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\r\n"
]
}
],
"source": [
"print(si.isconnected())"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"object <WLAN> is of type WLAN\n",
" active -- <function>\n",
" connect -- <function>\n",
" disconnect -- <function>\n",
" status -- <function>\n",
" scan -- <function>\n",
" isconnected -- <function>\n",
" config -- <function>\n",
" ifconfig -- <function>\n",
"b'0\\xae\\xa4\\x80x '\n"
]
}
],
"source": [
"help(si)\n",
"#si.connect(doeswifiname, \"decafbad00\")\n",
"print(si.config(\"mac\"))"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\r\n"
]
}
],
"source": [
"#print(si.config('mac'))\n",
"print(si.isconnected())\n",
"#print(si.connect())\n"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Connect through wifi to 192.168.4.1\r\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": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No serial connected\n",
" %serialconnect to connect\n",
" %lsmagic to list commands"
]
}
],
"source": [
"%lsmagic"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No serial connected\n",
" %serialconnect to connect\n",
" %lsmagic to list commands"
]
}
],
"source": [
"print(si.ifconfig())\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#python -c \"import serial.tools.list_ports;print [port for port in serial.tools.list_ports.comports() if port[2] != 'n/a']\"\n"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import upip"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Installing to: /lib/\n",
".Warning: pypi.python.org SSL certificate is not validated\n",
"Installing picoweb 1.2.2 from https://pypi.python.org/packages/22/be/7e7cabe1ac2607d849557314f8e48f6d9e0c13c64c47029ae250e9020942/picoweb-1.2.2.tar.gz\n",
"Installing micropython-uasyncio 1.2.2 from https://pypi.python.org/packages/40/75/f0f8854733ec02c3ebfd88fc1ad1bbfdcc2101466850b06a4231abf03597/micropython-uasyncio-1.2.2.tar.gz\n",
".Installing micropython-pkg_resources 0.2.1 from https://pypi.python.org/packages/58/87/5acc262e0f642186891ef8d944bd727989000ce8d9263514f73b8feb7fdc/micropython-pkg_resources-0.2.1.tar.gz\n",
"Installing micropython-uasyncio.core 1.5 from https://pypi.python.org/packages/92/57/fdd6a51baa180627e40dfe7e53378c92c4984cd5fb582408ec48d0e19141/micropython-uasyncio.core-1.5.tar.gz\n",
"Installing micropython-logging 0.1.3 from https://pypi.python.org/packages/31/60/604096f5ceed147ac985927072ffd6952c0dd89e0d937a8822ad0c832880/micropython-logging-0.1.3.tar.gz\n"
]
}
],
"source": [
"upip.install(\"picoweb\")"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Installing to: /lib/\n",
"Error installing 'utemplate': 23, packages may be partially installed\n"
]
}
],
"source": [
"upip.install(\"utemplate\")\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Connecting to Serial (/dev/ttyUSB0, 115200)\n",
"\n",
" ** Serial connected **\n",
"\n",
"Serial<id=0x7efd7c8b3eb8, open=True>(port='/dev/ttyUSB0', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=0.5, xonxoff=False, rtscts=False, dsrdtr=False)\n",
"raw REPL; CTRL-B to exit\n",
">"
]
}
],
"source": [
"%serialconnect"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import picoweb"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"app = picoweb.WebApp(__name__)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#help(app)"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 lines sent so far\n",
"20 lines sent so far\n",
"22 lines sent done"
]
}
],
"source": [
"%sendtofile templates/sensor.tpl\n",
"\n",
"{% args sensor %}\n",
"<html>\n",
"<body>\n",
"<table>\n",
" <tr>\n",
" <th>value</th>\n",
" <th>timestamp</th>\n",
" </tr> \n",
" <tr>\n",
" <td>{{sensor['tmpr']}}</td>\n",
" <td>11:00</td>\n",
" </tr>\n",
" <tr>\n",
" <td>{{sensor['hmdty']}}</td>\n",
" <td>09:00</td>\n",
" </tr>\n",
"</table>\n",
"\n",
"</body>\n",
"\n",
"</html>"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['boot.py', 'lib', 'templates']\r\n"
]
}
],
"source": [
"import os\n",
"#os.mkdir(\"templates\")\n",
"print(os.listdir(\"\"))\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<html>\n",
"<body>\n",
"<table>\n",
" <tr>\n",
" <th>value</th>\n",
" <th>timestamp</th>\n",
" </tr> \n",
" <tr>\n",
" <td>60</td>\n",
" <td>11:00</td>\n",
" </tr>\n",
" <tr>\n",
" <td>50</td>\n",
" <td>09:00</td>\n",
" </tr>\n",
"</table>\n",
"\n",
"</body>\n",
"\n",
"</html>\n"
]
}
],
"source": [
"class G:\n",
" def awrite(self, y):\n",
" #print(y)\n",
" return y\n",
"sensor={\"tmpr\":60,\"hmdty\":50}\n",
"resp = G()\n",
"x = app.render_template(resp, \"sensor.tpl\", (sensor,))\n",
"print(\"\".join(x))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on http://192.168.0.29:8081/\n",
"Traceback (most recent call last):\n",
" File \"<stdin>\", line 6, in <module>\n",
" File \"/lib/picoweb/__init__.py\", line 240, in run\n",
" File \"/lib/uasyncio/core.py\", line 124, in run_forever\n",
" File \"/lib/uasyncio/core.py\", line 88, in run_forever\n",
" File \"/lib/uasyncio/__init__.py\", line 222, in start_server\n",
"OSError: [Errno 12] ENOMEM\n"
]
}
],
"source": [
"@app.route(\"/temp\")\n",
"def html(req, resp):\n",
" sensor={\"tmpr\":30,\"hmdty\":33}\n",
" yield from picoweb.start_response(resp, content_type = \"text/html\")\n",
" yield from app.render_template(resp, \"sensor.tpl\", (sensor,))\n",
"app.run(debug=True, host=ipnumber)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on http://sdfsdf:8081/\n",
".\n",
"\n",
"*** Sending x03\n",
"\n"
]
}
],
"source": [
"app.run(debug=True, host=\"sdfsdf\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#see: https://docs.micropython.org/en/latest/esp8266/esp8266/quickref.html?highlight=websocket\n",
" \n",
"import network\n",
"\n",
"ap = network.WLAN(network.AP_IF) # create access-point interface\n",
"ap.active(True) # activate the interface\n",
"ap.config(essid='ESP-AP') # set the ESSID of the access point\n",
"\n",
"#A useful function for connecting to your local WiFi network is:\n",
"\n",
"def do_connect():\n",
" import network\n",
" wlan = network.WLAN(network.STA_IF)\n",
" wlan.active(True)\n",
" if not wlan.isconnected():\n",
" print('connecting to network...')\n",
" wlan.connect('essid', 'password')\n",
" while not wlan.isconnected():\n",
" pass\n",
" print('network config:', wlan.ifconfig())\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 63,
"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": [
"import machine\n",
"help(machine)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"MicroPython v1.9.2-273-g56f18602 on 2017-09-25; ESP32 module with ESP32\n",
"Type \"help()\" for more information.\n",
">>> \n",
">>> \n",
"PYB: soft reboot\n",
"OSError: [Errno 2] ENOENT\n",
"MicroPython v1.9.2-273-g56f18602 on 2017-09-25; ESP32 module with ESP32\n",
"Type \"help()\" for more information.\n",
">>> \n",
">>> \n",
">>> \n",
">>> \n",
">>> \n",
">>> \n",
"raw REPL; CTRL-B to exit\n",
">"
]
}
],
"source": [
"%rebootdevice"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import network\n",
"si = network.WLAN(network.AP_IF)\n"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I (602173) wifi: mode : softAP (30:ae:a4:80:78:21)\n",
"True\n"
]
}
],
"source": [
"si.active(True)\n",
"print(si.active())\n"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[None]\r\n"
]
}
],
"source": [
"print([si.status()])\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"object <WLAN> is of type WLAN\n",
" active -- <function>\n",
" connect -- <function>\n",
" disconnect -- <function>\n",
" status -- <function>\n",
" scan -- <function>\n",
" isconnected -- <function>\n",
" config -- <function>\n",
" ifconfig -- <function>\n"
]
}
],
"source": [
"help(si)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"si = network.WLAN(network.STA_IF)\n",
"si.active(True)\n",
"#assert not si.isconnected()\n",
"\n",
"doeswifiname = max((sc for sc in si.scan() if sc[0][:4] == b'DoES'), key=lambda X: X[3])[0] # X[3]=RSSI (received signal strength)\n",
"\n",
"print(\"strongest DoESWifi signal\", doeswifiname)\n",
"si.connect(doeswifiname, \"decafbad00\")\n",
"\n",
"ipnumber = si.ifconfig()[0]\n",
"print(\"Connect through wifi to\", ipnumber)"
]
}
],
"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
}