Re: SPI OLED Example
Posted: Wed Aug 24, 2016 11:39 pm
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:
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
Code: Select all
dhtpin = 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