/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2023 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include #include #include /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ UART_HandleTypeDef huart1; UART_HandleTypeDef huart2; UART_HandleTypeDef huart3; /* USER CODE BEGIN PV */ #define PREF_SMS_STORAGE "\"SM\"" uint8_t slot = 0; //uint8_t rx_buffer[100] = {0}; uint8_t rx_index = 0; uint8_t rx_data; int8_t Tx_data[34]; int8_t Rx_data[31]; int8_t sms_data[14]; char veri[2]; uint8_t drm; uint8_t baslangic_h; uint8_t kanal; uint8_t adres; uint8_t baslangic_m; uint8_t bitis_h; uint8_t bitis_m; uint8_t cikis_; char destination[25]; int start = 1; int len = 2; char mobileNumber[] = "+905432259912"; // Enter the Mobile Number you want to send to char msj[50]; // Enter the Mobile Number you want to send to char ATcommand[80]; uint8_t buffer[100] = {0}; uint8_t ATisOK = 0; uint8_t msjDrm=0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART2_UART_Init(void); static void MX_USART1_UART_Init(void); static void MX_USART3_UART_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ void sms_gonder(char mesaj[]) { if(ATisOK==0){ sprintf(ATcommand,"AT\r\n"); HAL_UART_Transmit(&huart1,(uint8_t *)ATcommand,strlen(ATcommand),1000); HAL_UART_Receive (&huart1, buffer, 30, 100); HAL_Delay(1000); if(strstr((char *)buffer,"OK")){ ATisOK = 1; } HAL_Delay(1000); memset(buffer,0,sizeof(buffer)); } sprintf(ATcommand,"at+cmgda=\"del all\"\n\r"); HAL_UART_Transmit(&huart1,(uint8_t *)ATcommand,strlen(ATcommand),1000); HAL_UART_Receive (&huart1, buffer, 30, 100); HAL_Delay(1000); if(strstr((char *)buffer,"OK")){ ATisOK = 1; } HAL_Delay(1000); memset(buffer,0,sizeof(buffer)); /* sprintf(ATcommand,"AT+CMGF=1\r\n"); HAL_UART_Transmit(&huart1,(uint8_t *)ATcommand,strlen(ATcommand),1000); HAL_UART_Receive (&huart1, buffer, 30, 100); HAL_Delay(100); memset(buffer,0,sizeof(buffer));*/ // Send AT+CNMI=2,1 to enable notification when SMS arrives sprintf(ATcommand,"AT+CNMI=0,0\r\n"); HAL_UART_Transmit(&huart1,(uint8_t *)ATcommand,strlen(ATcommand),1000); // Enabling interrupt receive HAL_Delay(100); sprintf(ATcommand,"AT+CMGS=\"%s\"\r\n",mobileNumber); HAL_UART_Transmit(&huart1,(uint8_t *)ATcommand,strlen(ATcommand),1000); HAL_Delay(100); sprintf(ATcommand,"%s%c",mesaj,0x1a); HAL_UART_Transmit(&huart1,(uint8_t *)ATcommand,strlen(ATcommand),1000); HAL_UART_Receive (&huart1, buffer, 30, 100); memset(buffer,0,sizeof(buffer)); HAL_Delay(100); // Send AT+CNMI=2,1 to enable notification when SMS arrives sprintf(ATcommand,"AT+CNMI=2,1\r\n"); HAL_UART_Transmit(&huart1,(uint8_t *)ATcommand,strlen(ATcommand),1000); // Enabling interrupt receive } // Function to implement substring function in C char* substring(char *destination, const char *source, int beg, int n) { // extracts `n` characters from the source string starting from `beg` index // and copy them into the destination string while (n > 0) { *destination = *(source + beg); destination++; source++; n--; } // null terminate destination string *destination = '\0'; // return the destination string return destination; } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USART2_UART_Init(); MX_USART1_UART_Init(); MX_USART3_UART_Init(); /* USER CODE BEGIN 2 */ sprintf(ATcommand,"at+cmgda=\"del all\"\n\r"); HAL_UART_Transmit(&huart1,(uint8_t *)ATcommand,strlen(ATcommand),1000); HAL_UART_Receive (&huart1, buffer, 30, 100); HAL_Delay(1000); if(strstr((char *)buffer,"OK")){ ATisOK = 1; } HAL_Delay(1000); memset(buffer,0,sizeof(buffer)); if(ATisOK==0){ sprintf(ATcommand,"AT\r\n"); HAL_UART_Transmit(&huart1,(uint8_t *)ATcommand,strlen(ATcommand),1000); HAL_UART_Receive (&huart1, buffer, 30, 100); HAL_Delay(1000); if(strstr((char *)buffer,"OK")){ ATisOK = 1; } HAL_Delay(1000); memset(buffer,0,sizeof(buffer)); } // Wait until getting response OK to AT // Send AT+CMGF=1 sprintf(ATcommand,"AT+CMGF=1\r\n"); HAL_UART_Transmit(&huart1,(uint8_t *)ATcommand,strlen(ATcommand),1000); HAL_UART_Receive (&huart1, buffer, 100, 100); HAL_Delay(1000); memset(buffer,0,sizeof(buffer)); // Send AT+CNMI=2,1 to enable notification when SMS arrives sprintf(ATcommand,"AT+CNMI=2,1\r\n"); HAL_UART_Transmit(&huart1,(uint8_t *)ATcommand,strlen(ATcommand),1000); // Enabling interrupt receive HAL_UART_Receive_IT(&huart1,&rx_data,1);// receive data (one character only) /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ // HAL_UART_Receive_IT(&huart1,&rx_data,1);// receive data (one character only) if (msjDrm==1) { sms_gonder(msj); msjDrm=0; } } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1; PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } } /** * @brief USART1 Initialization Function * @param None * @retval None */ static void MX_USART1_UART_Init(void) { /* USER CODE BEGIN USART1_Init 0 */ /* USER CODE END USART1_Init 0 */ /* USER CODE BEGIN USART1_Init 1 */ /* USER CODE END USART1_Init 1 */ huart1.Instance = USART1; huart1.Init.BaudRate = 9600; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; if (HAL_UART_Init(&huart1) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN USART1_Init 2 */ /* USER CODE END USART1_Init 2 */ } /** * @brief USART2 Initialization Function * @param None * @retval None */ static void MX_USART2_UART_Init(void) { /* USER CODE BEGIN USART2_Init 0 */ /* USER CODE END USART2_Init 0 */ /* USER CODE BEGIN USART2_Init 1 */ /* USER CODE END USART2_Init 1 */ huart2.Instance = USART2; huart2.Init.BaudRate = 38400; huart2.Init.WordLength = UART_WORDLENGTH_8B; huart2.Init.StopBits = UART_STOPBITS_1; huart2.Init.Parity = UART_PARITY_NONE; huart2.Init.Mode = UART_MODE_TX_RX; huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart2.Init.OverSampling = UART_OVERSAMPLING_16; huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; if (HAL_UART_Init(&huart2) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN USART2_Init 2 */ /* USER CODE END USART2_Init 2 */ } /** * @brief USART3 Initialization Function * @param None * @retval None */ static void MX_USART3_UART_Init(void) { /* USER CODE BEGIN USART3_Init 0 */ /* USER CODE END USART3_Init 0 */ /* USER CODE BEGIN USART3_Init 1 */ /* USER CODE END USART3_Init 1 */ huart3.Instance = USART3; huart3.Init.BaudRate = 9600; huart3.Init.WordLength = UART_WORDLENGTH_8B; huart3.Init.StopBits = UART_STOPBITS_1; huart3.Init.Parity = UART_PARITY_NONE; huart3.Init.Mode = UART_MODE_TX_RX; huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart3.Init.OverSampling = UART_OVERSAMPLING_16; huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; if (HAL_UART_Init(&huart3) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN USART3_Init 2 */ /* USER CODE END USART3_Init 2 */ } /** * @brief GPIO Initialization Function * @param None * @retval None */ static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET); /*Configure GPIO pin : B1_Pin */ GPIO_InitStruct.Pin = B1_Pin; GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pin : LD2_Pin */ GPIO_InitStruct.Pin = LD2_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct); } /* USER CODE BEGIN 4 */ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if(huart->Instance==USART1) { // if the character received is other than 'enter' ascii13, save the data in buffer if(rx_data!=13)// { buffer[rx_index++]=rx_data; } else { // if new message arrived, read the message if(sscanf((char*)buffer, "\n+CMTI: " PREF_SMS_STORAGE ",%hhd", &slot)==1) { sprintf(ATcommand,"AT+CMGR=%d\r\n",slot); HAL_UART_Transmit_IT(&huart1,(uint8_t *)ATcommand,strlen(ATcommand)); } // if message read contains "ledon", switch the LED ON else if (strstr((char *)buffer,"ledon")) { HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, 1); sprintf(msj,"Led acildi"); msjDrm=1; } // if message read contains "ledoff", switch the LED OFF else if (strstr((char *)buffer,"ledoff")) { HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, 0); sprintf(msj,"Led kapandi"); msjDrm=1; } // This will delete all messages in the SIM card. (Is it ok for you?) else if (strstr((char *)buffer,"READ")) { sprintf(ATcommand,"AT+CMGD=,4\r\n"); HAL_UART_Transmit_IT(&huart1,(uint8_t *)ATcommand,strlen(ATcommand)); } else { /************* ayarlamLAR *************/ substring(destination, (char *)buffer, start, len); if (strstr((char *)destination,"ch")) { /***********kanal************/ veri[0]=buffer[3]; veri[1]=buffer[4]; kanal = (uint8_t)atoi(veri); /***********adres************/ veri[0]=buffer[5]; veri[1]=buffer[6]; adres = (uint8_t)atoi(veri); /*****hangı cıkıs?******/ veri[0]=buffer[7]; veri[1]=buffer[8]; cikis_ = (uint8_t)atoi(veri); if (cikis_== 1) { veri[0]=buffer[9]; veri[1]=buffer[10]; drm = (uint8_t)atoi(veri); veri[0]=buffer[11]; veri[1]=buffer[12]; baslangic_h = (uint8_t)atoi(veri); veri[0]=buffer[13]; veri[1]=buffer[14]; baslangic_m = (uint8_t)atoi(veri); veri[0]=buffer[15]; veri[1]=buffer[16]; bitis_h = (uint8_t)atoi(veri); veri[0]=buffer[17]; veri[1]=buffer[18]; bitis_m = (uint8_t)atoi(veri); } Tx_data[0]=0x00;//başlangıc biti Tx_data[1]=adres;//adress Tx_data[2]=kanal;// kanal Tx_data[3]=drm;//0 Tx_data[4]=baslangic_h; Tx_data[5]=baslangic_m; Tx_data[6]=bitis_h; Tx_data[7]=bitis_m;//0 Tx_data[8]=8; Tx_data[9]=9; Tx_data[10]=9; Tx_data[11]=9;//0 Tx_data[12]=9; Tx_data[13]=9; Tx_data[14]=9; Tx_data[15]=8;//0 Tx_data[16]=8; Tx_data[17]=8; Tx_data[18]=8; Tx_data[19]=7;//0 Tx_data[20]=7; Tx_data[21]=7; Tx_data[22]=7; Tx_data[23]=7;//0 Tx_data[24]=7; Tx_data[25]=7; Tx_data[26]=7; Tx_data[27]=7;//0 Tx_data[28]=7; Tx_data[29]=7; Tx_data[30]=7; Tx_data[31]=7;//0 Tx_data[32]=7; Tx_data[33]=7; // HAL_UART_Transmit(&huart3, Tx_data, sizeof(Tx_data), 100); sprintf(msj,"YAZDIRILDI"); msjDrm=1; sprintf(ATcommand,"AT\r\n",slot); HAL_UART_Transmit_IT(&huart1,(uint8_t *)ATcommand,strlen(ATcommand)); } } rx_index=0; memset(buffer,0,sizeof(buffer)); } } // Enabling interrupt receive again HAL_UART_Receive_IT(&huart1,&rx_data,1); // receive data (one character only) } /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #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 CODE BEGIN 6 */ /* 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) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */