NodeMCU ESP8266 http relay control with 12v battery monitor and wifi configuration – Part 1

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.

all_esp_modules1

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.

nodemcu

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

  1. Navigate to https://nodemcu-build.com/ then enter in your email information.
Enter in your email info as the download info for your firmware is emailed to you.

Enter in your email info as the download info for your firmware is emailed to you. Also, just use the master branch.

2. Next, just select the modules to include.

Select your options, for my project I needed: adc, file, gpio, net, node, pwm, tmr, uart, wifi.

Select your options. For my project I needed: adc, file, gpio, net, node, pwm, tmr, uart, wifi.

 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.

  1. Connect your ESP8266 then run the ESP8266Flasher tool. It will look like this:

ESP8266Flasher

 

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

 

pay attention to the memory addresses

pay attention to the memory addresses under the ‘config’ tab

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.

more addresses

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.

esplorertool

 

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:

esplorerright

  1. Select the com port your ESP8266 is on.
  2. Click on the open Button to open a connection (choose 115200 baud)
  3. 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.
  4. 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.
  5. 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:

firmwareboot

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.

esplorerleft

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.

filesystem

ESPlorer showing filesystem information and a clickable list of the .lua files on the right

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:

  1. Establish an open connection to the unit (steps 1-3 written above)
  2. Click on the Reload button (a list of all files on the file system should appear under the Reload button).
  3. 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!

Read Part 2 on how to set up a NodeMCU ESP8266 which provides a web interface for relay control, 12v battery monitor, and wifi configuration.

Leave a Reply

Your email address will not be published. Required fields are marked *