Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Payload passed to the publish method of MQTT did not send properly #9

Open
chuxuanhy opened this issue Dec 24, 2015 · 9 comments
Open

Comments

@chuxuanhy
Copy link

I tried publishing a message, the message did not being sent correctly, the value of topic was always sent as payload message.

@nihaopaul
Copy link

Try sharing code snippets
On 24 Dec 2015 6:19 pm, "chuxuanhy" [email protected] wrote:

I tried publishing a message, the message did not being sent correctly,
the value of topic was always sent as payload message


Reply to this email directly or view it on GitHub
#9.

@chuxuanhy
Copy link
Author

I used the following code snippets and I always received "/tangtret/phongtrung/tran/tu/relay1" or "/tangtret/phongtrung/tran/tu/relay2" instead of "ON" / "OFF" value.

print("------mqtt demo------")

cfg={
ssid='xxxxxx',
pwd='xxxxxx'}
wifi.startsta(cfg);
cfg=nil;

tmr.start(1,1000,function()
print('Check STA Status:'..wifi.sta.getip())
if(wifi.sta.getip()~='0.0.0.0') then
tmr.stop(1)
doMQTT()
end
end)

clientid = "EMW3165_" .. mcu.chipid()
keepalive = 999999999
username = 'xxxxxx'
password = 'xxxxxx'
server = 'ntm.diyoracle.com'
port = 1883
subTopic = "/tangtret/phongtrung/tran/toi/#"
mainTopic = "/tangtret/phongtrung/tran"
QoS = mqtt.QOS0
mqttClt = mqtt.new(clientid,keepalive, username,password)
function doMQTT()
mqtt.on(mqttClt,'connect',
function()
print('mqtt connected to server')
mqtt.subscribe(mqttClt,subTopic,QoS)
print('mqtt subscribe topic:'..subTopic)
end)
mqtt.on(mqttClt,'offline',
function()
print('mqtt disconnected from server')
end)
mqtt.on(mqttClt,'message',cb_messagearrived)
mqtt.start(mqttClt,server,port)
end
...
gpio04 = 4
gpio.mode(gpio04,gpio.INPUT_PULL_UP)
tmr.start(3, 100, function()
if gpio.read(gpio04) == 0 then
-- print ("Button 0 = On")
if switch2 == 1 then
--print ("Switch already on")
else
switch2 = 1
print ("Switch2 on")
mqtt.publish(mqttClt, "/tangtret/phongtrung/tran/tu/relay2",mqtt.QOS0, 'OFF')
end
else
-- print ("Button 0 = Off")
if switch2 == 0 then
--print ("Switch already Off")
else
switch2 = 0
print ("Switch2 off")
mqtt.publish(mqttClt, "/tangtret/phongtrung/tran/tu/relay2",mqtt.QOS0, 'ON')
end
end
end)

@SmartArduino
Copy link
Owner

What do you get if you try:test.mosquitto.org for test?

@chuxuanhy
Copy link
Author

It also behaved as mention before, the client sent the topic string as payload. Please look at the following picture!
http://i.imgur.com/0GTtjBU.jpg

Would you please fix this soon?

@nihaopaul
Copy link

you'll have to use callbacks:

  1. setup connections/config
  2. initiate connection, return callback
  3. on callback subscribe to channel, return callback
  4. on callback publish payload

if you receive a value, that is not a string, then you'll need to cast it to a number:
value = tonumber(value)

  • sorry i dont have my wifimcu with me to test on

@chuxuanhy
Copy link
Author

Oh my god, believe me, I played with MQTT protocol for several projects before, I am sure the MQTT library of this wifimcu has issue with publishing message. The demo also did not publish 'hiwifimcu', it published the topic instead. Please tell the one who developed the library about this problem. Please ... please, I am not good at C++ to understand the original source in C of this library.

@SmartArduino
Copy link
Owner

The demo is tested between two wifimcus, and it works. It's true that it published the topic if only one wifimcu was used. We found this while coding. This is the function provided from MXChip.inc SDK. Let us know if any more prolems.

@wuming123057
Copy link

Mqtt client publish message, message content is the topic name.like this:

message recevied from /a/a: qos=0 
/a/a
message recevied from /a/a: qos=0     
/a/a
message recevied from /a/a: qos=0    
/a/a
message recevied from /a/a: qos=0   
/a/a
message recevied from /a/a: qos=0   
/a/a

This is my code

topic='/a/a'
mqtt.publish(mqttClt,topic,mqtt.QOS0, 'hiwifimcu').

And I have test

topic='/a/a'
mqtt.publish(mqttClt,topic,mqtt.QOS0, 0,'hiwifimcu').

It is normal when I use other client's API.

@SmartArduino
Copy link
Owner

The mqtt publish bug is fixed.
The firmware is updated to 0.9.8.1
Thanks all. @chuxuanhy @nihaopaul @wuming123057

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants