Sim900 CloudMQTT, problems with the code?

mqtt

#1

Hi, this is my first project with Arduino and also the first attempt at connecting to the Cloud.
I have no experience with the troubles I am encountering and I would really appreciate your help.

I have an Arduino Mega 2560 and a SIM900 and I am trying to connect to a server at cloudmqtt.com

So far from the responses from the Serial Monitor it should be working properly, but sadly it is not sending the message to the server.

I insert the responses from the AT commands in the Serial Monitor down below, and also the Code I am running.

Any idea on to why it is not working?

When I go into the LOG page in the server, it displays this.

2019-02-05 10:09:21: New client connected from 88.0.235.55 as cloudmqtt-ws-ui-0.42801106527850075 (c1, k60, u’sefbkjmy’).
2019-02-05 10:09:31: New connection from 2.141.129.160 on port 11529.
2019-02-05 10:09:44: Invalid protocol version 3 in CONNECT from 2.141.129.160.
2019-02-05 10:09:44: Socket error on client <unknown>, disconnecting.

#include <SoftwareSerial.h>

SoftwareSerial SIM900(10,11);

void setup () {
  
  SIM900.begin(19200);
  Serial.begin(19200);
  Serial.println("Setup");
  delay(5000);

}


void loop () {

  Send_data_cloud ();
  delay(3000);

}


void Send_data_cloud (){

  Serial.println("");
  Serial.println("Communication SIM900/Arduino");
  SIM900.println("AT");
  delay(1000);
  printSerialData();
  delay(1000);

  Serial.println("");
  SIM900.println("AT+CGATT=1"); //
  delay(1000);
  printSerialData();
  delay(1000);

  Serial.println("");
  SIM900.println("AT+CMEE=2");
  delay(2000);
  printSerialData();
  delay(1000);
  
  Serial.println("Connecting to APN");
  SIM900.println("AT+CSTT=\"movistar.es\",\"\",\"\""); // 'AT+CSTT='movistar.es','','','OK'
  delay(5000);
  printSerialData();
  delay(1000);

  Serial.println("GPRS connection");
  SIM900.println("AT+CIICR"); //la resposta esperada és “OK”
  delay(5000);
  printSerialData();
  delay(2000);

  Serial.println("Local IP");
  SIM900.println("AT+CIFSR");
  delay(5000);
  printSerialData();
  delay(1000);

  Serial.println("Connecting to Port and Server");
  SIM900.println("AT+CIPSTART=\"TCP\",\"m13.cloudmqtt.com\",\"11529\"");
  delay(5000);
  printSerialData();
  delay(3000);
  
  Serial.println("Enviant CONNECT i PUBLISH al servidor...");
  SIM900.println("AT+CIPSEND");
  delay(5000);
  printSerialData(); 
  delay(1000);
  send_connect(); 
  delay(1000);
  send_publish(); 
  delay(5000);
  printSerialData(); 
  
  Serial.println("Shutting down...");
  SIM900.println("AT+CIPSHUT");
  delay(5000);
  printSerialData();
  delay(2000);
  
}


void send_connect(){ 
  
  int i;
  
  char con[50] = {0x10,0x2A,0x00,0x04,0x4D,0x51,0x54,0x54,0x03,0xC2,0x00,0x3C,0x00,0x06,0x43,0x4C,0x49,0x45,0x4E,0x54,0x00,0x08,0x73,0x65,0x66,0x62,0x6B,0x6A,0x6D,0x79,0x00,0x0C,0x53,0x47,0x72,0x76,0x4D,0x49,0x43,0x66,0x55,0x63,0x6C,0x4F};
  //CONN  RL PLEN M Q T T  LVL FL KA   CIDLEN C L I E N T  
  //10    2A 0004 4D515454 03  C2 003C 0006   434C49454E54 
  //ULEN s e f b k j m y  PWLEN S G r v M I C f U c l O
  //0008 736566626B6A6D79 000C  534772764D49436655636C4F

  for (i=0;i<44;i++){
  
    SIM900.write(con[i]);
  
  }

}


void send_publish(){

  int i;
  char pub[50] = {0x30,0x15,0x00,0x0A,0x6A,0x6F,0x73,0x65,0x70,0x74,0x6F,0x70,0x69,0x63,0x68,0x6F,0x6C,0x61,0x6A,0x6F,0x73,0x65,0x70};
  //PUB  RL  TPLEN   j o s e p t o p i c   h o l a j o s e p
  //30   15  000A    6A6F736570746F706963  686F6C616A6F736570


    for (i=0;i<23;i++){ 
  
      SIM900.write(pub[i]);
  
    }
    
    SIM900.print("}");
    SIM900.write(0x1A);
  
}


void printSerialData(){ //Response from AT commands
  
  while (SIM900.available()!=0){
    
     Serial.write(SIM900.read());
     
  }
  
}


#2

Look at connect acknowledgement packets which are 2 byte hex values , not visible on Arduino terminal. Use TMFT or Docklight for seeing those values.