SHARE
TWEET

Untitled

a guest Nov 9th, 2018 144 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include <string.h>
  4. #include <stdio.h>
  5.  
  6. /* Private typedef -----------------------------------------------------------*/
  7.  
  8. /* Private define ------------------------------------------------------------*/
  9. #define UART_Transmit_DELAY 1000
  10.  
  11. /* Private macro -------------------------------------------------------------*/
  12.  
  13. /* Private variables ---------------------------------------------------------*/
  14.  
  15. /* UART handler declaration */
  16. UART_HandleTypeDef UartHandle;
  17. __IO ITStatus UartReady = RESET;
  18. char UART_GPRS_Initialized = 0;
  19.  
  20. /* Private function prototypes -----------------------------------------------*/
  21. static void SystemClock_Config(void);
  22. static void Error_Handler(void);
  23. static uint16_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
  24.  
  25. /* Private functions ---------------------------------------------------------*/
  26.  
  27. uint8_t StringLength ( uint8_t *String ) {
  28.     unsigned char Length = 0;
  29.     if (!String) return 0;
  30.     while (String[Length] !='\0') Length++; // Length = string hossza
  31.     return Length;
  32. }
  33.  
  34. void UART_Send(char *aTxBuffer, int delay) {
  35.    
  36.     uint8_t length = 0;
  37.     length = StringLength((uint8_t *)aTxBuffer);
  38.     if ( length == 0 ) return;
  39.    
  40.     if(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)aTxBuffer, length)!= HAL_OK) {Error_Handler();}
  41.   while (UartReady != SET) {}
  42.     HAL_Delay(delay);  
  43.   UartReady = RESET;
  44. }
  45.  
  46. void UART_GPRS_Init() {
  47.    
  48.     if(!UART_GPRS_Initialized) {
  49.         // To enable verbode error code
  50.         UART_Send("AT+CMEE=2\r\n", UART_Transmit_DELAY);
  51.         // Close the GPRS PDP context
  52.         UART_Send("AT+CIPSHUT\r\n", UART_Transmit_DELAY);
  53.         //Perform a GPRS Attach.
  54.         UART_Send("AT+CGATT=1\r\n", UART_Transmit_DELAY);
  55.         // AT+CSTT AT command sets up the apn, user name and password for the PDP context.
  56.         UART_Send("AT+CSTT=\"internet\",\"\",\"\"\r\n", UART_Transmit_DELAY);
  57.         // Set the connection type to GPRS
  58.         UART_Send("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"\r\n", UART_Transmit_DELAY);
  59.         // Set the APN to
  60.         UART_Send("AT+SAPBR=3,1,\"APN\",\"internet\"\r\n", UART_Transmit_DELAY);
  61.         // APN User
  62.         UART_Send("AT+SAPBR=3,1,\"USER\",\"\"\r\n", UART_Transmit_DELAY);
  63.         // APN Password
  64.         UART_Send("AT+SAPBR=3,1,\"PWD\",\"\"\r\n", UART_Transmit_DELAY);
  65.         // Enable the GPRS
  66.         UART_Send("AT+SAPBR=1,1\r\n", UART_Transmit_DELAY);
  67.         // AT+CIICR command brings up the GPRS or CSD call depending on the configuration previously set by the AT+CSTT command.
  68.         UART_Send("AT+CIICR\r\n", UART_Transmit_DELAY);
  69.         // Returns the status of the IP connection and the IP address of the SIM808
  70.         UART_Send("AT+SAPBR=2,1\r\n", UART_Transmit_DELAY);
  71.        
  72.         // Status
  73.         UART_GPRS_Initialized = 1;
  74.     }
  75. }
  76.  
  77. void UART_GPRS_HTTP_INIT() {
  78.     // Close old HTTP Request
  79.     UART_Send("AT+HTTPTERM\r\n", UART_Transmit_DELAY);
  80.    
  81.     // Start HTTP
  82.     UART_Send("AT+HTTPINIT\r\n", UART_Transmit_DELAY);
  83.     UART_Send("AT+HTTPPARA=\"CID\",1\r\n", UART_Transmit_DELAY);
  84. }
  85.  
  86. void UART_GPRS_HTTP_URL(char url[]) {
  87.     char* at_command = "AT+HTTPPARA=\"URL\",\"%s\"\r\n";
  88.     char at_command_with_url[strlen(at_command)+strlen(url)-1];
  89.     snprintf(at_command_with_url, sizeof(at_command_with_url), at_command, url );
  90.     UART_Send(at_command_with_url, UART_Transmit_DELAY);
  91. }
  92.  
  93. void UART_GPRS_HTTP_GET(char url[]) {
  94.     UART_GPRS_HTTP_INIT();
  95.     // URL
  96.     UART_GPRS_HTTP_URL(url);
  97.     // Set to GET
  98.     UART_Send("AT+HTTPACTION=0\r\n", UART_Transmit_DELAY);
  99.     // Get Respond from server
  100.     UART_Send("AT+HTTPREAD\r\n", UART_Transmit_DELAY);
  101. }
  102.  
  103. void UART_GPRS_HTTP_POST(char url[], char data[]) {
  104.     UART_GPRS_HTTP_INIT();
  105.     // URL
  106.     UART_GPRS_HTTP_URL(url);
  107.     // Content Type
  108.     UART_Send("AT+HTTPPARA=\"CONTENT\",\"application/json\"\r\n", UART_Transmit_DELAY);
  109.    
  110.    
  111.     // Data
  112.    
  113.     char* at_httpdata = "AT+HTTPDATA=%d,10000\r\n";
  114.     int data_length = strlen(data);
  115.     char at_httpdata_with_size[strlen(at_httpdata)+sizeof(data_length)-1];
  116.     snprintf(at_httpdata_with_size, sizeof(at_httpdata_with_size), at_httpdata, data_length);
  117.    
  118.     UART_Send(at_httpdata_with_size, UART_Transmit_DELAY);
  119.     UART_Send(data, UART_Transmit_DELAY);
  120.     UART_Send("0x1A", UART_Transmit_DELAY);
  121.  
  122.     // Set to POST
  123.     UART_Send("AT+HTTPACTION=1\r\n", UART_Transmit_DELAY);
  124.     // Get Respond from server
  125.     UART_Send("AT+HTTPREAD\r\n", UART_Transmit_DELAY);
  126. }
  127.  
  128. /**
  129.   * @brief  Main program.
  130.   * @param  None
  131.   * @retval None
  132.   */
  133. int main(void)
  134. {
  135.  
  136.   /* STM32L0xx HAL library initialization:
  137.        - Configure the Flash prefetch, Flash preread and Buffer caches
  138.        - Systick timer is configured by default as source of time base, but user
  139.              can eventually implement his proper time base source (a general purpose
  140.              timer for example or other time source), keeping in mind that Time base
  141.              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  142.              handled in milliseconds basis.
  143.        - Low Level Initialization
  144.      */
  145.   HAL_Init();
  146.  
  147.   /* Configure LED3 */
  148.   BSP_LED_Init(LED3);
  149.  
  150.   /* Configure the system clock to 32 Mhz */
  151.   SystemClock_Config();
  152.  
  153.   /*##-1- Configure the UART peripheral ######################################*/
  154.   /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
  155.   /* UART1 configured as follow:
  156.       - Word Length = 8 Bits
  157.       - Stop Bit = One Stop bit
  158.       - Parity = None
  159.       - BaudRate = 9600 baud
  160.       - Hardware flow control disabled (RTS and CTS signals) */
  161.            
  162.            
  163.     // GSM UART
  164.    
  165.   UartHandle.Instance        = USARTx;
  166.   UartHandle.Init.BaudRate   = 9600;
  167.   UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  168.   UartHandle.Init.StopBits   = UART_STOPBITS_1;
  169.   UartHandle.Init.Parity     = UART_PARITY_NONE;
  170.   UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  171.   UartHandle.Init.Mode       = UART_MODE_TX_RX;
  172.  
  173.     // GPS UART
  174. /*
  175. huart2.Instance        = USART2;
  176.   huart2.Init.BaudRate   = 9600;
  177.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  178.   huart2.Init.StopBits   = UART_STOPBITS_1;
  179.   huart2.Init.Parity     = UART_PARITY_NONE;
  180.   huart2.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  181.   huart2.Init.Mode       = UART_MODE_TX_RX;
  182.     */
  183.   if(HAL_UART_Init(&UartHandle) != HAL_OK)
  184.   {
  185.     Error_Handler();
  186.   }
  187.  
  188.   /* Infinite loop */
  189.   while (1)
  190.   {
  191.    
  192.         UART_GPRS_Init();
  193.         UART_GPRS_HTTP_POST("http://www./gps/testas.php", "{\"gps\": \"123\"}");
  194.        
  195.  
  196.   }
  197. }
  198.  
  199. /**
  200.   * @brief  System Clock Configuration
  201.   *         The system Clock is configured as follow :
  202.   *            System Clock source            = PLL (HSI)
  203.   *            SYSCLK(Hz)                     = 32000000
  204.   *            HCLK(Hz)                       = 32000000
  205.   *            AHB Prescaler                  = 1
  206.   *            APB1 Prescaler                 = 1
  207.   *            APB2 Prescaler                 = 1
  208.   *            HSI Frequency(Hz)              = 16000000
  209.   *            PLL_MUL                        = 4
  210.   *            PLL_DIV                        = 2
  211.   *            Flash Latency(WS)              = 1
  212.   *            Main regulator output voltage  = Scale1 mode
  213.   * @param  None
  214.   * @retval None
  215.   */
  216. static void SystemClock_Config(void)
  217. {
  218.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  219.   RCC_OscInitTypeDef RCC_OscInitStruct;
  220.  
  221.   /* Enable Power Control clock */
  222.   __HAL_RCC_PWR_CLK_ENABLE();
  223.  
  224.   /* The voltage scaling allows optimizing the power consumption when the device is
  225.      clocked below the maximum system frequency, to update the voltage scaling value
  226.      regarding system frequency refer to product datasheet.  */
  227.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  228.  
  229.   /* Enable HSI Oscillator and activate PLL with HSI as source */
  230.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  231.   RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
  232.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  233.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  234.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  235.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
  236.   RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2;
  237.   RCC_OscInitStruct.HSICalibrationValue = 0x10;
  238.   HAL_RCC_OscConfig(&RCC_OscInitStruct);  
  239.  
  240.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  241.      clocks dividers */
  242.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  243.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  244.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  245.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;  
  246.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;  
  247.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
  248. }
  249.  
  250. /**
  251.   * @brief  Tx Transfer completed callback
  252.   * @param  UartHandle: UART handle.
  253.   * @note   This example shows a simple way to report end of IT Tx transfer, and
  254.   *         you can add your own implementation.
  255.   * @retval None
  256.   */
  257. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
  258. {
  259.   /* Set transmission flag: trasfer complete*/
  260.   UartReady = SET;
  261. }
  262.  
  263. /**
  264.   * @brief  Rx Transfer completed callback
  265.   * @param  UartHandle: UART handle
  266.   * @note   This example shows a simple way to report end of IT Rx transfer, and
  267.   *         you can add your own implementation.
  268.   * @retval None
  269.   */
  270. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
  271. {
  272.   /* Set transmission flag: trasfer complete*/
  273.   UartReady = SET;
  274. }
  275.  
  276. /**
  277.   * @brief  UART error callbacks
  278.   * @param  UartHandle: UART handle
  279.   * @note   This example shows a simple way to report transfer error, and you can
  280.   *         add your own implementation.
  281.   * @retval None
  282.   */
  283.  void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
  284. {
  285.     while(1)
  286.     {
  287.     }
  288. }
  289.  
  290. /**
  291.   * @brief  Compares two buffers.
  292.   * @param  pBuffer1, pBuffer2: buffers to be compared.
  293.   * @param  BufferLength: buffer's length
  294.   * @retval 0  : pBuffer1 identical to pBuffer2
  295.   *         >0 : pBuffer1 differs from pBuffer2
  296.   */
  297. static uint16_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
  298. {
  299.   while (BufferLength--)
  300.   {
  301.     if ((*pBuffer1) != *pBuffer2)
  302.     {
  303.       return BufferLength;
  304.     }
  305.     pBuffer1++;
  306.     pBuffer2++;
  307.   }
  308.  
  309.   return 0;
  310. }
  311.  
  312. /**
  313.   * @brief  This function is executed in case of error occurrence.
  314.   * @param  None
  315.   * @retval None
  316.   */
  317. static void Error_Handler(void)
  318. {
  319.     while(1)
  320.     {
  321.             BSP_LED_On(LED3);
  322.             HAL_Delay(100);
  323.             BSP_LED_Off(LED3);
  324.     }
  325. }
  326.  
  327. #ifdef  USE_FULL_ASSERT
  328.  
  329. /**
  330.   * @brief  Reports the name of the source file and the source line number
  331.   *         where the assert_param error has occurred.
  332.   * @param  file: pointer to the source file name
  333.   * @param  line: assert_param error line source number
  334.   * @retval None
  335.   */
  336. void assert_failed(uint8_t* file, uint32_t line)
  337. {
  338.   /* User can add his own implementation to report the file name and line number,
  339.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  340.  
  341.   /* Infinite loop */
  342.   while (1)
  343.   {
  344.   }
  345. }
  346. #endif
  347.  
  348. /**
  349.   * @}
  350.   */
  351.  
  352. /**
  353.   * @}
  354.   */
  355.  
  356. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top