Post your best Lua script examples here

User avatar
By Robot Hacker
#53758 It has been a while but I had modified the example at https://github.com/nodemcu/nodemcu-firm ... s_test.lua to read a dht11 and display the temperature and humidity every 5 seconds:

Code: Select alldhtpin = 1;

function init_spi_display()
    -- Hardware SPI CLK  = GPIO14
    -- Hardware SPI MOSI = GPIO13
    -- Hardware SPI MISO = GPIO12 (not used)
    -- CS, D/C, and RES can be assigned freely to available GPIOs
    local cs  = 8; -- GPIO15, pull-down 10k to GND
    local dc  = 4; -- GPIO2
    local res = 0; -- GPIO16

    spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8);
    disp = u8g.ssd1306_128x64_hw_spi(cs, dc, res);
end

function prepare()
    disp:setFont(u8g.font_6x10);
    disp:setFontRefHeightExtendedText();
    disp:setDefaultForegroundColor();
    disp:setFontPosTop();
end

function draw(dispstring)
   disp:drawStr(0, 0, dispstring);
end

function graphics_test()

   status, temp, humi, temp_dec, humi_dec = dht.read(dhtpin);

   if status == dht.OK then

      disp:firstPage();
      repeat
         draw( string.format("DHT T:%d;H:%d\r\n", math.floor(temp), math.floor(humi) ));
      until ( disp:nextPage() == false )

   elseif status == dht.ERROR_CHECKSUM then
      print( "DHT Checksum error." );
   elseif status == dht.ERROR_TIMEOUT then
      print( "DHT timed out." );
   end

    print("Heapb: " .. node.heap());
   collectgarbage();
    print("Heapa: " .. node.heap());
    -- retrigger timer to give room for system housekeeping
    tmr.start(0);
end

init_spi_display();

disp:firstPage();
prepare();

-- set up timer 0 with short interval, will be retriggered in graphics_test()
tmr.register(0, 5000, tmr.ALARM_SEMI, function() graphics_test() end);
tmr.start(0);


To get the u8g_graphics_test.lua to work with the SPI OLED I had to change one of the last lines to comment out the I2C init and uncomment the SPI init.

Also, there is a good YouTube video on the picture loop, it is using Arduino but has good infomation on how the picture loop (or draw loop) functions: https://www.youtube.com/watch?v=33sOIHPwpfQ
User avatar
By Arakon
#82738 Moin.

I'm using the SSD1306 display for a blood glucose monitor.
Note that I'm no coder, I merely pieced together the current functionality from another project.
My problem is that after a random amount of time, the display simply turns off. This can happen after 30 seconds or 2 hours, seems to be totally random. Power source doesn't seem to matter. The only way to get it working is by removing power and plugging back in, resetting doesn't change anything.
I found a few people reporting similar such issues, but no solution.
Here's my code so far:
https://github.com/Arakon/NodeMCU-NightScoutDisplay

Any ideas on this?