<ahref="https://Hieromon.github.io/AutoConnect/"title="AutoConnect for ESP8266/ESP32"aria-label="AutoConnect for ESP8266/ESP32"class="md-header-nav__button md-logo">
<h2id="updates-with-the-update-server">Updates with the update server<aclass="headerlink"href="#updates-with-the-update-server"title="Permanent link">¶</a></h2>
<p>Since the v1.0.0 release, AutoConnect provides new feature for updating sketch firmware of ESP8266 or ESP32 modules via OTA using the <ahref="apiupdate.html#autoconnectupdate">AutoConnectUpdate</a> class that is an implementation of the sketch binary update by the HTTP server mentioned in the <ahref="https://arduino-esp8266.readthedocs.io/en/latest/ota_updates/readme.html#http-server">OTA update</a> of the ESP8266 Arduino Core documentation, which inherits from the ESP8266HTTPUpdate class (as HTTPUpdate class in the case of ESP32). It acts as a client agent for a series of update operations.</p>
<p>This method allows you to remotely update the ESP module's firmware beyond the network segments from the update server, as long as you can ensure proper routing and forwarding.</p>
<p>If you choose this update method, you need to prepare the server process as a variant of the HTTP server that supplies the binary sketch files to the updating client agent. Its server requires to be able to handle the HTTP headers extended by ESP8266HTTPUpdate class as described in the <ahref="https://arduino-esp8266.readthedocs.io/en/latest/ota_updates/readme.html#server-request-handling">ESP8266 Arduino Core documentation</a>. There are various implementations of the update server that provide binary sketch files. For example, the ESP8266 Arduino Core documentation suggests an <ahref="https://arduino-esp8266.readthedocs.io/en/latest/ota_updates/readme.html#id5">advanced updater</a> php script that can be fully communicated with the client agent using the ESP8266HTTPUpdate class. That is, the update server for AutoConnect must work with the client agent, and its implementation should make the handshake well with the AutoConnectUpdate class which wraps an ESP8266HTTPUpdate class.<br/>
The AutoConnect library provides an update server script implemented in Python that can combine with the AutoConnectUpdate class.</p>
<h3id="how-to-embed-autoconnectupdate-to-your-sketch"><iclass="fa fa-edit"></i> How to embed AutoConnectUpdate to your sketch<aclass="headerlink"href="#how-to-embed-autoconnectupdate-to-your-sketch"title="Permanent link">¶</a></h3>
<p>To embed the AutoConnectUpdate class into your sketch, basically follow these steps:</p>
<ol>
<li>Declare an ESP8266WebServer object. (In ESP32, as WebServer)</li>
<li>Declare an AutoConnect object with an ESP8266WebServer object.</li>
<li>Declare an <ahref="apiupdate.html">AutoConnectUpdate</a> object with the update server address and the HTTP port as parameters.</li>
<h3id="behavior-of-the-autoconnectupdate-class"><iclass="fas fa-desktop"></i> Behavior of the AutoConnectUpdate class<aclass="headerlink"href="#behavior-of-the-autoconnectupdate-class"title="Permanent link">¶</a></h3>
<p>A sketch incorporating the AutoConnectUpdate class has an extended menu item as <strong>UPDATE</strong> in the AutoConnect menu. <strong>UPDATE</strong> as menu item will be attached by the AutoConnectUpdate automatically.</p>
<p>When an UPDATE item started, its first action is requesting a <ahref="#2-the-catalog-list-content">catalog list</a> of updatable binary sketch files to the <ahref="#update-server-for-the-autoconnectupdate-class">update server</a>. Then the update server sends back the catalog list of stored binary sketch files to a client which is the ESP module. The AutoConnectUpdate class will display responded list to a custom Web page<supid="fnref:1"><aclass="footnote-ref"href="#fn:1">1</a></sup> on the browser.</p>
<p>The substance of Available firmware list is a custom Web page by AutoConnectAux, and you can select the target binary sketch file with the radio button (AutoConnectRadio). A progress bar will appear to notify the updating status once the update has begun. When the update finished, the ESP module will reset automatically to launch a new firmware.</p>
<p>The AutoConnectUpdate class performs the above series of operations in conjunction with the update server. All you need to do is attach the AutoConnectUpdate class to AutoConnect and execute the <ahref="api.html#handleclient">AutoConnect::handleClient</a> function in the <code>loop()</code>.</p>
<h3id="update-server-for-the-autoconnectupdate-class"><iclass="fas fa-server"></i> Update server for the AutoConnectUpdate class<aclass="headerlink"href="#update-server-for-the-autoconnectupdate-class"title="Permanent link">¶</a></h3>
<p>The above series of operations using AutoConnectUpdate class requires an update server that can work with it. AutoConnect provides an update server script implemented in Python. This server script conforms to a sketch that uses the AutoConnectUpdate class as an update client agent.<supid="fnref:2"><aclass="footnote-ref"href="#fn:2">2</a></sup></p>
<dd><spanclass="apidef"><strong>--help | -h</strong></span><spanclass="apidesc">Show help message and exit.</span>
<dd><spanclass="apidef"><strong>--port | -p</strong></span><spanclass="apidesc">Specifies <strong>PORT</strong> number (Default: 8000)</span>
<dd><spanclass="apidef"><strong>--bind | -b</strong></span><spanclass="apidesc">Specifies the IP address to which the update server binds. Usually, it is the host address of the update server. When multiple NICs configured, specify one of the IP addresses. (Default: HOST IP or 127.0.0.0)</span>
<dd><spanclass="apidef"><strong>--catalog | -d</strong></span><spanclass="apidesc">Specifies the directory path on the update server that contains the binary sketch files. (Default: The current directory)</span>
<dd><spanclass="apidef"><strong>--log | -l</strong></span><spanclass="apidesc">Specifies the level of logging output. It accepts the <ahref="https://docs.python.org/3/library/logging.html?highlight=logging#logging-levels">Logging Levels</a> specified in the Python logging module.</span>
First, prepare a Python environment. It is also possible with a tiny single-board computer like the <ahref="https://www.raspberrypi.org/">raspberry pi</a>. Popular distributions such as Ubuntu for Linux include Python. You can easily set up a Python 2 or 3 environment. If you are using a Mac, you already have the Python 2 environment. macOS is equipped with Python 2.7 by default. In the case of Windows OS, it is necessary to install the Python environment intentionally. Please refer to the <ahref="https://wiki.python.org/moin/BeginnersGuide/Download">Python official page</a> to install Python in your environment.</p>
Use the Arduino IDE to output a binary file of sketches and deploy it<supid="fnref:3"><aclass="footnote-ref"href="#fn:3">3</a></sup> under the update server. The path which specifies for the <strong>--catalog</strong> option of updateServer.py is the path of the binary sketch files you deployed.</p>
For example, to start the update server on the host with IP address 172.16.1.10 using 8080 port<supid="fnref:4"><aclass="footnote-ref"href="#fn:4">4</a></sup>, execute the following command:
In this example assumes that the binary sketch files are deployed under the path <code>bin</code> from the current directory.</p>
</li>
</ol>
</div>
<divclass="admonition note">
<pclass="admonition-title">Limitations of the updateserver.py</p>
<p>The updateserver.py script equips only the minimum facility because it assumes a private small OTA platform without identifying individual modules and version restrictions etc. To operate a larger OTA platform, it is necessary to identify the individual ESP module and to consider version control and security.</p>
</div>
<h3id="http-contents-and-the-sequence-for-the-autoconnectupdate-class"><iclass="far fa-handshake"></i> HTTP contents and the sequence for the AutoConnectUpdate class<aclass="headerlink"href="#http-contents-and-the-sequence-for-the-autoconnectupdate-class"title="Permanent link">¶</a></h3>
<p>You can also equip an update server that works with the AutoConnectUpdate class. It can be improved more widely applicable by adding extensions such as version control and authentication to the updateserver.py script. It is necessary to understand the specifications related to HTTP data streams and sequences to enhance the update server that the AutoConnectUpdate class assumes.</p>
<p>This section describes the contents of the HTTP data stream required by the communication with AutoConnectUpdate class. To work correctly with the AutoConnectUpdate class, the update server must meet two requirements:</p>
<li>The update server notifies the catalog list of updatable binary files which stored in the update server to the client agent. <supid="fnref:5"><aclass="footnote-ref"href="#fn:5">5</a></sup></li>
<li>Send an updating binary file and MD5 hash to a client in response to URI request (HTTP GET). <supid="fnref:6"><aclass="footnote-ref"href="#fn:6">6</a></sup></li>
<p>Above requirements will be implemented on along the HTTP protocol. The AutoConnectUpdate class requests an update server to notify the client for a catalog list of binary sketch files using an HTTP URL query string. The specifications of the HTTP query and the contents of the catalog list to be returned are as follows:</p>
<h4id="1-http-url-query-for-the-catalog-list-of-the-updatable">1. HTTP URL query for the catalog list of the updatable<aclass="headerlink"href="#1-http-url-query-for-the-catalog-list-of-the-updatable"title="Permanent link">¶</a></h4>
<dd><spanclass="apidef"><strong>address</strong></span><spanclass="apidesc">URL of the update server</span>
<dd><spanclass="apidef"><strong>/_catalog</strong></span><spanclass="apidesc">Request path, it is fixed.</span>
<dd><spanclass="apidef"><strong>op</strong></span><spanclass="apidesc">Operation command for the update server. Currently, only '<strong>list</strong>' occurs.</span>
<dd><spanclass="apidef"><strong>path</strong></span><spanclass="apidesc">Path containing the updatable binary files on the update server.</span>
</dl></p>
<h4id="2-the-catalog-list-content">2. The catalog list content<aclass="headerlink"href="#2-the-catalog-list-content"title="Permanent link">¶</a></h4>
<p>The response (that is, the catalog list) to the above query from the server is the following specification in JSON format.</p>
<dd><spanclass="apidef"><strong>name</strong></span><spanclass="apidesc">Binary sketch file name for update (String)</span>
<dd><spanclass="apidef"><strong>type</strong></span><spanclass="apidesc">One of '<strong>bin</strong>', '<strong>directory</strong>' or '<strong>file</strong>'. AutoConnect Update recognizes only file types of '<strong>bin</strong>' as update targets. (String)</span>
<dd><spanclass="apidef"><strong>date</strong></span><spanclass="apidesc">File update date. AutoConnect v1.0.0 treats the file update date as an annotation and is not equip the version control feature yet. (String)</span>
<dd><spanclass="apidef"><strong>time</strong></span><spanclass="apidesc">File update time. AutoConnect v1.0.0 treats the file update date as an annotation and is not equip the version control feature yet. (String)</span>
<p>The above JSON object is one entry. The actual catalog list is an array of this entry since it assumes that an update server will provide multiple update binary files in production. The update server should respond with the MIME type specified as <code>application/json</code> for the catalog list.<supid="fnref:7"><aclass="footnote-ref"href="#fn:7">7</a></sup></p>
<h4id="3-the-binary-sketch-file-used-for-updating">3. The binary sketch file used for updating<aclass="headerlink"href="#3-the-binary-sketch-file-used-for-updating"title="Permanent link">¶</a></h4>
<p>The AutoConnectUpdate class issues a HTTP GET request with the specified host address and URI. The update server responds by sending back a binary sketch file with the following header:</p>
<p>The header <strong>x-MD5</strong> is a 128-bit hash value (digest in hexadecimal) that represents the checksum of the binary sketch file for updates required for the ESP8266HTTPUpdate class.</p>
<p>You can scroll horizontally on the browser to see the timestamp and file size that the catalog list contains. <aclass="footnote-backref"href="#fnref:1"title="Jump back to footnote 1 in the text">↩</a></p>
For Python3: <em>AUTOCONNECT_LIBRARY_PATH</em>/src/updateserver/python3 <aclass="footnote-backref"href="#fnref:2"title="Jump back to footnote 2 in the text">↩</a></p>
<p>Deploying the binary sketch file output by Arduino IDE is usually just copying to the folder for deployment. However, its folder must be accessible from the updateserver.py script. <aclass="footnote-backref"href="#fnref:3"title="Jump back to footnote 3 in the text">↩</a></p>
<p>The port of the update server and the port used by the AutoConnectUpdate class must be the same. <aclass="footnote-backref"href="#fnref:4"title="Jump back to footnote 4 in the text">↩</a></p>
<p>The <strong>client agent</strong> is an instance of the AutoConnectUpdate class. <aclass="footnote-backref"href="#fnref:5"title="Jump back to footnote 5 in the text">↩</a></p>
<p>The client agent will send its URI request to the update server. <aclass="footnote-backref"href="#fnref:6"title="Jump back to footnote 6 in the text">↩</a></p>
<p>It should be represented as <code>Content-Type: application/json</code> in the HTTP response header. <aclass="footnote-backref"href="#fnref:7"title="Jump back to footnote 7 in the text">↩</a></p>