182 lines
5.5 KiB
C
182 lines
5.5 KiB
C
|
/******************************************************************************
|
|||
|
* @file drv_eg25gminipice.c
|
|||
|
* @brief drv_eg25gminipice.c drivers
|
|||
|
* @version V1.0
|
|||
|
* @author Gary
|
|||
|
* @copyright
|
|||
|
******************************************************************************/
|
|||
|
|
|||
|
#include "drv_eg25gminipice.h"
|
|||
|
|
|||
|
#define UART2_BAUDRATE 115200
|
|||
|
#define MAX_RETRY 3
|
|||
|
|
|||
|
//4Gʵ<47><CAB5><EFBFBD><EFBFBD>
|
|||
|
static uint8_t sim_rec_buf[MAX_RECV_LEN] = {0};
|
|||
|
LTE4G_STATIC_INIT(sim_item, 1, 0, 0, 1024,sim_rec_buf);
|
|||
|
|
|||
|
|
|||
|
void drv_sim_push_data(Lte4GItem* item, uint8_t *buf, uint16_t len)
|
|||
|
{
|
|||
|
uint16_t i = 0;
|
|||
|
|
|||
|
if((item != NULL) && (item->buf_pos + len < item->buf_size))
|
|||
|
{
|
|||
|
for (i = 0; i < len; i++)
|
|||
|
{
|
|||
|
item->buf[item->buf_pos++] = buf[i];
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void drv_uart2_Init(void)
|
|||
|
{
|
|||
|
RCC->APB1ENR |= RCC_APB1ENR_USART2EN; // ʹ<><CAB9> USART2 ʱ<><CAB1>
|
|||
|
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; // ʹ<><CAB9> GPIOA ʱ<><CAB1>
|
|||
|
|
|||
|
GPIOA->MODER |= (2 << (2 * 2)) | (2 << (3 * 2)); // PA2(TX), PA3(RX) <20><><EFBFBD><EFBFBD>ģʽ
|
|||
|
GPIOA->AFR[0] |= (7 << (2 * 4)) | (7 << (3 * 4)); // <20><><EFBFBD><EFBFBD> AF7 (USART2)
|
|||
|
|
|||
|
USART2->BRR = SystemCoreClock / UART2_BAUDRATE;
|
|||
|
USART2->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE; // ʹ<>ܴ<EFBFBD><DCB4>ڡ<EFBFBD><DAA1><EFBFBD><EFBFBD>ͺͽ<CDBA><CDBD><EFBFBD>
|
|||
|
}
|
|||
|
|
|||
|
void drv_uart2_sendbyte(char c)
|
|||
|
{
|
|||
|
while (!(USART2->SR & USART_SR_TXE)); // <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
USART2->DR = c;
|
|||
|
}
|
|||
|
|
|||
|
void drv_uart2_sendString(const char *str)
|
|||
|
{
|
|||
|
while (*str) {
|
|||
|
drv_uart2_sendbyte(*str++);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void drv_send_at_cmd(const char *cmd, int delay_ms)
|
|||
|
{
|
|||
|
drv_uart2_sendString(cmd);
|
|||
|
drv_uart2_sendString("\r\n"); // AT ָ<><D6B8><EFBFBD><EFBFBD> "\r\n" <20><>β
|
|||
|
kit_time_dly_ms(30);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
void drv_eg25g_init(void)
|
|||
|
{
|
|||
|
drv_send_at_cmd("AT", 500); // 1. <20><><EFBFBD><EFBFBD> AT ָ<><D6B8>
|
|||
|
drv_send_at_cmd("AT+CPIN?", 500); // 2. <20><>ѯ SIM <20><>״̬
|
|||
|
drv_send_at_cmd("AT+COPS=0", 500); // 3. <20><>ģ<EFBFBD><C4A3><EFBFBD>Զ<EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>Ӫ<EFBFBD><D3AA>
|
|||
|
drv_send_at_cmd("AT+CEREG?", 1000); // 4. <20><>ѯ 4G ע<><D7A2>״̬
|
|||
|
// drv_send_at_cmd("AT+CGDCONT=1,\"IP\",\"your_apn\"", 500); // 5. <20><><EFBFBD><EFBFBD> APN <20><><EFBFBD><EFBFBD>֧<EFBFBD><D6A7><EFBFBD>Զ<EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>apn
|
|||
|
drv_send_at_cmd("AT+CGACT=1,1", 1000); // 6. <20><><EFBFBD><EFBFBD> PDP<44><50><EFBFBD><EFBFBD>ȡ IP<49><50>
|
|||
|
drv_send_at_cmd("AT+CGPADDR=1", 500); // 7. <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IP <20><>ַ
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD> AT ָ<><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>ֵ,<2C><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4>Ż<EFBFBD><C5BB><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
int drv_send_at_cmd2(const char *cmd, int delay_ms)
|
|||
|
{
|
|||
|
for (int i = 0; i < MAX_RETRY; i++) // ʧ<><CAA7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> MAX_RETRY <20><>
|
|||
|
{
|
|||
|
drv_uart2_sendString(cmd);
|
|||
|
drv_uart2_sendString("\r\n"); // AT ָ<><D6B8><EFBFBD><EFBFBD> "\r\n" <20><>β
|
|||
|
kit_time_dly_ms(30);
|
|||
|
|
|||
|
if (sim_item.buf_pos > 0 && strstr((const char*)sim_item.buf, "OK")) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|||
|
{
|
|||
|
return 1; // <20>ɹ<EFBFBD>
|
|||
|
}
|
|||
|
}
|
|||
|
return 0; // ʧ<><CAA7>
|
|||
|
}
|
|||
|
|
|||
|
void drv_eg25g_init2(void)
|
|||
|
{
|
|||
|
if (!drv_send_at_cmd2("AT", 500)) return; // 1. <20><><EFBFBD><EFBFBD> AT ָ<><D6B8>
|
|||
|
if (!drv_send_at_cmd2("AT+CPIN?", 500)) return; // 2. <20><>ѯ SIM <20><>״̬
|
|||
|
if (!drv_send_at_cmd2("AT+COPS=0", 500)) return; // 3. <20><>ģ<EFBFBD><C4A3><EFBFBD>Զ<EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>Ӫ<EFBFBD><D3AA>
|
|||
|
if (!drv_send_at_cmd2("AT+CEREG?", 1000)) return; // 4. <20><>ѯ 4G ע<><D7A2>״̬
|
|||
|
if (!drv_send_at_cmd2("AT+CGDCONT=1,\"IP\",\"your_apn\"", 500)) return; // 5. <20><><EFBFBD><EFBFBD> APN
|
|||
|
if (!drv_send_at_cmd2("AT+CGACT=1,1", 1000)) return; // 6. <20><><EFBFBD><EFBFBD> PDP<44><50><EFBFBD><EFBFBD>ȡ IP<49><50>
|
|||
|
if (!drv_send_at_cmd2("AT+CGPADDR=1", 500)) return; // 7. <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IP <20><>ַ
|
|||
|
}
|
|||
|
|
|||
|
#define MQTT_BROKER "47.120.14.45" // <20><><EFBFBD><EFBFBD> MQTT <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|||
|
#define MQTT_PORT 3011 // MQTT <20>˿<EFBFBD>
|
|||
|
#define MQTT_CLIENT_ID "fc41d_client" // <20>ͻ<EFBFBD><CDBB><EFBFBD> ID
|
|||
|
#define MQTT_USER "user" // MQTT <20>û<EFBFBD><C3BB><EFBFBD>
|
|||
|
#define MQTT_PASSWORD "password" // MQTT <20><><EFBFBD><EFBFBD>
|
|||
|
#define MQTT_TOPIC "BCU01" // <20><><EFBFBD><EFBFBD>/<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
#define MQTT_MESSAGE "Hello from FC41D!" // <20><><EFBFBD>͵<EFBFBD><CDB5><EFBFBD>Ϣ
|
|||
|
#define WIFI_TIMEOUT 1000
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD> MQTT <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
uint8_t drv_mqtt_connect()
|
|||
|
{
|
|||
|
|
|||
|
//1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD> MQTT Э<><D0AD><EFBFBD>İ汾<C4B0><E6B1BE>Ϊ V4
|
|||
|
// snprintf(cmd, sizeof(cmd), "AT+QMTCFG=\"version\",1,4");
|
|||
|
// if (!drv_at_send_cmd(cmd, "OK", WIFI_TIMEOUT))
|
|||
|
// {
|
|||
|
// KIT_PRINTF("Failed to set MQTT server!\r\n");
|
|||
|
// return 1;
|
|||
|
// }
|
|||
|
|
|||
|
//2<><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD> MQTT <20>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD>ĻỰ<C4BB><E1BBB0><EFBFBD><EFBFBD>
|
|||
|
char cmd[100] = {0};
|
|||
|
|
|||
|
snprintf(cmd, sizeof(cmd), "AT+QMTOPEN=0,\"%s\",%d",MQTT_BROKER,MQTT_PORT);
|
|||
|
if (!drv_at_send_cmd(cmd, "OK", WIFI_TIMEOUT))
|
|||
|
{
|
|||
|
// KIT_PRINTF("Failed to set MQTT server!\r\n");
|
|||
|
return 1;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
//2<><32>MQTT <20>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD>佨<EFBFBD><E4BDA8> MQTT <20><><EFBFBD><EFBFBD> AT+QMTCONN=<clientID>,<client_identity>,<username>,<password>
|
|||
|
//snprintf(cmd, sizeof(cmd), "AT+QMTCONN=0,%s,%s,%s",MQTT_CLIENT_ID,MQTT_USER, MQTT_PASSWORD);
|
|||
|
if (!drv_at_send_cmd("AT+QMTCONN=0,\"clientBCUwifi112233\"", "OK", WIFI_TIMEOUT))
|
|||
|
{
|
|||
|
// KIT_PRINTF("Failed to set MQTT client credentials!\r\n");
|
|||
|
return 1;
|
|||
|
}
|
|||
|
|
|||
|
// KIT_PRINTF("MQTT Connection Successful!\r\n");
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
//void drv_mqtt_connect(void)
|
|||
|
//{
|
|||
|
//// drv_send_at_cmd("AT+QMTCFG=\"recv/mode\",0,0,1", 500);
|
|||
|
// drv_send_at_cmd("AT+QMTOPEN=0,\"47.120.14.45\",3011", 5000);
|
|||
|
// drv_send_at_cmd("AT+QMTCONN=0,\"clientBCUwifi01\"", 5000);
|
|||
|
// drv_send_at_cmd("AT+QMTSUB=0,1,\"BCU01\",2", 5000);
|
|||
|
//}
|
|||
|
/*
|
|||
|
void drv_mqtt_publish(const char *topic, const char *message)
|
|||
|
{
|
|||
|
char cmd[128];
|
|||
|
sprintf(cmd, "AT+QMTPUB=0,0,0,0,\"%s\",\"%s\"", topic, message);
|
|||
|
drv_send_at_cmd(cmd, 500);
|
|||
|
}
|
|||
|
*/
|
|||
|
|
|||
|
/*
|
|||
|
|
|||
|
int main(void) {
|
|||
|
SystemInit();
|
|||
|
drv_uart2_Init();
|
|||
|
drv_eg25g_init();
|
|||
|
drv_mqtt_connect();
|
|||
|
|
|||
|
while (1)
|
|||
|
{
|
|||
|
drv_mqtt_publish("sensor/data", "{\"temperature\":25.3}");
|
|||
|
for (volatile int i = 0; i < 10000000; i++); // <20><><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD>
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
*/
|
|||
|
|