Sample Code for MQTT in ec25

Hi,

I don’t have mqtt library in EC25. I need to develop a application in openCPU sdk. Please share me if there is any mqtt demo code.

Thanks

Hi Vysakh,

I’m sorry that we don’t have demo code related to MQTT in OpenLinux SDK.
If you you want to find some examples for reference, you could take a look on website below.

Best Regards!
Zach

1 Like

Hi Team,
1)In our project we have connected the AMT1001 sensor to the EC-25E module. we need to publish IMEI Number, Temperature, Humidity, Latitude, Longitude, and Timestamp values to MQTT Broker.
2) We are publishing those values to the MQTT broker properly but after some time we are not publishing those values on the MQTT broker. Please say me how to clean up/destroy the MQTT broker messages through EC-25 Module.
Please help me as soon as possible.
Thank You.

Hi there,

I’m sorry that I don’t quite understand what you mean, I list those that I’m not sure below:

Does this mean that you can’t publish messages to broker?

Does this mean that you want to delete history messages related to one or more specific topics?

And could you please tell me those information below.

  1. Which MQTT server you’re using?
  2. The firmware version of EC25-E you’re using.
  3. How does you achieve MQTT function? Using AT command process or quectel function interfaces?

Thanks & Best Regards!
Zach

Does this mean that you want to delete history messages related to one or more specific topics?
Yes, I want to delete history messages related to specific topic.

  1. Which MQTT server you’re using?

Ans: broker.hivemq.com

  1. The firmware version of EC25-E you’re using.

Ans: EC25EFAR06A01M4G_OCPU_01.001.01.001_SDK

  1. How does you achieve MQTT function? Using AT command process or quectel function interfaces?

Ans: i used both AT commands and Quectel function same as a below

#include “ql_oe.h”
#include “ql_adc.h”
#include “ql_gpio.h”
#define QUEC_AT_PORT “/dev/smd9”

static int smd_fd = -1;

void mqtt_init(void);
void mqtt_clean(void);

char mqtt_buff[200];
char buff[100];
char temp_date[10];
char read_date[10]=“DD/MM/YY”;
char read_time[10]=“HH/MM/SS”;
char latitude[10]=“0000.0000”;
char longitude[10]=“0000.0000”;
char IMEI[16];
int i,j;
int adc_temp=0;
int adc_vol=0;
int Temperature=0;
int Humidity=0;
int temp=0;

int main()
{
smd_fd = open(QUEC_AT_PORT, O_RDWR | O_NONBLOCK | O_NOCTTY);
mqtt_init();
Ql_SendAT(“AT+CGSN”,“OK”,1000);
printf(“value of CGSN is: %s\r\n”,buff);
imei=strtok(buff,"\n");
imei=strtok(NULL,"\n");
memcpy(IMEI,imei,15);
printf(“IMEI num is: %s\r\n”,IMEI);
sleep(1);

while(1)
{											sprintf(mqtt_buff,"{\"IMEI\":\"%s\",\"TEMPERATURE\":\"%d°C\",\"HUMIDITY\":\"%d%\",\"LATITUDE\":\"%s\",\"LONGITUDE\":\"%s\",\"DATE\":\"%s\",\"TIME\":\"%s\"}",IMEI,Temperature,Humidity,latitude,longitude,read_date,read_time);
	if(strlen(mqtt_buff)==146)
	{
		Ql_SendAT("AT+QMTPUBEX=0,0,0,0,\"EC_ARGUS_TEST\",146", "NO", 1000);
		Ql_SendAT((char *)&mqtt_buff, "OK", 1000);
	}
	sleep(2);
}

}

void mqtt_init(void)
{
Ql_SendAT(“AT+COPS=0”, “OK”, 1000);
sleep(2);
Ql_SendAT(“AT+CFUN=1”, “OK”, 1000);
sleep(2);
Ql_SendAT(“AT+QMTCFG=“version”,0,4”, “OK”, 1000);
sleep(2);
Ql_SendAT(“AT+QMTCFG=“recv/mode”,0,0,1”, “OK”, 1000);
sleep(2);
Ql_SendAT(“AT+QMTOPEN=0,“broker.hivemq.com”,1883”, “OK”, 1000);
sleep(5);
Ql_SendAT("AT+QMTCONN=0,“1947"”, “OK”, 1000);
sleep(5);
}

Hi Upendhar,

For those queued information and messages from last persistent session, you could set clean session flag to true to achieve messages deletation.
For retained message, you could send a retained message with a zero-byte payload on the topic where you want to delete the previous retained message. The broker deletes the retained message and new subscribers no longer get a retained message for that topic.

Thanks & Best Regards!
Zach

Hi Zach,

Thanks for your help, with the help of your information, I solved my issue. Now EC-25 Module publishing all my parameters continuously to MQTT broker.

Regards,
Upendhar.

Hi Upendhar,

You’re welcome. Feel free to ask questions when you’re troubleshooting and all we quectelers would be happy to help you. Wish you all the best!

BR
Zach

Hi Zach,

When I am placing the module in constant place all parameters are updating properly to the MQTT broker but when I move the module for 100 meters long, parameters are not updating on the MQTT server.
Ql_SendAT(“AT+QMTPUBEX=0,1,1,0,“EC_ARGUS_TEST”,146”, “NO”, 1000);
res = Ql_SendAT((char *)&mqtt_buff, “OK”, 1000);
when I am getting res = 0, Packets are updating on MQTT broker.
when I am getting res = 1, Packets are not updating on MQTT broker.
How I can solve this issue?

Thanks & Regards,
Upendhar.

Hi Upendhar,

There’re maybe multiple reasons which will cause this problem such as network unstable in that area.
Besides, the return value 1 of API Ql_SendAT() means timeout.

Br
Zach

Hi Zach,

I tried to connect mqtt.datacake.co with Ec25 Module. But it is not connecting. Please find below log and suggest me how to connect mqtt.datacake.co with EC25 module.
AT+COPS=0
OK
AT+CFUN=1
OK
AT+QMTCFG=“recv/mode”,0,0,1
OK
AT+QMTOPEN=0,“mqtt.datacake.co”,1883
OK

+QMTOPEN: 0,3
AT+QMTOPEN=0,“mqtt.datacake.co”,8883
OK

+QMTOPEN: 0,3

Thanks & Regards,
Upendhar.