Sunday, October 18, 2015

ESP8266 experimentation continues - Trying out LUA

In my previous video on the ESP8266, I used the Arduino environment to do a little bit of work with it. However, Arduino is a bit low level and the programming is tedious to get very much done. Another option with this chip is the language LUA, which is far more full featured. I'm going to talk about that a bit here.

I'm also going to use the original little board with the 8 pin header, because I have them sitting around and because the project I'm planning only needs one I/O pin. I don't recommend buying this module anymore, it's not really significantly cheaper than the ones with a ton of I/O broken out, so unless you have some specific use for one with almost no I/O buy the bigger modules.

I watched several videos and looked at a few wiring diagrams, there's a lot of info out there. I don't have anything original here, I'm just recording what worked for me, as much for my own memory as anything else. As such I hope this is useful to you but it's not a very polished article.



You need a USB to serial converter board that supports 3.3V - don't try to use a 5V one. I recommend one with the CH340 chip.  I bought this sweet looking board off eBay for about $3.50:

I don't know if Windows is now loading CH340 drivers, or they came in via Arduino. In any case if you plug yours in and it doesn't register as a com port, try this driver (I haven't tried it, it's just a link I found):

http://www.arduined.eu/files/windows8/CH341SER.zip

Installing LUA on your board

To hook the converter to the ESP8266, you need to wire things up like so:


Image from Arduino.cc
- Attach GND and GPIO0 to the GND pin in your converter (ONLY during this stage)
- Attach RX to TX on your converter and TX to RX on your converter
- Attach VCC and CH_PD to +3.3V supply
- if you're using a separate 3.3V supply, attach the ground of the supply to this ground also, for a common reference.
I had to use an external 3.3v power supply to run the ESP8266 board, it just draws too much power and the USB to serial converter just kept resetting and causing trouble.

To install LUA on your board, you need to download the software to do it at this location:

https://github.com/nodemcu/nodemcu-flasher/archive/master.zip

Unzip that wherever you like. 

Connecting GPIO0 to ground before applying power causes the ESP8266 to enter bootloader mode, which will make it sit and wait for you to upload new firmware to it when you turn it on.  Plug your USB to serial converter and if you don't already know what port it's on, look in device manager to see what serial port just appeared:



In the NodeMCU-Flasher that you extracted from the ZIP before, Go into the Win64/release or Win32/release directory as appropriate and run the ESP8266Flasher.exe program.  Make sure the correct COM port (discovered above) is selected and click the "Flash" button. You should get a pair of MAC addresses and it should start programming LUA into your ESP8266. Let it finish. You'll get a nice green check in the lower left when it's done.



Now in order to begin using LUA on your board, remove power from it and move the GPIO0 pin to VCC (3.3v) and power it back up again.

The program I found worked for me for experimenting with LUA is LUA Uploader, here's a video intro from the author:



From the links on that video, here's the URL for grabbing a copy:


Click on the latest version, then click the "Raw" button to download the ZIP. Extract that wherever you like.

When you run that you can connect to the COM port and start playing with LUA.

Here's a page that also covers everything I've said here and also has good example code for controlling a couple of LEDs from a web page. This is a really good thing to do for your first project, it's very straightforward and shows some useful functionality that you can expand on.

http://randomnerdtutorials.com/esp8266-web-server/

For my actual project, I wanted to create a temperature monitor.  I had a HELL of a time getting the board to talk to a DS18B20 temp sensor, there is some rather flaky example code out there that is just broken, but finally I found some that worked.

Here's a photo of the board I made.  The power goes in the left, the programmer (when needed) plugs in the top row of 5 pins. The DS18B20 attaches up top there.  There's a switch under the board that connects GPIO0 to ground for initial firmware flashing.


and with the programmer plugged in:
Hitting the server with the example code:

Here's the code that I finally got working:

No comments:

New printer: JGAurora A3

This week I decided I'd had it with all the other printers in my stable.  The CTC is stable and decent but it just bugs me (can't st...