We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
hi, I wrote a code for an SMS controlled irrigation system. it has three modes: manual mode which can turn a Solenoid valve on and off with an SMS, semi-auto which can turn a solenoid valve on for a specific duration and turn it off using an SMS. and automatic mode which can for example turn a solenoid valve on for an hour every day at a specific time for instance 5 am. I have no problem with the first two modes. but I encountered a problem with the third mode. I need to use a gsm clock for this mode and I wrote a function for testing that extracts the time after we send a message to sim800 and texts back the time. I want to use this time to compare it to 5. when I use this function in my code nothing works and I can't turn the LED on with SMS. when I delete this function the code works fine. what is the problem here?
my compiler is Keil and I use stm32f103ret6.and the function that I have a problem with it is send_clock.
/** ****************************************************************************** * File Name : main.c * Description : Main program body ****************************************************************************** * * COPYRIGHT(c) 2020 STMicroelectronics * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "stm32f1xx_hal.h" /* USER CODE BEGIN Includes */ #include <string.h> #include <stdio.h> #include <stdlib.h> unsigned char E[]={10},C[]={13},G[]={34},S[]={26}; #define Enter HAL_UART_Transmit(&huart1,(unsigned char*)E,1,10); #define CR HAL_UART_Transmit(&huart1,(unsigned char*)C,1,10); #define GIM HAL_UART_Transmit(&huart1,(unsigned char*)G,1,10); #define SUB HAL_UART_Transmit(&huart1,(unsigned char*)S,1,10); #define LED HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0) /* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/ TIM_HandleTypeDef htim3; UART_HandleTypeDef huart1; /* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ UART_HandleTypeDef huart1; char str_tmp[50]; char buffer; char m1[2]; int sec=0; int b=0 ; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); void Error_Handler(void); static void MX_GPIO_Init(void); static void MX_USART1_UART_Init(void); static void MX_TIM3_Init(void); /* USER CODE BEGIN PFP */ /* Private function prototypes -----------------------------------------------*/ void uart1_puts(char *str); void Send_State(void); void CMGF_1(void); char get_char(void); void wait_to_get(char ch); void del_All_SMS(void); void cmti(void) ; void cmgr(void); void Send_SMS(char *text); //**************************************************** void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if (htim->Instance==TIM3) { sec++; GPIOB->BSRR=(1<<16); if (sec==10) { GPIOB->BSRR=0x1; sec=0; } else if (sec==5 && b==1) { GPIOB->BSRR=0x1; sec=0; } } } void uart1_puts(char *str) { while (*str!=0) { HAL_UART_Transmit(&huart1,(unsigned char*)str,1,10); str++; } } //********************** //******************************************************* //******************************************************************************* void auto1(uint32_t x,uint32_t y) { HAL_Delay(y); GPIOB->BSRR=0x01; HAL_Delay(x); GPIOB->BSRR=(1<<16); } //******************************************************** void CMGF_1(void) { str_tmp[0]='\0'; HAL_Delay(100); strcpy(str_tmp,"AT+CMGF=1"); HAL_Delay(500); uart1_puts(str_tmp); Enter CR } //******************************************************** char get_char(void) { char cha[1]; HAL_UART_Receive(&huart1,(unsigned char *)cha,1,200000); return cha[0]; } //******************************************************** void wait_to_get(char ch) { char Rch[1]; while(ch != Rch[0]){ HAL_UART_Receive(&huart1,(unsigned char *)Rch,1,200000); } } //******************************************************** void del_All_SMS(void) { strcpy(str_tmp,"AT+CMGDA="); HAL_Delay(500); uart1_puts(str_tmp); GIM str_tmp[0]='\0'; HAL_Delay(500); strcpy(str_tmp,"DEL ALL"); HAL_Delay(500); uart1_puts(str_tmp); GIM //AT+CMGDA="del all" Enter CR HAL_Delay(500); str_tmp[0]='\0'; } //********************************************************* void cmti(void) { wait_to_get(','); uart1_puts("AT+CMGR=1"); Enter CR } //********************************************************* void Send_State(void) { CMGF_1(); HAL_Delay(500); str_tmp[0]='\0'; HAL_Delay(100); strcpy(str_tmp,"AT+CMGS="); HAL_Delay(500); uart1_puts(str_tmp); GIM str_tmp[0]='\0'; HAL_Delay(100); strcpy(str_tmp,"09154866346"); HAL_Delay(500); uart1_puts(str_tmp); GIM Enter CR HAL_Delay(500); if(LED==1)// send State { uart1_puts("LED is on"); } else { uart1_puts("LED is off"); } Enter CR str_tmp[0]='\0'; HAL_Delay(500); SUB HAL_Delay(5000); del_All_SMS(); } //********************************************************* void Send_SMS(char *text) { CMGF_1(); HAL_Delay(500); str_tmp[0]='\0'; HAL_Delay(100); strcpy(str_tmp,"AT+CMGS="); HAL_Delay(500); uart1_puts(str_tmp); GIM str_tmp[0]='\0'; HAL_Delay(100); strcpy(str_tmp,"09154866346"); HAL_Delay(500); uart1_puts(str_tmp); GIM Enter CR HAL_Delay(500); uart1_puts(text); Enter CR str_tmp[0]='\0'; HAL_Delay(500); SUB HAL_Delay(2000); del_All_SMS(); } //************************************************ void send_clock(void) { int y,z,m,j; char chert[19]={0}; char date[22]={0}; char buffer2[50]; char new_date[30]={0}; wait_to_get(','); for(y=0;y<19;y++) { chert[y]=get_char(); } for (z=0;z<22;z++) { date[z]=get_char(); } Send_SMS(date); // strcpy(str_tmp,"AT+CCLK="); // HAL_Delay(500); // uart1_puts(str_tmp); // GIM // str_tmp[0]='\0'; // HAL_Delay(500); // strcpy(str_tmp,date); // HAL_Delay(500); // uart1_puts(str_tmp); // GIM //AT+CMGDA="del all" // Enter // CR // // str_tmp[0]='\0'; // uart1_puts("AT+CCLK?"); // Enter // CR // HAL_Delay(500); // for (j=0;j<30;j++) // { // new_date[j]=get_char(); // } // Send_SMS(new_date); } //************************************************** void cmgr(void) { int x; char message[2]={0}; wait_to_get(E[0]); for(x=0;x<2;x++) { message[x]=get_char(); } if(strcmp(message,"on")==0) { GPIOB->BSRR=0x1; del_All_SMS(); } else if(strcmp(message,"of")==0) { GPIOB->BSRR=(1<<16); del_All_SMS(); } else if(strcmp(message,"st")==0) { Send_State(); del_All_SMS(); } else if (strcmp(message,"s1")==0) { auto1(1000,2000); del_All_SMS(); } else if (strcmp(message,"s2")==0) { auto1(1000,5000); del_All_SMS(); } else if (strcmp(message,"s3")==0) { auto1(1000,10000); del_All_SMS(); } else if (strcmp(message,"at")==0) { HAL_TIM_Base_Start_IT(&htim3); del_All_SMS(); b=0; } else if (strcmp(message,"mn")==0) { HAL_TIM_Base_Stop_IT(&htim3); del_All_SMS(); } else if (strcmp(message,"a2")==0) { b=1; del_All_SMS(); } // else if (strcmp(message,"ck")==0) // { // send_clock(); // del_All_SMS(); // } } /* USER CODE END PFP */ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ 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(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USART1_UART_Init(); MX_TIM3_Init(); /* USER CODE BEGIN 2 */ CMGF_1(); del_All_SMS(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { buffer=get_char(); if(buffer=='+') { buffer=get_char(); if(buffer=='C') { buffer=get_char(); if(buffer=='M') { buffer=get_char(); if(buffer=='T') { buffer=get_char(); if(buffer=='I') { buffer=get_char(); if(buffer==':') cmti(); } } else if(buffer=='G') { buffer=get_char(); if(buffer=='R') { buffer=get_char(); if(buffer==':') cmgr(); send_clock(); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } } } } } } } /* USER CODE END 3 */ /** System Clock Configuration */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct; /**Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = 16; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /**Initializes the CPU, AHB and APB busses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { Error_Handler(); } /**Configure the Systick interrupt time */ HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); /**Configure the Systick */ HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); /* SysTick_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); } /* TIM3 init function */ static void MX_TIM3_Init(void) { TIM_ClockConfigTypeDef sClockSourceConfig; TIM_MasterConfigTypeDef sMasterConfig; htim3.Instance = TIM3; htim3.Init.Prescaler = 7999; htim3.Init.CounterMode = TIM_COUNTERMODE_UP; htim3.Init.Period = 999; htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; if (HAL_TIM_Base_Init(&htim3) != HAL_OK) { Error_Handler(); } sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK) { Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK) { Error_Handler(); } } /* USART1 init function */ static void MX_USART1_UART_Init(void) { huart1.Instance = USART1; huart1.Init.BaudRate = 115200; 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; if (HAL_UART_Init(&huart1) != HAL_OK) { Error_Handler(); } } /** Configure pins as * Analog * Input * Output * EVENT_OUT * EXTI */ static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); /*Configure GPIO pin : PB0 */ GPIO_InitStruct.Pin = GPIO_PIN_0; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @param None * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler */ /* User can add his own implementation to report the HAL error return state */ while(1) { } /* USER CODE END Error_Handler */ } #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 /** * @} */ /** * @} */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/