Arduino MQTT Library for SIM800 GSM Modem

Hello tech freaks, hobbyists, enthusiasts and professionals,
we are pleased to announce the open source project “SIM800 Arduino MQTT Library“. We welcome you to come and collaborate on this project. MQTT  is an ISO standard publish-subscribe-based messaging protocol for use on top of the TCP/IP protocol (is the underlying technology behind  Facebook Messenger). This MQTT client application connects to MQTT capable servers. And it can collect information from the device and publish the information to the server. It can also subscribe to topics and receive messages from the server.  This Library is tested in Arduino Uno with SIM800 GSM Modem.

Those who just want to use implement MQTT with GSM modem can use this library. And those who want to go deep can go through the code and can contribute to it. For more, see our GitHub repositories and Blog announcements.

Functionalities:

  • Auto connect
    • Automatically connect to TCP and to MQTT server.
    •  This function if you want to use autoconnect(and auto reconnect) facility
    •  This function is called whenever TCP connection is established (or re-established).
  • connect function
    • This function can be used to connect your client to MQTT broker.
    • Use only if you do not use Auto connect functionality.
    • Optionally  you can use username, password, WILL topic and WILL Message.
  • OnConnect CallBack function
    • This call back function is called when MQTT connection is established.
    • You can call subscription and publish functions inside it (according to your need).
  • publish function

    • This function can be used to publish messages to different topics.
    • You can select  QoS levels and RETAIN flag according to your need.
  • subscribe function
    • This function can be used to subscribe messages from different topics.
  • OnMessage CallBack function
    • This callback function is called when messages are received from  subscribed topics
    •  Topic, TopicLength, Message, MessageLength are the arguments of  OnMessage callback function.
    • Inside this, you can write your custom code.
  • unsubscribe function

    • This function can be used to unsubscribe from a previously subscribed topic.
  • disconnect function

    • This function can be used to disconnect your client from MQTT broker.
  • available function
    • return true when connection with mqtt brocker is existing
  • You can specify your KeepAlive duration while initializing.
  • Ping requests are sent and received automatically.

Subscribe Example:

#include "GSM_MQTT.h"
#include <SoftwareSerial.h>
String MQTT_HOST = "test.mosquitto.org";
/*
   MQTT host address
*/
String MQTT_PORT = "1883";
/*
   MQTT port
*/
SoftwareSerial mySerial(10, 11); // RX, TX
/*
   Software Serial through which mqtt events log is printed at 9600 baud rate
*/
void GSM_MQTT::AutoConnect(void)
{
  /*
     Use this function if you want to use autoconnect(and auto reconnect) facility
     This function is called whenever TCP connection is established (or re-established).
     put your connect codes here.
  */
  connect("qwertyuiop", 0, 0, "", "", 1, 0, 0, 0, "", "");
  /*    void connect(char *ClientIdentifier, char UserNameFlag, char PasswordFlag, char *UserName, char *Password, char CleanSession, char WillFlag, char WillQoS, char WillRetain, char *WillTopic, char *WillMessage);
          ClientIdentifier  :Is a string that uniquely identifies the client to the server.
                            :It must be unique across all clients connecting to a single server.(So it will be better for you to change that).
                            :It's length must be greater than 0 and less than 24
                            :Example "qwerty"
          UserNameFlag      :Indicates whether UserName is present
                            :Possible values (0,1)
                            :Default value 0 (Disabled)
          PasswordFlag      :Valid only when  UserNameFlag is 1, otherwise its value is disregarded.
                            :Indicates whether Password is present
                            :Possible values (0,1)
                            :Default value 0 (Disabled)
          UserName          :Mandatory when UserNameFlag is 1, otherwise its value is disregarded.
                            :The UserName corresponding to the user who is connecting, which can be used for authentication.
          Password          :alid only when  UserNameFlag and PasswordFlag are 1 , otherwise its value is disregarded.
                            :The password corresponding to the user who is connecting, which can be used for authentication.
          CleanSession      :If not set (0), then the server must store the subscriptions of the client after it disconnects.
                            :If set (1), then the server must discard any previously maintained information about the client and treat the connection as "clean".
                            :Possible values (0,1)
                            :Default value 1
          WillFlag          :This flag determines whether a WillMessage published on behalf of the client when client is disconnected involuntarily.
                            :If the WillFlag is set, the WillQoS, WillRetain, WillTopic, WilMessage fields are valid.
                            :Possible values (0,1)
                            :Default value 0 (Disables will Message)
          WillQoS           :Valid only when  WillFlag is 1, otherwise its value is disregarded.
                            :Determines the QoS level of WillMessage
                            :Possible values (0,1,2)
                            :Default value 0 (QoS 0)
          WillRetain        :Valid only when  WillFlag is 1, otherwise its value is disregarded.
                            :Determines whether the server should retain the Will message.
                            :Possible values (0,1)
                            :Default value 0
          WillTopic         :Mandatory when  WillFlag is 1, otherwise its value is disregarded.
                            :The Will Message will published to this topic (WillTopic) in case of involuntary client disconnection.
          WillMessage       :Mandatory when  WillFlag is 1, otherwise its value is disregarded.
                            :This message (WillMessage) will published to WillTopic in case of involuntary client disconnection.
  */
  
}
void GSM_MQTT::OnConnect(void)
{
  /*
     This function is called when mqqt connection is established.
     put your subscription publish codes here.
  */
  subscribe(0, _generateMessageID(), "SampleTopic", 1);
  /*    void subscribe(char DUP, unsigned int MessageID, char *SubTopic, char SubQoS);
          DUP       :This flag is set when the client or server attempts to re-deliver a SUBSCRIBE message
                    :This applies to messages where the value of QoS is greater than zero (0)
                    :Possible values (0,1)
                    :Default value 0
          Message ID:The Message Identifier (Message ID) field
                    :Used only in messages where the QoS levels greater than 0 (SUBSCRIBE message is at QoS =1)
          SubTopic  :Topic names to which  subscription is needed
          SubQoS    :QoS level at which the client wants to receive messages
                    :Possible values (0,1,2)
                    :Default value 0
  */

//  publish(0, 0, 0, _generateMessageID(), "SampleTopic", "Hello");
  /*  void publish(char DUP, char Qos, char RETAIN, unsigned int MessageID, char *Topic, char *Message);
      DUP       :This flag is set when the client or server attempts to re-deliver a PUBLISH message
                :This applies to messages where the value of QoS is greater than zero (0)
                :Possible values (0,1)
                :Default value 0
      QoS       :Quality of Service
                :This flag indicates the level of assurance for delivery of a PUBLISH message
                :Possible values (0,1,2)
                :Default value 0
      RETAIN    :if the Retain flag is set (1), the server should hold on to the message after it has been delivered to the current subscribers.
                :When a new subscription is established on a topic, the last retained message on that topic is sent to the subscriber
                :Possible values (0,1)
                :Default value 0
      Message ID:The Message Identifier (Message ID) field
                :Used only in messages where the QoS levels greater than 0
      Topic     :Publishing topic
      Message   :Publishing Message
  */
}
void GSM_MQTT::OnMessage(char *Topic, int TopicLength, char *Message, int MessageLength)
{
  /*
    This function is called whenever a message received from subscribed topics
    put your subscription publish codes here.
  */

  /*
     Topic        :Name of the topic from which message is coming
     TopicLength  :Number of characters in topic name
     Message      :The containing array
     MessageLength:Number of characters in message
  */
  mySerial.println(TopicLength);
  mySerial.println(Topic);
  mySerial.println(MessageLength);
  mySerial.println(Message);

}
GSM_MQTT MQTT(20);
/*
   20 is the keepalive duration in seconds
*/

void setup()
{
  // initialize mqtt:
  // GSM modem should be connected to Harware Serial
  //  index =0;
  MQTT.begin();

  /*
     You can write your code here
  */

}
void loop()
{

  /*
     You can write your code here
  */
  if (MQTT.available)
  {
    /*
      if you want to do something when mqtt connection is live.
      You can write your code here
    */
  }

  MQTT.processing();
}

 Publish Example:

#include "GSM_MQTT.h"
#include <SoftwareSerial.h>
String MQTT_HOST = "test.mosquitto.org";
/*
   MQTT host address
*/
String MQTT_PORT = "1883";
/*
   MQTT port
*/
SoftwareSerial mySerial(10, 11); // RX, TX
/*
   Software Serial through which mqtt events log is printed at 9600 baud rate
*/
void GSM_MQTT::AutoConnect(void)
{
  /*
     Use this function if you want to use autoconnect(and auto reconnect) facility
     This function is called whenever TCP connection is established (or re-established).
     put your connect codes here.
  */
  connect("qwertyuiop", 0, 0, "", "", 1, 0, 0, 0, "", "");
  /*    void connect(char *ClientIdentifier, char UserNameFlag, char PasswordFlag, char *UserName, char *Password, char CleanSession, char WillFlag, char WillQoS, char WillRetain, char *WillTopic, char *WillMessage);
          ClientIdentifier  :Is a string that uniquely identifies the client to the server.
                            :It must be unique across all clients connecting to a single server.(So it will be better for you to change that).
                            :It's length must be greater than 0 and less than 24
                            :Example "qwerty"
          UserNameFlag      :Indicates whether UserName is present
                            :Possible values (0,1)
                            :Default value 0 (Disabled)
          PasswordFlag      :Valid only when  UserNameFlag is 1, otherwise its value is disregarded.
                            :Indicates whether Password is present
                            :Possible values (0,1)
                            :Default value 0 (Disabled)
          UserName          :Mandatory when UserNameFlag is 1, otherwise its value is disregarded.
                            :The UserName corresponding to the user who is connecting, which can be used for authentication.
          Password          :alid only when  UserNameFlag and PasswordFlag are 1 , otherwise its value is disregarded.
                            :The password corresponding to the user who is connecting, which can be used for authentication.
          CleanSession      :If not set (0), then the server must store the subscriptions of the client after it disconnects.
                            :If set (1), then the server must discard any previously maintained information about the client and treat the connection as "clean".
                            :Possible values (0,1)
                            :Default value 1
          WillFlag          :This flag determines whether a WillMessage published on behalf of the client when client is disconnected involuntarily.
                            :If the WillFlag is set, the WillQoS, WillRetain, WillTopic, WilMessage fields are valid.
                            :Possible values (0,1)
                            :Default value 0 (Disables will Message)
          WillQoS           :Valid only when  WillFlag is 1, otherwise its value is disregarded.
                            :Determines the QoS level of WillMessage
                            :Possible values (0,1,2)
                            :Default value 0 (QoS 0)
          WillRetain        :Valid only when  WillFlag is 1, otherwise its value is disregarded.
                            :Determines whether the server should retain the Will message.
                            :Possible values (0,1)
                            :Default value 0
          WillTopic         :Mandatory when  WillFlag is 1, otherwise its value is disregarded.
                            :The Will Message will published to this topic (WillTopic) in case of involuntary client disconnection.
          WillMessage       :Mandatory when  WillFlag is 1, otherwise its value is disregarded.
                            :This message (WillMessage) will published to WillTopic in case of involuntary client disconnection.
  */
  
}
void GSM_MQTT::OnConnect(void)
{
  /*
     This function is called when mqqt connection is established.
     put your subscription publish codes here.
  */
  //  subscribe(0, _generateMessageID(), "SampleTopic", 1);
  /*    void subscribe(char DUP, unsigned int MessageID, char *SubTopic, char SubQoS);
          DUP       :This flag is set when the client or server attempts to re-deliver a SUBSCRIBE message
                    :This applies to messages where the value of QoS is greater than zero (0)
                    :Possible values (0,1)
                    :Default value 0
          Message ID:The Message Identifier (Message ID) field
                    :Used only in messages where the QoS levels greater than 0 (SUBSCRIBE message is at QoS =1)
          SubTopic  :Topic names to which  subscription is needed
          SubQoS    :QoS level at which the client wants to receive messages
                    :Possible values (0,1,2)
                    :Default value 0
  */

  publish(0, 0, 0, _generateMessageID(), "SampleTopic", "Hello");
  /*  void publish(char DUP, char Qos, char RETAIN, unsigned int MessageID, char *Topic, char *Message);
      DUP       :This flag is set when the client or server attempts to re-deliver a PUBLISH message
                :This applies to messages where the value of QoS is greater than zero (0)
                :Possible values (0,1)
                :Default value 0
      QoS       :Quality of Service
                :This flag indicates the level of assurance for delivery of a PUBLISH message
                :Possible values (0,1,2)
                :Default value 0
      RETAIN    :if the Retain flag is set (1), the server should hold on to the message after it has been delivered to the current subscribers.
                :When a new subscription is established on a topic, the last retained message on that topic is sent to the subscriber
                :Possible values (0,1)
                :Default value 0
      Message ID:The Message Identifier (Message ID) field
                :Used only in messages where the QoS levels greater than 0
      Topic     :Publishing topic
      Message   :Publishing Message
  */
}
void GSM_MQTT::OnMessage(char *Topic, int TopicLength, char *Message, int MessageLength)
{
  /*
    This function is called whenever a message received from subscribed topics
    put your subscription publish codes here.
  */

  /*
     Topic        :Name of the topic from which message is coming
     TopicLength  :Number of characters in topic name
     Message      :The containing array
     MessageLength:Number of characters in message
  */
  mySerial.println(TopicLength);
  mySerial.println(Topic);
  mySerial.println(MessageLength);
  mySerial.println(Message);

}
GSM_MQTT MQTT(20);
/*
   20 is the keepalive duration in seconds
*/

void setup()
{
  // initialize mqtt:
  // GSM modem should be connected to Harware Serial
  //  index =0;
  MQTT.begin();

  /*
     You can write your code here
  */

}
void loop()
{

  /*
     You can write your code here
  */
  if (MQTT.available)
  {
    /*
      if you want to do something when mqtt connection is live.
      You can write your code here
    */
  }

  MQTT.processing();
}

Subscribe Publish Example:

 #include "GSM_MQTT.h"
#include <SoftwareSerial.h>
String MQTT_HOST = "test.mosquitto.org";
/*
   MQTT host address
*/
String MQTT_PORT = "1883";
/*
   MQTT port
*/
SoftwareSerial mySerial(10, 11); // RX, TX
/*
   Software Serial through which mqtt events log is printed at 9600 baud rate
*/
void GSM_MQTT::AutoConnect(void)
{
  /*
     Use this function if you want to use autoconnect(and auto reconnect) facility
     This function is called whenever TCP connection is established (or re-established).
     put your connect codes here.
  */
  connect("qwertyuiop", 0, 0, "", "", 1, 0, 0, 0, "", "");
  /*    void connect(char *ClientIdentifier, char UserNameFlag, char PasswordFlag, char *UserName, char *Password, char CleanSession, char WillFlag, char WillQoS, char WillRetain, char *WillTopic, char *WillMessage);
          ClientIdentifier  :Is a string that uniquely identifies the client to the server.
                            :It must be unique across all clients connecting to a single server.(So it will be better for you to change that).
                            :It's length must be greater than 0 and less than 24
                            :Example "qwerty"
          UserNameFlag      :Indicates whether UserName is present
                            :Possible values (0,1)
                            :Default value 0 (Disabled)
          PasswordFlag      :Valid only when  UserNameFlag is 1, otherwise its value is disregarded.
                            :Indicates whether Password is present
                            :Possible values (0,1)
                            :Default value 0 (Disabled)
          UserName          :Mandatory when UserNameFlag is 1, otherwise its value is disregarded.
                            :The UserName corresponding to the user who is connecting, which can be used for authentication.
          Password          :alid only when  UserNameFlag and PasswordFlag are 1 , otherwise its value is disregarded.
                            :The password corresponding to the user who is connecting, which can be used for authentication.
          CleanSession      :If not set (0), then the server must store the subscriptions of the client after it disconnects.
                            :If set (1), then the server must discard any previously maintained information about the client and treat the connection as "clean".
                            :Possible values (0,1)
                            :Default value 1
          WillFlag          :This flag determines whether a WillMessage published on behalf of the client when client is disconnected involuntarily.
                            :If the WillFlag is set, the WillQoS, WillRetain, WillTopic, WilMessage fields are valid.
                            :Possible values (0,1)
                            :Default value 0 (Disables will Message)
          WillQoS           :Valid only when  WillFlag is 1, otherwise its value is disregarded.
                            :Determines the QoS level of WillMessage
                            :Possible values (0,1,2)
                            :Default value 0 (QoS 0)
          WillRetain        :Valid only when  WillFlag is 1, otherwise its value is disregarded.
                            :Determines whether the server should retain the Will message.
                            :Possible values (0,1)
                            :Default value 0
          WillTopic         :Mandatory when  WillFlag is 1, otherwise its value is disregarded.
                            :The Will Message will published to this topic (WillTopic) in case of involuntary client disconnection.
          WillMessage       :Mandatory when  WillFlag is 1, otherwise its value is disregarded.
                            :This message (WillMessage) will published to WillTopic in case of involuntary client disconnection.
  */
  
}
void GSM_MQTT::OnConnect(void)
{
  /*
     This function is called when mqqt connection is established.
     put your subscription publish codes here.
  */
  subscribe(0, _generateMessageID(), "SampleTopic", 1);
  /*    void subscribe(char DUP, unsigned int MessageID, char *SubTopic, char SubQoS);
          DUP       :This flag is set when the client or server attempts to re-deliver a SUBSCRIBE message
                    :This applies to messages where the value of QoS is greater than zero (0)
                    :Possible values (0,1)
                    :Default value 0
          Message ID:The Message Identifier (Message ID) field
                    :Used only in messages where the QoS levels greater than 0 (SUBSCRIBE message is at QoS =1)
          SubTopic  :Topic names to which  subscription is needed
          SubQoS    :QoS level at which the client wants to receive messages
                    :Possible values (0,1,2)
                    :Default value 0
  */

  publish(0, 0, 0, _generateMessageID(), "SampleTopic", "Hello");
  /*  void publish(char DUP, char Qos, char RETAIN, unsigned int MessageID, char *Topic, char *Message);
      DUP       :This flag is set when the client or server attempts to re-deliver a PUBLISH message
                :This applies to messages where the value of QoS is greater than zero (0)
                :Possible values (0,1)
                :Default value 0
      QoS       :Quality of Service
                :This flag indicates the level of assurance for delivery of a PUBLISH message
                :Possible values (0,1,2)
                :Default value 0
      RETAIN    :if the Retain flag is set (1), the server should hold on to the message after it has been delivered to the current subscribers.
                :When a new subscription is established on a topic, the last retained message on that topic is sent to the subscriber
                :Possible values (0,1)
                :Default value 0
      Message ID:The Message Identifier (Message ID) field
                :Used only in messages where the QoS levels greater than 0
      Topic     :Publishing topic
      Message   :Publishing Message
  */
}
void GSM_MQTT::OnMessage(char *Topic, int TopicLength, char *Message, int MessageLength)
{
  /*
    This function is called whenever a message received from subscribed topics
    put your subscription publish codes here.
  */

  /*
     Topic        :Name of the topic from which message is coming
     TopicLength  :Number of characters in topic name
     Message      :The containing array
     MessageLength:Number of characters in message
  */
  mySerial.println(TopicLength);
  mySerial.println(Topic);
  mySerial.println(MessageLength);
  mySerial.println(Message);

}
GSM_MQTT MQTT(20);
/*
   20 is the keepalive duration in seconds
*/

void setup()
{
  // initialize mqtt:
  // GSM modem should be connected to Harware Serial
  //  index =0;
  MQTT.begin();

  /*
     You can write your code here
  */

}
void loop()
{

  /*
     You can write your code here
  */
  if (MQTT.available)
  {
    /*
      if you want to do something when mqtt connection is live.
      You can write your code here
    */
  }

  MQTT.processing();
}

Components used for developing this library are available at Arduino Uno and SIM800 GSM Modem. And this project is available at SIM800_MQTT_Arduino along with code examples. So please come and contribute. This library is also compatible with SIM900 MODEMs

“Don’t reinvent the wheel when you don’t have to, Just realign it”

 

Advertisements

71 thoughts on “Arduino MQTT Library for SIM800 GSM Modem

  1. Great! Thanks for sharing it, I needed it for my Gateway application. I will use it and will give feedback.

  2. I’m trying to use your librarie but I don’t understand how connect to my server without any username, password. Anyone could tell me an example to connect to my mqtt server without username, password?

    • Hi masalinas,
      we have added an AutoConnect call back function in the library. If you go through the example file, you can see that. And “connect” function is called inside that function. It looks like this,
      ———————————————————————————————————————————–
      void GSM_MQTT::AutoConnect(void)
      {
      connect(“qwertyuiop”, 0, 0, “”, “”, 1, 0, 0, 0, “”, “”);
      // void connect(char *ClientIdentifier, char UserNameFlag, char PasswordFlag, char *UserName, char *Password, char CleanSession, char WillFlag, char WillQoS, char WillRetain, char *WillTopic, char *WillMessage)
      }
      ———————————————————————————————————————————–
      Autoconnect without username and password.

      In connect function 2nd and 3rd arguments are UserNameFlag and PasswordFlag.
      If you want to connect to your MQTT broker with username then you have to pass 1 as second argument (UserNameFlag). Similarly if you want to connect to your MQTT brocker with password you have to pass 1 as third argument (PasswordFlag). And pass your user name as 4th argument (UserName) and password as 5th argument (Password).
      ———————————————————————————————————————————–
      void GSM_MQTT::AutoConnect(void)
      {
      connect(“qwertyuiop”, 1, 1, “user12”, “pass123”, 1, 0, 0, 0, “”, “”);
      // void connect(char *ClientIdentifier, char UserNameFlag, char PasswordFlag, char *UserName, char *Password, char CleanSession, char WillFlag, char WillQoS, char WillRetain, char *WillTopic, char *WillMessage)
      }
      ———————————————————————————————————————————–
      Autoconnect with username user12 and password pass123 .

  3. Hi everybody,
    We have updated Library code with new functionalities
    – AutoConnect call back function
    – available function
    Go to the repository and get the updated code. Those who want to contribute, please give pull requests.

  4. Its absolutly greatt!! I was trying to write a library same of this! And now I dont need to 🙂 Thank you sooo much for this great work! After I use it I will give feedback.

  5. Thanks for writing this library. I tested it on my Arduino MEGA with Harware Serial port connected to SIM808. It worked fine.
    But when i tried to change the Harware ports from “Serial” to “Serial1” and “mySerial” to “Serial” its not working. I changed the serialEvent() to serialEvent1() also..

    Any ideas? I’ve raised it as an issue in Github. Please help.

    • I have tested with mega.. SIM800 on Serial1 and Debug on Serial …. It works fine… I have changed all instance of Serial–> Serial1 and mySerial—>Serial and serialEvent() to serialEvent1()….

      Initially publish is great but subscribe with message length more than 10 bytes hangs the code… By the way I Have changed Serial Buffer size from 64 to 256 and now no issue upto atleast 150 char message with 40 char topic name…

      and many thanks to elementzonline for provide this lib in public domain….

    • I have done similer thing and bingo it works with my mega… also change serial buffer size in arduino hardwareserial.cpp so it can handle subscribed message more than 10 bytes… And many thanks to elementzonline for providing this very useful lib on public domain…

      • Hi Ravi,
        Thanks for the response.
        I tried changing the HardwareSerial.h
        replaced line : #define SERIAL_TX_BUFFER_SIZE 64
        with line: #define SERIAL_TX_BUFFER_SIZE 256
        and
        replaced line : #define SERIAL_RX_BUFFER_SIZE 64
        with line: #define SERIAL_RX_BUFFER_SIZE 256

        To check if the Buffer really has changed, i added a statement in my loop() method
        Serial.println(SERIAL_RX_BUFFER_SIZE);

        I still see that its printing 64.

        But another interesting thing i noticed that, i started receiving the incoming message and my Serial is now printing the incoming messages.

        So to re-check if this has happened due to change in the Serial buffer, i revoke my changes from HardwareSerial.h and made both the buffers back to 64, but i didn’t remove my Serial.print() statement in the loop.

        Now, i am still receiving my message though not full message but partial message.

        So that bring me to the conclusion that if i don’t give Serial.print() statement in the loop, it doesn’t print any incoming MQTT message on the Serial monitor but if i give Serial.print(), it prints in the Serial Monitor.

        Below is my code in the loop() method.
        void loop()
        {

        Serial.print(SERIAL_RX_BUFFER_SIZE);

        if (MQTT.available())
        {

        }
        MQTT.processing();
        }

        Any idea why is it like this ?
        Also, what does MQTT.available() means?

  6. Can Anyone confirm that this library is working with mosquitto? I ported the library to mbed and I am getting the following error from mosquitto mqtt broker: Invalid protocol version 0 in CONNECT from xxx.xxx.xxx.xxx

  7. Any support for mbed would be highly appreciated … tried to convert the library – but as I am a novice I am running into lot’s of issues … thanks anyhow for your efforts!

  8. I have some doubts.
    I’m using the shield of icomsat v1.1 exactly as pictured .

    What I did was change SoftwareSerial mySerial (10, 11); to SoftwareSerial mySerial (18,19) .
    Is that correct?
    I did not quite understand how to publish using the library , how do I post a variable?
    I did: publish(0, 0, 0, _generateMessageID(), “SampleTopic”, variable);
    Is that correct?

    I am new to programming so I apologize if I asked something stupid

  9. I did not understand how to call the functions, for example : I want to publish something when MQTT are available , how can I do this?
    One more thing, that serial is for gprs ?
    I need to communicate my arduino mega with gprs to the fair school science , but I could not understand the examples. My code looks like this:

    #include “GSM_MQTT.h”
    #include
    String MQTT_HOST = “test.mosquitto.org”;
    /*
    MQTT host address
    */
    String MQTT_PORT = “1883”;
    /*
    MQTT port
    */
    SoftwareSerial mySerial(10, 11); // RX, TX
    /*
    Software Serial through which mqtt events log is printed at 9600 baud rate
    */
    void GSM_MQTT::AutoConnect(void)
    {
    connect(“Gabriel”, 0, 0, “”, “”, 1, 0, 0, 0, “”, “”);

    }
    void GSM_MQTT::OnConnect(void)
    {

    publish(0, 0, 0, _generateMessageID(), “;Test”, “Hello everybody”);

    }
    void GSM_MQTT::OnMessage(char *Topic, int TopicLength, char *Message, int MessageLength)
    {
    mySerial.println(TopicLength);
    mySerial.println(Topic);
    mySerial.println(MessageLength);
    mySerial.println(Message);

    }
    GSM_MQTT MQTT(20);
    /*
    20 is the keepalive duration in seconds
    */

    void setup()
    {
    // initialize mqtt:
    // GSM modem should be connected to Hardware Serial
    // index =0;
    MQTT.begin();
    Serial.begin(9600);

    }
    void loop()
    {

    AutoConnect();

    if (MQTT.available())
    {
    OnConnect();

    }

    MQTT.processing();
    }

    I want to show ” Hello everybody ” message on a topic on my computer, but I could not .

    It’s really hard to start program alone :T

  10. Hi, I am using the example and it’s getting through until

    “CONNECT
    MQTT.TCP_Flag = True”

    but then it stops. I believe it’s supposed to run AutoConnect() next with the Connect function inside it, however this doesn’t seem to be running. Anyone have the same issue?

    Thanks

      • I’v modified both actually, had to swap mySerial and Serial in the .cpp because my gsm module is connected via software serial whilst using hardware serial when connected to my PC. I also had to modify the check for network because I have a global sim card which is never on a ‘home network’.

        Is there a way I can send all to you so you can check? It would be greatly appreciated.

    • Hey Jordan,

      I’m experiencing completely the same issue. Did you have a chance to overcome it ?
      Would you mind to share your experience with me ?

      Thanks for your time and effort. I really appreciate it.

      Sincerely,
      Bojan.

  11. Okay, I’ve sorted this issue now. Now the next thing is that I am trying to send data from a current sensor in the loop() method. I can see that I am reading data but the MQTT.publish() method isn’t working when called. Any idea why that is?

    Here is my code:

    void loop()
    {

    /*
    You can write your code here
    */
    if (MQTT.available())
    {
    unsigned int x=0;
    float AcsValue=0.0, Samples=0.0, AvgAcs=0.0, AcsValueF=0.0;

    for (int x = 0; x <150; x++){
    AcsValue = analogRead(A0);
    Samples = Samples + AcsValue;
    delay (3);
    }
    AvgAcs=Samples/150.00;
    AcsValueF = ((AvgAcs * ((5.0 / 1024.0)) – 2.5 )/0.185);
    dtostrf(AcsValueF, 9, 5, stracsvalue);

    MQTT.publish(1, 2, 1, MessageID+1, "gogo", stracsvalue);
    Serial.println(stracsvalue);

    }

    • How exactly have you sorted that issue? I’m stuck at CONNECT. Also changed hw and software serial ports.
      Could you please send me some working files? THANKS in advance

      • Hey G, I get stuck on the same point (@ CONNECT) when I try to swap mySerial and Serial ports.
        Would you be so kind to share you experience with me ? How did you resolve this swapping issue ?

        Thanks in advance,
        Bojan.

      • Well, my experience was very variable and weird. It worked, it stopped working. I’m tired of the itead gboard. Perhaps the sim900 module is not so compatible. So I bought a couple of Arduino Uno’s and a SIM800L in order to reproduce the exact setup. I will let you know as soon as I test it.

      • Well, as I told you, I bought a SIM800L Module and it works like a charm. Evidently they are not so compatible.

        I will continue with my project using SIM800L and later on if I have some spare time I will try to discover where to touch, as I have two itead gboards that I intend to use.

  12. Great job! Thanks for sharing. I manged to get it working using sim900 and arduino pro mini 3.3v 8mhz.
    I have added those lines in (void GSM_MQTT::OnMessage) function to control the LED and later will add some relays for my project:

    if (strcmp(Message, “on”) == 0) digitalWrite(13, HIGH);
    if (strcmp(Message, “off”) == 0) digitalWrite(13, LOW);

    I hope to see a tutorial on how to integrate this library with Blynk service.

  13. Thank you for sharing. I tried the library and it is working fine on arduino uno and SIM900 module. Is it possible to integrate it with Blynk service?
    ive added these lines to (GSM_MQTT::OnMessage) function:
    if (strcmp(Message, “on”) == 0) digitalWrite(13, HIGH);
    if (strcmp(Message, “off”) == 0) digitalWrite(13, LOW);

  14. Hi,
    Your work is awsome !
    The good thing is the At commands are close to the A6 gsm/Gprs so i try to convert your library for the A6!
    But i’ve a main problem because i can’t understand a big part of the Code:

    case 3:
    {
    if (GSM_ReplyFlag != 7)
    {
    _tcpStatus = sendATreply(“AT+CIPSTATUS\r\n”, “+IPSTATUS:”, 4000);
    if (_tcpStatusPrev == _tcpStatus)
    {
    tcpATerrorcount++;
    if (tcpATerrorcount >= 10)
    {
    tcpATerrorcount = 0;
    _tcpStatus = 7;
    }

    }
    else
    {
    _tcpStatusPrev = _tcpStatus;
    tcpATerrorcount = 0;
    }
    }
    _tcpStatusPrev = _tcpStatus;
    mySerial.print(_tcpStatus);
    switch (_tcpStatus)
    {
    case 2:
    {

    _sendAT(“AT+CGDCONT=1,\”IP\”,\”free\”\r\n”, 5000);
    break;
    }
    case 3:
    {
    //_sendAT(“AT+CIICR\r\n”, 5000) ;
    _sendAT(“AT+CGACT=1,1\r\n”, 5000);
    break;
    }
    case 4:
    {
    sendATreply(“AT+CIFSR\r\n”, “.”, 4000) ;
    break;
    }
    case 5:
    {
    Serial.print(“AT+CIPSTART=\”TCP\”,\””);
    Serial.print(MQTT_HOST);
    Serial.print(“\”,”); //Change “\”,\”” to “\”,”
    Serial.print(MQTT_PORT);
    if (_sendAT(“\r\n”, 5000) == 1) //Change “\”\r\n to “\r\n”
    {
    unsigned long PrevMillis = millis();
    unsigned long currentMillis = millis();
    while ( (GSM_Response != 4) && ((currentMillis – PrevMillis) < 20000) )
    {
    // delay(1);
    serialEvent();
    currentMillis = millis();
    }
    }
    break;
    }

    I can't understand this part, how it works ?
    What is the job of the MQTT.GSM_ReplyFlag ?
    Specialy this one:
    else if ((strstr(MQTT.inputString, " CONNECT OK") != 0) || (strstr(MQTT.inputString, "CONNECT FAIL") != NULL) || (strstr(MQTT.inputString, "PDP DEACT") != 0))
    {
    MQTT.GSM_ReplyFlag = 7;

    Because it's in this part something goes wrong:

    ^CINIT: 1, 0, 0

    ^CINIT: 2, 32, 41891

    ^CINIT: 8, 2048, 1

    ^CINIT: 16, 0, 1638450

    ^CINIT: 32, 0, 0

    +CREG: 0

    +CREG: 5
    AT+CREG?

    +CREG: 1,5

    OK
    AT+CGATT?

    +CGATT:0

    OK
    AT+CGATT=1

    COMMAND NO RESPONSE!

    +CTZV:16/9/13,2:47:34,8

    +CGREG: 5

    OK
    AT+CIPSTATUS

    +IPSTATUS: IP INITIAL

    OK
    AT+CGDCONT=1,"IP","free"

    OK
    AT+CIPSTATUS

    +IPSTATUS: IP INITIAL

    OK
    AT+CGDCONT=1,"IP","free"

    OK
    AT+CIPSTATUS

    +IPSTATUS: IP INITIAL

    OK
    AT+CGDCONT=1,"IP","free"

    OK
    AT+CIPSTATUS

    +IPSTATUS: IP INITIAL

    OK
    AT+CGDCONT=1,"IP","free"

    OK
    AT+CIPSTATUS

    +IPSTATUS: IP INITIAL

    OK
    AT+CGDCONT=1,"IP","free"

    Idk why it's blocked at AT+CGDCONT=1,"IP","free" Maybe because there is a link with the result of CIPSTATUS ?

      • Hi yes it’s what i do but i cant find a real exemple of the CipStatus on internet for the Sim800 and i don’t have this board so i cant know if the tcpStatus is basesd on the number gived in the State of each step for connectionn. Because in the A6 CipStatus response there is no numbers and in the Sim800 At commands there is a lot but idk if it’s only a way to present the response in the pdf or if they are in the response when we write AT+CIPSTATUS

  15. Hello there, the people from elementzonline!
    You did a quite good job with this library, guys. It seems that the examples work smooth when downloaded on Arduino Uno.

    What I would like to do in my project is the following:
    – I need to publish some data on MQTT broker (I know the mqtt_host, mqtt_port, username, and password parameters of the broker). I was able to connect with the broker easily by using your connect(xxx) function.

    However, I have a problem to publish data on the broker. I know that you have a function

    GSM_MQTT::publish(char DUP, char Qos, char RETAIN, unsigned int MessageID, char *Topic, char *Message)

    for that purpose. However, what I send through MQTT should be properly formatted (JSON format). There are two events (temperature and location). Both of them have their details:

    # temperature
    {
    details: {
    celsius: “50”
    }
    }

    # location
    {
    details: {
    latitude: ‘-25.572176’,
    longitude: ‘124.453125’
    }
    }

    Here is how the data are sent by using a Ruby language:

    event = {
    uuid: “velo_1003”,
    event: {
    event_type: ‘location’,
    details: {
    longitude: “24.437148”,
    latitude: “5.273438”
    },
    device_info: {
    battery_status: “47”
    }
    }
    }.to_json
    => “{\”uuid\”:\”velo_1003\”,\”event\”:{\”event_type\”:\”location\”,\”details\”:{\”longitude\”:\”24.437148\”,\”latitude\”:\”5.273438\”},\”device_info\”:{\”battery_status\”:\”47\”}}}”
    client = MQTT::Client.connect(conn_opts)

    client.publish(
    ‘event’,
    event
    )

    My question is, what should I do to be able to properly send data from SIM900 !? Let’s say that I would like to send just a GPS coordinates to the broker. i suppose that Topic parameter for the publish(xxx) function should be “location”. What would be the content of the Message ?

    Thank you very much for your time and effort to help me.

    Sincerely,
    Bojan.

  16. Hi there,

    thank you very much for your great work.
    Unfortunately, i had some problems. I extract the files in the library directory and it will be found. If i try to compile your example sketch, i got some errors:

    * with a standard ubuntu 16.04 with arduino 1.0.5, i got this error:
    /home/gfr/sketchbook/libraries/GSM_MQTT/GSM_MQTT.cpp: In function ‘void serialEvent()’:
    /home/gfr/sketchbook/libraries/GSM_MQTT/GSM_MQTT.cpp:671:25: error: ‘_delay_us’ was not declared in this scope
    _delay_us(10);
    ^

    * with the latest arduino 1.8.0, i got a lot of warnings, for example:
    /home/gfr/arduino-1.8.0/libraries/GSM_MQTT/GSM_MQTT.cpp:154:43: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
    _sendAT(“AT+CIICR\r\n”, 5000) ;

    and finally, i got
    home/gfr/arduino-1.8.0/libraries/GSM_MQTT/GSM_MQTT.cpp:154:43: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
    _sendAT(“AT+CIICR\r\n”, 5000) ;

    Do you have a hint for me? Thank you very much.

    best regards

  17. Hello, thanks for your work. I have tested your library, It works fine on arduino.
    I have a new board msp430, i’m using energia to program for it. Energia like Arduino.
    But when I edit your library your library and use it for energia. My project NOT working. When i look at SoftwareSerial for debug information, it looks like this:
    OK
    AT
    ATE1

    OK
    AT+CREG?

    OK

    +CREG: 0,1

    OK
    AT+CIPMUX=0
    AT+CIPMODE=1

    ERROR
    AT+CGATT?

    +CGATT: 1

    OK
    AT+CIPSTATUS

    OK

    STATE: CONNECT OK
    AT+CIPSHUT

    SHUT OK
    AT

    OK
    ATE1

    OK
    AT+CREG?

    +CREG: 0,1

    OK
    AT+CIPMUX=0
    AT+CIPMODE=1

    OK
    AT+CGATT?

    OK

    +CGATT: 1

    OK
    AT+CIPSTATUS

    OK

    STATE: IP INITIAL
    AT+CSTT=”AIRTELGPRS.COM”

    OK
    AT+CIPSTATUS

    OK

    STATE: IP START
    AT+CIICR

    OK
    5EQUM
    OK

    STATE: IP GPRSACT
    AT+CIFSR

    10.91.209.18
    AT+CIPSTATUS

    OKTE: IP STATUS
    AT+CIPSTATUS

    OK

    STATE: IP STATUS
    AT+CIPSTART=”TCP”,”m13.cloudmqtt.com”,”15571″

    OK
    ¡¨ÊÊ
    Q
    CLOSED
    AT+CIPSTATUS

    OK

    STATE: TCP CLOSED
    AT+CIPSTART=”TCP”,”m13.cloudmqtt.com”,”15571″

    OK
    99
    Q

    Do you have any idea for my proplem ??????

    • Hii Huskar (and hopefully Elementzonline),
      You said you have tested the library and it was fine with arduino.
      I am wondering how did you connect the SIM800L to the arduino board?
      Did you make any changes to the library ?

      Because unless I swap Serial to mySerial and vice versa, the library just doesn’t work.
      Even if I did swap, the whole thing sometimes just stop at TCP_Flag = true.

      I think serialEvent() is not firing properly.

      I am using arduino nano board.
      Need some help please.

      thx

      • Hi sam. This may be of use to you.

        I had the same behaviour and it ended being the GSM sim cards that my provider gave me. The prepaid type had some filter that allowed the tcp connect (syn, syn ack, ack) but nothing more. The “postpaid”, with invoice, work great.

        Try to test the simcards in another environment, a.k.a. with a smartphone and MQTT app.

        Regards

      • Hi Guillermo,
        I am guessing you have successfully run mqtt-sim800-arduino using the original source code right ?
        How did you connect the modem to arduino ? (which pin)
        And are you able to see the logging console at the same time without confusing the serial communication?

        I have managed to make it run but I have to modify the codes:
        1. communicating to the modem using “mySerial” (modem is connected to pin 10 and 11) and console log using “Serial” (In other words, I replaced all “Serial” to “mySerial”, and vice verca)
        2. added serial data checking in “mySerial” inside GSM_MQTT::processing and GSM_MQTT::available functions and if data is available call serialEvent() (I have to do this because in the original code serialEvent never get called when there is data coming in from the modem)

    • hi Sam,
      i did same stuff you did on step on. replaces mySerial with Serials. but i dont get the second step? so i`m still stuck on
      MQTT.TCP_Flag = True
      i have same setup nano & sim800L USING 10,11 RX,TX could you please guide me on this?

    • Hi Huskar,
      Can you send me your working code ? I changed only “HOST ” ,” PORT” =”1883″ , Topic and massage in Originall Publish code. I can see CONNECT OK , But not update my MQTT sever ?
      Can U plz help me?

      Thankx….

  18. Hi Guys, great job with the library, but i am also having problems with changing it for a SIM900. i have modified the serial ports etc and that all seems good. The below is what I get in the debug print. Can anyone help me please. The SIM900 is on serial2

    OK
    AT+CREG?

    +CREG: 0,4

    OK

    +PACSP: 1
    AT+CREG?

    +CREG: 0,2

    OK
    AT+CREG?

    +CREG: 0,1

    OK
    AT+CIPMUX=0
    AT+CIPMODE=1

    OK
    AT+CGATT?

    +CGATT: 0

    OK
    AT+CGATT=1

    OK
    AT+CIPSTATUS

    OK

    STATE: IP INITIAL
    AT+CSTT=”everywhere”,”eesecure”,”secure”

    OK
    AT+CIPSTATUS

    OK

    STATE: IP START
    AT+CIICR

    OK
    AT+CIPSTATUS

    OK

    STATE: IP GPRSACT
    AT+CIFSR

    10.188.223.216
    AT+CIPSTATUS

    OK

    STATE: IP STATUS
    AT+CIPSTART=”TCP”,”www.listo-portal.com”,”1883″

    OK
    AT+CIPSTATUS

    OK

    STATE: TCP CONNECTING

    CONNECT OK
    AT+CIPSTATUS

    OK

    STATE: CONNECT OK
    AT+CIPSHUT

  19. Hello,

    i have tryed to Change the Code to Use Arduino Mega 2560 with HW-Serial.
    Can someone post a valid Code with HW-Serial Configuration?

    Greetings

  20. Hello! Help a newbie. I do not understand where to write the name of the access point, user name and password for my operator? AP, username, passvord?

  21. Hi,

    Thanks to elementzonline for this great work.

    I have tested this library to publish & subscribe messages.

    I need help in following things
    1. How would i get the acknowledgement of published messages?
    2. When everything is working fine & i remove the power supply of SIM800 module still library give me MQTT.available(). is there any way to recognize whether module is connected, lost network or hang.
    3. Also some times it got stuck in loop of CIPSTATUS giving STATE: IP START then i have to restart the system.

  22. Hi…
    what is the “AIRTELGPRS.COM” ….? in below mentioned part of code. what it is important in code?
    And, is it change to my website or not ?

    STATE: IP INITIAL
    AT+CSTT=”AIRTELGPRS.COM”

    OK

  23. Hi, i try to use the lib sim900 with arduino, but i can communicate gsm with arduino.
    So when Ardunino sends AT, arduino not receive a response from sim900.
    connection :
    SoftwareSerial mySerial(2, 3); // RX, TX
    GSMTX —–> D2
    GSMRX —–> D3

  24. Hi
    With the mysensors.org development, they use a serial gateway which receives the sensor values and then send the mqtt with an ethernet cable to the broker, my question is, they use 2 modules W5100 or the ENC28J60 for the TCP/IP stack, could this be replaced with a SIM800 module that will send mqtt to a cloud based server via GSM? Please see https://www.mysensors.org/build/ethernet_gateway and https://www.mysensors.org/build/mqtt_gateway , I’m desperate to get this to work, so if you can give me some advice, I would appreciate it
    Regards

  25. hi, I am an beginner to MQTT and GSM tech, could anybody provide with an detailed tutorial on how to go on about using this library tht would be great.

  26. HI
    Is there any way to send data more than 50 character at a time.
    I am facing issues while sending data more than 50 characters.

    • step1: browse to arduino-1.8.2\hardware\arduino\avr\cores\arduino from your arduino installation folder
      step2: open HardwareSerial.h
      step3: find this line #define SERIAL_RX_BUFFER_SIZE 64
      step4: change 64 to 150 or even high
      step5: in GSM_MQTT.h find these three line..
      #define UART_BUFFER_LENGTH 300 //Maximum length allowed for UART data
      #define TOPIC_BUFFER_LENGTH 50 //Maximum length allowed Topic
      #define MESSAGE_BUFFER_LENGTH 250 //Maximum length allowed data
      and update buffer sizes as required…
      thats it…

  27. I just downloaded the library. I don’t know how to publish a message from my Arduino to broker mosquitto. How to use the functions and stuff. Can anyone help ?

  28. Hi can I use it with the adafruit MQTT server ? i am able to connect to the server but then it disconnects automatically

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s