tag:blogger.com,1999:blog-66169563390012305332024-03-13T00:02:15.635-07:00The HetmanMusings of a (mostly) technological natureTheHetmanhttp://www.blogger.com/profile/01148204798921665372noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-6616956339001230533.post-59676510366649762972016-03-22T17:28:00.000-07:002016-03-22T17:28:55.809-07:00Updating firmware on ESP8266 devices using the Raspberry Pi<span style="font-family: Times, "Times New Roman", serif;">I've outline <a href="http://paulbyford.blogspot.co.uk/2016/03/updating-firmware-on-pimoroni-esp8266.html">how to update the firmware on the Pimoroni ESP8266 pHAT</a> and I thought I would expand on that a little by detailing how to use the Raspberry Pi to flash the firmware for other ESP8266 devices. I've have a couple of <a href="http://nodemcu.com/index_en.html">NodeMCU boards</a> which I got shipped from China for £3.75 each. Here is one of them along side a Pimoroni pHAT:</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUYI1zUcOeZLmihm-ClRKKZNInQDNeGAhyphenhyphenz7s8wV8VVcUiiHBU2PPG8Wyz81c44cvEZaxSdoXH25Tm8KwLUsbI8-XPJFxCneyDPWIkzq3RXq__YDqr_jfp0LggcL2LRfFwc5XpnZlzBeWA/s1600/ESP8266_1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUYI1zUcOeZLmihm-ClRKKZNInQDNeGAhyphenhyphenz7s8wV8VVcUiiHBU2PPG8Wyz81c44cvEZaxSdoXH25Tm8KwLUsbI8-XPJFxCneyDPWIkzq3RXq__YDqr_jfp0LggcL2LRfFwc5XpnZlzBeWA/s320/ESP8266_1.jpg" width="320" /></a></div>
<br />
<span style="font-family: Times, "Times New Roman", serif;">I've been playing around with them on a PC but thought I would give one a try on a Raspberry Pi. This device is a nice little package containing a ESP8266, a USB to serial controller and a 3.3V regulator so that the device can be powered directly from USB. There are also 2 buttons. One marked 'flash' and the other 'user'. They came with the stock </span><span style="font-family: Times, "Times New Roman", serif;">Espressif firmware but I have been experimenting with other files. Using it with a Raspberry Pi is much like using the pHAT with the same tools needed.</span><br />
<br />
<span style="font-family: Times, "Times New Roman", serif;">Start with a fully patched and upgraded 'Jessie' image and install Minicom:</span><br />
<span style="font-family: Times, "Times New Roman", serif;"><br /></span>
<span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;">sudo apt-get install minicom</span></span><br />
<br />
<span style="font-family: Times, "Times New Roman", serif;">and then create a convenient working folder where you can download and make executable the flashing tool:</span><br />
<br />
<span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "courier new" , "courier" , monospace;">wget https://raw.githubusercontent.com/themadinventor/esptool/master/esptool.py</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">chmod +x esptool.py</span></span><br />
<br />
<span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: Times, "Times New Roman", serif;">More information about this tool can be found <a href="https://github.com/themadinventor/esptool">here</a> and also in my previous post. Once you have done this you have all the tools that you need and so you can connect the ESP8266 to the Pi via a USB cable. The Pi will happily allow you to hot plug it and so we are ready to connect to the device but first we need to know what the serial port is named. It is probably /dev/ttyUSB0 but we can check this by searching for serial devices that actually have a driver (and not just virtual devices) as follows:</span></span></span><br />
<br />
<span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: Times, "Times New Roman", serif;"> <span style="font-family: "Courier New", Courier, monospace;">ls -l /sys/class/tty/*/device/driver</span></span></span></span><br />
<br />
<span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;">Which on my Pi gives:</span></span></span></span></span><br />
<br />
<span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;">lrwxrwxrwx 1 root root 0 Mar 22 17:06 /sys/class/tty/ttyAMA0/device/driver -> ../../../../bus/amba/drivers/uart-pl011<br />lrwxrwxrwx 1 root root 0 Mar 22 17:09 /sys/class/tty/ttyUSB0/device/driver -> ../../../../../../../../../bus/usb-serial/drivers/ch341-uart</span></span></span></span></span></span><br />
<br />
<span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;">both the built in UART and the USB device. Now we can connect to the ESP8266 using Minicom:</span></span></span></span></span></span></span><br />
<br />
<span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;">minicom -b 115200 -o -D /dev/ttyUSB0</span></span></span></span></span></span></span></span><br />
<br />
<span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;">and send it an AT command:</span></span></span></span></span></span></span></span></span><br />
<br />
<span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;">AT<br /><br />OK</span></span></span></span></span></span></span></span></span></span><br />
<br />
<span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;">You have to add Ctrl-j after the carriage return for it to work. Once you see that it is working you can reset the device. To do this the 'rst' pin needs to be grounded. You can use a paper clip to briefly connect the pin to the neighbouring ground pin and you will see the LED on the device blink and loads of random characters printed to Minicom before ending in 'ready'. The device has been hard reset. The next thing to test is programming mode. Hold down the button marked 'flash' while doing a reset with the paper clip. This time the text in Minicom won't display 'ready' and you can't enter commands. The device is in programming mode. Close down Minicom so that it doesn't conflict and you are now ready to test with the programming tool:</span></span></span></span></span></span></span></span></span></span></span><br />
<br />
<span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;">./esptool.py -p /dev/ttyUSB0 read_mac</span></span></span></span></span></span></span></span></span></span></span></span><br />
<br />
<span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;">should show:</span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<br />
<span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;">Connecting...<br />MAC: 5c:cf:7f:0c:40:d5</span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<br />
<span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;">which is the MAC address of the device. It's now ready to be programmed and I explained how to do that in my last post.</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
TheHetmanhttp://www.blogger.com/profile/01148204798921665372noreply@blogger.com0tag:blogger.com,1999:blog-6616956339001230533.post-51202070338460801682016-03-21T14:34:00.001-07:002016-03-21T14:34:45.254-07:00Updating the firmware on the Pimoroni ESP8266 pHAT<span style="font-family: "times" , "times new roman" , serif;"><span style="font-family: "times" , "times new roman" , serif;">Pimoroni recently released their <a href="https://shop.pimoroni.com/collections/new-products/products/esp8266-phat">ESP8266 pHAT</a> for the Raspberry Pi and while there is plenty of information on the ESP8266 board itself there is much less about how it is connected to the Pi. Richard Hayler has a post about <a href="http://richardhayler.blogspot.co.uk/2016/02/getting-started-with-esp8266-iot-phat.html">getting started with the pHAT</a> but it doesn't go beyond the basics. I've explored the board a little more and have managed to write firmware to the device from the Pi which I will document here.</span></span><br />
<span style="font-family: "times" , "times new roman" , serif;"><br /></span>
<span style="font-family: "times" , "times new roman" , serif;">Richard has shown how to get started which I will only cover briefly here. They are:</span><br />
<ul>
<li><span style="font-family: "times" , "times new roman" , serif;">Create a new Rasbian 'Jessie' image.</span></li>
<li><span style="font-family: "times" , "times new roman" , serif;">Do updates.</span></li>
<li><span style="font-family: "times" , "times new roman" , serif;">Do upgrade.</span></li>
<li><span style="font-family: "times" , "times new roman" , serif;">Install Minicom.</span></li>
<li><span style="font-family: "times" , "times new roman" , serif;">Edit /boot/cmdline.txt to remove the reference to <span style="font-family: "courier new" , "courier" , monospace;">console=ttyAMA0</span></span></li>
<li><span style="font-family: "times" , "times new roman" , serif;"><span style="font-family: inherit;">Power<span style="font-family: inherit;"> down and <span style="font-family: inherit;">attach</span> the pHAT.</span></span></span></li>
<li><span style="font-family: "times" , "times new roman" , serif;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;">Power<span style="font-family: inherit;"> back <span style="font-family: inherit;">up and<span style="font-family: inherit;"> con<span style="font-family: inherit;">nect to the ESP8266 over serial using Minicom.</span></span></span></span></span></span></span></span></li>
</ul>
<span style="font-family: "times" , "times new roman" , serif;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;">For <span style="font-family: inherit;">convenience</span> I w<span style="font-family: inherit;">rote a quick she<span style="font-family: inherit;">ll<span style="font-family: inherit;"> script to start up Minicom<span style="font-family: inherit;">. <span style="font-family: inherit;">Open a new file called 'term' in your favourite te<span style="font-family: inherit;">xt editor and type</span></span></span>:</span></span></span></span></span></span></span></span></span></span></span><br />
<br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;">#!/bin/bash<br />minicom -b 115200 -o -D /dev/ttyAMA0</span></span></span></span></span></span></span></span></span></span></span><br />
<br />
<span style="font-family: inherit;"><span style="font-family: inherit;">Save the file and exit and the make it <span style="font-family: inherit;">executable by typing<span style="font-family: inherit;">:</span></span></span></span><br />
<br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;">chmod +x term</span></span></span></span></span><br />
<br />
<span style="font-family: inherit;">from a command prompt. You can now run it with:</span><br />
<br />
<span style="font-family: inherit;"> <span style="font-family: "courier new" , "courier" , monospace;">./term</span></span><br />
<br />
<span style="font-family: inherit;">Now you <span style="font-family: inherit;">should be able to <span style="font-family: inherit;">communicate</span> using the AT commands a<span style="font-family: inherit;">s</span> Richard outlined. Don't forget that you need to press <span style="font-family: inherit;">CTRL+j after<span style="font-family: inherit;"> hitting return.</span></span> So far, so much <span style="font-family: inherit;">the same but we need a way to reset the ESP8266 and put it into 'flash' mode. There is not much documentation on the pHAT but a few minutes with a multimeter <span style="font-family: inherit;">showed me that the 'reset' pin on the ESP8266 is connected to GPIO 17 on the Pi and GPIO #<span style="font-family: inherit;">0</span> on th<span style="font-family: inherit;">e ESP8266 is connected to GPIO 27 on the Pi. This allows <span style="font-family: inherit;">the ESP<span style="font-family: inherit;">82<span style="font-family: inherit;">66 to be both <span style="font-family: inherit;">hardware reset and put into programming mode. The latest 'Jessie' version of Raspbian has the handy gpiozero <span style="font-family: inherit;">Python module <span style="font-family: inherit;">installed and we can use this to <span style="font-family: inherit;">toggle the GPIO lines with a simple <span style="font-family: inherit;">Python script. Open your <span style="font-family: inherit;">editor<span style="font-family: inherit;"> or, if you prefer, Idle f<span style="font-family: inherit;">or Python 3 and enter the following code:</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;">#!/usr/bin/env python3<br />from gpiozero import OutputDevice<br />from time import sleep<br /><br /># reset on pin 17, active low<br />rst = OutputDevice(17, False)<br /><br />rst.on()<br />sleep(0.5)<br />rst.off()<br />rst.close()</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;">then save it at 'reset.py'. Make sure y<span style="font-family: inherit;">ou have Minicom open and connected to the ESP8266 so that you can see that the script is working. </span>You can test it from within Idle or make it executabl<span style="font-family: inherit;">e before running it from the command line:</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;">chmod +x reset.py</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;">./reset.py</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;">You should see lots of rubbish scroll by in Minicon before seeing 'ready' and allowing you to type AT commands again. What the script above does is to define pin 17 as an output called 'rst' which is 'active' low. ie it is normally high but when it is turned 'on' it goes low. <span style="font-family: "times" , "times new roman" , serif;">F<span style="font-family: "times" , "times new roman" , serif;">irst</span></span> the pin is turned 'on' (goes low), then we wait for half a second and turn the pin 'off' again (goes high). This will reset the ESP8266 and restart the firmware. We now have a handy way of resetting the device.</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;">The next task is to put it into programming mode. To do this GPIO #0 on the ESP8266 has to be sent low while we do the reset. Here is the script to do this:</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;"> <span style="font-family: "courier new" , "courier" , monospace;">#!/usr/bin/env python3<br />from gpiozero import OutputDevice<br />from time import sleep<br /><br /># reset on pin 17, active low<br />rst = OutputDevice(17, False)<br /># flash mode select on pin 27, active low<br />flash = OutputDevice(27, False)<br /><br />flash.on()<br />sleep(0.5)<br />rst.on()<br />sleep(0.5)<br />rst.off()<br />sleep(0.5)<br />flash.off()<br />rst.close()<br />flash.close()</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;">This script defines a second pin (GPIO 27) as 'flash'. As you can see we turn 'flash' 'on' before doing the reset. Then we turn it off (remember that these pins are set as active low and so 'on' means going low). Save the script as 'flash.py' and then make it executable before running it:</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">chmod +x flash.py</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">./flash.py </span><br />
<br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;">When this script is run you should see the while LED on the pHAT flash briefly and this time Minicom will no longer come back to being ready and you can't type commands. We are in programming mode. We can get back into the normal mode of operation by simply running the 'reset.py' script.</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br />
<br />
Next we need a tool to access the device in programming mode. There is a <a href="https://github.com/themadinventor/esptool">platform independent tool</a> on GitHub which is handily also written in Python. There is loads of information on the tool which includes a manual for all the commands. We can download it to the Pi using wget and then make it execuatable:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">wget https://raw.githubusercontent.com/themadinventor/esptool/master/esptool.py</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">chmod +x esptool.py</span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;">Now we put the ESP8266 into programming mode and test the tool to see that we have a connection. Make sure you close Minicom first so that it doesn't conflict:</span></span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">./flash.py</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">./esptool.py -p /dev/ttyAMA0 read_mac</span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;">Hopefully you should see a message like the following:</span></span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;"><span style="font-family: "courier new" , "courier" , monospace;">Connecting...<br />MAC: 5c:cf:7f:12:72:e7</span></span></span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;">and the MAC address for the WiFi chip will be displayed. Another useful command tells us the device id for the flash chip in the device:</span></span></span></span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;"><span style="font-family: "courier new" , "courier" , monospace;">./esptool.py -p /dev/ttyAMA0 flash_id<br />Connecting...<br />Manufacturer: e0<br />Device: 4016</span></span></span></span></span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;">As you can see the numbers returned are pretty opaque but another GitHub project has a <a href="https://code.coreboot.org/p/flashrom/source/tree/HEAD/trunk/flashchips.h">list of many of them</a>. The chip in my device is 32 Mbits in size, ie 4 MBytes.</span></span></span></span></span></span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "times" , "times new roman" , serif;">Now we need some firmware to flash. Of the many pages of resources for the ESP8266, <a href="http://www.agcross.com/2015/09/the-esp8266-wifi-chip-part-3-flashing-custom-firmware/">this is one of the most useful</a>. For this test I'm going to update <span style="font-family: "times" , "times new roman" , serif;">to the latest official firmware from<span style="font-family: "times" , "times new roman" , serif;"> </span></span></span></span></span></span></span></span>Espressif. Espressif's board is difficult to navigate and much of it is in Chinese but the latest firmware is posted on their <a href="http://bbs.espressif.com/viewforum.php?f=46">announcement page</a>. You want to go to 'latest release' Then click the download link for the 'Non-OS SDK' and on that page scroll past the notes and comments to where the file is attached (I said it was complicated, and the exact location might change.) As I write this, version 1.5.2 is the current file. Also useful is the <a href="http://bbs.espressif.com/viewtopic.php?f=51&t=1022">current AT commands document</a>. Besides the command list it also shows which files should be flashed into which locations for different flash sizes. The firmware is a zip file that contains all source code as well as the binaries. I unzipped this file on a PC and just moved the 'bin' folder over to the Pi. Then using the document above I worked out which file I had to flash to which location and then used this command to write them:<br />
<br />
<span style="font-family: "Courier New", Courier, monospace;">./esptool.py -p /dev/ttyAMA0 write_flash 0x7e000 bin/blank.bin 0x3fe000 bin/blank.bin 0x3fc000 bin/esp_init_data_default.bin 0x00000 bin/boot_v1.5.bin 0x01000 bin/at/512+512/user1.1024.new.2.bin</span><br />
<br />
<span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;">and after 40 seconds I had this:</span></span><br />
<br />
<span style="font-family: "Courier New", Courier, monospace;">Connecting...<br />Erasing flash...<br />Took 0.09s to erase flash block<br />Wrote 4096 bytes at 0x0007e000 in 0.4 seconds (84.2 kbit/s)...<br />Erasing flash...<br />Took 0.08s to erase flash block<br />Wrote 4096 bytes at 0x003fe000 in 0.4 seconds (84.7 kbit/s)...<br />Erasing flash...<br />Took 0.08s to erase flash block<br />Wrote 1024 bytes at 0x003fc000 in 0.1 seconds (84.4 kbit/s)...<br />Erasing flash...<br />Took 0.13s to erase flash block<br />Wrote 4096 bytes at 0x00000000 in 0.4 seconds (81.5 kbit/s)...<br />Erasing flash...<br />Took 1.66s to erase flash block<br />Wrote 397312 bytes at 0x00001000 in 39.4 seconds (80.7 kbit/s)...<br /><br />Leaving...</span><br />
<br />
<span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;">All that remaind was to use the reset command to boot the new firmware, connect with Minicom and check the version:</span></span><br />
<br />
<span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;">AT+GMR<br />AT version:0.60.0.0(Jan 29 2016 15:10:17)<br />SDK version:1.5.2(80914727)<br />compile time:Jan 29 2016 19:07:08<br />OK</span></span></span><br />
<br />
<span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;">and we have flashed new firmware!</span> </span> </span> </span><br />
<span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"> </span> </span><br />
<span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: inherit;"></span> </span></span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: inherit;"></span></span> </span></span></span></span>TheHetmanhttp://www.blogger.com/profile/01148204798921665372noreply@blogger.com4tag:blogger.com,1999:blog-6616956339001230533.post-8350390529131512442016-03-20T15:59:00.000-07:002016-03-20T15:59:35.644-07:00WelcomeWelcome to my blog. I don't expect to be blogging that often and this is really just a place to post technical information that interests me and to document any projects that I might be working on. It's not going to be a place for random musings, so expect posts on software and hardware projects and maybe tabletop gaming, writing and science fiction and fantasy.<br />
<br />
PaulTheHetmanhttp://www.blogger.com/profile/01148204798921665372noreply@blogger.com0