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


#1

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.


#2

I’m using IBM Watson Service for MQTT.


#3

Hi,

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.

Cheers!
Parth Temkar


#4

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.


#5

Please suggest any working library OR code.


#6

You can try this,


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


#7

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 test.mqtt.org 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

Cheers!
Parth Temkar


#8

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


#9

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.


#10

Hi,
Please share the code you are using.

Cheers!
Parth Temkar


#11

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_SIM900
//#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 + “.messaging.internetofthings.ibmcloud.com”;

// 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);
delay(1000);
digitalWrite(sim900lreset, HIGH);
Serial.begin(115200);
SerialAT.begin(115200);
Serial.println("\n\nESP Start\n\n");
delay(3000);

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

Serial.print(“Connecting to “);
Serial.print(apn);
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;

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

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

postEvt(evtTopic, evtPayload);
delay(5000);
Serial.println(“End Loop\n”);
}
void postEvt(char* DeviceTopic, String evtPayload)
{
mqttConnect();
Serial.print("\nSending payload: “);
Serial.println(evtPayload);
if (cmdClient.publish(DeviceTopic, (char*) evtPayload.c_str()))
Serial.println(“Publish ok”);
else
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()))
{
Serial.print(”.”);
delay(50);
counter++;
if (counter > 10)
{
break;
}
}
initManagedDevice();
}
}
void initManagedDevice()
{
if (cmdClient.subscribe(cmdTopic))
Serial.println(“subscribe to cmd OK”);
else
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);
}


#12

Hi,
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

Cheers!
Parth Temkar


#13

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:


#14

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


#15

Hey,
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.

Cheers!
Parth Temkar


#16

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
+CIPR

but my msg was My name is Najaf Haider.