As the title says... Chat on...

User avatar
By Cobozco
#18771 Hello and thanks for whoever takes the time to read this - I've been working on this to no success for the past couple weeks for school. Basic ways to use Mosquitto on the Raspberry Pi to publish/subscribe to the ESP2866 on the NodeMCU - visa versa.

In terms of the node I`ve tried the test and example code posted herehttp://www.esp8266.com/viewtopic.php?f=19&t=1278

In terms of R.Pi
I'm running two separate terminal windows one running a subscribing mosquittto line, and another publishing
mosquitto_sub -h 192.168.0.102 -p 1883 -t /#
while true; do mosquitto_pub -h 192.168.0.102 -p 1883 -t /test -m Hi; done

Code: Select allSSID = "Bakery229"
password = "electronics229"

wifi.setmode(wifi.STATION)
wifi.sta.config(SSID, password)
wifi.sta.connect()

print("ESP8266 mode is: " .. wifi.getmode())
print("The module MAC address is: " .. wifi.ap.getmac())
print("Config done, IP is "..wifi.sta.getip())


m = mqtt.Client(wifi.sta.getmac(), 120)
m:lwt("/lwt", wifi.sta.getmac(), 0, 0)

m:on("offline", function(con)
     print ("reconnecting...")
     print(node.heap())
     tmr.alarm(1, 10000, 0, function()
          m:connect("192.168.0.102", 1883, 0)
     end)
end)

-- on publish message receive event
m:on("It works", function(conn, topic, data)
  print(topic .. ":" )
  if data ~= nil then
    print(data)
  end
end)

tmr.alarm(0, 1000, 1, function()
 if wifi.sta.status() == 5 then
     tmr.stop(0)
     m:connect("192.168.0.102", 1883, 0, function(conn)
          print("connected")
          m:subscribe("/test",0, function(conn)
               m:publish("/test","hello",0,0, function(conn) print("sent") end)
          end)
     end)
 end
end)


I'm getting the message sent through to the other terminal, however nothing for the NodeMCUoutput:
> dofile("test270515.lua")
ESP8266 mode is: 1
The module MAC address is: 1A-FE-34-A0-7A-FC
Config done, IP is 192.168.0.186
test270515.lua:19: method not supported
>

The 19th line:
Code: Select allm:on("It works", function(conn, topic, data)


http://b.truzzi.me/home-temperature-monitoring-using-openhab-rpi-esp8266-and-hdc1000/
I also looked at his code for the NodeMCU - similar errors with the m:connect line

Thanks again if you're willing to help
Last edited by Cobozco on Thu May 28, 2015 3:31 pm, edited 1 time in total.
User avatar
By Cobozco
#18772 The dream initially was to have an OpenHAB app publish a 0 or 1 to the NodeMCU which would later send a HI or LO depending to a GPIO pin for other circuitry...

Code: Select all-- connecting to R.PI(broker)
--Test over wifi with MQTT & LUA scripting

broker = "192.168.0.112"     -- IP or hostname of MQTT broker
mqttport = 1883          -- MQTT port (default 1883)
userID = ""              -- username for authentication if required
userPWD  = ""            -- user password if needed for security
clientID = "/light"        -- Device ID
ledPin = 4                -- IO Index of GPIO2 which is connected to an LED
count = 0                -- Test number of mqtt_do cycles
mqtt_state = 0           -- State control
WiFiUserID = "Bakery229"
WiFiPWD = "electronics229"
RoomID = "1"

gpio.mode(ledPin, gpio.OUTPUT)
gpio.write(ledPin, gpio.HIGH)

wifi.setmode(wifi.STATION)
wifi.sta.config(WiFiUserID,WiFiPWD)
wifi.sta.connect()

tmr.alarm(1, 1000, 1, function()
  if wifi.sta.getip()== nil then
  print("IP unavaiable, Waiting...")
 else
  tmr.stop(1)
 print("ESP8266 mode is: " .. wifi.getmode())
 print("The module MAC address is: " .. wifi.ap.getmac())
 print("Config done, IP is "..wifi.sta.getip())
 end
 end)

m = mqtt.Client("ESP8266".. clientID, 180)
m:lwt("/lwt", "ESP8266", 0, 0)
m:on("offline", function(con)   
    print ("Mqtt Reconnecting...")   
    tmr.alarm(1, 10000, 0, function() 
      m:connect(broker, 1883, 0, function(conn)   
        print("Mqtt Connected to:" .. broker) 
        mqtt_sub() --run the subscription function 
      end) 
    end) 
 end) 
 
  m:on("message", function(conn, topic, data)   
    print("Recieved:" .. topic .. ":" .. data)   
      if (data=="0") then 
      print("LED OFF")   
      gpio.write(ledPin,gpio.LOW) 
      m:publish("/light","ON",0,0) 
    elseif (data=="1") then 
      print("LED ON")   
      gpio.write(ledPin,gpio.HIGH) 
      m:publish("light","OFF",0,0) 
    else 
      print("Invalid - Ignoring")   
    end   
 end) 
 
 function mqtt_sub() 
    m:subscribe("/light",0, function(conn)   
      print("Mqtt Subscribed to OpenHAB feed for device " .. clientID) 
    end) 
 end 
 tmr.alarm(0, 1000, 1, function() 
  if wifi.sta.status() == 5 and wifi.sta.getip() ~= nil then 
    tmr.stop(0) 
    m:connect(broker, 1883, 0, function(conn)   
      print("Mqtt Connected to:" .. broker) 
      mqtt_sub() --run the subscription function 
    end) 
  end 
 end


The result of this was output of the node heap (bunch of numbers) and it "reconnecting"