{"id":221,"date":"2017-11-29T18:31:47","date_gmt":"2017-11-29T18:31:47","guid":{"rendered":"http:\/\/wp.spoton.cz\/?p=221"},"modified":"2017-11-29T18:31:47","modified_gmt":"2017-11-29T18:31:47","slug":"micropython-on-esp-01-8266","status":"publish","type":"post","link":"https:\/\/spoton.cz\/index.php\/2017\/11\/29\/micropython-on-esp-01-8266\/","title":{"rendered":"microPython on ESP-01 (8266)"},"content":{"rendered":"<p>Using the ESP8266 in it&#8217;s minimal form is a pain, especially with the built-in stock firmware and AT commands. If you want and \/ or need a sensible, known interface to your ESP8266, just<br \/>\n <!--more--><\/p>\n<h2>Few simple steps, big difference<\/h2>\n<p>In order to upload micropython to your ESP8266, you&#8217;ll need:<\/p>\n<ol>\n<li><a href=\"https:\/\/github.com\/espressif\/esptool\/\">https:\/\/github.com\/espressif\/esptool\/<\/a> &#8211;<br \/>\n the obvious esp tool for flashing, written in python<\/li>\n<li>micropython firmware for ESP-8266-01s &#8211;\u00a0\u00a0<a href=\"http:\/\/micropython.org\/resources\/firmware\/esp8266-20171101-v1.9.3.bin\">This one worked for me<\/a><\/li>\n<li>basic python knowledge to continue on with configuration, but you can just learn as you go<\/li>\n<\/ol>\n<p>Start off by reading a bit of documentation, on\u00a0<a href=\"https:\/\/docs.micropython.org\/en\/latest\/esp8266\/esp8266\/tutorial\/intro.html\">docs.micropython.org\/en\/latest\/esp8266\/esp8266\/tutorial\/intro.html<\/a>. It tells more or less the whole story and there is little need to deviate from that. I&#8217;ll give you a quick rundown of what worked for me.<\/p>\n<p>The manual says, we should reset the flash. OK.<\/p>\n<pre>.\/esptool.py --port \/dev\/ttyUSB1 erase_flash\nConnecting........_____.....\nDetecting chip type... ESP8266\nChip is ESP8266EX\nUploading stub...\nRunning stub...\nStub running...\nErasing flash (this may take a while)...\nChip erase completed successfully in 2.6s\nHard resetting...<\/pre>\n<p>and then some other stuff I tried, just for fun<\/p>\n<pre>.\/esptool.py --port \/dev\/ttyUSB1 flash_id\nesptool.py v2.2-dev\nConnecting........_____....._____....._____....._____....._____....._____....._____....._____....._____..\nDetecting chip type... ESP8266\nChip is ESP8266EX\nUploading stub...\nRunning stub...\nStub running...\nManufacturer: e0\nDevice: 4014\nDetected flash size: 1MB\nHard resetting...\n\n.\/esptool.py --port \/dev\/ttyUSB1 chip_id\nesptool.py v2.2-dev\nConnecting........_____.....\nDetecting chip type... ESP8266\nChip is ESP8266EX\nUploading stub...\nRunning stub...\nStub running...\nChip ID: 0x003a6e57\nHard resetting...\n\n.\/esptool.py --port \/dev\/ttyUSB1 read_mac\nesptool.py v2.2-dev\nConnecting........____\nDetecting chip type... ESP8266\nChip is ESP8266EX\nUploading stub...\nRunning stub...\nStub running...\nMAC: 60:01:94:3a:6e:57\nHard resetting...\n\nventil@Temelin:~\/git\/A___PERSONAL\/esptool$ .\/esptool.py --port \/dev\/ttyUSB1 read_flash_status\nesptool.py v2.2-dev\nConnecting........_____....._\nDetecting chip type... ESP8266\nChip is ESP8266EX\nUploading stub...\nRunning stub...\nStub running...\nStatus value: 0x0200\nHard resetting...\n\n.\/esptool.py --port \/dev\/ttyUSB1 read_mem 0x00000\nesptool.py v2.2-dev\nConnecting........_____....._____....._____....._\nDetecting chip type... ESP8266\nChip is ESP8266EX\nUploading stub...\nRunning stub...\nStub running...\nA fatal error occurred: Invalid head of packet ('F')\n<\/pre>\n<p>and the actual flashing command<\/p>\n<pre>.\/esptool.py --port \/dev\/ttyUSB0 --baud 115200 write_flash --flash_size=detect 0 .\/esp8266-20171101-v1.9.3.bin ...<\/pre>\n<p>\n To access your new python thingie, use picocom os similar, and you should get a REPL at 115200 baud.<\/p>\n<pre>picocom \/dev\/ttyUSB1 -b 115200<\/pre>\n<p>Now get some simple network setup and zoom away. You&#8217;d like to import the network module and set up the basic interfaces though. There are two, STA, the standard, that connects to your network and AP, which is the access point. At this time, I&#8217;d like to disable the access point.<\/p>\n<pre>&gt;&gt;&gt; import network                      # import module\n&gt;&gt;&gt; sta = network.WLAN(network.STA_IF)  # this is our standard, dhcp capable interface\n&gt;&gt;&gt; ap = network.WLAN(network.AP_IF)    # this is the one to be disabled\n&gt;&gt;&gt; ap.status()        # is it already disabled?\n-1\n&gt;&gt;&gt; ap.ifconfig()      # if you ever want to us the AP, this is it's config\n('192.168.4.1', '255.255.255.0', '192.168.4.1', '208.67.222.222')\n&gt;&gt;&gt; ap.active(False)   # disable it\n&gt;&gt;&gt; ap.active()        # Check? Check!\nFalse\n&gt;&gt;&gt; ap.ifconfig()      # really disabled?\n('0.0.0.0', '0.0.0.0', '0.0.0.0', '208.67.222.222')<\/pre>\n<p>Now with the AP interface out of the way, I&#8217;ll just connect it to my WiFi<\/p>\n<pre>&gt;&gt;&gt; sta.status()              # No idea what 3 means\n3\n&gt;&gt;&gt; sta.active()              # But it's active\nTrue\n&gt;&gt;&gt; sta.isconnected()         # Just lacking connection and credentials\nFalse\n&gt;&gt;&gt; sta.connect('Wifi ID \/ SSID', 'password') # Connect using these credentials\n&gt;&gt;&gt; sta.ifconfig()            # aaand we're connected\n('192.168.0.27', '255.255.255.0', '192.168.0.1', '192.168.0.1')<\/pre>\n<p>Now that we have a connection, let&#8217;s do a stupidly simple test. Create a php file on a server and look at the reply. Let&#8217;s begin with the php file, let&#8217;s call it <strong>esp_post.php<\/strong><\/p>\n<pre>&lt;?\nheader('Expires: Sun, 01 Jan 2014 00:00:00 GMT');\nheader('Cache-Control: no-store, no-cache, must-revalidate');\nheader('Content-Type: application\/json');\nheader('Accept: application\/json');\nheader('Cache-Control: post-check=0, pre-check=0', FALSE);\nheader('Pragma: no-cache');\n\n$json = file_get_contents('php:\/\/input');\n$values = json_decode($json, true);\nvar_dump($values);\n?&gt;<\/pre>\n<p>You have to put this on a pulbicly available server, that can run PHP. OK, got the file on a server? What&#8217;s the server, mine is host.com \ud83d\ude42<\/p>\n<p>\n Let&#8217;s bring in the big guns in small package, the ESP post example. In the REPL, which is the ReadEvaluateProgramLoop (or something similar to that) &#8211; in other words your interactive python shell on the ESP, just type in:<\/p>\n<pre>&gt;&gt;&gt; r = requests.post('http:\/\/host.com\/esp_post.php', data = '{\"this\":\"that\"}')\n&gt;&gt;&gt; print(r.text)\narray(1) {\n [\"this\"]=&gt;\n string(4) \"that\"\n}<\/pre>\n<p>\n The r.text line tells you what yur server actually replied. In this case, with the sucesffull variable dump. Tadaaaa. Now we can make a lot of big projects, with small effort and footprint.<\/p>\n<p>\n A special thanks to\u00a0<a href=\"http:\/\/dpgeorge.net\/\">http:\/\/dpgeorge.net\/<\/a>, btw&#8230;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using the ESP8266 in it&#8217;s minimal form is a pain, especially with the built-in stock firmware and AT commands. If [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":229,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,8],"tags":[],"_links":{"self":[{"href":"https:\/\/spoton.cz\/index.php\/wp-json\/wp\/v2\/posts\/221"}],"collection":[{"href":"https:\/\/spoton.cz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/spoton.cz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/spoton.cz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/spoton.cz\/index.php\/wp-json\/wp\/v2\/comments?post=221"}],"version-history":[{"count":0,"href":"https:\/\/spoton.cz\/index.php\/wp-json\/wp\/v2\/posts\/221\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/spoton.cz\/index.php\/wp-json\/wp\/v2\/media\/229"}],"wp:attachment":[{"href":"https:\/\/spoton.cz\/index.php\/wp-json\/wp\/v2\/media?parent=221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/spoton.cz\/index.php\/wp-json\/wp\/v2\/categories?post=221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/spoton.cz\/index.php\/wp-json\/wp\/v2\/tags?post=221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}