工具
1、ec600
2、stm32f030c8
3、keil5
4、腾讯云服务器(ubutu20.04)
mqtt服务器
sudo apt install mosquitto mosquitto-clients
sudo systemctl start mosquitto
sudo vim /etc/mosquitto/mosquitto.conf
sudo systemctl status mosquitto
listener 1883 # 监听端口为 1883,可按需修改
protocol mqtt # 使用 MQTT 协议
firewall-cmd --add-port=1883/tcp --permanent
服务器配置
1、EMQX 开源版 4.4.1 for Ubuntu 18.04
1、查看系统版本号
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.4 LTS
Release: 18.04
2、下载服务器
https://www.emqx.io/downloads?os=Ubuntu
wget https://www.emqx.com/en/downloads/broker/4.1.5/emqx-ubuntu18.04-v4.1.5_amd64.deb
安装EMQX
sudo apt install ./emqx-ubuntu18.04-v4.1.5_amd64.deb
运行 EMQX
sudo systemctl start emqx
3、安装测试mosquitto
1、 引入mosquitto仓库
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
2、 安装mosquitto
sudo apt-get install mosquitto
3、 查看mosquitto状态
sudo service mosquitto status
4、 开启停止服务
sudo service mosquitto start
sudo service mosquitto stop
4、 关闭服务器防火墙
2、EC600配置
1、4g模块启动
ERR_RET EC600E_PowerOn(void)
{
delay_ms(50);
//1. power on
GPIO_WriteBit(USART_4G_GPIO_PORT, USART_4G_PWRKEY, Bit_SET); //pull up powerkey low
//delay >= 500ms
delay_ms(510);
GPIO_WriteBit(USART_4G_GPIO_PORT, USART_4G_PWRKEY, Bit_RESET); //pull down powerkey high
//delay >= 27ms, release reset
delay_ms(30);
GPIO_WriteBit(USART_4G_GPIO_PORT, USART_4G_RESETN, Bit_RESET); //pull down
//
delay_ms(11000);
return ERR_NONE;
}
2、模块初始化
ERR_RET EC600E_Init(void)
{
ERR_RET iRet = ERR_NONE;
char *AckStr = NULL;
int timeout = 0;
delay_ms(200);
USART_4G_SendStr("AT+CGATT?\r\n");
ClearRxBuffer();
delay_ms(50);
USART_4G_SendStr("AT+CPIN?\r\n");
ClearRxBuffer();
delay_ms(50);
USART_4G_SendStr("AT+QIACT=1\r\n");
ClearRxBuffer();
delay_ms(50);
USART_4G_SendStr("AT+QMTCFG=\"qmtping\",0,30\r\n");
ClearRxBuffer();
delay_ms(50);
USART_4G_SendStr("AT+QMTCFG=\"recv/mode\",0,0,1\r\n");
ClearRxBuffer();
delay_ms(50);
_end:
ClearRxBuffer();
return iRet;
}
3、连接服务器
uint8_t EC600_CONNECT_SERVER()
{
uint16_t i=1;
USART_4G_SendStr("AT+QMTOPEN=0,\"ip\",8083\r\n");
delay_ms(1000);
ClearRxBuffer();
}
4、创建连接客户端
```c
uint16_t EC600_mqtt_Connection()
{
uint16_t i=1;
USART_4G_SendStr("AT+QMTCONN=0,\"ec600\",\"test\",\"test\",\r\n");
delay_ms(1000);
ClearRxBuffer();
return 0;
}
5、订阅消息
uint8_t EC600_mqtt_subscribe()
{
USART_4G_SendStr("AT+QMTSUB=0,1,\"test\",0,\"test1\",0\r\n");
ClearRxBuffer();
delay_ms(1000);
}
6、发送消息
uint8_t EC600_Send_data()
{
float Dianya = 255.5;
float Dianliu = 10.0;
USART_4G_SendStr("AT+QMTPUB=0,0,0,0,\"test_pub\"\r\n");
delay_ms(500);
char buffer[50];
sprintf(buffer, "{\"method\":\"report\",\"params\":{\"Dianya\":%.2f,\"Dianliu\":%.2f}}\r\n", Dianya, Dianliu);
USART_4G_SendStr(buffer);
delay_ms(1000);
ClearRxBuffer();
}
3、4G模块发送,服务器接收测试
1、开启话题监听
mosquitto_sub -h localhost -t "test_pub" -v
2、电表发送数据
main函数中EC600_mqtt_Connection();的连接持续10秒
EC600E_PowerOn();
EC600E_Init();
EC600_CONNECT_SERVER();
EC600_mqtt_Connection();
while (1)
{
EC600_Send_data();
}
3、测试结果
服务器接收到数据