{ "cells": [ { "cell_type": "markdown", "id": "acb97724-2696-409c-b7bd-f7d1d7f8453a", "metadata": {}, "source": [ "# Requests\n", "\n", "For downloading web content, Python features the [`requests`](https://docs.python-requests.org/en/master/user/quickstart/) library. MicroPython `urequests` implements a subset of this library. Let's download it." ] }, { "cell_type": "code", "execution_count": 7, "id": "aad16dc5-e4a7-4a99-a894-61c34451db44", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[0m\u001b[0m\u001b[46m\u001b[30mConnected to esp32 @ serial:///dev/ttyUSB0\u001b[0m\n", "\u001b[0mInstalling micropython-urequests 0.6 from https://micropython.org/pi/urequests/urequests-0.6.tar.gz\n", "\u001b[0m\u001b[34mUPDATE /lib/urequests.py\n", "\u001b[0m\u001b[0m" ] } ], "source": [ "%connect esp32\n", "%upip -t internet/code/lib install urequests\n", "%rsync" ] }, { "cell_type": "markdown", "id": "f9762039-b02a-476b-b76b-7106507270b4", "metadata": {}, "source": [ "Fetch a sample webpage:" ] }, { "cell_type": "code", "execution_count": 8, "id": "628875b6-515b-484a-9848-1001ca5c2db5", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "tags": [ "hide-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[0m\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "\n", " MicroPython - Python for microcontrollers\n", "\n", " \n", "\n", " \n", "\n", " \n", " \n", " \n", "\n", "\n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
\n", " \n", "\n", "\n", "
\n", "\n", "
\n", " \n", "\n", "
\n", "
\n", "
\n", "

MicroPython

\n", "

\n", " MicroPython is a lean and efficient implementation of the\n", " Python 3 programming language\n", " that includes a small subset of the Python standard library and is\n", " optimised to run on microcontrollers and in constrained environments.\n", "

\n", "

\n", " The MicroPython pyboard is a compact electronic\n", " circuit board that runs MicroPython on the bare metal, giving you a low-level\n", " Python operating system that can be used to control all kinds of electronic\n", " projects.\n", "

\n", "

\n", " MicroPython is packed full of advanced features such as an interactive\n", " prompt, arbitrary precision integers, closures, list comprehension,\n", " generators, exception handling and more. Yet it is compact enough to fit and\n", " run within just 256k of code space and 16k of RAM.\n", "

\n", "

\n", " MicroPython aims to be as compatible with normal Python as possible to\n", " allow you to transfer code with ease from the desktop to a microcontroller\n", " or embedded system.\n", "

\n", "
\n", "
\n", "\n", "
\n", "
\n", " \n", " TEST DRIVE A PYBOARD\n", " \n", " \n", " BUY A PYBOARD\n", " \n", " \n", " USE MICROPYTHON ONLINE\n", " \n", "
\n", "
\n", "
\n", "\n", "
\n", "
\n", "

Proper Python with hardware-specific modules

\n", "
\n", "
\n", "

\n", " MicroPython is a full Python compiler and runtime that runs on the bare-metal.\n", " You get an interactive prompt (the REPL) to execute commands immediately,\n", " along with the ability to run and import scripts from the built-in filesystem.\n", " The REPL has history, tab completion, auto-indent and paste mode for a great\n", " user experience.\n", "

\n", "

\n", " MicroPython strives to be as compatible as possible with normal Python (known\n", " as CPython) so that if you know Python you already know MicroPython. On the\n", " other hand, the more you learn about MicroPython the better you become at Python.\n", "

\n", "

\n", " In addition to implementing a selection of core Python libraries, MicroPython\n", " includes modules such as "machine" for accessing low-level hardware.\n", "

\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "\n", "
\n", "
\n", "

The pyboard

\n", "
\n", "
\n", " \n", " \"pyboard\n", " \n", "
\n", "
\n", " The pyboard is the official MicroPython microcontroller board with\n", " full support for software features. The hardware has:\n", "
    \n", "
  • STM32F405RG microcontroller
  • \n", "
  • 168 MHz Cortex M4 CPU with hardware floating point
  • \n", "
  • 1024KiB flash ROM and 192KiB RAM
  • \n", "
  • Micro USB connector for power and serial communication
  • \n", "
  • Micro SD card slot, supporting standard and high capacity SD cards
  • \n", "
  • 3-axis accelerometer (MMA7660)
  • \n", "
  • Real time clock with optional battery backup
  • \n", "
  • 24 GPIO on left and right edges and 5 GPIO on bottom row, plus LED and switch GPIO available on bottom row
  • \n", "
  • 3x 12-bit analog to digital converters, available on 16 pins, 4 with analog ground shielding
  • \n", "
  • 2x 12-bit digital to analog (DAC) converters, available on pins X5 and X6
  • \n", "
  • 4 LEDs (red, green, yellow and blue)
  • \n", "
  • 1 reset and 1 user switch
  • \n", "
  • On-board 3.3V LDO voltage regulator, capable of supplying up to 250mA, input voltage range 3.6V to 16V
  • \n", "
  • DFU bootloader in ROM for easy upgrading of firmware
  • \n", "
\n", " \n", "
\n", " \n", "
\n", " \n", " \"pyboard\n", " \n", "
\n", "
\n", "\n", "
\n", "
\n", "

Watch MicroPython in action

\n", "
\n", "
\n", " \n", "
\n", "
\n", "\n", "
\n", "
\n", "

Completely free, open source software

\n", "
\n", "
\n", " \"MicroPython\n", "
\n", "
\n", "

\n", " MicroPython is written in C99 and the entire MicroPython core is\n", " available for general use under the very liberal\n", " MIT license.\n", " Most libraries and extension modules (some of which are from a third party) are\n", " also available under MIT or similar licenses.\n", "

\n", "

\n", " You can freely use and adapt MicroPython for personal use, in education, and\n", " in commercial products.\n", "

\n", "

\n", " MicroPython is developed in the open on GitHub and the source code is available\n", " at the GitHub page,\n", " and on the download page.\n", " Everyone is welcome to contribute to the project.\n", "

\n", "
\n", "
\n", "\n", "
\n", "
\n", "

Code: state-of-the-art and highly robust

\n", "
\n", "
\n", "

\n", " MicroPython employs many advanced coding techniques, and lots of tricks\n", " to maintain a compact size while still having a full set of features.\n", "

\n", "

\n", " Some of the more notable items are:\n", "

    \n", "
  • highly configurable due to many compile-time configuration options
  • \n", "
  • support for many architectures (x86, x86-64, ARM, ARM Thumb, Xtensa)
  • \n", "
  • extensive test suite with over 590 tests, and more than 18,500 individual testcases
  • \n", "
  • code coverage at 98.4% for the core and at 96.3% for the core plus extended modules
  • \n", "
  • fast start-up time from boot to loading of first script (150 microseconds to get to boot.py, on PYBv1.1 running at 168MHz)
  • \n", "
  • a simple, fast and robust mark-sweep garbage collector for heap memory
  • \n", "
  • a MemoryError exception is raised if the heap is exhausted
  • \n", "
  • a RuntimeError exception is raised if the stack limit is reached
  • \n", "
  • support for running Python code on a hard interrupt with minimal latency
  • \n", "
  • errors have a backtrace and report the line number of the source code
  • \n", "
  • constant folding in the parser/compiler
  • \n", "
  • pointer tagging to fit small integers, strings and objects in a machine word
  • \n", "
  • transparent transition from small integers to big integers
  • \n", "
  • support for 64-bit NaN boxing object model
  • \n", "
  • support for 30-bit stuffed floats, which don't require heap memory
  • \n", "
  • a cross-compiler and frozen bytecode, to have pre-compiled scripts that don't take any RAM (except for any dynamic objects they create)
  • \n", "
  • multithreading via the "_thread" module, with an optional global-interpreter-lock (still work in progress, only available on selected ports)
  • \n", "
  • a native emitter that targets machine code directly rather than the bytecode virtual machine
  • \n", "
  • inline assembler (currently Thumb and Xtensa instruction sets only)
  • \n", "
\n", "

\n", "
\n", "
\n", "\n", "
\n", "
\n", "

Online resources

\n", "
\n", "
\n", " You can learn more about MicroPython and keep up-to-date with developments\n", " via the following resources:\n", " \n", "
\n", "
\n", "\n", "
\n", "
\n", " \n", " \"pyboards\n", " \n", "
\n", "
\n", " Take me to the store!\n", "
\n", "
\n", "\n", "\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\u001b[0m" ] } ], "source": [ "import urequests as requests\n", "\n", "res = requests.get('https://micropython.org')\n", "print(res.text)\n", "\n", "# recycle the memory when downloading large pages\n", "res = None" ] }, { "cell_type": "markdown", "id": "08958fb9-28b1-4baa-ae77-1a56567b0564", "metadata": {}, "source": [ "The output is rather verbose, with many links to images, etc. Great for humans, not so useful for computers.\n", "\n", "Fortunately many sites offer special APIs with data formatted for computers. As an example, let's fetch weather data from https://openweathermap.org/. You'll need to first sign in and get an \"API Key\". I've added mine to `secrets.py`. \n", "\n", "Then consult the API (tab at the top of the page) for the correct formatting of the request. For example, to get current weather data, we send the following request:" ] }, { "cell_type": "code", "execution_count": 8, "id": "c08d9e14-b065-46a3-8fd9-07ce925edfde", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[0m{\"coord\":{\"lon\":-115.8071,\"lat\":46.0963},\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"clear sky\",\"icon\":\"01d\"}],\"base\":\"stations\",\"main\":{\"temp\":303.62,\"feels_like\":301.65,\"temp_min\":302.21,\"temp_max\":305.08,\"pressure\":1013,\"humidity\":13,\"sea_level\":1013,\"grnd_level\":905},\"visibility\":10000,\"wind\":{\"speed\":1.21,\"deg\":4,\"gust\":1.67},\"clouds\":{\"all\":0},\"dt\":1627179321,\"sys\":{\"type\":2,\"id\":2039021,\"country\":\"US\",\"sunrise\":1627128986,\"sunset\":1627183364},\"timezone\":-25200,\"id\":5599665,\"name\":\"Tahoe\",\"cod\":200}\n", "\u001b[0m" ] } ], "source": [ "import urequests as requests\n", "import secrets\n", "\n", "url = 'https://api.openweathermap.org/data/2.5/weather?q={},{},{}&appid={}'\n", "\n", "res = requests.get(url.format(\n", " 'Tahoe',\n", " 'CA',\n", " 'US',\n", " secrets.openweathermap_apiid\n", "))\n", "\n", "print(res.text)" ] }, { "cell_type": "markdown", "id": "4bb6477b-ee04-4bf0-a2ee-a11b59b79828", "metadata": {}, "source": [ "The response is formatted in [json](https://www.json.org/json-en.html) and can easily be parsed with Python. In fact, json decoding is built right into the requests libray. Let's read and format the output:" ] }, { "cell_type": "code", "execution_count": 8, "id": "dc3597e5-4439-4708-a225-6ffc7cdee264", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[0mtimezone -25200\n", "\u001b[0mcod 200\n", "dt 1627179321\n", "base stations\n", "\u001b[0mweather [{'id': 800, 'icon': '01d', 'main': 'Clear', 'description': \n", "sys {'country': 'US', 'sunrise': 1627128986, 'sunset': 162718336\n", "name Tahoe\n", "clouds {'all': 0}\n", "coord {'lon': -115.8071, 'lat': 46.0963}\n", "visibility 10000\n", "wind {'gust': 1.67, 'speed': 1.21, 'deg': 4}\n", "id 5599665\n", "\u001b[0mmain {'feels_like': 301.65, 'pressure': 1013, 'temp_max': 305.08,\n", "\u001b[0m" ] } ], "source": [ "j = res.json()\n", "for k,v in j.items():\n", " # format and truncate long lines ...\n", " print(\"{:10} {}\".format(k, str(v)[:60]))" ] }, { "cell_type": "markdown", "id": "b93dcdd5-04ae-463e-9c7d-15c0a6b2eb59", "metadata": {}, "source": [ "The weather information is stored in `main`. Let's display just that:" ] }, { "cell_type": "code", "execution_count": 8, "id": "8b68d017-10ad-428c-991a-374276f71ce3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[0mWeather in Tahoe\n", "\u001b[0m feels_like 301.65\n", " pressure 1013\n", " temp_max 305.08\n", " temp 303.62\n", "\u001b[0m temp_min 302.21\n", " humidity 13\n", " sea_level 1013\n", " grnd_level 905\n", "\u001b[0m" ] } ], "source": [ "print(\"Weather in {}\".format(j['name']))\n", "for k,v in j['main'].items():\n", " print(\" {:10} {}\".format(k, v))" ] }, { "cell_type": "markdown", "id": "b8840b42-ef16-41b5-bfa4-205d7ad864fe", "metadata": {}, "source": [ "Many websites offer APIs for communicating with computers. For example, [IFTTT](https://ifttt.com/) is a site organized entirely around offering services for computer automation. \n", "\n", "Cloud service providers, like [IBM](https://www.ibm.com/cloud), [Microsoft Azure](https://azure.microsoft.com/), [Google Cloud](https://cloud.google.com/), [Amazon AWS](https://aws.amazon.com/) and many more offer web APIs. \n", "\n", "In fact, most websites and apps are backed by one of these services. Typically IoT devices, like ESP32's or Raspberry PIs, send data to the cloud service via one of these APIs. The data are analyzed and stored in the cloud and, if appropriate, made available as web pages for human viewing." ] }, { "cell_type": "code", "execution_count": null, "id": "978eb12d-7e25-4a73-b635-226dfdc7f596", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "IoT", "language": "python", "name": "iot_kernel" }, "language_info": { "codemirror_mode": { "name": "python", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "pygments_lexer": "python3", "version": "3" } }, "nbformat": 4, "nbformat_minor": 5 }