Guest User

uart gsm

a guest
Feb 10th, 2020
371
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20.  
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "main.h"
  23.  
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26.  
  27. /* USER CODE END Includes */
  28.  
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */
  31.  
  32. /* USER CODE END PTD */
  33.  
  34. /* Private define ------------------------------------------------------------*/
  35. /* USER CODE BEGIN PD */
  36. /* USER CODE END PD */
  37.  
  38. /* Private macro -------------------------------------------------------------*/
  39. /* USER CODE BEGIN PM */
  40.  
  41. /* USER CODE END PM */
  42.  
  43. /* Private variables ---------------------------------------------------------*/
  44. UART_HandleTypeDef huart1;
  45. UART_HandleTypeDef huart2;
  46.  
  47. /* USER CODE BEGIN PV */
  48.  
  49. /* USER CODE END PV */
  50.  
  51. /* Private function prototypes -----------------------------------------------*/
  52. void SystemClock_Config(void);
  53. static void MX_GPIO_Init(void);
  54. static void MX_USART1_UART_Init(void);
  55. static void MX_USART2_UART_Init(void);
  56. /* USER CODE BEGIN PFP */
  57. void sendByGSM(void);
  58.  
  59. /* USER CODE END PFP */
  60.  
  61. /* Private user code ---------------------------------------------------------*/
  62. /* USER CODE BEGIN 0 */
  63.  
  64. /* USER CODE END 0 */
  65.  
  66. /**
  67.   * @brief  The application entry point.
  68.   * @retval int
  69.   */
  70. int main(void)
  71. {
  72.   /* USER CODE BEGIN 1 */
  73.  
  74.   /* USER CODE END 1 */
  75.  
  76.  
  77.   /* MCU Configuration--------------------------------------------------------*/
  78.  
  79.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  80.   HAL_Init();
  81.  
  82.   /* USER CODE BEGIN Init */
  83.  
  84.   /* USER CODE END Init */
  85.  
  86.   /* Configure the system clock */
  87.   SystemClock_Config();
  88.  
  89.   /* USER CODE BEGIN SysInit */
  90.  
  91.   /* USER CODE END SysInit */
  92.  
  93.   /* Initialize all configured peripherals */
  94.   MX_GPIO_Init();
  95.   MX_USART1_UART_Init();
  96.   MX_USART2_UART_Init();
  97.   /* USER CODE BEGIN 2 */
  98.   HAL_Delay(30000);
  99.   HAL_UART_Transmit(&huart2, "Begin\n\r", 40, 200);
  100.   HAL_Delay(100);
  101.   sendByGSM();
  102.   HAL_UART_Transmit(&huart2, "End\n\r", 25, 200);
  103.   HAL_Delay(100);
  104.  
  105.   /* USER CODE END 2 */
  106.  
  107.   /* Infinite loop */
  108.   /* USER CODE BEGIN WHILE */
  109.   while (1)
  110.   {
  111.     /* USER CODE END WHILE */
  112.  
  113.     /* USER CODE BEGIN 3 */
  114.   }
  115.   /* USER CODE END 3 */
  116. }
  117.  
  118. /**
  119.   * @brief System Clock Configuration
  120.   * @retval None
  121.   */
  122. void SystemClock_Config(void)
  123. {
  124.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  125.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  126.  
  127.   /** Initializes the CPU, AHB and APB busses clocks
  128.   */
  129.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  130.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  131.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  132.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  133.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  134.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  135.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  136.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  137.   {
  138.     Error_Handler();
  139.   }
  140.   /** Initializes the CPU, AHB and APB busses clocks
  141.   */
  142.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  143.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  144.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  145.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  146.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  147.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  148.  
  149.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  150.   {
  151.     Error_Handler();
  152.   }
  153. }
  154.  
  155. /**
  156.   * @brief USART1 Initialization Function
  157.   * @param None
  158.   * @retval None
  159.   */
  160. static void MX_USART1_UART_Init(void)
  161. {
  162.  
  163.   /* USER CODE BEGIN USART1_Init 0 */
  164.  
  165.   /* USER CODE END USART1_Init 0 */
  166.  
  167.   /* USER CODE BEGIN USART1_Init 1 */
  168.  
  169.   /* USER CODE END USART1_Init 1 */
  170.   huart1.Instance = USART1;
  171.   huart1.Init.BaudRate = 9600;
  172.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  173.   huart1.Init.StopBits = UART_STOPBITS_1;
  174.   huart1.Init.Parity = UART_PARITY_NONE;
  175.   huart1.Init.Mode = UART_MODE_TX_RX;
  176.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  177.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  178.   if (HAL_UART_Init(&huart1) != HAL_OK)
  179.   {
  180.     Error_Handler();
  181.   }
  182.   /* USER CODE BEGIN USART1_Init 2 */
  183.  
  184.   /* USER CODE END USART1_Init 2 */
  185.  
  186. }
  187.  
  188. /**
  189.   * @brief USART2 Initialization Function
  190.   * @param None
  191.   * @retval None
  192.   */
  193. static void MX_USART2_UART_Init(void)
  194. {
  195.  
  196.   /* USER CODE BEGIN USART2_Init 0 */
  197.  
  198.   /* USER CODE END USART2_Init 0 */
  199.  
  200.   /* USER CODE BEGIN USART2_Init 1 */
  201.  
  202.   /* USER CODE END USART2_Init 1 */
  203.   huart2.Instance = USART2;
  204.   huart2.Init.BaudRate = 9600;
  205.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  206.   huart2.Init.StopBits = UART_STOPBITS_1;
  207.   huart2.Init.Parity = UART_PARITY_NONE;
  208.   huart2.Init.Mode = UART_MODE_TX_RX;
  209.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  210.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  211.   if (HAL_UART_Init(&huart2) != HAL_OK)
  212.   {
  213.     Error_Handler();
  214.   }
  215.   /* USER CODE BEGIN USART2_Init 2 */
  216.  
  217.   /* USER CODE END USART2_Init 2 */
  218.  
  219. }
  220.  
  221. /**
  222.   * @brief GPIO Initialization Function
  223.   * @param None
  224.   * @retval None
  225.   */
  226. static void MX_GPIO_Init(void)
  227. {
  228.  
  229.   /* GPIO Ports Clock Enable */
  230.   __HAL_RCC_GPIOD_CLK_ENABLE();
  231.   __HAL_RCC_GPIOA_CLK_ENABLE();
  232.  
  233. }
  234.  
  235. /* USER CODE BEGIN 4 */
  236.  
  237. void sendByGSM(void){
  238.     //uint8_t buff[150];
  239.     //sprintf((char *)buff, "Gercon %d \r\n",gercon);
  240.     HAL_UART_Transmit(&huart1, "AT+CNMI=2,2,0,0,0\n\r", 25, 200);
  241.     HAL_Delay(300);
  242.     HAL_UART_Transmit(&huart1, "AT+CMGF=1\n\r", 25, 200);
  243.     HAL_Delay(300);
  244.     HAL_UART_Transmit(&huart1, "AT+CLIP=1\n\r", 25, 200);
  245.     HAL_Delay(300);
  246.     HAL_UART_Transmit(&huart1, "AT+CSTT= \"internet\", \"gdata\", \"gdata\"\n\r", 60, 200);
  247.     HAL_Delay(800);
  248.     HAL_UART_Transmit(&huart1, "AT+CIICR\n\r", 25, 200);
  249.     HAL_Delay(1000);
  250.     HAL_UART_Transmit(&huart1, "AT+CIPSTART= \"TCP\" , \"83.221.211.248\", 80\n\r", 60, 200);
  251.     HAL_Delay(500);
  252.     HAL_UART_Transmit(&huart1, "AT+CIPSEND\n\r", 30, 200);
  253.     HAL_Delay(300);
  254.     HAL_UART_Transmit(&huart1, "GET /testMbee.php?value=32 HTTP/1.1\n\r", 60, 200);
  255.     HAL_Delay(300);
  256.     HAL_UART_Transmit(&huart1, "Host: 83.221.211.248\n\r\n", 60, 200);
  257.     HAL_Delay(300);
  258.     HAL_UART_Transmit(&huart1, (char)26, 2, 200);
  259.     HAL_Delay(300);
  260.     HAL_UART_Transmit(&huart1, "\n\r", 5, 200);
  261.     HAL_Delay(300);
  262. }
  263.  
  264. /* USER CODE END 4 */
  265.  
  266. /**
  267.   * @brief  This function is executed in case of error occurrence.
  268.   * @retval None
  269.   */
  270. void Error_Handler(void)
  271. {
  272.   /* USER CODE BEGIN Error_Handler_Debug */
  273.   /* User can add his own implementation to report the HAL error return state */
  274.  
  275.   /* USER CODE END Error_Handler_Debug */
  276. }
  277.  
  278. #ifdef  USE_FULL_ASSERT
  279. /**
  280.   * @brief  Reports the name of the source file and the source line number
  281.   *         where the assert_param error has occurred.
  282.   * @param  file: pointer to the source file name
  283.   * @param  line: assert_param error line source number
  284.   * @retval None
  285.   */
  286. void assert_failed(uint8_t *file, uint32_t line)
  287. {
  288.   /* USER CODE BEGIN 6 */
  289.   /* User can add his own implementation to report the file name and line number,
  290.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  291.   /* USER CODE END 6 */
  292. }
  293. #endif /* USE_FULL_ASSERT */
  294.  
  295. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
RAW Paste Data