SIM800 GSM send data to Azure IoT Hub

Hello,
I`m trying to send data from my Arduino Uno using SIM800 GSM module to Azure IoT Hub. I have already created my azure iot hub, registered new device and downloaded Device Explorer. Im using microSIM with static IP address.
My question is: what should I do next?

You can send data to it by enabling SSL in your module and adding the certificates and use HTTP or MQTT to send the data.

Hello, I have a problem with azure. I’am using MQTT.
I started with an example from mbari_peter - sim800-mqtt-ravi but it doesn’t work.
I started to combine with other examples, but still without success.
I will be grateful for your help, or maybe someone has a working code? I don’t see any package reaching Azure.

I get answers:
CIPSTART - “connect”
CIPSEND (connect packet) - “send ok”
CIPSEND (published packet) - “send ok, close ok”

Settings:
const char * MQTTHost = "AAA.azure-devices(.)net”;
const char * MQTTPort = “8883”;
const char * MQTTClientID = “BBB”;
const char * MQTTTopic = “devices/BBB/messages/events/”;
//const char * MQTTTopic2 = “devices/BBB/messages/devicebound/#”;
const char * MQTTProtocolName = “MQQTisd”;
const char MQTTLVL = 0x03;
const char MQTTFlags = 0xC2;
const unsigned int MQTTKeepAlive = 60;
const char * MQTTUsername = “AAA. azure-devices(.)net/BBB/?api-version=2018-06-30”;
const char * MQTTPassword = “SharedAccessSignature sr=AAA.azure-devices(.) net%2Fdevices%2FBBB&sig=w%2FQ3IrJ%2B5wcjAARW3ph%2FNwyf2TlRJwH1Sa%2BorTVc8po%3D&se=2645177488”;
const char MQTTQOS = 0x00;
const char MQTTPacketID = 0x0001;

I tried to change:
MQTTProtocolName = “MQQT”;
MQTTLVL = 0x03 or 0x04;
MQTTQOS = 0x00 or 0x01;

modeminit:
sendData( “AT+CFUN=1”, 2000, DEBUG);
sendData( “AT+CIPSHUT”, 2000, DEBUG);
sendData( “AT+CGATT=0”, 2000, DEBUG);
sendData( “AT+CGATT=1”, 2000, DEBUG);
delay(10000);
sendData( “AT+CGDCONT=1,“IP”,“internet””, 2000, DEBUG);
sendData( “AT+CGACT=1,1”, 2000, DEBUG);
sendData( “AT+CIPMUX=0”, 2000, DEBUG);
sendData( “AT+CIPMODE=1”, 2000, DEBUG);
sendData( “AT+CIPSRIP=0”, 2000, DEBUG);
sendData( “AT+CGATT?”, 2000, DEBUG);
sendData( “AT+CSTT=internet”, 2000, DEBUG);
sendData( “AT+CIICR”, 2000, DEBUG);
sendData( “AT+CIFSR\r\n”, 2000, DEBUG);
sendData( “AT+SSLOPT=1,1”, 2000, DEBUG);
sendData( “AT+CIPSSL=1”, 2000, DEBUG);

Digicert:
#define CERT_FILE “C:\USER\digicert.cer”

void digicert()
{
SerialMon.println(F(“Initializing modem…”));
// modem.init();
initgsm();

modem.sendAT(GF("+FSCREATE=" CERT_FILE));
if (modem.waitResponse() != 1) return;

const int cert_size = sizeof(cert);

modem.sendAT(GF("+FSWRITE=" CERT_FILE “,0,”), cert_size, GF(",10"));
if (modem.waitResponse(GF(">")) != 1) {
return;
}

for (int i = 0; i < cert_size; i++) {
char c = pgm_read_byte(&cert[i]);
modem.stream.write©;
}

modem.stream.write(GSM_NL);
modem.stream.flush();

if (modem.waitResponse(2000) != 1) return;

modem.sendAT(GF("+SSLSETCERT="" CERT_FILE “”"));
if (modem.waitResponse() != 1) return;
if (modem.waitResponse(5000L, GF(GSM_NL “+SSLSETCERT:”)) != 1) return;
const int retCode = modem.stream.readStringUntil(’\n’).toInt();

SerialMon.println();
SerialMon.println();
SerialMon.println(F(“"));
SerialMon.print(F("Setting Certificate: "));
SerialMon.println((0 == retCode) ? “OK” : “FAILED”);
SerialMon.println(F("
”));
}

digicert.h:
const char cert[] PROGMEM =
“-----BEGIN CERTIFICATE-----\r\n”
“MIIDdzCCAl+gAwIBAgIEAgAAuTANBgkqhkiG9w0BAQUFADBaMQswCQYDVQQGEwJJ\r\n”
“RTESMBAGA1UEChMJQmFsdGltb3JlMRMwEQYDVQQLEwpDeWJlclRydXN0MSIwIAYD\r\n”
“VQQDExlCYWx0aW1vcmUgQ3liZXJUcnVzdCBSb290MB4XDTAwMDUxMjE4NDYwMFoX\r\n”
“DTI1MDUxMjIzNTkwMFowWjELMAkGA1UEBhMCSUUxEjAQBgNVBAoTCUJhbHRpbW9y\r\n”
“ZTETMBEGA1UECxMKQ3liZXJUcnVzdDEiMCAGA1UEAxMZQmFsdGltb3JlIEN5YmVy\r\n”
“VHJ1c3QgUm9vdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKMEuyKr\r\n”
“mD1X6CZymrV51Cni4eiVgLGw41uOKymaZN+hXe2wCQVt2yguzmKiYv60iNoS6zjr\r\n”
“IZ3AQSsBUnuId9Mcj8e6uYi1agnnc+gRQKfRzMpijS3ljwumUNKoUMMo6vWrJYeK\r\n”
“mpYcqWe4PwzV9/lSEy/CG9VwcPCPwBLKBsua4dnKM3p31vjsufFoREJIE9LAwqSu\r\n”
“XmD+tqYF/LTdB1kC1FkYmGP1pWPgkAx9XbIGevOF6uvUA65ehD5f/xXtabz5OTZy\r\n”
“dc93Uk3zyZAsuT3lySNTPx8kmCFcB5kpvcY67Oduhjprl3RjM71oGDHweI12v/ye\r\n”
“jl0qhqdNkNwnGjkCAwEAAaNFMEMwHQYDVR0OBBYEFOWdWTCCR1jMrPoIVDaGezq1\r\n”
“BE3wMBIGA1UdEwEB/wQIMAYBAf8CAQMwDgYDVR0PAQH/BAQDAgEGMA0GCSqGSIb3\r\n”
“DQEBBQUAA4IBAQCFDF2O5G9RaEIFoN27TyclhAO992T9Ldcw46QQF+vaKSm2eT92\r\n”
“9hkTI7gQCvlYpNRhcL0EYWoSihfVCr3FvDB81ukMJY2GQE/szKN+OMY3EU/t3Wgx\r\n”
“jkzSswF07r51XgdIGn9w/xZchMB5hbgF/X++ZRGjD8ACtPhSNzkE1akxehi/oCr0\r\n”
“Epn3o0WC4zxe9Z2etciefC7IpJ5OCBRLbf1wbWsaY71k5h+3zvDyny67G7fyUIhz\r\n”
“ksLi4xaNmjICq44Y3ekQEe5+NauQrz4wlHrQMz2nZQ/1/I6eYs9HRCwBXbsdtTLS\r\n”
“R9I4LtD+gdwyah617jzV/OeBHRnDJELqYzmp\r\n”
“-----END CERTIFICATE-----\r\n”;

void SendConnectPacket(void)
{
Serial1.println(“void SendConnectPacket\r\n”);
//Serial3 .print("\r\nAT+CIPSEND\r\n");
sendData(“AT+CIPSEND\r\n”, 4000, DEBUG);

delay(2000);
Serial3.write(0x10);

MQTTProtocolNameLength = strlen(MQTTProtocolName);
MQTTClientIDLength = strlen(MQTTClientID);
MQTTUsernameLength = strlen(MQTTUsername);
MQTTPasswordLength = strlen(MQTTPassword);
datalength = MQTTProtocolNameLength+2+4+MQTTClientIDLength+2+MQTTUsernameLength+2+MQTTPasswordLength+2;

X=datalength;
do
{encodedByte = X%128;
X = X/128;
if ( X > 0 ) { encodedByte |= 128;}

    Serial3.write(encodedByte);
    
    }

while ( X > 0 );

Serial3.write(MQTTProtocolNameLength >> 8);
Serial3.write(MQTTProtocolNameLength & 0xFF);
for (Counter = 0; Counter < MQTTProtocolNameLength; Counter++)
{
Serial3.write(MQTTProtocolName[Counter]);
}

Serial3.write(MQTTLVL); // LVL
Serial3.write(MQTTFlags); // Flags
Serial3.write(MQTTKeepAlive >> 8);
Serial3.write(MQTTKeepAlive & 0xFF);

Serial3.write(MQTTClientIDLength >> 8);
Serial3.write(MQTTClientIDLength & 0xFF);
for (Counter = 0; Counter < MQTTClientIDLength; Counter++)
{
Serial3.write(MQTTClientID[Counter]);
}

Serial3.write(MQTTUsernameLength >> 8);
Serial3.write(MQTTUsernameLength & 0xFF);
{
Serial3.write(MQTTUsername[Counter]);
}

Serial3.write(MQTTPasswordLength >> 8);
Serial3.write(MQTTPasswordLength & 0xFF);
for (Counter = 0; Counter < MQTTPasswordLength; Counter++)
{

Serial3.write(MQTTPassword[Counter]);
}

Serial3.write(0x1A);

}

void SendPublishPacket(void) {

//Serial3. print("\r\nAT+CIPSEND\r\n");
Serial1.println(“void SendPublishPacket”);
sendData(“AT+CIPSEND\r\n”, 4000, DEBUG);
delay(2000);

memset(str,0,250);

topiclength = sprintf((char*)topic,MQTTTopic);
datalength = sprintf((char*)str,"%s%u",topic,Counter);

Serial3.write(0x30);
X=datalength+2;
do
{
encodedByte = X%128;
X = X/128;
if ( X > 0 ) { encodedByte |= 128; }
Serial3.write(encodedByte);
}

while ( X > 0 );

Serial3.write(topiclength >> 8);
Serial3.write(topiclength & 0xFF);
Serial3.print(str);
Serial1.print(str);

Serial3.write(0x1A);
}

void loop(){
Serial1.print(“void loop”);
sendData(“AT+CIPSTART=“TCP”,“AAA.azure-devices(.)net”,“8883”\r\n”, 5000, DEBUG);
delay(5000);
SendConnectPacket();
delay(5000);

while(1)

{ delay(2000);
SendPublishPacket();

//SendSubscribePacket();
Counter++;

}
}

hi,
Please use pasteBin , to paste your code and show it in a formatted manner, or use the tools provided in the form to put the code in a better way so that it can be understood by us.

speaking of which, please re-send the code in a proper format so that we can have further look into it.

Regards
Parth Yatin Temkar

1 Like