421 lines
No EOL
20 KiB
HTML
421 lines
No EOL
20 KiB
HTML
<!doctype html>
|
||
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
|
||
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
|
||
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
|
||
<!--[if gt IE 8]><!--> <html> <!--<![endif]-->
|
||
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
|
||
<title>ESP8266 and MicroPython - Part 1 - Home Assistant</title>
|
||
<meta name="author" content="Fabian Affolter">
|
||
<meta name="description" content="Using MicroPython on ESP8266 based devices and Home Assistant.">
|
||
|
||
<meta name="viewport" content="width=device-width">
|
||
<link rel="canonical" href="https://home-assistant.io/blog/2016/07/28/esp8266-and-micropython-part1/">
|
||
|
||
<meta property="fb:app_id" content="338291289691179">
|
||
<meta property="og:title" content="ESP8266 and MicroPython - Part 1">
|
||
<meta property="og:site_name" content="Home Assistant">
|
||
<meta property="og:url" content="https://home-assistant.io/blog/2016/07/28/esp8266-and-micropython-part1/">
|
||
<meta property="og:type" content="article">
|
||
<meta property="og:description" content="Using MicroPython on ESP8266 based devices and Home Assistant.">
|
||
<meta property="og:image" content="https://home-assistant.io/images/blog/2016-07-micropython/social.png">
|
||
|
||
<meta name="twitter:card" content="summary_large_image">
|
||
<meta name="twitter:site" content="@home_assistant">
|
||
|
||
<meta name="twitter:title" content="ESP8266 and MicroPython - Part 1">
|
||
<meta name="twitter:description" content="Using MicroPython on ESP8266 based devices and Home Assistant.">
|
||
<meta name="twitter:image" content="https://home-assistant.io/images/blog/2016-07-micropython/social.png">
|
||
|
||
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet">
|
||
<link href="/atom.xml" rel="alternate" title="Home Assistant" type="application/atom+xml">
|
||
<link rel='shortcut icon' href='/images/favicon.ico' />
|
||
<link rel='icon' type='image/png' href='/images/favicon-192x192.png' sizes='192x192' />
|
||
</head>
|
||
|
||
<body >
|
||
|
||
<header>
|
||
<div class="grid-wrapper">
|
||
<div class="grid">
|
||
|
||
<div class="grid__item three-tenths lap-two-sixths palm-one-whole ha-title">
|
||
<a href="/" class="site-title">
|
||
<img width='40' src='/demo/favicon-192x192.png'>
|
||
<span>Home Assistant</span>
|
||
</a>
|
||
</div>
|
||
|
||
<div class="grid__item seven-tenths lap-four-sixths palm-one-whole">
|
||
<nav>
|
||
<input type="checkbox" id="toggle">
|
||
<label for="toggle" class="toggle" data-open="Main Menu" data-close="Close Menu"></label>
|
||
<ul class="menu pull-right">
|
||
|
||
<li><a href='/getting-started/'>Getting started</a></li>
|
||
<li><a href='/components/'>Components</a></li>
|
||
<li><a href='/cookbook/'>Examples</a></li>
|
||
<li><a href="/developers/">Developers</a></li>
|
||
<li><a href="/blog/">Blog</a></li>
|
||
<li><a href="/help/">Need help?</a></li>
|
||
</ul>
|
||
</nav>
|
||
</div>
|
||
|
||
</div>
|
||
</div>
|
||
</header>
|
||
|
||
|
||
|
||
<div class="grid-wrapper">
|
||
<div class="grid grid-center">
|
||
|
||
<div class="grid__item two-thirds lap-one-whole palm-one-whole">
|
||
|
||
|
||
<article class="post">
|
||
|
||
<header>
|
||
|
||
<h1 class="title indent">ESP8266 and MicroPython - Part 1</h1>
|
||
|
||
|
||
|
||
<div class="meta clearfix">
|
||
<time datetime="2016-07-28T04:00:00+00:00" pubdate data-updated="true"><i class="icon-calendar"></i> July 28, 2016</time>
|
||
<span class="byline author vcard"><i class='icon-user'></i> Fabian Affolter</span>
|
||
<span><i class='icon-time'></i> five minutes reading time</span>
|
||
<span>
|
||
<i class="icon-tags"></i>
|
||
<ul class="tags unstyled">
|
||
|
||
|
||
<li><a class='category' href='/blog/categories/how-to/'>How-To</a></li>
|
||
|
||
|
||
</ul>
|
||
</span>
|
||
|
||
<a class='comments'
|
||
href="#disqus_thread"
|
||
>Comments</a>
|
||
|
||
</div>
|
||
|
||
</header>
|
||
|
||
|
||
<p><img src="/images/blog/2016-07-micropython/micropython.png" style="clear: right; border:none; box-shadow: none; float: right; margin-bottom: 12px;" width="200" /><br />
|
||
The first release of Micropython for ESP8266 was delivered a couple of weeks ago. The <a href="http://docs.micropython.org/en/latest/esp8266/esp8266_contents.html">documentation</a> covers a lot of ground. This post is providing only a little summary which should get you started.</p>
|
||
|
||
<p>Until a couple of weeks ago, the pre-built MicroPython binary for the ESP8266 was only available to backers. This has changed now and it is available to the public for <a href="https://micropython.org/download/#esp8266">download</a>.</p>
|
||
|
||
<a name="read-more"></a>
|
||
|
||
<p>The easiest way is to use <a href="https://github.com/themadinventor/esptool">esptool.py</a> for firmware handling tasks. First erase the flash:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ sudo python esptool.py --port /dev/ttyUSB0 erase_flash
|
||
esptool.py v1.0.2-dev
|
||
Connecting...
|
||
Erasing flash (this may take a while)...
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>and then load the firmware. You may adjust the file name of the firmware binary.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ sudo python esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=8m 0 esp8266-2016-07-10-v1.8.2.bin
|
||
esptool.py v1.2-dev
|
||
Connecting...
|
||
Running Cesanta flasher stub...
|
||
Flash params set to 0x0020
|
||
Writing 540672 @ 0x0... 540672 (100 %)
|
||
Wrote 540672 bytes at 0x0 in 13.1 seconds (330.8 kbit/s)...
|
||
Leaving...
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>Now reset the device. You should then be able to use the <a href="http://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/repl.html#getting-a-micropython-repl-prompt">REPL (Read Evaluate Print Loop)</a>. On Linux there is <code>minicom</code> or <code>picocom</code>, on a Mac you can use <code>screen</code> (eg. <code>screen /dev/tty.SLAB_USBtoUART 115200</code>), and on Windows there is Putty to open a serial connection and get the REPL prompt.</p>
|
||
|
||
<p>The <a href="http://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/repl.html#webrepl-a-prompt-over-wifi">WebREPL</a> work over a wireless connection and allows easy access to a prompt in your browser. An instance of the WebREPL client is hosted at <a href="http://micropython.org/webrepl">http://micropython.org/webrepl</a>. Alternatively, you can create a local clone of their <a href="https://github.com/micropython/webrepl">GitHub repository</a>. This is neccessary if your want to use the command-line tool <code>webrepl_cli.py</code> which is mentionend later in this post.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ sudo minicom -D /dev/ttyUSB0
|
||
#4 ets_task(4020e374, 29, 3fff70e8, 10)
|
||
WebREPL daemon started on ws://192.168.4.1:8266
|
||
Started webrepl in setup mode
|
||
could not open file 'main.py' for reading
|
||
|
||
#5 ets_task(4010035c, 3, 3fff6360, 4)
|
||
MicroPython v1.8.2-9-g805c2b9 on 2016-07-10; ESP module with ESP8266
|
||
Type "help()" for more information.
|
||
>>>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p class="note">
|
||
The public build of the firmware may be different than the firmware distributed to the backers of the campaign. Especially in regard of the <a href="http://docs.micropython.org/en/latest/esp8266/py-modindex.html">available modules</a>, turned on debug messages, and alike. Also, the WebREPL may not be started by default.
|
||
</p>
|
||
|
||
<p>Connect a LED to pin 5 (or another pin of your choosing) to check if the ESP8266 is working as expected.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>>>> <span class="keyword">import</span> <span class="include">machine</span>
|
||
>>> pin = machine.Pin(<span class="integer">5</span>, machine.Pin.OUT)
|
||
>>> pin.high()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>You can toogle the LED by changing its state with <code>pin.high()</code> and <code>pin.low()</code>.</p>
|
||
|
||
<p>Various ESP8266 development board are shipped with an onboard photocell or a light dependent resistors (LDR) connected to the analog pin of your ESP8266 check if you are able to obtain a value.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>>>> <span class="keyword">import</span> <span class="include">machine</span>
|
||
>>> brightness = machine.ADC(<span class="integer">0</span>)
|
||
>>> brightness.read()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>Make sure that you are familiar with REPL and WebREPL because this will be needed soon. Keep in mind the password for the WebREPL access.</p>
|
||
|
||
<p>Read the <a href="http://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/network_basics.html">instructions</a> about how to setup your wireless connection. Basically you need to upload a <code>boot.py</code> file to the microcontroller and this file is taking care of the connection setup. Below you find a sample which is more or less the same as shown in the <a href="http://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/network_basics.html#configuration-of-the-wifi">documentation</a>.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre><span class="keyword">def</span> <span class="function">do_connect</span>():
|
||
<span class="keyword">import</span> <span class="include">network</span>
|
||
|
||
SSID = <span class="string"><span class="delimiter">'</span><span class="content">SSID</span><span class="delimiter">'</span></span>
|
||
PASSWORD = <span class="string"><span class="delimiter">'</span><span class="content">PASSWORD</span><span class="delimiter">'</span></span>
|
||
|
||
sta_if = network.WLAN(network.STA_IF)
|
||
ap_if = network.WLAN(network.AP_IF)
|
||
<span class="keyword">if</span> ap_if.active():
|
||
ap_if.active(<span class="predefined-constant">False</span>)
|
||
<span class="keyword">if</span> <span class="keyword">not</span> sta_if.isconnected():
|
||
print(<span class="string"><span class="delimiter">'</span><span class="content">connecting to network...</span><span class="delimiter">'</span></span>)
|
||
sta_if.active(<span class="predefined-constant">True</span>)
|
||
sta_if.connect(SSID, PASSWORD)
|
||
<span class="keyword">while</span> <span class="keyword">not</span> sta_if.isconnected():
|
||
<span class="keyword">pass</span>
|
||
print(<span class="string"><span class="delimiter">'</span><span class="content">Network configuration:</span><span class="delimiter">'</span></span>, sta_if.ifconfig())
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>Upload this file with <code>webrepl_cli.py</code> or the WebREPL:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ python webrepl_cli.py boot.py 192.168.4.1:/boot.py
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>If you reboot, you should see your current IP address in the terminal.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>>>> Network configuration: ('192.168.0.10', '255.255.255.0', '192.168.0.1', '192.168.0.1')
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>First let’s create a little consumer for Home Assistant sensor’s state. The code to place in <code>main.py</code> is a mixture of code from above and the <a href="/developers/rest_api/">RESTful API</a> of Home Assistant. If the temperature in the kitchen is higher than 20 °C then the LED connected to pin 5 is switched on.</p>
|
||
|
||
<p class="note">
|
||
If a module is missing then you need to download is it from <a href="https://github.com/micropython/micropython-lib">MicroPython Library overview</a> and upload it to the ESP8266 with <code>webrepl_cli.py</code> manually.
|
||
</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre><span class="comment"># Sample code to request the state of a Home Assistant entity.</span>
|
||
|
||
API_PASSWORD = <span class="string"><span class="delimiter">'</span><span class="content">YOUR_PASSWORD</span><span class="delimiter">'</span></span>
|
||
URL = <span class="string"><span class="delimiter">'</span><span class="content">http://192.168.0.5:8123/api/states/</span><span class="delimiter">'</span></span>
|
||
ENTITY = <span class="string"><span class="delimiter">'</span><span class="content">sensor.kitchen_temperature</span><span class="delimiter">'</span></span>
|
||
TIMEOUT = <span class="integer">30</span>
|
||
PIN = <span class="integer">5</span>
|
||
|
||
<span class="keyword">def</span> <span class="function">get_data</span>():
|
||
<span class="keyword">import</span> <span class="include">urequests</span>
|
||
url = <span class="string"><span class="delimiter">'</span><span class="content">{}{}</span><span class="delimiter">'</span></span>.format(URL, ENTITY)
|
||
headers = {<span class="string"><span class="delimiter">'</span><span class="content">x-ha-access</span><span class="delimiter">'</span></span>: API_PASSWORD,
|
||
<span class="string"><span class="delimiter">'</span><span class="content">content-type</span><span class="delimiter">'</span></span>: <span class="string"><span class="delimiter">'</span><span class="content">application/json</span><span class="delimiter">'</span></span>}
|
||
resp = urequests.get(URL, headers=headers)
|
||
<span class="keyword">return</span> resp.json()[<span class="string"><span class="delimiter">'</span><span class="content">state</span><span class="delimiter">'</span></span>]
|
||
|
||
<span class="keyword">def</span> <span class="function">main</span>():
|
||
<span class="keyword">import</span> <span class="include">machine</span>
|
||
<span class="keyword">import</span> <span class="include">time</span>
|
||
|
||
pin = machine.Pin(PIN, machine.Pin.OUT)
|
||
<span class="keyword">while</span> <span class="predefined-constant">True</span>:
|
||
<span class="keyword">try</span>:
|
||
<span class="keyword">if</span> <span class="predefined">int</span>(get_data()) >= <span class="integer">20</span>:
|
||
pin.high()
|
||
<span class="keyword">else</span>:
|
||
pin.low()
|
||
<span class="keyword">except</span> <span class="exception">TypeError</span>:
|
||
<span class="keyword">pass</span>
|
||
time.sleep(TIMEOUT)
|
||
|
||
<span class="keyword">if</span> __name__ == <span class="string"><span class="delimiter">'</span><span class="content">__main__</span><span class="delimiter">'</span></span>:
|
||
print(<span class="string"><span class="delimiter">'</span><span class="content">Get the state of {}</span><span class="delimiter">'</span></span>.format(ENTITY))
|
||
main()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>Upload <code>main.py</code> the same way as <code>boot.py</code>. After a reboot (<code>>>> import machine</code> and <code>>>> machine.reboot()</code>) or power-cycling your physical notifier is ready.</p>
|
||
|
||
<p>If you run into trouble, press “Ctrl+c” in the REPL to stop the execution of the code, enter <code>>>> import webrepl</code> and <code>>>> webrepl.start()</code>, and upload your fixed file.</p>
|
||
</article>
|
||
|
||
|
||
<section id="disqus">
|
||
<h3 class="indent title">Comments</h3>
|
||
<div id="disqus_thread" aria-live="polite"><noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript></div>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
|
||
|
||
<aside id="sidebar" class="grid__item one-third lap-one-whole palm-one-whole">
|
||
<div class="grid">
|
||
|
||
|
||
<section class="aside-module grid__item one-whole lap-one-half">
|
||
<h1 class="title delta">About Home Assistant</h1>
|
||
<ul class="divided">
|
||
<li>
|
||
Home Assistant is an open-source home automation platform running on Python 3. Track and control all devices at home and automate control.
|
||
</li>
|
||
<li><a href='/getting-started/'>Get started with Home Assistant</a></li>
|
||
<li><a href='/demo/'>Try the online demo</a></li>
|
||
<li><a class="twitter-follow-button" href="https://twitter.com/Home_Assistant">Follow Home Assistant on Twitter</a></li>
|
||
<li><div class="fb-like" data-href="https://www.facebook.com/homeassistantio/" data-layout="standard" data-action="like" data-size="small" data-show-faces="true" data-share="false"></div></li>
|
||
</ul>
|
||
</section>
|
||
<div id="fb-root"></div>
|
||
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.async=true;js.src='//platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
|
||
<script>(function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(d.getElementById(id)){return;}js=d.createElement(s);js.id=id;js.async=true;js.src="//connect.facebook.net/en_US/all.js#appId=338291289691179&xfbml=1";fjs.parentNode.insertBefore(js,fjs);}(document,'script','facebook-jssdk'));</script>
|
||
|
||
|
||
<section class="sharing aside-module grid__item one-whole lap-one-half">
|
||
<h1 class="title delta">Share this post</h1>
|
||
|
||
<a href="//twitter.com/share"
|
||
class="twitter-share-button"
|
||
data-via="home_assistant"
|
||
data-related="home_assistant"
|
||
data-url="https://home-assistant.io/blog/2016/07/28/esp8266-and-micropython-part1/"
|
||
data-counturl="https://home-assistant.io/blog/2016/07/28/esp8266-and-micropython-part1/" >Tweet</a>
|
||
|
||
|
||
<div class="fb-share-button" style='top: -6px;'
|
||
data-href="https://home-assistant.io/blog/2016/07/28/esp8266-and-micropython-part1/"
|
||
data-layout="button_count">
|
||
</div>
|
||
|
||
|
||
<div class="g-plusone" data-size="standard"></div>
|
||
|
||
</section>
|
||
|
||
<script src="https://apis.google.com/js/platform.js" async defer></script>
|
||
<section id="recent-posts" class="aside-module grid__item one-whole lap-one-half">
|
||
<h1 class="title delta">Recent Posts</h1>
|
||
<ul class="divided">
|
||
|
||
|
||
<li class="post">
|
||
<a href="/blog/2016/08/16/we-have-apps-now/">We Have Apps Now</a>
|
||
</li>
|
||
|
||
|
||
|
||
<li class="post">
|
||
<a href="/blog/2016/08/13/foursquare-fast-com-ffmpeg-gpsd/">0.26: Foursquare, Fast.com, FFMPEG and GPSD</a>
|
||
</li>
|
||
|
||
|
||
|
||
<li class="post">
|
||
<a href="/blog/2016/08/07/optimizing-the-home-assistant-mobile-web-app/">Optimizing the Home Assistant mobile web app</a>
|
||
</li>
|
||
|
||
|
||
|
||
<li class="post">
|
||
<a href="/blog/2016/08/03/laundry-automation-update/">Laundry Sensors with NodeMCU and Home Assistant</a>
|
||
</li>
|
||
|
||
|
||
|
||
<li class="post">
|
||
<a href="/blog/2016/07/30/custom-frontend-panels-jupyter-notebooks-directv/">0.25: Custom frontend panels, Jupyter notebooks, DirecTV.</a>
|
||
</li>
|
||
|
||
|
||
</ul>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</aside>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
<footer>
|
||
<div class="grid-wrapper">
|
||
<div class="grid">
|
||
<div class="grid__item">
|
||
<div class="copyright">
|
||
<a rel="me" href='https://twitter.com/home_assistant'><i class="icon-twitter"></i></a>
|
||
<a rel="me" href='https://facebook.com/homeassistantio'><i class="icon-facebook"></i></a>
|
||
<a rel="me" href='https://plus.google.com/110560654828510104551'><i class="icon-google-plus"></i></a>
|
||
<a rel="me" href='https://github.com/home-assistant/home-assistant'><i class="icon-github"></i></a>
|
||
|
||
<div class="credit">
|
||
Contact us at <a href='mailto:hello@home-assistant.io'>hello@home-assistant.io</a>.<br>
|
||
Website powered by <a href='http://jekyllrb.com/'>Jekyll</a> and the <a href='https://github.com/coogie/oscailte'>Oscalite theme</a>.<br />
|
||
Hosted by <a href='https://pages.github.com/'>GitHub</a> and served by <a href='https://cloudflare.com'>CloudFlare</a>.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</footer>
|
||
|
||
<script>
|
||
var _gaq=[['_setAccount','UA-57927901-1'],['_trackPageview']];
|
||
(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
|
||
g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
|
||
s.parentNode.insertBefore(g,s)}(document,'script'));
|
||
</script>
|
||
<script>
|
||
var disqus_shortname = 'home-assistant';
|
||
|
||
|
||
// var disqus_developer = 1;
|
||
var disqus_identifier = 'https://home-assistant.io/blog/2016/07/28/esp8266-and-micropython-part1/';
|
||
var disqus_url = 'https://home-assistant.io/blog/2016/07/28/esp8266-and-micropython-part1/';
|
||
var disqus_script = 'embed.js';
|
||
|
||
(function () {
|
||
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
|
||
dsq.src = '//' + disqus_shortname + '.disqus.com/' + disqus_script;
|
||
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
|
||
}());
|
||
</script>
|
||
</body>
|
||
</html> |