1

I want to send data from my arduino ATmega2560,sim808 using mqtt protocol. By executing my code, successfully connected to broker but couldn't publish message.

#include "Arduino.h"

int led = 13;
unsigned int Counter = 0;
unsigned long datalength, CheckSum, RLength;
unsigned short topiclength;
unsigned char topic[30];
char str[250];
unsigned char encodedByte;
int X;
unsigned short MQTTProtocolNameLength;
unsigned short MQTTClientIDLength;
unsigned short MQTTUsernameLength;
unsigned short MQTTPasswordLength;
const char MQTTHost[30] = "m14.cloudmqtt.com";
const char MQTTPort[10] = "12079";
const char MQTTClientID[20] = "bolod1";
const char MQTTTopic[30] = "iqbal - test/testiqbal";
const char MQTTProtocolName[10] = "MQIsdp";
const char MQTTLVL = 0x03;
const char MQTTFlags = 0xC2;
const unsigned int MQTTKeepAlive = 30;
const char MQTTUsername[30] = "username";
const char MQTTPassword[35] = "password";
const char MQTTQOS = 0x0001;
const char MQTTPacketID = 0x0001;
void setup()
{
    pinMode(led, OUTPUT);
    Serial.begin(9600);
    Serial1.begin(9600);
    Serial.println("Arduino MQTT Tutorial, Valetron Systems @www.raviyp.com ");
    delay(3000);
}
void SendConnectPacket(void)
{
    Serial.print("\r\nAT+CIPSEND\r\n");
    delay(3000);
    Serial1.write(0x10);
    MQTTProtocolNameLength = strlen(MQTTProtocolName);
    MQTTClientIDLength = strlen(MQTTClientID);
    MQTTUsernameLength = strlen(MQTTUsername);
    MQTTPasswordLength = strlen(MQTTPassword);
    datalength = MQTTProtocolNameLength + 2 + 4 + MQTTClientIDLength + 2 + MQTTUsernameLength + 2 + MQTTPasswordLength + 2;
    X = datalength;
    do
    {
        encodedByte = X % 128;
        X = X / 128;
        if (X > 0)
        {
            encodedByte |= 128;
        }
        Serial1.write(encodedByte);
    } while (X > 0);
    Serial1.write(MQTTProtocolNameLength >> 8); delay(3000);
    Serial1.write(MQTTProtocolNameLength & 0xFF); delay(3000);
    Serial1.print(MQTTProtocolName); delay(3000);
    Serial1.write(MQTTLVL); delay(3000);// LVL
    Serial1.write(MQTTFlags); delay(3000); // Flags
    Serial1.write(MQTTKeepAlive >> 8); delay(3000);
    Serial1.write(MQTTKeepAlive & 0xFF); delay(3000);
    Serial1.write(MQTTClientIDLength >> 8); delay(3000);
    Serial1.write(MQTTClientIDLength & 0xFF); delay(3000);
    Serial.print(MQTTClientID); delay(3000);
    Serial1.write(MQTTUsernameLength >> 8); delay(3000);
    Serial1.write(MQTTUsernameLength & 0xFF); delay(3000);
    Serial.print(MQTTUsername); delay(3000);
    Serial1.write(MQTTPasswordLength >> 8); delay(3000);
    Serial1.write(MQTTPasswordLength & 0xFF); delay(3000);
    Serial.print(MQTTPassword); Serial.write(0x1A); delay(3000);
}
void SendPublishPacket(void)
{
    Serial.print("\r\nAT+CIPSEND\r\n");
    delay(3000);
    memset(str, 0, 250);
    topiclength = sprintf((char*)topic, MQTTTopic);
    datalength = sprintf((char*)str, "%s%u", topic, Counter);
    delay(1000);
    Serial1.write(0x30);
    X = datalength + 2;
    do
    {
        encodedByte = X % 128;
        X = X / 128;
        if (X > 0)
        {
            encodedByte |= 128;
        }
        Serial.write(encodedByte);
    } while (X > 0);
    Serial1.write(topiclength >> 8); delay(3000);
    Serial1.write(topiclength & 0xFF); delay(3000);
    Serial.print(str); delay(3000);
    Serial1.write(0x1A); delay(3000);
}
void SendSubscribePacket(void)
{
    Serial.print("\r\nAT+CIPSEND\r\n");
    delay(3000);
    memset(str, 0, 250);
    topiclength = strlen(MQTTTopic);
    datalength = 2 + 2 + topiclength + 1;
    delay(1000);
    Serial1.write(0x82);
    X = datalength;
    do
    {
        encodedByte = X % 128;
        X = X / 128;
        if (X > 0) {
            encodedByte |= 128;
        }
        Serial.write(encodedByte);
    } while (X > 0);
    Serial1.write(MQTTPacketID >> 8); delay(3000);
    Serial1.write(MQTTPacketID & 0xFF); delay(3000);
    Serial1.write(topiclength >> 8); delay(3000);
    Serial1.write(topiclength & 0xFF); delay(3000);
    Serial.print(MQTTTopic); delay(3000);
    Serial1.write(MQTTQOS); delay(3000);
    Serial1.write(0x1A); delay(3000);
}

void loop()
{
    Serial.print("AT+CSTT=\"APN\",\"blweb\",\"\"\r\n");
    delay(1000);
    Serial.print("AT+CIPMODE=0\r\n");
    delay(1000);
    Serial.print("AT+CIICR\r\n");
    delay(5000);
    Serial.print("AT+CIPSTART=\"TCP\",\"m14.cloudmqtt.com\",\"12079\"\r\n");
    delay(4000);
    SendConnectPacket();
    delay(5000);

    SendSubscribePacket();
    while (1)
    {
        delay(5000);
        SendPublishPacket();
        Counter++;
    }
}
  • 1
    You can use this client library and there are many more client libraries available online which can help you github.com/adafruit/Adafruit_MQTT_Library This api as well pubsubclient.knolleary.net – Hitesh Pratyush V Jul 11 '18 at 13:03
  • This is the Arduino MQTT library I use for all my Arduino sensors: pubsubclient.knolleary.net – JD Allen Jul 11 '18 at 15:08
  • If you have suggestions to use the above libraries, please post an answer below rather than use comments. Commenting here is primarily for asking for clarification rather than suggesting a solution to the problem. – Aurora0001 Jul 11 '18 at 15:46
  • @Iqbal What happens when you run your publish method? Have you tried connecting to a locally hosted broker to see what is being sent/received? – Aurora0001 Jul 11 '18 at 15:57
  • i tried to use cloudmqtt as my broker. i used mqttlens chrome exte. to check whether there is problem with the broker. seems to work fine. but when i execute this code on the device side it seems everything is going ok. but when i check the log in broker it seems no connection was established. should i post broker details also? in the declaration part is the protocol name and keep alive set correctly? – Iqbal Hossain Jul 12 '18 at 4:37

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.