0

I'm currently using this as a learning example:

https://os.mbed.com/teams/mqtt/code/MQTTPacket/file/aedcaf7984d5/samples/simple-publish.txt/

However, some of the code is specific to whatever embedded system the example is using.

What I got so far is:

#include "hw_util.h"
#include "MQTTPacket.h"
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
int main (void) {


    int i;
    float temperatura;
    unsigned char buffer[50];





    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    int rc = 0;
    char buf[200];
    int buflen = sizeof(buf);
    MQTTString topicString = MQTTString_initializer;
    char* payload = "I'm Alive";
    int payloadlen = strlen(payload);
    int len = 0;
    data.clientID.cstring = "Testing";
    data.keepAliveInterval = 20;
    data.cleansession=1;
    data.MQTTVersion=3;
    len = MQTTSerialize_connect(buf,buflen,&data);
    topicString.cstring="SampleTopic";
    len += MQTTSerialize_publish(buf + len, buflen - len, 0, 0, 0, 0, topicString, payload, payloadlen);

    printf("Hello world");
    rc = 0;
    while(1)
    {

        ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);







    }
}

I downloaded HiveMQ, which is a Broker and it's running:

2018-03-05 19:28:08,195 INFO - Starting TCP listener on address 0.0.0.0 and port 1883

Now what I want to do, is send something like "Hello World" to this Broker or to Putty or something that would display the entire MQTT payload. How does C handle this? The documentation helped me understand what's going on but didnt really help me write C code, since I am still very new to it.

Nephilim
  • 494
  • 5
  • 25

1 Answers1

0

This took a while but I figured it out. There are still plenty of kinks to work out, but at least it sends an MQTT(Wireshark approved) packet to localhost.

  1. Create an MQTT packet using this library: https://os.mbed.com/teams/mqtt/code/MQTTPacket/

Using this code:

MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
char buf[200];

MQTTString topicString = MQTTString_initializer;
data.clientID.cstring = "TESTIRAM";
data.keepAliveInterval = 20;
data.cleansession=1;
data.MQTTVersion=3;
len = MQTTSerialize_connect(buf,buflen,&data);
topicString.cstring="ka";
len += MQTTSerialize_publish(buf + len, buflen - len, 0, 0, 0, 0, 
topicString, payload, payloadlen);
  1. Set up a socket:

This code worked for me, but its LINUX SPECIFIC!

#include "hw_util.h"
#include "MQTTPacket.h"
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
char buf[200];
int mysock=0;
char *host = "127.0.0.1";
int port = 1883;
mysock = socket(AF_INET, SOCK_STREAM, 0);

AF_INET specifies IPv4 and SOCK_STREAM specifies TCP.

  1. Connect to socket

Use this:

struct sockaddr_in cliaddr;

int rc = 0;
    //initialize the host address

    memset(&cliaddr, 0, sizeof(cliaddr));
    //specify IPv4 protocol
    /*following includes are necessary for this:#include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>*/
    cliaddr.sin_family = AF_INET;
    //intialize the IP address of the host as "127.0.0.1"
    cliaddr.sin_addr.s_addr = inet_addr(host);
    //initialize the port, 1883
    cliaddr.sin_port = htons(port);
    //client has no bind
    //socket()>connect()->send()<->recv()->close
    int statusConn = connect(mysock,(const struct sockaddr*)&cliaddr,sizeof(cliaddr));
    if(statusConn=0){
        printf("Success!");
    }
    if(statusConn=-1){
        printf("Connect unsuccessful!\n");
  1. Send over socket!

This should work:

while(1)
{
        printf("Sending to hostname %s port %d\n", host, port);
        //infinite loop, sending packets to the specified ASOCKET
        //after sending sleep for 10 seconds
        //sleep uses unistd.h

        printf("Sent packets: ");

        int countSend = send(mysock ,buf,buflen,0);

        printf("%d",countSend);

        printf("\n");
        sleep(10);
}

Some of the imports might be useless and some might be Linux specific. I'm moving onto AT commands so most of the issues in this code won't transfer to the new codebase.

Nephilim
  • 494
  • 5
  • 25