SmartArduino / WiFiMCU

WiFiCMU-v0.9.8-pre release
https://github.com/SmartArduino/WiFiMCU
123 stars 65 forks source link

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

Open chuxuanhy opened 8 years ago

chuxuanhy commented 8 years ago

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

nihaopaul commented 8 years ago

Try sharing code snippets On 24 Dec 2015 6:19 pm, "chuxuanhy" notifications@github.com 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 https://github.com/SmartArduino/WiFiMCU/issues/9.

chuxuanhy commented 8 years ago

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 commented 8 years ago

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

chuxuanhy commented 8 years ago

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 commented 8 years ago

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)

chuxuanhy commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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