Post your best Lua script examples here

User avatar
By gwizz
#7587
Fr4gg0r wrote:I transformed your lua code into a lua module (https://github.com/jrahlf/nodemcu-firmw ... 512224d0c3).
Edit: But it is bugged currently.


Wow - I'm delighted that you thought my code/idea was worth implementing - I'd be really pleased if it got pulled into the lua firmware!!

I will try to look at the code with my (much better programmer than I) buddy in the next day or two - together we might be able to squash some bugs!

Can I make a suggestion that may make it more useful for all of us?

Either - optionally the module could initialise the i2c bus with the pins that it found during scanning, returning the i2c address,
OR
The scan function could return the sda, scl and addr values - rather than just printing them.

The basic idea is that instead of defining pins, you could just call i2c_scan function and have the automagic do it's thing.

I really like this idea of getting the device to work out the wiring - I can't tell you how much time I've wasted getting the tx and rx the wrong way round etc.
User avatar
By Fr4gg0r
#7624 I got it working now, mistakes I made were:
-I used GPIO 9,10 which are wired to the flash normally (don't use them :p)
-If SDA is pulled low (in my case GPIO0 after flashing), the i2c master always returns an address

The scanner now returns SDA, SCL, and device address (of the last found device!), since the method is called "scan" it should not change any states. The method takes approximately 1s to scan all standard GPIOs.

Sample output:
Code: Select all=i2c.scan(0)
scanning on i2c bus 0
found device at 0x0/0, SDA at GPIO0, SCL at GPIO2
found device at 0x1/1, SDA at GPIO0, SCL at GPIO2
found device at 0x2/2, SDA at GPIO0, SCL at GPIO2
found device at 0x3/3, SDA at GPIO0, SCL at GPIO2
skipping GPIO0 , maybe SDA/GPIO0 is pulled low?
found device at 0x3d/61, SDA at GPIO4, SCL at GPIO5
4       5       61
>
> =i2c.scan(0,5,4)
scanning on i2c bus 0
found device at 0x3d/61, SDA at GPIO4, SCL at GPIO5
4       5       61
>
> =i2c.scan(0,13,14)
scanning on i2c bus 0
no i2c devices found!
nil     nil     nil


Code can be found here: https://github.com/jrahlf/nodemcu-firmw ... ules/i2c.c
The code won't directly work with the original nodemcu version, because I am working without GPIO remapping.
User avatar
By gwizz
#7637 Hey that's really great work, and very quick too!

Would it be able to be pulled into the main firmware if the GPIO mapping was sorted out?

Thanks again - good job fr4gg0r!