MQTT to Google IoT

Thanks for updating Patrick. We would be waiting to hear from you soon :slight_smile:

Update… SIM868 also seems to have TCP over SSL as standard, though I have not fully tested it to Azure MQTT.

Hi Patrick,
Can you please share the steps followed to send data over encrypted TCP/MQTT connection. Did you generate the keys needed for it?

Hello,

I am also trying to create connect packet for microsoft azure and after pushing packets I am getting 32,2,0,1 as response in bytes
What it means ? whether it is connected or not ?

Hi Parth,

Id this in hex or decimal?

32 would make no sense as a packet header for a Connack:
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#Figure_2.2-

Its in bytes[4] = {32,2,0,1}
After Connect Packet I am passing publish packet and as I can see message is not getting published
Can you cross check my publish packet
0x30
, 0x28 //RL
, 0x00
, 0x24 //Topic Length [ Topic for Azure : devices/EnergyMeter/messages/events/]
, 0x64
, 0x65
, 0x76
, 0x69
, 0x63
, 0x65
, 0x73
, 0x2f
, 0x45
, 0x6e
, 0x65
, 0x72
, 0x67
, 0x79
, 0x4d
, 0x65
, 0x74
, 0x65
, 0x72
, 0x2f
, 0x6d
, 0x65
, 0x73
, 0x73
, 0x61
, 0x67
, 0x65
, 0x73
, 0x2f
, 0x65
, 0x76
, 0x65
, 0x6e
, 0x74
, 0x73
, 0x2f
, 0x48 //H
, 0x69 //i

Here is some of my connect code to assist. It is based heavily on Ravi’s previous MQTT examples so thanks to him for this.
Where you see XXXXXX your own details go. The password you need to generate using tokens yourself for each device.

One other thing I have learned is that the “Keep Alive” seems to have a maximum of 2 minutes but I am investigating this.

const char MQTTClientID[20] = “XXXXXXXX”;
const char MQTTSubscribeTopic[100] = “devicesXXXXXXXXX/messages/devicebound/#”;
const char MQTTPublishTopic[100] = “devices/XXXXXXX/messages/events/”;
const char MQTTUsername[100] = “XXXXXXX.azure-devices.net/XXXXXXXX”;
const char MQTTPassword[200] = “SharedAccessSignature sr=XXXXXXX.azure-devices.net%2Fdevices%2FXXXXXXXXX&sig=95B790Gi5KhxdwY2QHxQroVslUsrEz7P4aVFkezDe%2FQ%3D&se=1534752364”;

void connect_MQTT(void)
{
SIMCOM_MODE = AT_COMMS;
//Calculate MQTT Lengths
MQTTProtocolNameLength = strlen(MQTTProtocolName);
MQTTClientIDLength = strlen(MQTTClientID);
MQTTUsernameLength = strlen(MQTTUsername);
MQTTPasswordLength = strlen(MQTTPassword);
datalength = MQTTProtocolNameLength + 2 + 4 + MQTTClientIDLength + 2 + MQTTUsernameLength + 2 + MQTTPasswordLength + 2;

//Get Prompt
__delay_ms(10);
__delay_ms(1);
putsU2("AT+CIPSEND"); //Send the command to 5300E
__delay_ms(200);
__delay_ms(1);

//Send Byte - Declare as connect
putU2(0x10); //0x10 = Fixed value determined by MQTT Standard
//Send Byte - remaining Length  -- Calculated Manually 

X = datalength;
while (X > 0)
    {
    encodedByte = (X % 128);
    X = (X / 128);
    // if there are more data to encode, set the top bit of this byte (to indicate more data)
    if (X > 0)
        encodedByte = (encodedByte | 128);
    putU2(encodedByte);
    }

//Send 2 Bytes - Protocol Length
putU2(MQTTProtocolNameLength >> 8);
putU2(MQTTProtocolNameLength & 0xFF);
//Send Bytes - Protocol Name
for (Counter = 0; Counter < MQTTProtocolNameLength; Counter++)
    {
    putU2(MQTTProtocolName[Counter]);
    }

//Send Byte - Level
putU2(MQTTLVL);
//Send Byte - Flags
putU2(MQTTFlags);
//Send 2 Bytes - Keep Alive
putU2(MQTTKeepAlive >> 8);
putU2(MQTTKeepAlive & 0xFF);

//Send 2 Bytes - Client ID Length
putU2(MQTTClientIDLength >> 8);
putU2(MQTTClientIDLength & 0xFF);
//Send Bytes - (MQTTClientID);
for (Counter = 0; Counter < MQTTClientIDLength; Counter++)
    {
    putU2(MQTTClientID[Counter]);
    }
//Send 2 Bytes - MQTTUsernameLength
putU2(MQTTUsernameLength >> 8);
putU2(MQTTUsernameLength & 0xFF);
// putU2(0x00);
// putU2(0x00);


// Send Bytes -(MQTTUsername);
for (Counter = 0; Counter < MQTTUsernameLength; Counter++)
    {
    putU2(MQTTUsername[Counter]);
    }
//Send 2 Bytes - MQTTPasswordLength
putU2(MQTTPasswordLength >> 8);
putU2(MQTTPasswordLength & 0xFF);
// Send Bytes - MQTTPassword
for (Counter = 0; Counter < MQTTPasswordLength; Counter++)
    {

    putU2(MQTTPassword[Counter]);
    }

//Send Byte - CTRL-Z
putU2(0x1A)
}

Followed and As I can see connack packet 2,0 in my bytes seems it is connected.
I think problem is in publish packet.
I am using C#.Net.

Keep Alive is 0x3c in my case. 2 minutes means 0x78 right ?

Pub ack should have a header 0x40

The code I use is nearly identical to Ravi’s

void Publish_MQTT(void)
{
SIMCOM_MODE = AT_COMMS;
//Calculate MQTT Lengths

MQTTPublishTopicLength = strlen(MQTTPublishTopic);
MQTTPublishPayloadLength = strlen(MQTTPublishPayload);
datalength = (2 + MQTTPublishTopicLength + MQTTPublishPayloadLength + 2);

//Get Prompt
__delay_ms(10);
__delay_ms(1);
putsU2("AT+CIPSEND"); //Send the command to 5300E
__delay_ms(200);
__delay_ms(1);

//Send Byte - Declare as publish
putU2(0x32); //Fixed value determined by MQTT Standard
//Send Bytes - remaining Length  

//putU2(datalength);
X = datalength;
while (X > 0)
    {
    encodedByte = (X % 128);
    X = (X / 128);
    // if there are more data to encode, set the top bit of this byte (to indicate more data)
    if (X > 0)
        encodedByte = (encodedByte | 128);
    putU2(encodedByte);
    }

//Send 2 Bytes - Topic Length
putU2(MQTTPublishTopicLength >> 8);
putU2(MQTTPublishTopicLength & 0xFF);
//Send Publish Topic
for (Counter = 0; Counter < MQTTPublishTopicLength; Counter++)
    {
    putU2(MQTTPublishTopic[Counter]);
    }

//Send Bytes - Packet ID
putU2(0x00);
putU2(0x03);

//Send Publish Payload
for (Counter = 0; Counter < MQTTPublishPayloadLength; Counter++)
    {

    putU2(MQTTPublishPayload[Counter]);
    }
//Send Byte - CTRL-Z
putU2(0x1A);

ListenServer(5);
SIMCOM_MODE = MQTT_STANDBY; //Revert the comms mode
}
1 Like

On publish server returns nothing.
Please check my publish packet. Can you tell me whether it is wrong ?
Please Help i am stuck…you said add header 0x40 but as your code suggest I have to add 0x32

Try with QoS set to 1.
Azure does not like it set to 0.

Changing 0xC2 to 0xCA for Qos1 results same for connect packet 32,2,0,1
Have converted bytes to Hex for 32,2,0,1 it is 0x20-0x02-0x00-0x01

Can you please take online session and have a look whats happening in my case ?

0xC2?
Header should be 0x32 like in my example.

0x20-0x02-0x00-0x01 looks like a valid Connack response from server.
Whats the Puback response?
it may just close the connection if its malformed.

But In my connect response as per below link
0x01 is not good

Puback response is nothing empty

Is the SIM800L module capable of communicating with the Azure iot hub?

Hi,
i guess as far as i have worked with the GSM Modules, all you need to see is that if the module is capable to establish a TCP connection. and supports 2G/3G/4G connectivity. If you have that, then you can use it anyway. and i Guess sim800L has that.

Cheers!
Parth Temkar

1 Like

Is the SIM800L module capable of communicating with the Azure iot hub?

The SIM868 certainly is. The AT instruction set is the same I think, just be sure it has SSL and is enabled.

1 Like

Hello there!
I have the same doubt the only difference is I am using STM32 L47RG board and SIM800. I have followed the tutorial by Ravi Sir “MQTT Protocol tutorial using SIM900/SIM800 modules - MQTT over TCP” and its working perfectly fine. Now I am facing problem to connect and publish data to google cloud iot core. Can you please guide me regarding the same. Please provide me steps accordingly.
Thanks in advance :slight_smile:

1 Like

At last u need GSM with TLS1.2 ver. Otherwise u will not able to connnect to Google cloud. SIM800 (And all 2-3G modems) has TLS1.0. So u need update it to 4G(SIM7000, for ex.)

SIM800C can support TLS1.2 with firmware update. The same firmware update might not be available for other parts in SIM800 family