Friday, February 5, 2016

Esp8266 Arduino IDE & NodeMCU Lua Tutorials


This Tutorial is made for your Esp8266 Boards 


(Any Model With Usb / External Usb2Serial)



Installing Usb to Serial 

Checking your Esp8266 : Open Device Manager > Ports > There Must Be a Port Available Like Com3 or Com31



Note: There is no Esp8266 Serial Port Detected here in the image.

You should install ch340g or cp2102 drivers. Find Here



You can install Any Firmware on Esp8266 

1) Arduino Sketch
2) Nodemcu Lua Firmware 
3) Smart.js Javascript
4) Micropython
5) Sming Framework
6) Basic Interpretter
7) Factory AT Commands Firmware (Version .92)


This Tutorial Points to Leading Two.



You can always Re-Flash Any Firmware by :

Press Flash Button and Hold it while connecting to PC , then After 2 Seconds, leave flash button, and upload your code from arduino 1.6.5 / other Firmware.








Installing Arduino IDE & Esp8266 Addons

1) Installing with Boards Manager

Starting with 1.6.4, Arduino allows installation of third-party platform packages using Boards Manager. We have packages available for Windows, Mac OS, and Linux (32 and 64 bit).
  • Install Arduino 1.6.5 from the Arduino website.
  • Start Arduino and open Preferences window.
  • Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.
  • Open Boards Manager from Tools > Board menu and install esp8266 platform (and don't forget to select your ESP8266 board from Tools > Board menu after installation).
The best place to ask questions related to this core is ESP8266 community forum: http://www.esp8266.com/arduino. If you find this forum or the ESP8266 Boards Manager package useful, please consider supporting it with a donation. 

Video on How to install Arduino IDE ADDON For Esp8266  

Video Tutorial 1


After Installing Esp8266 Addons in Board Manager
Open Arduino 1.6.5 > Now Choose Tools > Board > Go to Bottom > Select Nodemcu 1.0 (Esp-12E)







Faq:

You can connect 5v sensors / input to your esp8266.
Because they are 5v tolerant pins.

Never apply 5v to esp8266. It works on 3.3v. ( converter included in usb boards)



Popular Links:


Arduino IDE for Esp8266 Github (All Informations) Link


WiFi(ESP8266WiFi library) Link

Filesystem Flash layout Link


Examples:

Some Basic Examples http://www.arduinesp.com/examples

Esp8266 Arduino & Lua Blog For Everything About Coding  
http://www.esp8266.com/viewforum.php?f=25











Installing NodeMcu Lua on Esp8266

How do I install NodeMCU on my ESP8266?

Plug the USB to serial adapter into your PC. Most adapters are automatically recognized by Windows 7 and 8. Users of XP or older versions of Windows may need a driver which should be supplied by the manufacturer of the USB adapter. The adapter will appear as a new COM port on your system. Check the Device Manager if necessary.
After Downloading, 
Press Flash Button and Hold it while connecting to PC , then leave flash button .
Run ESP8266Flasher.exe
Do not change anything except Com port, It will auto detect memory size.

NodeMcuFlasher

If you only have one COM device, the port will appear automatically. If you have more than one, select the correct one from the drop down menu.
The flasher program comes with default NodeMCU firmware in memory. This is unlikely to be the latest version, but to get started quickly, use the default internal image. 
Click on Flash(E) to start the programming process. If the MAC addresses don't appear within a few seconds, disconnect the power to the board and reconnect it. If it still doesn't work, check your connections. Make sure that the jumper between GND and GPIO0 is there so that the board is in reflash mode.
Reflashing the chip will take a couple of minutes. When the job is done, disconnect power from the board and remove the jumper from GND to GPIO0 to leave reflash mode.
You are now ready to program the ESP8266 for your own application.

How do I communicate with the ESP8266?

The NodeMCU Lua firmware you have just installed communicates with the PC over the serial link. You can use PuTTY, SimpleTerm, or any other terminal program that you like to write Lua programs or enter direct commands to the board. The default speed is 9600 baud, but that can be changed to any other baud rate once you have established communications.

These are Top Popular Softwares fot this job:
1) Esplorer  (Download Latest From Esplorer.zip )

2) LuaLoader (See Below)

LuaLoader

LuaLoader is a Windows program for uploading files to the ESP8266 and working with the Lua serial interface. As well as being a simple terminal program, it has built in Lua command buttons that make it easy to interact and experiment with the ESP8266 board.
Download: LuaLoader.zip
LuaLoader.exe is a simple Windows application that requires no special installation.

From the Settings menu, select COM Port Settings and choose the appropriate COM port for your USB to serial adapter. Don't change any other setting.
Clicking buttons on LuaLoader sends commands to the board. Power your board and watch for the initial message. After displaying some funny characters (these are firmware reset parameters sent at 74880 baud), you should see the version information of the NodeMCU build:
NodeMCU 0.9.5 build 20150107 powered by Lua 5.1.4
LuaLoader will warn you if a later firmware build is available.
After booting, NodeMCU will attempt to run a file called init.lua in flash memory. Since you haven't put it there, it will report an error and show the > prompt. You are now ready to interact with Lua on the ESP8266.
Click the Heap button to display the amount of RAM available. LuaLoader will type the command = node.heap()and the ESP8266 will respond with 23016 or some other value, followed by the > prompt again.

How do I connect to an Access Point?


Enter the SSID and password of your closest access point in the appropriate text box on LuaLoader. Click Set AP. LuaLoader will type the commands to set the WiFi mode and connect. This information is saved in flash memory and will be remembered until changed, even if the board is powered down.
Click the  WiFi button to check the connection status.
Click Get IP to confirm that the ESP8266 is connected to your access point.
Click the Survey button to list the available access points visible to the ESP8266. The SSID, authentication mode, signal strength, MAC address and channel are shown.
All of the commands typed by LuaLoader can be entered manually, or saved in a file to be run automatically.

How do I read a GPIO input?

The Lua function gpio.read(pin) reads the value of the input specified by pin if the GPIO has been set up as an input.
LuaLoader makes this easy to test. Select pin 3 GPIO0 from the GPIO dropdown menu, select input and floating from the mode dropdown menus. Click Set. Then, click Read. The current pin value will be 0 or 1. In this case, 1 as that pin is held high normally.
Note that if GPIO0 is low on reset, the chip will enter reflashing mode, so you must ensure that any use of that GPIO0 for user input is high when the device is powered on or reset.
To read the value of the input pin repeatedly, in LuaLoader, click on the clock beside the Read button. Note, that if you are using the NodeMCU development board, the button marked FLASH will change the value from 1 to 0 when the button is pressed as it is connected to GPIO0 and ground. The read rate can be changed in the Settings menu.

How do I send data to a server?

The demo script httpget.lua connects to this webserver and sends the time it took to connect (in microseconds) and the amount of heap (RAM) available at that time.
Download the script to your computer, then upload it to your ESP8266 using theUpload File button.
-- tested on NodeMCU 0.9.5 build 20141222...20150108
-- sends connection time and heap size to http://benlo.com/esp8266/test.php

print('httpget.lua started')
Tstart  = tmr.now()

conn = nil
conn = net.createConnection(net.TCP, 0) 

-- show the retrieved web page

conn:on("receive", function(conn, payload) 
                       success = true
                       print(payload) 
                       end) 

-- once connected, request page (send parameters to a php script)

conn:on("connection", function(conn, payload) 
                       print('\nConnected') 
                       conn:send("GET /esp8266/test.php?"
                        .."T="..(tmr.now()-Tstart)
                        .."&heap="..node.heap()
                        .." HTTP/1.1\r\n" 
                        .."Host: benlo.com\r\n" 
                        .."Connection: close\r\n"
                        .."Accept: */*\r\n" 
                        .."User-Agent: Mozilla/4.0 "
                        .."(compatible; esp8266 Lua; "
                        .."Windows NT 5.1)\r\n" 
                        .."\r\n")
                       end) 

-- when disconnected, let it be known
conn:on("disconnection", function(conn, payload) print('\nDisconnected') end)
                                             
conn:connect(80,'benlo.com') 

Connect to your access point first using the Set AP button. This is not normally necessary if you have set the correct AP since the last time you flashed new firmware.
Use the dofile button to run the file you have just uploaded.
The contents of the retrieved page should be displayed.
httpget.lua test
Feel free to use my server for a few tests, but please change to your own webserver or sign up for one of the public internet of things servers, such as thingspeak.com. Claudiuchiru has posted an example of sending temperature (using an inexpensiveBS18B20 I2C sensor) to thingspeak.com.
This simple script is all it takes to make useful gadgets from the simplest ESP-01 board. The board can be connected so that it receives power when a device is on, a button is pressed, motion is detected, a magnet trips a reed switch, etc. etc.
The php script will receive the signal and can be programmed to send an email, send an sms, push a notification to your phone, or simply log events for future reference.
Our well pump is now monitored by an ESP-01 running a simple script once every 30 seconds when the power is on. As well as logging the activity and sending a daily email report, the script sends out emergency notifications if the pump runs for more than 2 hours, or hasn't run for more than 36 hours.
motion detector with a relay turns on another ESP-01 which runs the same program but calls a different php script to alert me when there is motion in front of the house. My phone receives the alert before the doorbell is pressed.

How do I make the program run automatically?

When the ESP8266 starts, it runs the init.lua file. Simply create a file called init.luawhich runs the file with your program instructions. It is also a good place to initialize any variables you are using, such as the SSID of the access point. While debugging, it is a good idea to use a timer to call your file after a few seconds. If you make a mistake, causing a crash, you will have a few seconds to stop the timer with thetmr.stop button before the program starts to run again. For the example above, one line will do:
tmr.alarm(0,10000,0,function() dofile('httpget.lua') end)
Arm timer number 0 to execute the function dofile('httpget.lua') in 10 seconds.

How can I learn more?

The Help menu of LuaLoader has a large number of links to ESP8266 and Lua resources for convenient access while programming. There is also a menu item linking files on your computer you may have downloaded for reference.
Useful Links:




Nodemcu CH340G Board



Nodemcu CP2102 Board



Other Versions Pinouts You Can Find From Internet