85 lines
2.3 KiB
C
85 lines
2.3 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
|
|||
|
|
|||
|
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><>β
|
|||
|
for (volatile int i = 0; i < delay_ms * 1000; i++); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
|
|||
|
}
|
|||
|
|
|||
|
void drv_eg25g_init(void)
|
|||
|
{
|
|||
|
drv_send_at_cmd("AT", 500);
|
|||
|
drv_send_at_cmd("AT+CPIN?", 500);
|
|||
|
drv_send_at_cmd("AT+CREG?", 500);
|
|||
|
drv_send_at_cmd("AT+CGATT=1", 500);
|
|||
|
drv_send_at_cmd("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"", 500);
|
|||
|
drv_send_at_cmd("AT+SAPBR=3,1,\"APN\",\"your_apn\"", 500);
|
|||
|
drv_send_at_cmd("AT+SAPBR=1,1", 2000);
|
|||
|
}
|
|||
|
|
|||
|
void drv_mqtt_connect(void)
|
|||
|
{
|
|||
|
drv_send_at_cmd("AT+QMTCFG=\"recv/mode\",0,0,1", 500);
|
|||
|
drv_send_at_cmd("AT+QMTOPEN=0,\"mqtt.example.com\",1883", 5000);
|
|||
|
drv_send_at_cmd("AT+QMTCONN=0,\"client_id\",\"username\",\"password\"", 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>
|
|||
|
}
|
|||
|
}
|
|||
|
*/
|
|||
|
|