Requests#
For downloading web content, Python features the requests
library. MicroPython urequests
implements a subset of this library. Let’s download it.
%connect esp32
%upip -t internet/code/lib install urequests
%rsync
Connected to esp32 @ serial:///dev/ttyUSB0
Installing micropython-urequests 0.6 from https://micropython.org/pi/urequests/urequests-0.6.tar.gz
UPDATE /lib/urequests.py
Fetch a sample webpage:
import urequests as requests
res = requests.get('https://micropython.org')
print(res.text)
# recycle the memory when downloading large pages
res = None
Show code cell output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head -->
<link rel="icon" href="/static/img/favicon.ico">
<title>MicroPython - Python for microcontrollers</title>
<link href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
<link href="/static/css/style.css?v=2" rel="stylesheet">
<link href="/static/css/pygments.css" rel="stylesheet">
<link href="/static/home/css/style.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700,800" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet">
<meta property="og:type" content="website" />
<meta property="og:url" content="http://micropython.org/" />
<meta property="og:title" content="MicroPython - Python for microcontrollers" />
<meta property="og:description" content="MicroPython is a lean and efficient
implementation of the Python 3 programming language
that includes a small subset of the Python standard library and is
optimised to run on microcontrollers and in constrained environments." />
<meta property="og:image" content="http://micropython.org/static/img/Mlogo_138wh.png" />
</head>
<body>
<nav class="navbar navbar-dark" role="navigation">
<div class="main-menu">
<a class="navbar-brand" href='http://micropython.org'>
<span>
<img src ="/static/img/Mlogo_138wh.png" width="50px">
</span>
<span>
MicroPython
</span>
</a>
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="http://forum.micropython.org">FORUM</a>
</li>
<li class="nav-item">
<a class="nav-link" href="http://docs.micropython.org">DOCS</a>
</li>
<li class="nav-item">
<a class="nav-link" href="http://docs.micropython.org/en/latest/pyboard/quickref.html"> QUICK-REF </a>
</li>
<li class="nav-item">
<a class="nav-link" href="/download">DOWNLOAD</a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://store.micropython.org">STORE</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/contact">CONTACT</a>
</li>
</ul>
</div>
</div>
</nav>
<div id="sidebar" style="float:right;margin-right:16px;">
<!--
<a class="twitter-timeline" width="250" height="360" data-dnt="true" href="https://twitter.com/micropython" data-widget-id="518789851701714944">Tweets by @micropython</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
-->
</div>
<div class="container">
<div class="headline">
<div class="row">
<div class="col-md-offset-1 col-md-5 col-xs-9">
<h1 class="huge-title">MicroPython</h1>
<p>
MicroPython is a lean and efficient implementation of the
<a href="http://www.python.org/">Python 3</a> programming language
that includes a small subset of the Python standard library and is
optimised to run on microcontrollers and in constrained environments.
</p>
<p>
The MicroPython <a href="/pyboard">pyboard</a> is a compact electronic
circuit board that runs MicroPython on the bare metal, giving you a low-level
Python operating system that can be used to control all kinds of electronic
projects.
</p>
<p>
MicroPython is packed full of advanced features such as an interactive
prompt, arbitrary precision integers, closures, list comprehension,
generators, exception handling and more. Yet it is compact enough to fit and
run within just 256k of code space and 16k of RAM.
</p>
<p>
MicroPython aims to be as compatible with normal Python as possible to
allow you to transfer code with ease from the desktop to a microcontroller
or embedded system.
</p>
</div>
</div>
<div class="row" style="margin:70px 0;">
<div class="col-md-12 text-center">
<span style="margin: 0 2em;">
<a href="http://micropython.org/live" class="red-button">TEST DRIVE A PYBOARD</a>
</span>
<span style="margin: 0 2em;">
<a href="https://store.micropython.org" class="red-button" style="background-color:#33aa33">BUY A PYBOARD</a>
</span>
<span style="margin: 0 2em;">
<a href="/unicorn" class="red-button">USE MICROPYTHON ONLINE</a>
</span>
</div>
</div>
</div> <!-- headline -->
<div class="row row-white">
<div class="col-md-12 text-center">
<h1> Proper Python with hardware-specific modules </h1>
</div>
<div class="col-md-offset-1 col-md-5">
<p>
MicroPython is a full Python compiler and runtime that runs on the bare-metal.
You get an interactive prompt (the REPL) to execute commands immediately,
along with the ability to run and import scripts from the built-in filesystem.
The REPL has history, tab completion, auto-indent and paste mode for a great
user experience.
</p>
<p>
MicroPython strives to be as compatible as possible with normal Python (known
as CPython) so that if you know Python you already know MicroPython. On the
other hand, the more you learn about MicroPython the better you become at Python.
</p>
<p>
In addition to implementing a selection of core Python libraries, MicroPython
includes modules such as "machine" for accessing low-level hardware.
</p>
</div>
<div class="col-md-5">
<div class="row">
<div id="example-code" class="col-md-12"></div>
<div id="example-prev" class="col-md-offset-3 col-md-1 text-center"> ← </div>
<div id="example-index" class="col-md-4 text-center"> </div>
<div id="example-next" class="col-md-1 text-center"> → </div>
</div>
</div>
</div>
<div class="row row-white">
<div class="col-md-12 text-center">
<h1> The pyboard </h1>
</div>
<div class="col-xs-6 col-xs-offset-3 col-md-offset-1 col-md-4">
<a href="/static/pyboard">
<img src ="/static/home/img/pybv11-persp.jpg" width="360px" class="img-responsive" alt="pyboard v1.1">
</a>
</div>
<div class="col-xs-12 col-md-6">
The pyboard is the official MicroPython microcontroller board with
full support for software features. The hardware has:
<ul>
<li> STM32F405RG microcontroller </li>
<li> 168 MHz Cortex M4 CPU with hardware floating point </li>
<li> 1024KiB flash ROM and 192KiB RAM </li>
<li> Micro USB connector for power and serial communication </li>
<li> Micro SD card slot, supporting standard and high capacity SD cards </li>
<li> 3-axis accelerometer (MMA7660) </li>
<li> Real time clock with optional battery backup </li>
<li> 24 GPIO on left and right edges and 5 GPIO on bottom row, plus LED and switch GPIO available on bottom row </li>
<li> 3x 12-bit analog to digital converters, available on 16 pins, 4 with analog ground shielding </li>
<li> 2x 12-bit digital to analog (DAC) converters, available on pins X5 and X6 </li>
<li> 4 LEDs (red, green, yellow and blue) </li>
<li> 1 reset and 1 user switch </li>
<li> On-board 3.3V LDO voltage regulator, capable of supplying up to 250mA, input voltage range 3.6V to 16V </li>
<li> DFU bootloader in ROM for easy upgrading of firmware </li>
</ul>
<!--
The pyboard is manufactured in the UK by <a href="http://www.jaltek.com">Jaltek Systems</a>.
<a href="http://www.jaltek.com"><img src ="/static/home/img/jaltek.png" width="200px" style="margin-top:5em; margin-left:3em;"></a>
-->
</div>
<div class="col-md-12 text-center" style="margin:50px 0;">
<a href="https://store.micropython.org" class="red-button">Visit the store to order!</a>
</div>
<div class="col-xs-12 col-md-8 col-md-offset-2 text-center">
<a href="http://docs.micropython.org/en/latest/pyboard/quickref.html">
<img src ="/static/home/img/pybv10b-pinout.jpg" width="750px" class="img-responsive" alt="pyboard quick reference">
</a>
</div>
</div>
<div class="row row-black">
<div class="col-md-12 text-center">
<h1> Watch MicroPython in action </h1>
</div>
<div class="col-xs-12 col-md-8 col-md-offset-2 text-center">
<iframe width="780" height="439" src="//www.youtube.com/embed/5LbgyDmRu9s?rel=0" frameborder="0" allowfullscreen style="width:100%;"></iframe>
</div>
</div>
<div class="row row-white">
<div class="col-md-12 text-center">
<h1> Completely free, open source software </h1>
</div>
<div class="col-xs-6 col-xs-offset-3 col-md-offset-2 col-md-4">
<img src ="/static/home/img/opensource.jpg" width="360px" class="img-responsive" alt="MicroPython is open-source">
</div>
<div class="col-xs-12 col-md-4">
<p>
MicroPython is written in C99 and the entire MicroPython core is
available for general use under the very liberal
<a href="https://github.com/micropython/micropython/blob/master/LICENSE">MIT license</a>.
Most libraries and extension modules (some of which are from a third party) are
also available under MIT or similar licenses.
</p>
<p>
You can freely use and adapt MicroPython for personal use, in education, and
in commercial products.
</p>
<p>
MicroPython is developed in the open on GitHub and the source code is available
at <a href="https://github.com/micropython/micropython">the GitHub page</a>,
and on the <a href="/download">download page</a>.
Everyone is welcome to contribute to the project.
</p>
</div>
</div>
<div class="row row-white">
<div class="col-md-12 text-center">
<h1> Code: state-of-the-art and highly robust </h1>
</div>
<div class="col-md-offset-2 col-md-8">
<p>
MicroPython employs many advanced coding techniques, and lots of tricks
to maintain a compact size while still having a full set of features.
</p>
<p>
Some of the more notable items are:
<ul>
<li> highly configurable due to many compile-time configuration options </li>
<li> support for many architectures (x86, x86-64, ARM, ARM Thumb, Xtensa)</li>
<li> extensive <a href="https://github.com/micropython/micropython/tree/master/tests">test suite</a> with over 590 tests, and more than 18,500 individual testcases </li>
<li> code coverage at 98.4% for the core and <a href="https://coveralls.io/github/micropython/micropython">at 96.3%</a> for the core plus extended modules </li>
<li> fast start-up time from boot to loading of first script (150 microseconds to get to boot.py, on PYBv1.1 running at 168MHz) </li>
<li> a simple, fast and robust mark-sweep garbage collector for heap memory </li>
<li> a MemoryError exception is raised if the heap is exhausted </li>
<li> a RuntimeError exception is raised if the stack limit is reached </li>
<li> support for running Python code on a hard interrupt with minimal latency </li>
<li> errors have a backtrace and report the line number of the source code </li>
<li> constant folding in the parser/compiler </li>
<li> <a href="https://github.com/micropython/micropython/blob/master/py/mpconfig.h#L51-L95">pointer tagging</a> to fit small integers, strings and objects in a machine word </li>
<li> transparent transition from small integers to big integers </li>
<li> support for 64-bit NaN boxing object model </li>
<li> support for 30-bit stuffed floats, which don't require heap memory </li>
<li> a cross-compiler and frozen bytecode, to have pre-compiled scripts that don't take any RAM (except for any dynamic objects they create) </li>
<li> multithreading via the "_thread" module, with an optional global-interpreter-lock (still work in progress, only available on selected ports) </li>
<li> a native emitter that targets machine code directly rather than the bytecode virtual machine</li>
<li> inline assembler (currently Thumb and Xtensa instruction sets only) </li>
</ul>
</p>
</div>
</div>
<div class="row row-white">
<div class="col-md-12 text-center">
<h1> Online resources </h1>
</div>
<div class="col-md-offset-2 col-md-8">
You can learn more about MicroPython and keep up-to-date with developments
via the following resources:
<ul>
<li> subscribe to <a href="/newsletter">the newsletter</a> </li>
<li> read <a href="http://docs.micropython.org">the documentation</a> </li>
<li> join the community at <a href="http://forum.micropython.org">the forum</a> </li>
<li> submit bug reports, and follow and join in development <a href="https://github.com/micropython/micropython">on GitHub</a> </li>
</ul>
</div>
</div>
<div class="row row-black">
<div class="col-xs-12 col-md-offset-2 col-md-8">
<a href="https://store.micropython.org/#/products/HOUSING-OB-1">
<img src ="/static/home/img/colourhousings.jpg" width="780px" class="img-responsive" alt="pyboards on parade">
</a>
</div>
<div class="col-md-12 text-center" style="margin-top:30px;">
<a href="https://store.micropython.org" class="red-button">Take me to the store!</a>
</div>
</div>
<div class="row" style="margin:30px 0;">
<div class="col-md-offset-2 col-md-8">
</div>
</div>
</div>
<footer>
<div class="container footercontent">
<div class="row">
<div class="col-xs-12 col-sm-3 col-md-2 col-md-offset-2">
<h3>MicroPython</h3>
<ul>
<li> <a href="http://micropython.org">Home</a> </li>
<li> <a href="http://wiki.micropython.org">Wiki</a> </li>
<li> <a href="http://forum.micropython.org">Forum</a> </li>
<li> <a href="http://www.github.com/micropython">GitHub</a> </li>
</ul>
</div>
<div class="col-xs-12 col-sm-3 col-md-2">
<h3>Resources</h3>
<ul>
<li> <a href="/help">Help!</a> </li>
<li> <a href="http://docs.micropython.org/en/latest/pyboard/tutorial/index.html">Tutorials</a> </li>
<li> <a href="http://docs.micropython.org">Documentation</a> </li>
<li> <a href="/download">Download</a> </li>
</ul>
</div>
<div class="col-xs-12 col-sm-3 col-md-2">
<h3>Contact</h3>
<ul>
<li> <a href="/contact">Contact us</a> </li>
<li> <a href="irc://freenode/micropython">IRC</a> </li>
<li> <a href="http://twitter.com/micropython">Twitter</a> </li>
<li> <a href="http://www.facebook.com/micropython">Facebook</a> </li>
<li> <a href="/privacypolicy">Privacy Policy</a> </li>
<li> <a href="/termsandconditions">Terms & Conditions</a> </li>
</ul>
</div>
<div class="col-xs-12 col-md-8 col-md-offset-2">
<p>A project by <a href="http://dpgeorge.net">Damien George</a></p>
<p>© 2014-2018 George Robotics Limited</p>
</div>
</div>
</div>
</footer>
<script src="/static/js/jquery-2.1.0.min.js"></script>
<script src="/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<script src="/static/home/js/codeexamples.js" type="text/javascript"></script>
<!--
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="site/js/functions.js"></script>
-->
</body>
</html>
The output is rather verbose, with many links to images, etc. Great for humans, not so useful for computers.
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
.
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:
import urequests as requests
import secrets
url = 'https://api.openweathermap.org/data/2.5/weather?q={},{},{}&appid={}'
res = requests.get(url.format(
'Tahoe',
'CA',
'US',
secrets.openweathermap_apiid
))
print(res.text)
{"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}
The response is formatted in json 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:
j = res.json()
for k,v in j.items():
# format and truncate long lines ...
print("{:10} {}".format(k, str(v)[:60]))
timezone -25200
cod 200
dt 1627179321
base stations
weather [{'id': 800, 'icon': '01d', 'main': 'Clear', 'description':
sys {'country': 'US', 'sunrise': 1627128986, 'sunset': 162718336
name Tahoe
clouds {'all': 0}
coord {'lon': -115.8071, 'lat': 46.0963}
visibility 10000
wind {'gust': 1.67, 'speed': 1.21, 'deg': 4}
id 5599665
main {'feels_like': 301.65, 'pressure': 1013, 'temp_max': 305.08,
The weather information is stored in main
. Let’s display just that:
print("Weather in {}".format(j['name']))
for k,v in j['main'].items():
print(" {:10} {}".format(k, v))
Weather in Tahoe
feels_like 301.65
pressure 1013
temp_max 305.08
temp 303.62
temp_min 302.21
humidity 13
sea_level 1013
grnd_level 905
Many websites offer APIs for communicating with computers. For example, IFTTT is a site organized entirely around offering services for computer automation.
Cloud service providers, like IBM, Microsoft Azure, Google Cloud, Amazon AWS and many more offer web APIs.
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.