For the last several weeks I have been programming my Esp8266's, I have experienced frustration, anguish, and some delight.
The frustration comes from the fact that the Esp8266 is a relatively new product and not all (or enough) documentation has been translated from Chinese to English. Or, the six or so, development environments (i.e. IDE's) are implementing conflicting strategies for supporting libraries and functions. Sorting out what is relevant, is sometimes difficult
So far, I have been using the Arduino IDE (originally Rev 1.6.1) which support the Esp8266. The included Examples are nice, but are not very useful as for an actual application, that is, they do not suggest how to implement typical combined solutions for some rudimentary functions.
For example, I think a typical home-grown project should have the ability to run in Software Access Point (SoftAP) mode at initial turn-on and accept configuration or connection information for Station (STN) mode connecting to local Access Point (AP) for further operations.
My Goal
My goal is to build that function into my project, but the details lacking on how to switch from one Channel or SSID to another via software control. The Esp8266 can be easily "Programmed" to attach to a selected AP, but that requires a new download for each environment. I am sure the solution is simple, but so far it eludes me.
Still Learning
I have learned a lot, and I have many of my desired functions working.
Note: two things I have learned about the Arduino IDE that I did NOT know, and documented here for my future reference, are;
- First, Arduino IDE Tab Sections (normally files) with a hidden ".ino" suffix are compiled as part of the "main" file ("*.ino") without the need for the usual compiler "forward" declaration and "export" declarations typically found in other software IDE's. This is very handy for quick small projects, where organization is more important than software development correctness.
- Second, Arduino IDE "control /" will convert a section of code into a "comment" and back again. This is very useful while debugging. This short-cut has always been there (I guess) but I had not previously used it, but now I use it all the time.
Project Template
In general, I am creating a "template" for my future projects. The template will have the following functions:
- A "Dashboard" page which is accessible via the assigned device IP Address or alias.
- A "Help" page that will list all of the available functions
- "Query" methods or functions to extract data, that can be used via a web page or non-typical web access methods (e.g., curl, telnet, etc).
- An "Admin" page, to control different aspects of the device.
- A "Navigator Bar" to switch between pages and "Links" to support information.
- A mDNS Responder, so that a device can be access via a "*.local" alias.
- My goal is to include MQTT functions
Here are some example Pages, The Navigator Bar can be used to switch pages. Note: code was included to make the pages, Desktop and Mobile Device Friendly:
|
Home Page - The Dashboard |
This is the "Admin Page" after a WiFi Scan, network selection and Passphrase can be entered to switch networks.
|
Admin Page - WiFi Scan |
The Help Page provides information and links.
|
Help Page |
Note: the Help page provides links and/or URL's so that a "Query" can easily be constructed for Raw Data access. For example, on a remote system:
$ curl Nod1590.local/q/uptime
The above will prints just the number of seconds (uptime) since power-on.
Some Hurtles
One of the most troublesome aspects of my Esp8266 development was dealing with unexpected web request. Initially, my tests were simple and successful, I was using a Firefox browser on my LinuxMint Workstation, where a simple single web requests received a simple single response from the Esp8266 App. The app provides a "
404 Error" for all unexpected web request. This all worked great, and as expected.
But Then . . .
But then, I tried my app using my
Cell Phone and
Tablet Browsers, which quickly crashed my Esp8266 Web Application. The problem was these browsers repeatedly (and relentlessly) request a "
/favicon.ico" file, which over-ran the Esp8266 buffers, or just "hogged" all of the available bandwidth ( which I have very little, I have a slow and terribly BAD Internet connection to my Shop ). The "/favicon.icon" requests can NOT be "ignored", they just keep coming and therefore the Esp8266 must deal with them. I spent several days (actually more than a few) looking for a solution, including creating and sending a dummy ( or contrived ) files to satisfy the requests. The online searches provided many suggestions, but in all cases the solutions were not optimal, they all still uses my (scarce) network bandwidth and small Esp8266 buffer space.
My devised and final solution was much more simple than I expected (and elegant, if I may say so). I pre-emptively re-direct all external "
/favicon.ico" requests to where they belong - that is, to
Espressif (the Manufacture) web site where they happily (or at least hopefully so) provide the file. The redirection takes place in the Browser and therefore the data transfer is between the browser and manufacture, and NOT with my Esp application. Espressif gets the web hits and I get bandwidth relief, the user sees a nice icon on the Exp8266 App browser tab, and my application does not have to deal with the issue. A single line in the Esp's web application html Header does the trick.
Very-Very Simple!
<link rel="shortcut icon" href="http://espressif.com/favicon.ico"></link>
Note: Yes, I know this solution may not be web polite, and if I find a better solution, I will post it.
Lack of Understanding
Another thing that plagued my early Esp8266 App development was my lack of understanding of the "
Strings Objects"! I have been working with too many languages lately. My friend
Jeff - KO7M has helped and provided suggestion. In general, I was originally inappropriately using Strings that could be trashed (and crashed) by the "Garbage Collection" (GC) process. Most web page generation is about generating Large strings, managing them is important so that the "Heap" is not stressed and so GC can properly reclaim space.
But Now, Progress
Also, to save RAM space, I am using a previously used technique (see
previous post) of moving all "string constants" to FLASH using
PROGMEM, and "not" using the Heap, and there by avoiding some of the RAM space issues. As stated above, most web pages generation is just manipulating long strings in RAM.
To Be Done
I just have a few more issues to work out, and then I will publish the code as an example of Stand-alone Esp8266 Development App.
-- Home Page:
https://WA0UWH.blogspot.com