Arduino IDE, Assignments, ESP8266, ESPlorer IDE, Firmware, Humidity, LUA, NodeMCU, Sensors, Temperature, Uncategorized

Assignment #3: NodeMCU (ESP8266) Arduino Programming Tutorial

Hello again! This week’s assignment is to create a NodeMCU (ESP8266) programming toturial. In this tutorial I will show how to

  • setup ArduinoIDE to program the NodeMCU
  • perform a LED blink test
    • Using the onboard LED
    • Using regular LEDs
  • flash a custom firmware to the NodeMCU unit
  • use ESPlorer IDE to program the NodeMCU unit
  • read Temperature and Humidity using the DHT-11

Setup ArduinoIDE

To setup the ArduinoIDE I followed the steps on THIS guide, presented under the title “Install the Arduino IDE 1.6.4 or greater“. First download and install the latest ArduinoIDE version (1.6.7 at the time of writing) from their official homepage. Then launch the application and -as instructed in the guide- enter the following URL in the Additional Board Manager URLs field under the preferences menu:

[code]
http://arduino.esp8266.com/stable/package_esp8266com_index.json
[/code]
Screen Shot 2016-02-08 at 8.43.02 PMAfter doing so, use the Board Manager to install the ESP8266 Package:
Screen Shot 2016-02-08 at 8.45.42 PMScreen Shot 2016-02-08 at 8.46.54 PM
Lastly, download and install the USB Serial interface drivers, which will enable your OS to talk to the USB controller of the ESP8266. In windows it will assign a COM port to it, and in Linux/OS X it will treat it as a /dev/ device. You can download the drivers HERE. After installing the drivers and the IDE, you can now start programming.

LED Blink Test

There are two ways you can perform this test. First I will show you how to do it by blinking the onboard LED light mapped to GPIO16 of the NodeMCU. Then I will show you how to light regular LEDs connected directly to +3.3V and GND, and connected to a standard GPIO pin as well. Note that in the last two cases you will also need 330Ω resistors.

Make sure that you have selected the following settings under the tools menu, as well as the appropiate port for the board:
Screen Shot 2016-02-08 at 11.33.04 PM

Also, for future reference, this is the pinout diagram of the board:
esp8266-nodemcu-dev-kit-v1-pins

OnBoard LED

To flash the onboard LED you simply have to connect your NodeMCU unit to your computer and fire up the Arduino IDE. Open a new script file and use the following code:

[code]
void setup() {
// initialize digital pin 16 as an output.
pinMode(16, OUTPUT);
}

void loop() {
digitalWrite(16, HIGH); // turn the LED off
delay(1000); // wait for a second
digitalWrite(16, LOW); // turn the LED on
delay(1000); // wait for a second
}
[/code]
After writing/copy-pasting the code to the editor, press the upload button to compile and load the software onto the NodeMCU:
Screen Shot 2016-02-08 at 11.39.11 PM
You’ll se a some output on the console at the bottom of the editor’s window and the module will flash continuously showing that data is being written into it:
Screen Shot 2016-02-08 at 11.41.55 PM20160207_211233
After the Arduino IDE finishes loading the program into the module, you’ll see the bright blue LED near the MicroUSB socket turn on and off with a 1 second delay between states (just as you programmed it):
20160207_211248
Voilá! You have succesfully programmed the NodeMCU module! Not let’s move to a more standard LED test.

 

Regular LEDs

Producing the same outcome as the previous step using regular LEDs is very simple. I have prepared the following setup to illustrate how it works. Below you can find both the picture and the schematic:
20160207_224831
BlinkingLed_bb
As you can see, the setup has two LEDs each with its own 330Ω resistor. The red LED is connected to a 3.3V pin and a GND pin. This means that as soon as the NodeMCU module is connected to power the LED will light up. No programming necessary.

Opposed to that, the yellow LED is connected to the same GND pin as the red LED, but instead is connected to the pin marked as D7, which corresponds to GPIO13 according to the pinout diagram. Therefore the program we will use will be identical to the one used with the builtin LED, but instead of using pin 16, we’ll be using pin 13:

[code]
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}

void loop() {
digitalWrite(13, HIGH); // turn the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off
delay(1000); // wait for a second
}
[/code]
And just as before, load the program into the module using the upload button, and wait for it to be transfered. Once that’s done, you shoul end up with something like this:
20160207_225039
Notice that only the yellow LED flashes (just as you programmed it) and the red one stays on.

NodeMCU Custom Firmware

In order to easily interface the DHT-11 sensor and the NodeMCU module we will replace the stock firmware that comes pre-loaded in the unit, with a custom build (based on the stock version) that includes the necessary LUA library to read data from the sensor.

Obtaining the Firmware

First go to the ESP8266 comunity website, and to the NodeMCU section of the page:Screen Shot 2016-02-08 at 10.44.51 AM
Scroll down to this part of the page:
Screen Shot 2016-02-08 at 10.44.56 AM
Click on the Firmware github link that will get you to the following page:
Screen Shot 2016-02-08 at 10.45.08 AM
On NodeMCUs Firmware page, scroll down to the section that says “Online firmware custom build” and click on the link:
Screen Shot 2016-02-08 at 10.45.15 AM
This will take you to the page portrayed below. Enter your email address in the fields shown (the custom firmware will be sent to this email address):
Screen Shot 2016-02-08 at 10.46.19 AM
Screen Shot 2016-02-08 at 10.45.57 AM
Then select the Master branch as the base for the firmware, and below you will see some default modules selected. Leave those selected and additionally, select the DHT module (which is the module we need):
Screen Shot 2016-02-08 at 10.46.07 AM
Scroll down on the page, and click the button “Start your build“:
Screen Shot 2016-02-08 at 10.48.19 AM
Once you have done this, you will be show this message, showing you the modules that will be built into your custom firmware:
Screen Shot 2016-02-08 at 10.48.31 AM
Shortly after that you shoud receive an email (to the address you used before) containing the custom binary firmwares you can flash into the NodeMCU module.
Screen Shot 2016-02-08 at 10.50.45 AM

Flashing the Firmware

Alright, so after obtaining our custom firmware, we need to get a program that enables us to write (or flash) the firmware onto the NodeMCU’s memory. Once again, go to the NodeMCU section of the ESP8266 comunity page, and click on the Flasher link:
Screen Shot 2016-02-08 at 11.21.12 AM
It will take you to another of NodeMCU’s github repositories which mantains the flasher utility. Sadly, there’s no OS X version available, and for now it’s only compatible with Windows. Click Download ZIP to download the flasher program:
Screen Shot 2016-02-08 at 11.21.22 AM
So, using a windows machine, connect the NodeMCU module via USB. It should recognize it and install the necessary drivers.
Screen Shot 2016-02-08 at 11.45.01 AM
If ythe drivers are not installed automatically, head on to THIS page to download and install the driver that best fits your system:
Screen Shot 2016-02-08 at 11.47.44 AMScreen Shot 2016-02-08 at 11.47.47 AMScreen Shot 2016-02-08 at 11.47.52 AMScreen Shot 2016-02-08 at 11.48.08 AMScreen Shot 2016-02-08 at 11.48.38 AM
After the installation Windows should recognize the module connected by USB.
Screen Shot 2016-02-08 at 11.48.57 AM
In Windows, navigate to the flasher program you downloaded earlier (or download it to your windows machine) and execute the 32 or 64bit version depending on your system specifications:
Screen Shot 2016-02-08 at 11.52.05 AM
Whan the program starts, make sure you check the Device Manager so that the selected port is the same as the one assigned to the NodeMCU module:
Screen Shot 2016-02-08 at 11.53.03 AM
In the Config tab, write the route to the custom binary firmware you downloaded in the previous section (the one you got via email):
Screen Shot 2016-02-08 at 11.57.21 AM
You can use any of the two files, the difference between them is that one is a able to measure floating point numbers, and the other one uses only integers:
Screen Shot 2016-02-08 at 12.02.05 PM
Make sure to select the new firmware file, and deselect the INTERNAL://NODEMCU file using the boxes on the left:
Screen Shot 2016-02-08 at 12.02.22 PM
Then, on the Advanced tab select 9600 as the Baudrate speed:
Screen Shot 2016-02-08 at 12.17.27 PM
And lastly, on the main tab (Operation), click the Flash(F) button to start flashing the custom firmeware. This will take a while, so be patient and let the process finish:
Screen Shot 2016-02-08 at 12.17.28 PM
Once it is done, the NodeMCU module will be ready for programming, and it will also be ready to make use of the DHT-11 sensor.

ESPlorer IDE and the ESP8266

In order to program the NodeMCU for this example, we’re going to use an IDE called ESPlorer. It is free, multi-platform (written in JAVA, distributed as a JAR file), and lets you program the module in LUA and Micropython.

Go to THIS website:Screen Shot 2016-02-08 at 11.27.04 AM
Scroll down, and hit the gigatic Download Now button to download the IDE:
Screen Shot 2016-02-08 at 11.27.15 AM
Also, while you’re there, sownload the Getting started with the ESPlorer IDE tutorial, under the Usefull Tutorials section:
Screen Shot 2016-02-08 at 11.27.17 AM
The tutorial guide is very helpful, and I recommend you read it. Pay particular attention to the instructions for running ESPlorer on your particular system:
Screen Shot 2016-02-08 at 11.27.28 AMScreen Shot 2016-02-08 at 11.27.48 AMScreen Shot 2016-02-08 at 11.27.59 AM
Because I will be using my OS X laptop, I executed the following command to start the IDE:
Screen Shot 2016-02-08 at 12.30.20 PM
After doing so, you should see ESPlorer load and appear in your screen:
Screen Shot 2016-02-08 at 12.30.45 PM

NodeMCU and DHT11 Sensor

Now that we have installed the drivers, flashed our custom firmware, and installed the IDE that will let us program our NodeMCU modue, we must assemble the following setup using the DHT-11 and the module:
Screen Shot 2016-02-08 at 10.10.34 AMScreen Shot 2016-02-08 at 10.11.01 AM
Having done so, we can proceed to programing and testing the setup using ESPlorer.
Start ESPlorer, and once the program is running (and having previously plugged the NodeMCU module to your computer) look for the device in the dropdown on the right side of the IDE and select it:
Screen Shot 2016-02-08 at 12.30.45 PM
Now, click the “Open” button to connect to the module. If you get the same warning I got (highlighted in red), press the RST (reset) button on the module, and then you should see the module’s firmware build information. Note that it says it is a custom build, and amongst the modules you should see the DHT module listed:
Screen Shot 2016-02-08 at 12.31.09 PM
After you verify the above, save a new file using the editor on the left as init.lua. This will be the file that we’ll load into the NodeMCU, and by naming it init.lua, this will be the file that will be executed everytime the module boots up:
Screen Shot 2016-02-08 at 12.34.38 PM

After doing so, write the following code into the editor, and click save. This will save the file, and

[code]
— Example using NodeMCU (ESP8266) and DHT11 Sensor
— By: Jorge A. Duarte G.
— Based on this video https://www.youtube.com/watch?v=6YOkcgZ_NGM
— upleaded to youtube by biblioman09

— Set WiFi mode to station mode
— and configure wireless connection
wifi.setmode(wifi.STATION)
wifi.sta.config(“SSID”, “PASSWD”)
print(wifi.sta.getip())
tmr.delay(5000)

pin = 5 — GPIO14
humi=0
temp=0

— Function to Read the DHT11 Sensor
function ReadDHT11()
status, temp, humi = dht.read11(pin)
if(status == dht.OK) then
print(“Temperature:”..temp..”;”..”Humidity:”..humi)
elseif(status == dht.ERROR_CHECKSUM) then
print(“Checksum ERROR”);
elseif(status == dht.ERROR_TIMEOUT) then
print(“Timeout”);
end
end

ReadDHT11()

tmr.alarm(1,5000,1, function() ReadDHT11() wifi.sta.connect()end)
[/code]

Once the code loads into the NodeMCU it will start running, and in the console window in the right part of the IDE you should see the temperature and humidity measurements being read by the ESP8266.
Screen Shot 2016-02-08 at 6.23.08 PM

I hope you enjoyed this tutorial, and now you know how to use the NodeMCU unit and the DHT-11 to measure Temperature and Humidity.

Sources:

Leave a Reply