-3

I'm trying to send data to my db (MySQL) with MQTT. But I cant get any information or just don't understand if I need to connect to my MQTT broker with my C-code first? Or if I just can put in my DB address and portnumber? Connection code down below.

I'm using HiveMQ as broker.

    while(1){

MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
        int rc = 0;
        char buf[200];
        MQTTString topicString = MQTTString_initializer;

                char* payload = "Hello World!";
                int payloadlen = strlen(payload);
                int buflen = sizeof(buf);

                data.clientID.cstring = "me";
                data.keepAliveInterval = 20;
                data.cleansession = 1;

                int len = MQTTSerialize_connect(buf, buflen, &data); /* 1 */

                topicString.cstring = "cc3200-ben";

                len += MQTTSerialize_publish(buf + len, buflen - len, 0, 0, 0, 0, topicString, payload, payloadlen); /* 2 */

                len += MQTTSerialize_disconnect(buf + len, buflen - len); /* 3 */

                // creating a TCP socket
                int mysock = sl_Socket(SL_AF_INET, SL_SOCK_STREAM, 0);
                  if( mysock < 0 )
                    {
                      ERR_PRINT(mysock);
                      LOOP_FOREVER();
                    }

                SlSockAddrIn_t addr;

                //filling the TCP server socket address
                addr.sin_family = SL_AF_INET;
                addr.sin_port = sl_Htons(1883);
                //addr.sin_port = sl_Htons(3306);
                addr.sin_addr.s_addr = sl_Htonl(0xC6291EF1);

                 // connecting to TCP server
                rc = sl_Connect(mysock, (SlSockAddr_t *) &addr, sizeof(addr));
                if(rc<0){
                    ERR_PRINT(rc);
                    LOOP_FOREVER();
                }
                 // sending packet
                rc = sl_Send(mysock, buf, len, NULL);
                if(rc<0){
                    ERR_PRINT(rc);
                    LOOP_FOREVER();
                }

                //closing the socket
                rc = sl_Close(mysock);
                if(rc<0){
                    ERR_PRINT(rc);
                    LOOP_FOREVER();
                                    }

        //      rc = Socket_new("0.0.0.0", 1883, &mysock);
        //      rc = write(mysock, buf, len);
        //      rc = close(mysock);

                osi_Sleep(200);
} 
hardillb
  • 54,545
  • 11
  • 67
  • 105
Ouizzo
  • 5
  • 5

1 Answers1

0

DB and MQTT broker are totally serape, follow the next steps to get what you want:

  1. During the application start up connect to your mqtt broker.
  2. Subscribe to a topic that you are waiting your data on it.
  3. Connect to your database.
  4. in the call back function ( from step 2 ), use the database connection and update your database by the payload.

To send data to your server do the following:

  1. Connect to your mqtt broker.
  2. Publish a message to your topic (from prev step 2).
Eric Hal
  • 7
  • 1
  • Hi! I have understand now what I want and that is to send data to my server,as you pointed out. But im stuck. Im using a broker to connect to from my cc3200 launchpad. And I (think I) get a connection to it, – Ouizzo May 08 '15 at 11:46
  • because the terminal winow for CCS (Tera term) dosent display any eror, as it does when I connect to the wrong IP or port. The thing is that the broker window dosent display any connection confirmation... Does anyone got any clues about that? I have enable the suport for websocket in the configuration properties file for HiveMQ broker so it listening on port 8000. Here is my socket connection code. Does anyone know if it is something wrong with it? addr.sin_family = SL_AF_INET; addr.sin_port = sl_Htons(8000); addr.sin_addr.s_addr = sl_Htonl("my network IP"); /Thanks in advance! – Ouizzo May 08 '15 at 11:46