A quick introduction to LUA on the ESP8266: create an Accespoint and LAN client to switch a lamp

If you have been using an ESP8266 with the Arduino IDE, you will nevertheless have heard about LUA and you may be curious to try it.

The object of this article is not to teach LUA, but rather to quickly show  how to get started.

Though some ESP8266 modules and boards come with the LUA interpreter installed, once you have flashed programs to your ESP8266 with your Arduino IDE, that interpreter is gone. So, first we may need to reflash LUA interpreter back in the ESP8266 again:

Putting LUA (back) on your ESP8266.

flasherGo to github.com/ nodemcu and choose the nodemcu-flasher. Download and unpack it. Connect your ESP8266 to your 3v3 USB TTL converter. Go to the Win32/Release folder and start the ESP8266Flasher.exe. Set your ESP8266 in flash mode in the way that is suitable for the board you are using.

Select the proper com port and click ‘Flash’. When the program is done it will return 2 MAC addresses: one for an Accespoint and the other one or a ‘station’.

Close the flasher and reset the ESP8266.

The ESP8266 is now ready for LUA programming.

Uploading LUA Programs

In order to upload a program to your LUA interpreter/ESP8266, there are several programs. I am using ESPlorer. Download and unpack that program and start it with ‘Esplorer.bat’ .

Presuming your ESP8266 is still connected to your computer, choose the proper Com port and baudrate in the right side pane of the program and click connect. The esp8266 will now likely reprt itself and come back with the message: “no init.lua”.

The ‘init.lua’ file is the file that is executed on boot. If we want our program to start on boot, that is the name we will give it.

As it is not my intention to teach LUA here, we will use an existing program: Go back to the Nodemcu github and choose “nodemcu-firmware”. Then clicl ‘lua-examples’, choose ‘webap_toggle_pin.lua, then  click RAW. Copy the code and go back to the ESPlorer program. Choose the ‘Nodemcu/Micropython’ tab and the Scripts tab and paste the copied program in the leftpane of the ESPlorer program.

Save the program as ‘init.lua’  and then upload it to the ESP (under the File tab).

Once the program is uploaded, reset the ESP8266.

As the program configures an Accespoint, go to the wireless connections on your computer. You should see an accesspoint there called ‘test’ log on to that one. When it asks for a password provide ‘ 12345678’. Once you are logged on, go to ip 192.168.4.1. You should be presented with a webpage that allows you to toggle a pin.

Now that may be handy, as te esp8266 is not connected to the internet it can only be accessed from a limited distance. But what if you want to switch a lamp from the other sidof the world? Well i will show you how to change the accespoint created in an accesspoint and LAN client so you can log into it either directly, or via your router.

You need to configure the first 2 lines as follows:

esplorer8266The firstline is changed into ‘wifi.STATIONAP’ this configures the ESP8266 both as an AP and a LAN client. The next line that configures the AP is left untouched, but the LAN still needs to be configured wih ssid and password as shown in the picture. Upload the altered program to the ESP and reset it. The ESP is now both a direct Accesspoint as well as connected to the internet via your router.

gpioOne more remark: the gpio pins of the ESP8266 are not directly translated to the numbers of the ‘pins’  in the program. If you are not using a Wemos D1 or NodeMCU, but rather an ESP8266-01 and you ant to switch GPIO2, in the program you need to state ‘pin4’

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s