ESP8266 MQTT Relay

relay

Another development board for the ESP8266 that I have tried is the Olimex ESP8266 EVB, which I bought from ebay.

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.

Advertisements
This entry was posted in gadgets, Home automation and tagged , , . Bookmark the permalink.

2 Responses to ESP8266 MQTT Relay

  1. Karl says:

    I’m attempting to create a text message / email message based on 3 different data input signals (high or low) I read your review on Amazon (thanks), but still don’t fully understand how these EPS 8266 units work. Is this a stand alone wifi that would be able to send a text message, or is it just a radio that needs to interact with something else? Any help? Any suggestions for further reading?

    Thanks,

    Karl

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s