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]
After doing so, use the Board Manager to install the ESP8266 Package:
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:
Also, for future reference, this is the pinout diagram of the board:
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:
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:
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):
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:
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:
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:
Scroll down to this part of the page:
Click on the Firmware github link that will get you to the following page:
On NodeMCUs Firmware page, scroll down to the section that says “Online firmware custom build” and click on the link:
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):
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):
Scroll down on the page, and click the button “Start your build“:
Once you have done this, you will be show this message, showing you the modules that will be built into your custom firmware:
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.
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:
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:
So, using a windows machine, connect the NodeMCU module via USB. It should recognize it and install the necessary drivers.
If ythe drivers are not installed automatically, head on to THIS page to download and install the driver that best fits your system:
After the installation Windows should recognize the module connected by USB.
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:
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:
In the Config tab, write the route to the custom binary firmware you downloaded in the previous section (the one you got via email):
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:
Make sure to select the new firmware file, and deselect the INTERNAL://NODEMCU file using the boxes on the left:
Then, on the Advanced tab select 9600 as the Baudrate speed:
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:
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:
Scroll down, and hit the gigatic Download Now button to download the IDE:
Also, while you’re there, sownload the Getting started with the ESPlorer IDE tutorial, under the Usefull Tutorials section:
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:
Because I will be using my OS X laptop, I executed the following command to start the IDE:
After doing so, you should see ESPlorer load and appear in your screen:
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:
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:
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:
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:
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.
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:
- https://learn.adafruit.com/adafruit-huzzah-esp8266-breakout/using-arduino-ide
- http://esp8266.ru/esplorer/
- https://primalcortex.wordpress.com/2014/12/30/esp8266-nodemcu-and-lua-language-and-some-arduino-issues/
- https://github.com/nodemcu
- http://www.esp8266.com/wiki/doku.php?id=nodemcu
- https://www.youtube.com/watch?v=Qj69qfneNzI
- https://github.com/niesteszeck/idDHT11
Leave a Reply