Guest User

Untitled

a guest
Feb 11th, 2020
101
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. void UART_ErrorHandler(int i);
  59.  
  60. /* USER CODE END PFP */
  61.  
  62. /* Private user code ---------------------------------------------------------*/
  63. /* USER CODE BEGIN 0 */
  64.  
  65. /* USER CODE END 0 */
  66.  
  67. /**
  68.   * @brief  The application entry point.
  69.   * @retval int
  70.   */
  71. int main(void)
  72. {
  73.   /* USER CODE BEGIN 1 */
  74.  
  75.   /* USER CODE END 1 */
  76.  
  77.  
  78.   /* MCU Configuration--------------------------------------------------------*/
  79.  
  80.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  81.   HAL_Init();
  82.  
  83.   /* USER CODE BEGIN Init */
  84.  
  85.   /* USER CODE END Init */
  86.  
  87.   /* Configure the system clock */
  88.   SystemClock_Config();
  89.  
  90.   /* USER CODE BEGIN SysInit */
  91.  
  92.   /* USER CODE END SysInit */
  93.  
  94.   /* Initialize all configured peripherals */
  95.   MX_GPIO_Init();
  96.   MX_USART1_UART_Init();
  97.   MX_USART2_UART_Init();
  98.   /* USER CODE BEGIN 2 */
  99.   HAL_Delay(30000);
  100.   if(HAL_UART_Transmit(&huart2, "Begin\n\r", 40, 1000) != HAL_OK) UART_ErrorHandler(2);
  101.   HAL_Delay(100);
  102.   sendByGSM();
  103.   if(HAL_UART_Transmit(&huart2, "End\n\r", 40, 1000) != HAL_OK) UART_ErrorHandler(2);
  104.   HAL_Delay(100);
  105.  
  106.   /* USER CODE END 2 */
  107.  
  108.   /* Infinite loop */
  109.   /* USER CODE BEGIN WHILE */
  110.   while (1)
  111.   {
  112.     /* USER CODE END WHILE */
  113.  
  114.     /* USER CODE BEGIN 3 */
  115.   }
  116.   /* USER CODE END 3 */
  117. }
  118.  
  119. /**
  120.   * @brief System Clock Configuration
  121.   * @retval None
  122.   */
  123. void SystemClock_Config(void)
  124. {
  125.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  126.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  127.  
  128.   /** Initializes the CPU, AHB and APB busses clocks
  129.   */
  130.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  131.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  132.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  133.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  134.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  135.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  136.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  137.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  138.   {
  139.     Error_Handler();
  140.   }
  141.   /** Initializes the CPU, AHB and APB busses clocks
  142.   */
  143.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  144.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  145.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  146.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  147.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  148.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  149.  
  150.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  151.   {
  152.     Error_Handler();
  153.   }
  154. }
  155.  
  156. /**
  157.   * @brief USART1 Initialization Function
  158.   * @param None
  159.   * @retval None
  160.   */
  161. static void MX_USART1_UART_Init(void)
  162. {
  163.  
  164.   /* USER CODE BEGIN USART1_Init 0 */
  165.  
  166.   /* USER CODE END USART1_Init 0 */
  167.  
  168.   /* USER CODE BEGIN USART1_Init 1 */
  169.  
  170.   /* USER CODE END USART1_Init 1 */
  171.   huart1.Instance = USART1;
  172.   huart1.Init.BaudRate = 9600;
  173.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  174.   huart1.Init.StopBits = UART_STOPBITS_1;
  175.   huart1.Init.Parity = UART_PARITY_NONE;
  176.   huart1.Init.Mode = UART_MODE_TX_RX;
  177.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  178.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  179.   if (HAL_UART_Init(&huart1) != HAL_OK)
  180.   {
  181.     Error_Handler();
  182.   }
  183.   /* USER CODE BEGIN USART1_Init 2 */
  184.  
  185.   /* USER CODE END USART1_Init 2 */
  186.  
  187. }
  188.  
  189. /**
  190.   * @brief USART2 Initialization Function
  191.   * @param None
  192.   * @retval None
  193.   */
  194. static void MX_USART2_UART_Init(void)
  195. {
  196.  
  197.   /* USER CODE BEGIN USART2_Init 0 */
  198.  
  199.   /* USER CODE END USART2_Init 0 */
  200.  
  201.   /* USER CODE BEGIN USART2_Init 1 */
  202.  
  203.   /* USER CODE END USART2_Init 1 */
  204.   huart2.Instance = USART2;
  205.   huart2.Init.BaudRate = 9600;
  206.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  207.   huart2.Init.StopBits = UART_STOPBITS_1;
  208.   huart2.Init.Parity = UART_PARITY_NONE;
  209.   huart2.Init.Mode = UART_MODE_TX_RX;
  210.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  211.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  212.   if (HAL_UART_Init(&huart2) != HAL_OK)
  213.   {
  214.     Error_Handler();
  215.   }
  216.   /* USER CODE BEGIN USART2_Init 2 */
  217.  
  218.   /* USER CODE END USART2_Init 2 */
  219.  
  220. }
  221.  
  222. /**
  223.   * @brief GPIO Initialization Function
  224.   * @param None
  225.   * @retval None
  226.   */
  227. static void MX_GPIO_Init(void)
  228. {
  229.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  230.  
  231.   /* GPIO Ports Clock Enable */
  232.   __HAL_RCC_GPIOC_CLK_ENABLE();
  233.   __HAL_RCC_GPIOD_CLK_ENABLE();
  234.   __HAL_RCC_GPIOA_CLK_ENABLE();
  235.  
  236.   /*Configure GPIO pin Output Level */
  237.   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
  238.  
  239.   /*Configure GPIO pin : PC13 */
  240.   GPIO_InitStruct.Pin = GPIO_PIN_13;
  241.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  242.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  243.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  244.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  245.  
  246. }
  247.  
  248. /* USER CODE BEGIN 4 */
  249.  
  250. void sendByGSM(void){
  251.     //uint8_t buff[150];
  252.     //sprintf((char *)buff, "Gercon %d \r\n",gercon);
  253.     //HAL_UART_Transmit(&huart1, "AT+CNMI=2,2,0,0,0\n\r", 25, 200);
  254.     if(HAL_UART_Transmit(&huart1, "AT+CNMI=2,2,0,0,0\n\r", 40, 1000) != HAL_OK) UART_ErrorHandler(1);
  255.     HAL_Delay(300);
  256.     if(HAL_UART_Transmit(&huart1, "AT+CMGF=1\n\r", 25, 1000)!= HAL_OK) UART_ErrorHandler(1);
  257.     HAL_Delay(300);
  258.     if(HAL_UART_Transmit(&huart1, "AT+CLIP=1\n\r", 25, 1000) != HAL_OK) UART_ErrorHandler(1);
  259.     HAL_Delay(300);
  260.     if(HAL_UART_Transmit(&huart1, "AT+CSTT= \"internet\", \"gdata\", \"gdata\"\n\r", 60, 1000)!=HAL_OK) UART_ErrorHandler(1);
  261.     HAL_Delay(800);
  262.     HAL_UART_Transmit(&huart1, "AT+CIICR\n\r", 25, 200);
  263.     HAL_Delay(1000);
  264.     HAL_UART_Transmit(&huart1, "AT+CIPSTART= \"TCP\" , \"83.221.211.248\", 80\n\r", 60, 200);
  265.     HAL_Delay(500);
  266.     HAL_UART_Transmit(&huart1, "AT+CIPSEND\n\r", 30, 200);
  267.     HAL_Delay(300);
  268.     HAL_UART_Transmit(&huart1, "GET /testMbee.php?value=32 HTTP/1.1\n\r", 60, 200);
  269.     HAL_Delay(300);
  270.     HAL_UART_Transmit(&huart1, "Host: 83.221.211.248\n\r\n\r", 60, 200);
  271.     HAL_Delay(300);
  272.     HAL_UART_Transmit(&huart1, (char)26, 2, 200);
  273.     HAL_Delay(300);
  274.     HAL_UART_Transmit(&huart1, "\n\r", 5, 200);
  275.     HAL_Delay(300);
  276. }
  277.  
  278. void UART_ErrorHandler(int i){
  279.     if(i==1){
  280.         while(1){
  281.             HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
  282.             HAL_Delay(500);
  283.             HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
  284.         }
  285.     }else{
  286.         while(1){
  287.             HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
  288.             HAL_Delay(5000);
  289.             HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
  290.         }
  291.     }
  292.  
  293. }
  294.  
  295. /* USER CODE END 4 */
  296.  
  297. /**
  298.   * @brief  This function is executed in case of error occurrence.
  299.   * @retval None
  300.   */
  301. void Error_Handler(void)
  302. {
  303.   /* USER CODE BEGIN Error_Handler_Debug */
  304.   /* User can add his own implementation to report the HAL error return state */
  305.  
  306.   /* USER CODE END Error_Handler_Debug */
  307. }
  308.  
  309. #ifdef  USE_FULL_ASSERT
  310. /**
  311.   * @brief  Reports the name of the source file and the source line number
  312.   *         where the assert_param error has occurred.
  313.   * @param  file: pointer to the source file name
  314.   * @param  line: assert_param error line source number
  315.   * @retval None
  316.   */
  317. void assert_failed(uint8_t *file, uint32_t line)
  318. {
  319.   /* USER CODE BEGIN 6 */
  320.   /* User can add his own implementation to report the file name and line number,
  321.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  322.   /* USER CODE END 6 */
  323. }
  324. #endif /* USE_FULL_ASSERT */
  325.  
  326. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
RAW Paste Data