Hello,
i have the following configuration.
Raspberry Pi with OpenHab and the Mosquitto MQTT broker.
I have built a thermometer on a NodeMCU connected to the well known and loved Ds18b20.
It works nicely and publishes the temperature via MQTT - there is a beautiful graph in OpenHab.
Now I want to make a transmitter that listens to MQTT but I have not succeeded.
I am trying with ESP8266 with Lua to subscribe to a topic on MQTT on Raspberry. I have set up a toggle switch in OpenHab with the topic "home/testsw/1". Switching from the OpenHab console results in nice 0 and 1 commands in the MQTT listen on the Rasbperry.
Unfortunately, while publish works for me with NodeMCU it does not see the commands from the topic, no response. In fact, even "subscribed" does not occur, although such a topic exists. I expected that when changing the topic, the "message" event assignment function would work...
Here is the code
Here is the working code for publishing the temperature:
Added after 5 [hours] 40 [minutes]:
problem solved
the code from this tutorial worked: https://geekgrandad.wordpress.com/2015/03/22/esp8266-mqtt-relay/
The ESP8266 responds nicely here to the subscribed topic and I can make the switch;
by the way, I have 220v sockets based on ESP8266 from a manufacturer in China and I am just going to program them
i have the following configuration.
Raspberry Pi with OpenHab and the Mosquitto MQTT broker.
I have built a thermometer on a NodeMCU connected to the well known and loved Ds18b20.
It works nicely and publishes the temperature via MQTT - there is a beautiful graph in OpenHab.
Now I want to make a transmitter that listens to MQTT but I have not succeeded.
I am trying with ESP8266 with Lua to subscribe to a topic on MQTT on Raspberry. I have set up a toggle switch in OpenHab with the topic "home/testsw/1". Switching from the OpenHab console results in nice 0 and 1 commands in the MQTT listen on the Rasbperry.
Unfortunately, while publish works for me with NodeMCU it does not see the commands from the topic, no response. In fact, even "subscribed" does not occur, although such a topic exists. I expected that when changing the topic, the "message" event assignment function would work...
Here is the code
ip = "192.168.0.195"
port = 1883
seconds = 10
m = mqtt.Client("1", 60)
m:on("message", function(conn, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
end
end)
m:connect(ip, port, 3000, function()
print ("connected")
m:subscribe("home/testsw/1",0, function(conn)
print("subscribed")
end)
end
)Here is the working code for publishing the temperature:
ip = "192.168.0.195"
port = 1883
seconds = 10
tmr.alarm(0, 1000 * seconds, 1, function()
m = mqtt.Client("1", 60)
m:connect(ip, port, 3000, function()
tmr.wdclr()
m:publish("home/humidity", 0, 0, 0, function()
tmr.wdclr()
m:publish("home/temperature", ds18b20.read(), 0, 0, function()
tmr.wdclr()
m:close()
end
)
end
)
end
)
end
)Added after 5 [hours] 40 [minutes]:
problem solved
the code from this tutorial worked: https://geekgrandad.wordpress.com/2015/03/22/esp8266-mqtt-relay/
The ESP8266 responds nicely here to the subscribed topic and I can make the switch;
by the way, I have 220v sockets based on ESP8266 from a manufacturer in China and I am just going to program them