Post your best Lua script examples here

User avatar
By gwizz
#6226 Hiya

Thought I'd just break out a line from a script I developed recently to allow the use of GPIO numbers as reference rather than the internal IO codes. It saves having to look-up the reference and also reduces the chances for simple mistakes. This array is good for the new mapping, after build 20141219.

Code: Select allgpio= {[0]=3,[2]=4,[4]=2,[5]=1,[12]=6,[13]=7,[14]=5,[15]=8,[16]=0}


After this line you can refer to pins like this:
Code: Select allsda=gpio[12] -- connect sda to pin GPIO12
led=gpio[14] -- connect led to pin GPIO14


Hope this is a handy line!
User avatar
By baracudaz
#6263 Yeah I noticed the same when I started working with nodemcu and learning lua. There seem to be many ways how to approach this. As I am using ESP-01 module my gpio mapping is rather simple:
Code: Select allt = require("ds18b20")

-- GPIO mapping for ESP-01
gpio0, gpio2 = 3, 4

t.setup(gpio0)

ESP-01 module pinout for reference, just in case: http://www.electrodragon.com/w/Wi07c#Pin_Wiring_.28V090.29
User avatar
By gwizz
#6335
ozayturay wrote:That's smart, and shows the power of lua. :)


Ah, thanks! I think it's quite neat, but not so neat that the meaning isn't clear!!! Going the other way round is even neater -
Code: Select allgpio_pin= {5,4,0,2,14,12,13} -- this maps internal IO references to GPIO numbers

because of the syntax shortcut of autonumbering the array (starting from 1 NOT 0 btw!).

Lua is great, I haven't got much deep experience of any programming language but I've hacked a bit on literally dozens over the years. Lua seems a bit easier than most, and very powerful at exactly the sort of things I'm interested in these days. I am especially interested in how it links easily with C libraries - this could be a real advantage to trying to re-use code from all over the web. We've got a rag-bag of sensors so easily re-using other libraries will be a great time saver.