Aug 132016
 

2 more I/O shields: Temperature and humity sensor (DHT22 AKA AM2302) and here the super-simple example:

print(dht.read(4))
0 26 1 800 0

That’s 26.8 °C and 1.0% humidity. I somehow don’t trust the humidity…

And here the push button module example, which shows well how much the contact bounce is:

do
 local pin=3
 gpio.mode(pin, gpio.INT, gpio.PULLUP)
 local pulse1=0

 local function pushed(level)
 local pulse2 = tmr.now()
 print("Level: ", level, "since last change: ", pulse2-pulse1, " ns")
 pulse1=pulse2
 end
 
 gpio.trig(pin, "down", pushed)
end

 

 

Aug 132016
 

It was surprisingly difficult to find some working nodeMCU examples. Thus here my findings and sample code:

First the WS2812 shield: It’s using D2 (NodeMCU numbering, which is printed on the board). The ws2812 module for NodeMCU uses D4 though. Here what it looks like:

Wemos-WS2812-modified

With this change, this sample program works:

ws2812.init()
ws2812.write(string.char(100, 20, 5))

Green 100, Red 20, Blue 5. GRB.

The OLED shield needs no hardware modification, but it’s using non-standard I²C pins it seems. Here a working example (using a very slightly modified https://github.com/nodemcu/nodemcu-firmware/blob/master/lua_examples/u8glib/u8g_graphics_test.lua).

I removed the SPI parts, adjusted the pins. The rest is unmodified:

-- ***************************************************************************
-- Rotation Test
--
-- This script executes the rotation features of u8glib to test their Lua
-- integration.
--
-- Note: It is prepared for SSD1306-based displays. Select your connectivity
-- type by calling either init_i2c_display() or init_spi_display() at
-- the bottom of this file.
--
-- ***************************************************************************

-- setup I2c and connect display
function init_i2c_display()
 -- SDA and SCL can be assigned freely to available GPIOs
 local sda = 2 -- 5 -- GPIO14
 local scl = 1 -- 6 -- GPIO12
 local sla = 0x3c
 i2c.setup(0, sda, scl, i2c.SLOW)
 disp = u8g.ssd1306_64x48_i2c(sla)
end


-- the draw() routine
function draw()
 disp:setFont(u8g.font_6x10)
 
 disp:drawStr( 0+0, 20+0, "Hello!")
 disp:drawStr( 0+2, 20+16, "Hello!")

 disp:drawBox(0, 0, 3, 3)
 disp:drawBox(disp:getWidth()-6, 0, 6, 6)
 disp:drawBox(disp:getWidth()-9, disp:getHeight()-9, 9, 9)
 disp:drawBox(0, disp:getHeight()-12, 12, 12)
end


function rotate()
 if (next_rotation < tmr.now() / 1000) then
  if (dir == 0) then
   disp:undoRotation()
  elseif (dir == 1) then
   disp:setRot90()
  elseif (dir == 2) then
   disp:setRot180()
  elseif (dir == 3) then
   disp:setRot270()
  end

  dir = dir + 1
  dir = bit.band(dir, 3)
  -- schedule next rotation step in 1000ms
  next_rotation = tmr.now() / 1000 + 1000
 end
end

function rotation_test()
 print("--- Starting Rotation Test ---")
 dir = 0
 next_rotation = 0
 print("size:", disp:getWidth(), disp:getHeight())
 local loopcnt
 for loopcnt = 1, 100, 1 do
  rotate()

  disp:firstPage()
  repeat
    draw()
  until disp:nextPage() == false

  tmr.delay(100000)
  tmr.wdclr()
  end

 print("--- Rotation Test done ---")
end

init_i2c_display()
rotation_test()

The important bits: SDA=2, SCL=1, ADDR=0x3C (there’s a jumper to change it to 0x3D if needed)

 

Aug 132016
 
Wemos D1

Got some new NodeMCU modules: Wemos D1. Very neat small modules with USB and “shields”, one of them being a 0.66″ I²C OLED display with 64×48 pixel. Very cute.

I needed some extra NodeMCU modules (specifically 1-wire and u8g), so a new firmware was in order. Best place for that is http://nodemcu-build.com/. Highly recommended.

One odd problem I saw though: when using the usual

./esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash 0x0 
~/Downloads/nodemcu-master-20-modules-2016-08-13-01-27-33-integer.bin

to program the FLASH, all I got is a stream of data on the serial port after a reset and not the usual prompt. Power cycle did not change that behavior. Neither did waiting. Even re-programming esp_data_init_default.bin did not help. What did help is adding the -fm and -fs parameters:

./esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash -fm dio -fs 32m 0x0 
~/Downloads/nodemcu-master-20-modules-2016-08-13-01-27-33-integer.bin

The interesting bit is that programming an older image from about a month ago worked fine without -fm and -fs. Which made me think there’s something buggy in the creation of the image (e.g. the new modules I added are broken).