This has a relay, a button for flashing, a UEXT socket which includes the serial lines used for flashing and communication, and a header for the GPIO and other pins.
I am again using nodemcu and Lua, and MQTT to publish and subscribe of data and commands.
The relay is controlled by publishing “on” or “off” to te relay topic (currently /relay). For testing purpose, I do this from nodered.
It is switching a 12 volt MR16 LED bulb, powered by a 12-volt battery pack. The Olimex ESP8266 EVB device is powered by a USB power pack.
Flashing this device was a little difficult as the board has no USB to serial on-board. (It is powered by a 5v barrel connector). So I had to use a USB TTL Serial Converter, and the one I have does not seem to work consistently for flashing. I don’t know what the reason is. It does not seem to be an issue with the current supplied as the Olimex device has its own power supply, and I have tried using a large capacitor across the supply, but it did not help. A lower serial speed does not help – in fact faster speeds work better. The only solution I have found is to use esptool.py on Linux, set the serial speed very high, and try it several times until it works.
My Lua code is a variant of the code that I used for my ESP8266 sensor.
Here is init.lua:
wifi.setmode(wifi.STATION) wifi.sta.config('ssid','password') wifi.sta.connect() pin = 1 gpio.mode(pin,gpio.OUTPUT) tmr.alarm(1, 5000, 1, function() if wifi.sta.getip()== nil then print('IP unavaiable, waiting...') else tmr.stop(1) print('IP is '..wifi.sta.getip()) dofile('mqtt.lc') tmr.alarm(1, 1000, 1, function() if not c then m:connect('192.168.0.101', 1883, 0, function(conn) print('mqtt connected'); c = true; dofile('subscribe.lc') end) end end) end end)
and here is mqtt.lua, which connects to my MQTT broker:
m = mqtt.Client('nodemcu', 120) c = false m:on('offline', function(con) print ('mqtt offline');c = false end) -- on publish message receive event m:on('message', function(conn, topic, data) print(topic .. ':') if data ~= nil then print(data) end if data == "on" then gpio.write(pin,gpio.HIGH) elseif data == "off" then gpio.write(pin,gpio.LOW) end end) m:connect('192.168.0.101', 1883, 0, function(conn) print('mqtt connected'); c = true; dofile('subscribe.lc') end)
and finally, here is subscribe.lua:
print('Subscribing') if c then m:subscribe('/relay',0, function(conn) print('subscribe success') end) end
I seem to lose the connection to MQTT regularly, so I reconnect, if necessary, on a 1 second timer.