Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include <string.h>
- #include <stdio.h>
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- #define UART_Transmit_DELAY 1000
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* UART handler declaration */
- UART_HandleTypeDef UartHandle;
- __IO ITStatus UartReady = RESET;
- char UART_GPRS_Initialized = 0;
- /* Private function prototypes -----------------------------------------------*/
- static void SystemClock_Config(void);
- static void Error_Handler(void);
- static uint16_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
- /* Private functions ---------------------------------------------------------*/
- uint8_t StringLength ( uint8_t *String ) {
- unsigned char Length = 0;
- if (!String) return 0;
- while (String[Length] !='\0') Length++; // Length = string hossza
- return Length;
- }
- void UART_Send(char *aTxBuffer, int delay) {
- uint8_t length = 0;
- length = StringLength((uint8_t *)aTxBuffer);
- if ( length == 0 ) return;
- if(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)aTxBuffer, length)!= HAL_OK) {Error_Handler();}
- while (UartReady != SET) {}
- HAL_Delay(delay);
- UartReady = RESET;
- }
- void UART_GPRS_Init() {
- if(!UART_GPRS_Initialized) {
- // To enable verbode error code
- UART_Send("AT+CMEE=2\r\n", UART_Transmit_DELAY);
- // Close the GPRS PDP context
- UART_Send("AT+CIPSHUT\r\n", UART_Transmit_DELAY);
- //Perform a GPRS Attach.
- UART_Send("AT+CGATT=1\r\n", UART_Transmit_DELAY);
- // AT+CSTT AT command sets up the apn, user name and password for the PDP context.
- UART_Send("AT+CSTT=\"internet\",\"\",\"\"\r\n", UART_Transmit_DELAY);
- // Set the connection type to GPRS
- UART_Send("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"\r\n", UART_Transmit_DELAY);
- // Set the APN to
- UART_Send("AT+SAPBR=3,1,\"APN\",\"internet\"\r\n", UART_Transmit_DELAY);
- // APN User
- UART_Send("AT+SAPBR=3,1,\"USER\",\"\"\r\n", UART_Transmit_DELAY);
- // APN Password
- UART_Send("AT+SAPBR=3,1,\"PWD\",\"\"\r\n", UART_Transmit_DELAY);
- // Enable the GPRS
- UART_Send("AT+SAPBR=1,1\r\n", UART_Transmit_DELAY);
- // AT+CIICR command brings up the GPRS or CSD call depending on the configuration previously set by the AT+CSTT command.
- UART_Send("AT+CIICR\r\n", UART_Transmit_DELAY);
- // Returns the status of the IP connection and the IP address of the SIM808
- UART_Send("AT+SAPBR=2,1\r\n", UART_Transmit_DELAY);
- // Status
- UART_GPRS_Initialized = 1;
- }
- }
- void UART_GPRS_HTTP_INIT() {
- // Close old HTTP Request
- UART_Send("AT+HTTPTERM\r\n", UART_Transmit_DELAY);
- // Start HTTP
- UART_Send("AT+HTTPINIT\r\n", UART_Transmit_DELAY);
- UART_Send("AT+HTTPPARA=\"CID\",1\r\n", UART_Transmit_DELAY);
- }
- void UART_GPRS_HTTP_URL(char url[]) {
- char* at_command = "AT+HTTPPARA=\"URL\",\"%s\"\r\n";
- char at_command_with_url[strlen(at_command)+strlen(url)-1];
- snprintf(at_command_with_url, sizeof(at_command_with_url), at_command, url );
- UART_Send(at_command_with_url, UART_Transmit_DELAY);
- }
- void UART_GPRS_HTTP_GET(char url[]) {
- UART_GPRS_HTTP_INIT();
- // URL
- UART_GPRS_HTTP_URL(url);
- // Set to GET
- UART_Send("AT+HTTPACTION=0\r\n", UART_Transmit_DELAY);
- // Get Respond from server
- UART_Send("AT+HTTPREAD\r\n", UART_Transmit_DELAY);
- }
- void UART_GPRS_HTTP_POST(char url[], char data[]) {
- UART_GPRS_HTTP_INIT();
- // URL
- UART_GPRS_HTTP_URL(url);
- // Content Type
- UART_Send("AT+HTTPPARA=\"CONTENT\",\"application/json\"\r\n", UART_Transmit_DELAY);
- // Data
- char* at_httpdata = "AT+HTTPDATA=%d,10000\r\n";
- int data_length = strlen(data);
- char at_httpdata_with_size[strlen(at_httpdata)+sizeof(data_length)-1];
- snprintf(at_httpdata_with_size, sizeof(at_httpdata_with_size), at_httpdata, data_length);
- UART_Send(at_httpdata_with_size, UART_Transmit_DELAY);
- UART_Send(data, UART_Transmit_DELAY);
- UART_Send("0x1A", UART_Transmit_DELAY);
- // Set to POST
- UART_Send("AT+HTTPACTION=1\r\n", UART_Transmit_DELAY);
- // Get Respond from server
- UART_Send("AT+HTTPREAD\r\n", UART_Transmit_DELAY);
- }
- /**
- * @brief Main program.
- * @param None
- * @retval None
- */
- int main(void)
- {
- /* STM32L0xx HAL library initialization:
- - Configure the Flash prefetch, Flash preread and Buffer caches
- - Systick timer is configured by default as source of time base, but user
- can eventually implement his proper time base source (a general purpose
- timer for example or other time source), keeping in mind that Time base
- duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
- handled in milliseconds basis.
- - Low Level Initialization
- */
- HAL_Init();
- /* Configure LED3 */
- BSP_LED_Init(LED3);
- /* Configure the system clock to 32 Mhz */
- SystemClock_Config();
- /*##-1- Configure the UART peripheral ######################################*/
- /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
- /* UART1 configured as follow:
- - Word Length = 8 Bits
- - Stop Bit = One Stop bit
- - Parity = None
- - BaudRate = 9600 baud
- - Hardware flow control disabled (RTS and CTS signals) */
- // GSM UART
- UartHandle.Instance = USARTx;
- UartHandle.Init.BaudRate = 9600;
- UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
- UartHandle.Init.StopBits = UART_STOPBITS_1;
- UartHandle.Init.Parity = UART_PARITY_NONE;
- UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- UartHandle.Init.Mode = UART_MODE_TX_RX;
- // GPS UART
- /*
- huart2.Instance = USART2;
- huart2.Init.BaudRate = 9600;
- huart2.Init.WordLength = UART_WORDLENGTH_8B;
- huart2.Init.StopBits = UART_STOPBITS_1;
- huart2.Init.Parity = UART_PARITY_NONE;
- huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart2.Init.Mode = UART_MODE_TX_RX;
- */
- if(HAL_UART_Init(&UartHandle) != HAL_OK)
- {
- Error_Handler();
- }
- /* Infinite loop */
- while (1)
- {
- UART_GPRS_Init();
- UART_GPRS_HTTP_POST("http://www./gps/testas.php", "{\"gps\": \"123\"}");
- }
- }
- /**
- * @brief System Clock Configuration
- * The system Clock is configured as follow :
- * System Clock source = PLL (HSI)
- * SYSCLK(Hz) = 32000000
- * HCLK(Hz) = 32000000
- * AHB Prescaler = 1
- * APB1 Prescaler = 1
- * APB2 Prescaler = 1
- * HSI Frequency(Hz) = 16000000
- * PLL_MUL = 4
- * PLL_DIV = 2
- * Flash Latency(WS) = 1
- * Main regulator output voltage = Scale1 mode
- * @param None
- * @retval None
- */
- static void SystemClock_Config(void)
- {
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_OscInitTypeDef RCC_OscInitStruct;
- /* Enable Power Control clock */
- __HAL_RCC_PWR_CLK_ENABLE();
- /* The voltage scaling allows optimizing the power consumption when the device is
- clocked below the maximum system frequency, to update the voltage scaling value
- regarding system frequency refer to product datasheet. */
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /* Enable HSI Oscillator and activate PLL with HSI as source */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
- RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2;
- RCC_OscInitStruct.HSICalibrationValue = 0x10;
- HAL_RCC_OscConfig(&RCC_OscInitStruct);
- /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
- clocks dividers */
- RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
- }
- /**
- * @brief Tx Transfer completed callback
- * @param UartHandle: UART handle.
- * @note This example shows a simple way to report end of IT Tx transfer, and
- * you can add your own implementation.
- * @retval None
- */
- void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
- {
- /* Set transmission flag: trasfer complete*/
- UartReady = SET;
- }
- /**
- * @brief Rx Transfer completed callback
- * @param UartHandle: UART handle
- * @note This example shows a simple way to report end of IT Rx transfer, and
- * you can add your own implementation.
- * @retval None
- */
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
- {
- /* Set transmission flag: trasfer complete*/
- UartReady = SET;
- }
- /**
- * @brief UART error callbacks
- * @param UartHandle: UART handle
- * @note This example shows a simple way to report transfer error, and you can
- * add your own implementation.
- * @retval None
- */
- void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
- {
- while(1)
- {
- }
- }
- /**
- * @brief Compares two buffers.
- * @param pBuffer1, pBuffer2: buffers to be compared.
- * @param BufferLength: buffer's length
- * @retval 0 : pBuffer1 identical to pBuffer2
- * >0 : pBuffer1 differs from pBuffer2
- */
- static uint16_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
- {
- while (BufferLength--)
- {
- if ((*pBuffer1) != *pBuffer2)
- {
- return BufferLength;
- }
- pBuffer1++;
- pBuffer2++;
- }
- return 0;
- }
- /**
- * @brief This function is executed in case of error occurrence.
- * @param None
- * @retval None
- */
- static void Error_Handler(void)
- {
- while(1)
- {
- BSP_LED_On(LED3);
- HAL_Delay(100);
- BSP_LED_Off(LED3);
- }
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement