MQTT packets not reaching server even with TCP connection opened

Hello, I take code, which Ravi give me. Insert in code my data. So I can’t see data on server. I see, that module connect to mqtt broker, but I didn’t see messages which send to broker. I use sim800l and arduino uno. Also when I send commmand AT+CIPMODE=0, I recived error.
Ardiuno_MQTT.ino (4.7 KB)

Hi Alexis,
You have to take care of command responses. The code is just a guideline to follow. Its not fool proof and needs other command responses to be taken care of. After module starts send CIPMODE only once. May be you already have a TCP connection open so its giving CIPMODE error. Send it before creating TCP connection.

Look for responses from module on serial port and share the errors here, I can suggest better.

1 Like

You are right Ravi, I change time delay in code and all working :slight_smile: As I see module didn’t catch data from network. Thank you Ravi

1 Like

So Dear Ravi, How to recived data from uart and write data to another array (rx_buffer). After this, compare data with array which I write in memory arduino. The code which I write in down my vision how as i understand. Please help me.

unsigned char Array1[] = “OK”;
unsigned char Array2[] = “ERROR”;
rx_buffer[100];
void setup()
{

Serial.begin(9600);    

}
bla…bla…bla
void loop()
{

Serial.print("AT+CSTT=\"internet.tele2.ru\",\"\",\"\"\r\n");
while (Serial.available() <0)

{
rx_buffer=Serial.readBytes();
if(rx_buffer==Array1[])
{flag=1;}
if(rx_buffer==Array2[])
{
Serial.print(“AT+CSTT=“internet.tele2.ru”,”",""\r\n");
}
}
}

Hi Alexis,
I think i showed one piece of code in this video where i read data from UART port coming from SIM900 and search for command. You can adopt the same.

Ravi, I take part from your code, but I don’t understand where I made error.

unsigned char Buff[250];
unsigned char BuffIndex;
unsigned char src[15]=“1234567891”;
void setup() {
Serial.begin(9600);
Serial.println("Arduino MQTT ALEXIS ");
memset(Buff, ‘\0’, 250); // Initialize the string
BuffIndex=0;
}

void loop() {
while(1)
{

// put your main code here, to run repeatedly:
if(Serial.available()>0)
{
Buff[BuffIndex] = Serial.read();
if (memcmp (src, Buff, 10) == 0) Serial.println(“Great”);

          BuffIndex++;
          if(BuffIndex>250) 
            {
              BuffIndex=0;
            }
  }
}

}

You cant compare whole buff with src string using memcmp, it will always do end to end comparision and you will always get false. Use strtok function for this. You will get ready examples online on strtok function.

Ravi I can’t find example, which I can understand. So… why you in code for arduino (in example “Sim800/900 SMS Reception” ) Your buffer index starts from 5, even after BuffIndex overflow you again assign a value 5. Thank you)))

I do that so that the index doesnt point to previous buffer. If i use 0 then 0-1 = -1 will be wrong index value, so i add that offset.
Here is strtok example. https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm

1 Like

Dear Ravi, I use this code to find the data I need, because Serial always has some information. If I write Serial.print(hh) in Serial Monitor always print new symbols until (hh) overflow. The String (hh) is overflowed and I no longer receive data. I send data to the topic (home). Before the send data, I put (*).

if( Serial.available() > 0 )
    {
   hh =  Serial.read();
       if(hh=='*') 
       {  
         tt = Serial.readString();
         Serial.print(tt);
         if(tt == "qwerty") {j=112;  Serial.println( j); }
        } hh=="";
     }

Dear Alexis,
I didn’t quite understand the question and the code and what you are trying to achieve.
Please explain .
Use the code option </> in editor while pasting code. It will highlight it properly

You code, where you send and read data from Mqtt broker. My code recived data in cycle while(1).Ok, I read buffer, but buffer always full some data. I use tthis command for write data to unsigned char (hh) -> hh = Serial.read (); After I looking for symbol ‘’ ->if (hh == '’). Data after ‘*’ I write to String (tt)->tt = Serial.readString (); If data in String(hh) =“qwerty” ->if (tt == “qwerty”) I perform {j = 112; Serial.println (j); } .
I can’t understand why my buffer always full some symbols. If I switch off pins tx from Sim800l data end.

Here are my few observations,
if(tt == “qwerty”) this is not a correct statement in C, I am not sure Arduino supports it too.
You cant compare strings like this, Use strcmp function. Search about it
Why you printing 112 here?

Why hh==""; is used. What does this statement do with ==

The statement (tt == “qwerty”) - is working.
Why you printing 112 here? - This just for indication.
hh=="" - my error I want to clear char hh.
But I write new code with strcmp. But Serial always send some symbols (I think strN or serial - overflow ). And after some time 20-30 seconds didn’t recived command. And data can’t compare.What am I doing wrong?

while(1)
    {

      inSize=0; // Reset variable
      memset(strN, '\0', 128); // clear massive
       if( Serial.available() > 0 )
         { 
            inSize = Serial.available(); // recived lenght string and write it to variable                           
            for (int i = 0; i < inSize; i++)
                {
                  strN[i] = Serial.read(); //read each symbol and write to massive                         
                } 
            if(strcmp(strN,"qwerty")==0) {Serial.println("Led is ON"); digitalWrite(LED_BUILTIN, HIGH);}
            else if(strcmp(strN,"123456")==0) {Serial.println("Led is OFF"); digitalWrite(LED_BUILTIN, LOW);}
            if(inSize>=128) {inSize=0;  memset(strN, '\0', 128);}
          } 

    }

I would suggest to use strtok here. strcmp will only work if you are doing absolute comparison.

But your serial buffer can have other data like the AT command responses like OK and ERROR etc.

So you should better search for a string like “qwerty” using strtok instead of comparing two strings using strcmp.

I read data from serial port. The data sent to serial without end. I think it overflow and I can’t recived data more.

Sorry, but I can’t understand how work strtok. Because i try to use another function.

If you are printing anything out at GSM module, it will echo it back. So make sure you have turned off echo using ATE0.
Otherwise you would print the LED on and LED is OFF messages and they would be echoed back and collected in your buffer in Arduino.

The code looks fine to me and there shouldnt be any overflows as you are printing fixed strings.

1 Like

Ok, I will try switch off echo. And after it I will reporting about this to you/ Thank you.

All working, thank you Ravi. So how I can hold connect with mqtt server without gap. I know that we can to increase time in field (MQTTKeepAlive). But I want never to gap connect, so as not to miss the data from publisher.

You need to keep sending ping packets to keep the connection alive.