PART 1. (looking for Part 2?)
ESP8266 internet stuff
The Esp8266 is an amazingly inexpensive wifi device which has all sorts of support from all sorts of IDE’s and Firmware. The problem is that the majority of ESP8266 information on the internet on how do to a simple thing such as control a relay using NodeMCU on a NodeMCU v0.9 development board and measure voltage with it is a little difficult to sort through. Sure, there’s tutorials using NodeMCU 0.9.5 or 0.9.6 on how to drive a relay, and a separate tutorial on how to create an easy http web page using 0.9.6, and even another tutorial on how to configure the ESP8266 as an access point…that’s not the issue. The problem (imho) is that the information takes days to sort through and figure out…especially if you’re a beginner in electronics like me because lots of the information isn’t that detailed, nor is it current for the ESP8266 NodeMCU development. Add to the problem the different versions and hardware differences of the ESP8266 whether be it an ESP-01 (which I have and absolutely hated), or an ESP8266 based development board (which is the best route for beginners like me) which one needs to take into account.
So this is my attempt to help guide someone who is beginning to familiarize themselves with the ESP8266 in getting the ESP8266 working with NodeMCU (and LUA scripts).
What I wanted to accomplish with the ESP8266
My problem was simple. I have a boat up in Shasta that has an electric trolling motor, and this motor needs a 12v deep cycle battery to function during boating season. I probably take 5 trips up to Shasta during the year where each trip I would take the battery up, and bring it back down to the SF bay area so I could keep the battery charged and in good health. Lugging the battery back and forth is a hassle as the battery is heavy and bulky, and it uses up gas and space in my van. I really wished I didn’t have to keep taking that battery with me every trip and wanted to be able to set up a system where I could leave the battery plugged into the old 20amp charger I had been given, leave it in the garage, and monitor and control everything through the internet.
Enter the ESP8266 NodeMCU v0.9 development board
The ESP8266 is cheap. I mean dirt cheap. It has the ability to act as a wifi client, wifi access point, or both at the same time! It also has an embedded TCP/IP stack which allows it to run a simple http server and do all sorts of neat things with it. If one opts for the ESP8266 development boards, such as the nodemcu v0.9 or v.1.0, the additional GPIO/ADC/VDC/GND pins make the platform even more attractive. Because of these offerings, the ESP8266 based NodeMCU development board was the best solution for me.
I initially purchased an ESP8266-01 module but got tired of working with it because its two GPIO pins need additional hardware bits to make it not boot up in Flash mode, and the hassle of flashing, powering, and connecting the 3.3v serial lines made me want to bash that little module to pieces almost every time I worked with it.
In the end, I ordered one of the NodeMCU v0.9 boards off ebay for less than $5. And that was the easiest part of the entire project.
Update the ESP8266 with the latest NodeMCU firmware
So now that you have the board in your hands, the first thing to do is get a current version of NodeMCU on it. DO NOT USE NodeMCU v.0.9.6 or lower based firmware. I’m warning you. Everywhere on the internet talks about v0.9.5 and v0.9.6 but its all old info. They are full of weird bugs, and those versions just don’t work very well. You also cant expect that a lua script written for those versions will work on newer firmware based on 1.4+, so you’ll end up rewriting your scripts…
Ill tell you a story…After a few days of coding (by which I mean cutting and pasting other peoples brilliant code into my own) I had a basic http server running a web page that would let me turn a relay on and off. It worked great on my local WIFI, but as soon as I wanted to VPN into my network and access the webpage, it wouldn’t work. Some googling led me to learn that it was a bug in the MTU size and that newer firmware from v1.4+ fixed it. Ok no problem I thought, ill just update the firmware. Easy right? NOPE. but keep reading and Ill guide you through it.
First thing you need to know is that new binaries aren’t pre-compiled for NodeMCU anymore, and although there’s a few ways to get the firmware, I found the easiest and most elegant solution is the NodeMCU custom builds site. This site compiles the firmware for your ESP8266 and allows you to download the binary to run the latest versions of NodeMCU on your ESP8266.
Compile the NodeMCU firmware
- Navigate to https://nodemcu-build.com/ then enter in your email information.
2. Next, just select the modules to include.
3. Scroll down and click the ‘Start your Build’ button (I didn’t check any of the miscellaneous options).
Download the firmware
Once your build finishes, you’ll get an email with the download information. I used the float binary as I was going to be working with decimal numbers. Note: The Integer build is for projects which don’t really require working with decimal numbers. The integer version is smaller and will free up a little space on your ESP8266, but I didn’t have any issues with the Float binary and space requirements. Download and save to your computer.
After you get your firmware you’ll need an easy way to flash the firmware to the ESP8266 board.
Flash NodeMCU to your ESP8266 with ESP8266Flasher
What I have been using (and i’ve used almost all of the other tools) which works very well is the ESP8266Flasher tool.
Get it here: https://github.com/nodemcu/nodemcu-flasher and click the green ‘clone or download’ button on the right hand side of the screen. Download the Zip file and extract it somewhere.
- Connect your ESP8266 then run the ESP8266Flasher tool. It will look like this:
2. Specify the firmware and memory addresses.
Next you’ll need to configure the ESP8266Flasher to flash the file you just downloaded from the NodeMCU custom build link which was emailed to you. You will also need to flash an esp_init_data_default.bin file to the specific memory address shown below. The reason being is that with the newer ESP8266 firmware, the init data memory address locations change. So if you do not flash the esp_init_data_default.bin file to the correct address, the ESP8266 unit wont boot properly (ask me how I know this).
Download: esp_init_data_default.bin
Below is a table with other assorted memory address locations for different flash capacities in case you need them. The NodeMCU v0.9 has a 4096 capacity.
3. Flash NodeMCU firmware on the ESP8266.
Finally, click back over to the Operation tab, select the com port your ESP8266 is on, and then click the Flash(F) button. At this point the unit will start flashing. Once its done there are still a few more steps to do to get the unit working correctly. But at least the firmware has been loaded onto the ESP8266. If the flash fails, dont worry, just can just reflash it again.
The awesome ESPlorer tool
Now that we’ve gotten the ESP8266 flashed with a current version of NodeMCU (v.1.5.4 at the time of this writing), we now need a different tool to upload lua scripts, test commands, see terminal output from the ESP8266, do other useful things such as format the file system, and run files located on the ESP8266.
Get Esplorer: http://esp8266.ru/esplorer/
Scroll to the bottom of the page and download the latest release. The software requires JAVA (SE version 7 and above) installed.
Once you have the software installed and running, the very first thing you need to do is format the ESP8266 file system, and we can do this with ESPlorer. Until you do, the unit will not run correctly and may display all sorts of weird gibberish on the screen.
To Format the file system on the ESP8266 follow these directions:
- Select the com port your ESP8266 is on.
- Click on the open Button to open a connection (choose 115200 baud)
- Click on any of the buttons Heap, Chip Info, Chip ID, or Flash ID to establish connection with the unit (once the red Reset button becomes clickable you know you are connected). If ESPlorer seems to not be able to connect to the MCU, press the reset button on the circuit board while the com port is still open. The unit should then respond to commands.
- Click on the format button. You will need to wait a bit for the terminal to tell you the format is complete as there is no status on the process during formatting. Be patient at this part.
- Once the format of the file system is complete, click the reset button and reboot the ESP8266.
The ESP8266 should now boot and show you the console output declaring the correct firmware that is loaded onto the unit. It may look something like this image below:
Your ESP8266 is now running the latest NodeMCU firmware! Now we can finally start uploading lua scripts.
In order to upload lua scripts, you just need to follow steps 1-3 as listed above to establish a working connection to the ESP8266. Next, click the Upload button near the bottom middle of the screen. You’ll then get to browse and select a file to upload.
What I have found is that uploading one file at a time works the best. I’ve had strange hangups when trying to upload more than one at a time.
Once you’ve uploaded a few files, you can just use ESPlorer to list the files, and click on them to run them. Follow the directions below to list and run a file on the ESP8266 file system using Esplorer:
- Establish an open connection to the unit (steps 1-3 written above)
- Click on the Reload button (a list of all files on the file system should appear under the Reload button).
- Click on a lua file button to run it.
You now know the basics of uploading and manually running lua scripts on the ESP8266 running NodeMCU v1.5.4!