esar / contiki-mqtt

MQTT client library for Contiki
http://hacks.esar.org.uk/contiki-mqtt-client/
102 stars 39 forks source link

How to add MQTT in Contiki OS #4

Open nabinpradhan opened 9 years ago

nabinpradhan commented 9 years ago

Hi

I want to add MQTT in contiki and I followed https://github.com/esar/contiki-mqtt I created a mqtt-service folder inside contiki/apps/directory and copied the files How will i add mqtt-service to the APPS variable in application's makefile???

please help

Regards Nabin Pradhan

esar commented 9 years ago

You should just need to add a line to your Makefile like this: APPS = mqtt-service

For example, see this Makefile for one of my projects (which also includes a couple of other libraries in the APPS variable): https://github.com/esar/myha/blob/master/devices/led-dimmer-6ch/Makefile

rafaeldelucena commented 8 years ago

Hi!

I have found some problems using your example with some adaptations, i setup mqtt-service as you describe above and create an example on examples directory with the makefile and the code below:

all: $(CONTIKI_PROJECT)
CONTIKI=../..
DEFAULT_TARGET=native

APPS = mqtt-service
PROJECT_SOURCEFILES = mqtt-publisher.c

include $(CONTIKI)/Makefile.include
#include "stdio.h"
#include "contiki.h"
#include "contiki-net.h"
#include "mqtt-service.h"

PROCESS(mqtt_publisher_process, "MQTT Publisher");
PROCESS_NAME(mqtt_process);
AUTOSTART_PROCESSES(&mqtt_process, &mqtt_publisher_process);

PROCESS_THREAD(mqtt_publisher_process, ev, data)
{
// The same example code...
}

The error that occurs is:

TARGET not defined, using target 'native'
  CC        mqtt-publisher.c
  LD        mqtt-publisher.native
obj_native/mqtt-publisher.o:(.data+0x0): multiple definition of `mqtt_publisher_process'
mqtt-publisher.co:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
../../Makefile.include:280: recipe for target 'mqtt-publisher.native' failed
make: *** [mqtt-publisher.native] Error 1
rm mqtt-publisher.co

I'm forgetting something?

Best regards, Rafael

esar commented 8 years ago

My first guess is that it might be building mqtt-publisher.c twice. If I'm remembering right then I think the Contiki build system automatically builds a .c file with the same name as the project, which means you don't need to add that one to PROJECT_SOURCEFILES in the Makefile, you only need to list any extra source files there.

Try leaving PROJECT_SOURCEFILES empty and see if that works.

rafaeldelucena commented 8 years ago

Thats it! Thanks a lot!

:+1:

shafygadgeon commented 8 years ago

Hi,

I am getting the following error in the build after following the same steps described in the above thread

/example.c:77: undefined reference to mqtt_svc_event_get_topic' /example.c:78: undefined reference tomqtt_svc_event_get_message'

i am not able to find where these funtions are defined. Please advise. Is anything missing ?

Thanks Shafy

esar commented 8 years ago

Hi Shafy,

That's actually really weird, the function names in example.c seem to be wrong, but looking at the history they've always been wrong! I'm not sure how you're the first one to notice.

Anyway... remove svc_ from each of them so you have mqtt_event_get_message and mqtt_event_get_topic. They're defined in mqtt-service.h

Give that a try and let me know if it fixes the problem, then I'll update the copy here.

kaleemullah360 commented 8 years ago

in Contiki OS version 3.MQTT already ported by Texas Instruments. so no need to add anything in APPSdirectory in Contiki OS. you can use MQTT-Demo Example.

PS: if you encounter ROM Overflow error then you need to update your MSP430-GCC to version 4.7.x. it has better support for memory management and optimizations.

kaleemullah360 commented 8 years ago

while you're right that ti offers a sample I found it to be rather confusing (why would I implement my own statemachine?)

One very strong reason to implement yours is that TIdoesn't provide QoS 2 (Upto My Knowledge) as it is written This application is an engine for MQTT v3.1. It supports QoS Levels 0 and 1. in file mqtt.h you can write your own engine that will provide QoS 2 along with other improvements.

shafygadgeon commented 8 years ago

Hi esar,

I found another function that is not defined - mqtt_event_get_message. I changed that to mqtt_event_get_data, this is correct ?

But now when running the code, i could see the message "Sending TCP packet to FFFF:FFFF:....." even after setting the server address using uip_ip6addr(). This is the broker ipv6 address , right ?

In the contiki-main, I am starting the tcpip_process. Anything else needed ? Pls let me know for which platoform you tested this ?

Thanks

esar commented 8 years ago

Sorry, yes, you're right it is mqtt_event_get_data.

The server_address that is passed to the mqtt_connect call is the address of the broker. I'm not sure what is producing the "Sending TCP packet" message, this mqtt client should print out "mqtt: connecting..." and then either "mqtt: connected" or "mqtt: connect failed", see here: https://github.com/esar/contiki-mqtt/blob/master/mqtt-service.c#L371

The platform I was using when I wrote this was a custom one derived from the avr-zigbit platform. I've not used it myself for a few years, partly due to the zigbit modules going end-of-life.

From what I remember it wasn't necessary to start any other processes. If you look here: https://github.com/esar/myha/blob/master/apps/myha/myha.c#L95 you can see what I did before starting the mqtt client, everything from line 95 until process_start is called at line 181, that process_start call started my application specific process that then essentially did what's in example.c

You can see my real world use of this client here (these are the processes started by process_start in the link above): https://github.com/esar/myha/blob/master/devices/led-dimmer-6ch/led-dimmer-6ch.c#L755

and here: https://github.com/esar/myha/blob/master/devices/rako-bridge/rako-bridge.c#L94

and here: https://github.com/esar/myha/blob/master/devices/touch-switch-controller/touch-switch-controller.c#L102

Hope this helps

shafygadgeon commented 8 years ago

Hi,

I could see that you are using global address for the mqtt node ? I am trying to communicate using link local address with a gateway. Hope this will work fine ?

tonnenpinguin commented 8 years ago

Given that this module internally just uses the standard tcp interface I'm pretty sure you'll run into no issues.

esar commented 8 years ago

I don't think I've ever tried it with a link local address, but I agree with tonnenpinguin, I can't think of any reason why it wouldn't work

shafygadgeon commented 8 years ago

Hi,

I have tested the example.c using a Raspberry pi node running mosquitto server.

The mosquitto is showing that the contiki node is connecting to the server, but after sometime it is showing the message " Socket error on client contiki, disconnecting."

Any idea how to debug this ?

Thanks Shafy S Gadgeon Smart Systems

On Mon, Jun 6, 2016 at 11:41 PM, esar notifications@github.com wrote:

I don't think I've ever tried it with a link local address, but I agree with tonnenpinguin, I can't think of any reason why it wouldn't work

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/esar/contiki-mqtt/issues/4#issuecomment-224040951, or mute the thread https://github.com/notifications/unsubscribe/ARRb3Dth4s0P8wB57z7NkSI_kwHmL4jsks5qJGLggaJpZM4DUkGX .

esar commented 8 years ago

I'd start by running wireshark or something similar to watch the network traffic to see what is happening to the network connection and why. Then it might be possible to see if the client is actively disconnecting, or if the server is sending a packet that is not responded to, or if it's just timing out, or something else entirely.

shafygadgeon commented 8 years ago

Thanks esar. I will try to use a wireshark.

I am getting this message from the contiki debug print terminal.

"nullrdc tx noack csma: rexmit noack 4 csma: drop with status 2 after 4 transmissions, 0 collisions"

So it seems like the mqtt server is not sending a Ack ?

Thanks Shafy S Gadgeon Smart Systems

On Sun, Jun 12, 2016 at 3:36 PM, esar notifications@github.com wrote:

I'd start by running wireshark or something similar to watch the network traffic to see what is happening to the network connection and why. Then it might be possible to see if the client is actively disconnecting, or if the server is sending a packet that is not responded to, or if it's just timing out, or something else entirely.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/esar/contiki-mqtt/issues/4#issuecomment-225424372, or mute the thread https://github.com/notifications/unsubscribe/ARRb3LNtBJ5YgzECHzSKyxIq_mRuP7tBks5qK9o9gaJpZM4DUkGX .

fuhchang commented 7 years ago

Hi esar I am new to contiki and mqtt so sorry for the newbie question. Is it possible if i create a node in cooja simluator and make it sub to recevied message or file from my broker mosquitto in my desktop?

And i must set the ipv6 of my broker to server_address? how do i do that? Because i keep getting error: invalid suffix "ff" on integer constant

Thank You

HailunTan commented 7 years ago

Hi esar, I checked the example.c. Is it the example code for subscriber only ? I wonder if you can provide the example code for publisher so that they can be tested?

Thank you.

shafygadgeon commented 7 years ago

Hi Tan,

You can use the function mqtt_publish_with_length() in the file mqtt-service.c for publishing.

Thanks Shafy

On Tue, Jan 3, 2017 at 8:38 AM, HailunTan notifications@github.com wrote:

Hi esar, I checked the example.c. Is it the example code for subscriber only ? I wonder if you can provide the example code for publisher so that they can be tested?

Thank you.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/esar/contiki-mqtt/issues/4#issuecomment-270045739, or mute the thread https://github.com/notifications/unsubscribe-auth/ARRb3CJlJIp7QSgOdGhUhOTSc9PuDtgiks5rObuzgaJpZM4DUkGX .

vishal623 commented 7 years ago

Hi sir,

I want to add MQTT in contiki and I followed https://github.com/esar/contiki-mqtt I created a mqtt-service folder inside contiki/apps/directory and copied the files.

I have found some problems using your example, i setup mqtt-service as you describe above and create an example on examples directory with the makefile and the code below:

PROJECT?=My_first_app all:$(PROJECT)

APPS=mqtt-service TARGET=sky

CONTIKI=../.. include $(CONTIKI)/Makefile.include

My application code is:

include

include

include "contiki.h"

include "contiki-net.h"

include "mqtt-service.h"

PROCESS(mqtt_publisher_process, "MQTT Publisher"); PROCESS_NAME(mqtt_process); AUTOSTART_PROCESSES(&mqtt_process, &mqtt_publisher_process);

PROCESS_THREAD(mqtt_publisher_process, ev, data) { // The same example code that already available in example.c file }

I am getting the following error in the build after following the same steps described above.

undefined reference to 'mqtt_event_get_message'

I had already removed '_svc' from my file but still it show me error. Is there any reference missing in my file?

Thanks Vishal

belikesyed commented 7 years ago

Hello Vishal, As it is already stated in the above conversation, 'mqtt_event_get_message' should be changed to 'mqtt_event_get_data'. Even you can go through this corrected one: https://github.com/syedahamad10/contiki-mqtt

Hope it helps.

Regards, Syed.

Tom7777 commented 6 years ago

Dear All,

I compiled the example.c. But now I'm stuck on the server address. My mosquitto server runs on a host with an IPV4 address. How can I translate the IPV4 address correctly to IPV6? The online converters I used gave 'wrong' results. At least the translated addresses didn*t work with the example. The Mosquitto itself and the hardware (Launchpad CC1310) are working properly. I checked this with the MQTT-example from Contiki 3.0 (cc26xx-web-demo). Any help would be appreciated.

Best regrads Tom

vishal623 commented 5 years ago

Can anybody help me to run the example? I have run the example and got a message mqtt: connecting after some time connect failed. In another terminal, I already run the mosquitto. How to pass a local ipv6 address to code?

vishal623 commented 5 years ago

This is the output of the terminal when I run the make TARGET=native command.

Contiki-c03a684 started with IPV6, RPL Rime started with address 1.2.3.4.5.6.7.8 MAC nullmac RDC nullrdc NETWORK sicslowpan Tentative link-local IPv6 address fe80:0000:0000:0000:0302:0304:0506:0708 mqtt: connecting... mqtt: connect failed mqtt: connecting... mqtt: connect failed mqtt: connecting... mqtt: connect failed mqtt: connecting... mqtt: connect failed mqtt: connecting...

In another terminal I run:

mypc:~$ mosquitto 1537898633: mosquitto version 1.4.8 (build date Wed, 05 Sep 2018 15:51:27 -0300) starting 1537898633: Using default config. 1537898633: Opening ipv4 listen socket on port 1883. 1537898633: Opening ipv6 listen socket on port 1883.

Need help.

Rockabilly15 commented 5 years ago

This is the output of the terminal when I run the make TARGET=native command.

Contiki-c03a684 started with IPV6, RPL Rime started with address 1.2.3.4.5.6.7.8 MAC nullmac RDC nullrdc NETWORK sicslowpan Tentative link-local IPv6 address fe80:0000:0000:0000:0302:0304:0506:0708 mqtt: connecting... mqtt: connect failed mqtt: connecting... mqtt: connect failed mqtt: connecting... mqtt: connect failed mqtt: connecting... mqtt: connect failed mqtt: connecting...

In another terminal I run:

mypc:~$ mosquitto 1537898633: mosquitto version 1.4.8 (build date Wed, 05 Sep 2018 15:51:27 -0300) starting 1537898633: Using default config. 1537898633: Opening ipv4 listen socket on port 1883. 1537898633: Opening ipv6 listen socket on port 1883.

Need help.

Dear vishal, I'm stuck in this problem too. Did you find the way to solve it?