How to use MQTT with SIM900A and NodeMCU D1 mini(ESP 8266)

Hi, I’m using Sim900A with NodeMCU D1 mini(ESP 8266) and i want to use MQTT protocol. So how i can do this? Please help me for doing this.
Thank You in advance.

I’m using IBM Watson Service for MQTT.


First off you need to understand how the Mqtt Works. If you are sure of the things then i will recommend you to use the TinyGsm Library (Assuming Your Using Arduino to program ESP) , The examples on how you can connect to the Mqtt server and use is given in the library examples itself.

Besides that if you face any issues feel free to ask.

Parth Temkar

1 Like

In this case ESP8266 will just be like any other MCU if you are sending data from SIM900A.
If ES8266 is being used as MCU to send AT commands to GSM module, then you can use any MQTT library that can run on Arduino. You can even use the code i had shown in my blog with little tuning to handle responses properly.

If ESP8266 itself is creating a TCP connection then you can use any ESP8266 MQTT library and SIM900A will be redundant.

Please suggest any working library OR code.

You can try this,
You need to handle command responses which i am not doing in this code. I have just used delays.

You can even try this if you are using the TinyGsm Library. Just Change Some Pins, and thats the sample code you would have to connect to for this to work. but you can easily change the parameters.
and if you wish to add the user and pass for MQTT then add it in the line 107.

Eg: if(!mqtt.connect(ClientName,User,Pass))

Change the client Name to whatever you like and the USer and PAss should be changed to what you have in the IBM Watson

Parth Temkar

1 Like

Thank you Parth Temkar
This Library is worked for me :slight_smile:

Thank You for your Help :slight_smile:
This lib is working but i have one issue with this is that when i publish any event from my Mobile App on this gsm(ESP826) MQTT subscribed topic so my continuous gsm(ESP826) publishing events is stopped and my code stuck in pubsub Client.loop() function and callback function is not receive any event.

Please share the code you are using.

Parth Temkar

I’m using NodeMCU D1 Mini(ESP8266) with GSM module SIM900A for MQTT i’m just in receiving event on which i subscribed successfully when i publish any event from my Mobile App to subscribed topic so my NodeMCU D1 Mini(ESP8266) Loop() stop and stuck in " return _client->read(); " read() method every time for forever.
Please tell me the solution what i tried for receiving events using MQTT using NodeMCU D1 Mini(ESP8266) with GSM module SIM900A.
Thank You.

uint8_t PubSubClient::readByte() {
while(!_client->available()) {Serial.println(“readByte() while loop”);}
Serial.println(“read() START”);
return _client->read(); <----------- stuck in this

Here is my Code:

// Select your modem:
//#define TINY_GSM_MODEM_SIM800
//#define TINY_GSM_MODEM_A6
//#define TINY_GSM_MODEM_M590

#include <TinyGsmClient.h>
#include <PubSubClient.h>

// Your GPRS credentials
// Leave empty, if missing user or pass
const char apn[] = “apn”; //portalnmms for vodafone
const char user[] = “”;
const char pass[] = “”;

void callback(char* topic, byte* Payload, unsigned int PayloadLength);
String orgId = “ordId”;
String authToken = “”;
String clientName;
String HubId = “”;
String HubType = “Hub”;

char* cmdTopic = “iot-2/cmd/AppEvents/fmt/json”;
char* evtTopic = “iot-2/evt/Events/fmt/json”;
char* authMethod = “use-token-auth”;
String server = orgId + “”;

// Use Hardware Serial on Mega, Leonardo, Micro
//#define SerialAT Serial1

// or Software Serial on Uno, Nano
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(14, 12); // RX, TX

TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
PubSubClient cmdClient((char*)server.c_str(), 1883, callback, client);

char sim900lreset = 5;
void setup() {
pinMode(sim900lreset, OUTPUT);
digitalWrite(sim900lreset, LOW);
digitalWrite(sim900lreset, HIGH);
Serial.println("\n\nESP Start\n\n");

Serial.print(“Waiting for network…”);
if (!modem.waitForNetwork()) {
Serial.println(" fail");
while (true);
Serial.println(" OK");

Serial.print(“Connecting to “);
if (!modem.gprsConnect(apn, user, pass)) {
Serial.println(” fail”);
while (true);
Serial.println(" OK");

HubId = “hubID”;
authToken = “token”;
clientName += “d:”;
clientName += orgId + “:”;
clientName += HubType + “:”;
clientName += HubId;

int count = 1;
void loop() {
Serial.println("\nStart Loop");
if (!cmdClient.loop())

String evtPayload = “{”;
evtPayload += "“hubId”: “LinkedThings_HeadOffice_Testing_TestingMac”, ";
evtPayload += "“type”: “Counter”, ";
evtPayload += "“value”: " + String(count++);
evtPayload += “}”;

postEvt(evtTopic, evtPayload);
Serial.println(“End Loop\n”);
void postEvt(char* DeviceTopic, String evtPayload)
Serial.print("\nSending payload: “);
if (cmdClient.publish(DeviceTopic, (char*) evtPayload.c_str()))
Serial.println(“Publish ok”);
Serial.println(“Publish failed”);
void mqttConnect()
if (!cmdClient.connected())
Serial.println(“cmdClient not connect”);
Serial.println(“Reconnecting MQTT client to : " + server);
int counter = 0;
while (!cmdClient.connect((char*) clientName.c_str(), authMethod, (char*) authToken.c_str()))
if (counter > 10)
void initManagedDevice()
if (cmdClient.subscribe(cmdTopic))
Serial.println(“subscribe to cmd OK”);
Serial.println(“subscribe to cmd FAILED”);
void callback(char* cmdTopic, byte* Payload, unsigned int PayloadLength)
String cmdPayload = “”;
Serial.print("callback invoked for topic: “); Serial.println(cmdTopic);
for (int i = 0; i < PayloadLength; i++)
cmdPayload += (char)Payload[i];
if (cmdPayload.length() > 0)
Serial.println(”\nCMD receive is : " + cmdPayload);

Unfortunately i don’t have an ESP to test this code. but i would like to ask you,

  1. What is the significance of using the IBM Mqtt Cloud?
  2. What exactly is your application and what are you trying to achieve?

Have you tried using the CloudMqtt as its free for 10 subscription , or maybe even the adafruit

Parth Temkar

Now on cloudMQTT it has same issue publishing evt from APP to Subscribed topic ESP8266 not received :frowning: and stuck in same as previouse :sob:

I’m using same pubsub lib with WiFi client it works perfectly. But with GSM client it’s not working.

Cloud mqtt will work, I’ve been using it in all of my projects. I’ll share a code with you which you can test. Give me some time.

Parth Temkar

i have just reinstall my pubsub lib now it’s working but received event is not completed.
my callback function called and receive event

callback invoked for topic: iot-2/cmd/AppEvents/fmt/json
CMD receive is : My name is Naja O

but my msg was My name is Najaf Haider.

hello. can I know how to program gsm 900a with nodemcu ? I need send SMS from gsm to my phone number. anybody can help me? please