/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * *

© Copyright (c) 2021 STMicroelectronics. * All rights reserved.

* * This software component is licensed by ST under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "adc.h" #include "tim.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "string.h" #include "stdlib.h" /* 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 */ uint16_t adc_volt = 0; uint16_t adc_volt_min = 4048; uint16_t adc_volt_max = 0; uint16_t adc_volt_aver = 0; int current = 0; int temper = 0; int temper_inside = 0; void sensor_read(); /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ // --- CMD --- volatile char cmd_char; uint16_t cmd_n = 0; uint8_t cmd_flag = 0; char cmd[100]; const char hello[] = " -------- TEST MODE -------- \r\n\r\n"; // --- MODEM --- enum response_status { SIM_ERROR, SIM_OK, SIM_txt_ready, SIM_NONE, TIME_OUT } r_stat = SIM_NONE; int at_n = 0; int at_flag = 0; volatile char at_char; char at_command[1000]; enum cirent_problems { ERROR_NONE, ERROR_UNKNOWN, ERROR_AT_FAIL, ERROR_CONNECT, ERROR_HTTPS }; uint8_t SIM_CREG = 1; uint8_t SIM_RDY = 0; uint8_t SIM_SMS_Ready = 0; uint8_t SIM_SAPBR = 1; uint8_t SIM_HTTPACTION = 0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ void error_programm(enum cirent_problems e); int sim_parsing_quote(char *str, int len_str, char *pointer_str[]); int sim_parsing_responses(); int send_command(char str[]); /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ // --- FOR ADC --- /* 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_ADC1_Init(); MX_ADC2_Init(); MX_USART1_UART_Init(); MX_USART2_UART_Init(); MX_TIM4_Init(); MX_USART3_UART_Init(); MX_TIM1_Init(); /* USER CODE BEGIN 2 */ HAL_UART_Receive_IT(&huart1, (uint8_t*) &at_char, 1); HAL_UART_Receive_IT(&huart2, (uint8_t*) &cmd_char, 1); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ HAL_UART_Transmit(&huart2, (uint8_t*) hello, sizeof(hello), 100); // --- AT init --- uint8_t at_count = 0; while (send_command("AT") != SIM_OK && at_count < 5) { at_count++; HAL_Delay(1000); } if (at_count == 5) error_programm(ERROR_AT_FAIL); HAL_Delay(10000); HAL_TIM_Base_Start_IT(&htim1); // --- First config --- if (send_command("ATE0") != SIM_OK) error_programm(ERROR_UNKNOWN); if (send_command("AT+GSMBUSY=0") != SIM_OK) error_programm(ERROR_UNKNOWN); // --- Registration on GSM --- if (send_command("AT+CREG?") != SIM_OK) error_programm(ERROR_UNKNOWN); if (SIM_CREG == 0) { if (send_command("AT+CREG=1") != SIM_OK) error_programm(ERROR_UNKNOWN); if (send_command("AT+CREG?") != SIM_OK) error_programm(ERROR_UNKNOWN); } // --- CONNECT TO SIM --- if (send_command("AT+SAPBR=2,1") != SIM_OK) error_programm(ERROR_UNKNOWN); if (SIM_SAPBR == 0) { if (send_command("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"") != SIM_OK) error_programm(ERROR_UNKNOWN); //if (send_command("AT+SAPBR=3,1,\"APN\",\"internet.yota\"") != SIM_OK) if (send_command("AT+SAPBR=3,1,\"APN\",\"internet\"") != SIM_OK) error_programm(ERROR_UNKNOWN); if (send_command("AT+SAPBR=1,1") != SIM_OK) error_programm(ERROR_CONNECT); if (send_command("AT+SAPBR=2,1") != SIM_OK) error_programm(ERROR_UNKNOWN); } char https_str[200] = { }; char count_str[20] = { }; uint32_t count = 0; while (1) { if (count != 0) { // --- HTTPS CONNECT --- if (send_command("AT+HTTPINIT") != SIM_OK) error_programm(ERROR_HTTPS); if (send_command("AT+HTTPPARA = \"CID\",1") != SIM_OK) error_programm(ERROR_UNKNOWN); strcpy(https_str, "AT+HTTPPARA=\"URL\",\"https://"); //strcat(https_str, "akb.inscom.pw/insert.php?"); strcat(https_str, "www.test1.inscom.pw/insert.php?"); strcat(https_str, "&min_volt="); adc_volt_min = (((adc_volt_min + ((adc_volt_min - 1700) * 10) / 72) * 100) / 248); itoa(adc_volt_min, count_str, 10); strcat(https_str, (const char*) &count_str); strcat(https_str, "&max_volt="); adc_volt_max = (((adc_volt_max + ((adc_volt_max - 1700) * 10) / 72) * 100) / 248); itoa(adc_volt_max, count_str, 10); strcat(https_str, (const char*) &count_str); strcat(https_str, "&aver_volt="); adc_volt_aver = (((adc_volt_aver + ((adc_volt_aver - 1700) * 10) / 72) * 100) / 248); itoa(adc_volt_aver, count_str, 10); strcat(https_str, (const char*) &count_str); adc_volt_min = 4048; adc_volt_max = 0; sensor_read(); strcat(https_str, "&temp="); itoa(temper, count_str, 10); strcat(https_str, (const char*) &count_str); strcat(https_str, "¤t="); current = (current / 124 - 15) / 2 - 5; itoa(current, count_str, 10); strcat(https_str, (const char*) &count_str); //// strcat(https_str, "&mes_nam="); itoa(count, count_str, 10); strcat(https_str, (const char*) &count_str); strcat(https_str, "&id_chip=8A45CD8A507D15B74"); strcat(https_str, "\""); if (send_command((char*) &https_str) != SIM_OK) error_programm(ERROR_UNKNOWN); if (send_command("AT+HTTPPARA =\"REDIR\",1") != SIM_OK) error_programm(ERROR_UNKNOWN); if (send_command("AT+HTTPSSL=1") != SIM_OK) error_programm(ERROR_UNKNOWN); if (send_command("AT+HTTPACTION=0") != SIM_OK) error_programm(ERROR_UNKNOWN); while (SIM_HTTPACTION == 0) { HAL_Delay(10); } if (SIM_HTTPACTION == 2) error_programm(ERROR_HTTPS); SIM_HTTPACTION = 0; // if (send_command("AT+HTTPREAD") != SIM_OK) // error_programm(ERROR_UNKNOWN); if (send_command("AT+HTTPTERM") != SIM_OK) error_programm(ERROR_UNKNOWN); } count++; uint32_t summ = 0; for (int i = 0; i < 30; i++) { summ += adc_volt; HAL_Delay(1000); } adc_volt_aver = summ / 30; //if (send_command("AT") != SIM_OK) //error_programm(ERROR_AT_FAIL); // if (cmd_flag == 1) { // cmd_flag = 0; // } /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* 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_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; 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_ADC; PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ void pin_on() { HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET); } void pin_off() { HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET); } void error_programm(enum cirent_problems e) { switch (e) { case ERROR_UNKNOWN: while (1) { pin_on(); HAL_Delay(500); pin_off(); HAL_Delay(500); } break; case ERROR_AT_FAIL: while (1) { pin_on(); HAL_Delay(100); pin_off(); HAL_Delay(500); } break; case ERROR_CONNECT: while (1) { pin_on(); HAL_Delay(100); pin_off(); HAL_Delay(100); pin_on(); HAL_Delay(100); pin_off(); HAL_Delay(500); } break; default: break; } } int sim_parsing_responses() { // Init module if (strstr(at_command, "RDY")) SIM_RDY = 1; if (strstr(at_command, "SMS Ready")) SIM_SMS_Ready = 1; //Registration check if (strstr(at_command, "+CREG")) { if (at_command[7] == '0') SIM_CREG = 0; else SIM_CREG = 1; }; // IP check if (strstr(at_command, "+SAPBR")) { if (at_command[13] == '0') SIM_SAPBR = 0; else SIM_SAPBR = 1; }; // HTTPS RESPOUNSE if (strstr(at_command, "+HTTPACTION")) { if (at_command[15] == '2') SIM_HTTPACTION = 1; else SIM_HTTPACTION = 2; }; // //check ip ---> +CNACT: 0,"0.0.0.0" // +CNACT: 1,"100.65.201.157" // if (strstr(at_command, "+CNACT:")) { // if (at_command[8] == '0') // sim7000.internet_status = 0; // else if (at_command[8] == '1') { // sim7000.internet_status = 1; // sim_parsing_quote(at_command, strlen(at_command), p_str); // strcpy(sim7000.ip, p_str[0]); // } else // return 0; // return 1; // } // // //MQTT parsing answer ---> +SMSUB: "update","hello" // if (strstr(at_command, "+SMSUB:")) { // sim_parsing_quote(at_command, strlen(at_command), p_str); // } return 0; } int sim_parsing_quote(char *str, int len_str, char *pointer_str[]) { int flag = 0, j = 0, n = 0; for (int i = 0; i < len_str; i++) { if (!flag && str[i] == '\"') { flag = 1; continue; } if (flag && str[i] == '\"') { n++; flag = 0; j = 0; continue; } if (flag) { pointer_str[n][j] = str[i]; j++; } } if (flag == 1) { pointer_str[n][0] = 0; } return 1; } void sim_uart_callback() { HAL_UART_Transmit(&huart2, (uint8_t*) &at_char, 1, 1000); if (at_flag == 0 && at_char == '\r') at_flag++; if (at_flag == 2) { if (at_char == '\r') // Standard msg \r\nOK\r\n at_flag++; else if (at_char == '>') // Enter txt message at_flag++; else at_command[at_n++] = at_char; } if (at_char == 0x20 && at_flag == 3) { at_flag = 0; at_n = 0; r_stat = SIM_txt_ready; } if (at_char == '\n' && at_flag == 3) { at_flag = 0; at_n = 0; if (strstr(at_command, "OK")) { r_stat = SIM_OK; } else if (strstr(at_command, "ERROR")) { r_stat = SIM_ERROR; } else { sim_parsing_responses(); } } if (at_char == '\n' && at_flag == 1) at_flag++; HAL_UART_Receive_IT(&huart1, (uint8_t*) &at_char, 1); } int send_command(char str[]) { volatile int a = strlen(str); HAL_UART_Transmit(&huart1, (uint8_t*) str, a, 1000); HAL_UART_Transmit(&huart2, (uint8_t*) &"SEND TO -> ", 11, 1000); HAL_UART_Transmit(&huart2, (uint8_t*) str, a, 1000); HAL_UART_Transmit(&huart1, (uint8_t*) &"\r\n", 2, 1000); HAL_UART_Transmit(&huart2, (uint8_t*) &"\r\n", 2, 1000); enum response_status b; uint8_t time_count = 0; while (r_stat == SIM_NONE && time_count < 100) { time_count++; HAL_Delay(10); } if (time_count == 100) return TIME_OUT; else b = r_stat; r_stat = SIM_NONE; return b; } void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if (huart == &huart1) { sim_uart_callback(); } if (huart == &huart2) { HAL_UART_Transmit(&huart2, (uint8_t*) &cmd_char, 1, 1000); if (cmd_char == '\r') { cmd[cmd_n] = '\0'; cmd_n = 0; cmd_flag = 1; } else cmd[cmd_n++] = cmd_char; HAL_UART_Receive_IT(&huart2, (uint8_t*) &cmd_char, 1); } } /////////////////////////////////////////////////////////////////// // --- TIMER FOR ADC voult --- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if (htim->Instance == TIM1) { HAL_ADC_Start(&hadc2); HAL_ADC_PollForConversion(&hadc2, 100); adc_volt = HAL_ADC_GetValue(&hadc2); HAL_ADC_Stop(&hadc2); //(((adc + ((adc - 1700) * 10) / 72) * 100) / 248) if (adc_volt > adc_volt_max) adc_volt_max = adc_volt; if (adc_volt < adc_volt_min) adc_volt_min = adc_volt; } } // Sensor read void sensor_read() { HAL_ADC_Start(&hadc1); HAL_ADC_PollForConversion(&hadc1, 100); current = HAL_ADC_GetValue(&hadc1); HAL_ADC_Start(&hadc1); HAL_ADC_PollForConversion(&hadc1, 100); temper = (HAL_ADC_GetValue(&hadc1) * 100) / 1226; HAL_ADC_Start(&hadc1); HAL_ADC_PollForConversion(&hadc1, 100); temper_inside = HAL_ADC_GetValue(&hadc1); HAL_ADC_Stop(&hadc1); } /////////////////////////////////////////////////////////////// /* 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 */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/