Norik Badalyan
Published © GPL3+

Impulse Edge Data forwarding for air

Impulse Edge data forwarding for air (wi-fi/tcp)

IntermediateProtip2 hours263
Impulse Edge Data forwarding for air

Things used in this project

Hardware components

STM32
×1
SparkFun Triple Axis Accelerometer Breakout - ADXL345
SparkFun Triple Axis Accelerometer Breakout - ADXL345
×1
ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1

Software apps and online services

STM32CubeIDE
TCP/Com
Edge Impulse Studio
Edge Impulse Studio
Edge Impulse CLI

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Code

Sending data for TCP

C/C++
Here is an example of connecting an ADXL345 accelerometer and sending data via esp 01
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * 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"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <string.h>
#include <stdarg.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 */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;

UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_SPI1_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void vprint(const char *fmt, va_list argp)
{
	char string[200];
	if (0 < vsprintf(string, fmt, argp)) // build string
	{
		HAL_UART_Transmit(&huart1, (uint8_t*) string, strlen(string), 0xffffff);
	}
}
void ei_printf(const char *format, ...)
{
	va_list myargs;
	va_start(myargs, format);
	vprint(format, myargs);
	va_end(myargs);
}

uint8_t spibuf[10];
int16_t pDataXYZ[3] =
{ 0 };
void spiwrite(uint8_t address, uint8_t value, uint16_t cs)
{
	uint8_t data[2];
	data[0] = address | 0x40;  //Or with 0x40 to do write/read multiple bits
	data[1] = value;
	HAL_GPIO_WritePin(GPIOA, cs, GPIO_PIN_RESET); // make chip select low to enable transmission
	HAL_SPI_Transmit(&hspi1, data, 2, 10);  // write data to register
	HAL_GPIO_WritePin(GPIOA, cs, GPIO_PIN_SET); // make chip select high at the end of transmission
}
void spiread(uint8_t address, uint16_t cs)
{
	address |= 0x80;  // //Or with 0x80 for read operations
	address |= 0x40;
	HAL_GPIO_WritePin(GPIOA, cs, GPIO_PIN_RESET);
	HAL_SPI_Transmit(&hspi1, &address, 1, 1);  // send address
	HAL_SPI_Receive(&hspi1, &spibuf[0], 6, 1);  // read the data from device
	HAL_GPIO_WritePin(GPIOA, cs, GPIO_PIN_SET);
}
void adxl0_init()
{
	spiwrite(0x2D, 0x00, NSS0_Pin);  // Reset before configuring power register
	spiwrite(0x2D, 0x08, NSS0_Pin); //Configure the power register, and turn on the device
	//  spiwrite(0x1E,-8768,GPIO_PIN_4);//x
	//  spiwrite(0x1F,-8768,GPIO_PIN_4);//y
	//  spiwrite(0x20,-8768,GPIO_PIN_4);//z  colib
	// Configuring the data format register
	//The 5th bit corresponds to setting interrupt to active low if set
	spiwrite(0x31, 0x2e, NSS0_Pin); //Lower nibble:Selecting +/- 4g range and 4 wire spi mode( FULL RESOLUTION+MSB FRIST
	//Configuring sampling rate to 100hz
	spiwrite(0x2C, 0x0d, NSS0_Pin);	  	//0a
	HAL_GPIO_WritePin(GPIOA, NSS0_Pin, GPIO_PIN_SET);
}
void esp8266()
{
	ei_printf("AT\r\n");
	HAL_Delay(5000);
	ei_printf("AT+CWMODE=1\r\n");
	HAL_Delay(5000);
	ei_printf("AT+CWJAP=\"Hi-Tech Gateway 221\",\"HiTech2020@)@)\"\r\n");
	HAL_Delay(8000);
	ei_printf("AT+CIFSR\r\n");
	HAL_Delay(8000);
	ei_printf("AT+CIPSTART=\"TCP\",\"172.20.200.74\",9090\r\n");
	HAL_Delay(5000);
	ei_printf("AT+CIPMODE=1\r\n");
	HAL_Delay(5000);
	ei_printf("AT+CIPSEND\r\n");
	HAL_Delay(5000);
}
/* 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_USART1_UART_Init();
  MX_SPI1_Init();
  /* USER CODE BEGIN 2 */
	esp8266();
	adxl0_init();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		spiread(0x32, NSS0_Pin); // Register address corresponding to required data
		pDataXYZ[0] = ((spibuf[1] << 8) | spibuf[0]);
		pDataXYZ[1] = ((spibuf[3] << 8) | spibuf[2]);
		pDataXYZ[2] = ((spibuf[5] << 8) | spibuf[4]);
	ei_printf("%f\t%f\t%f\n",static_cast<float>(pDataXYZ[0]) / 100.0f,
			static_cast<float>(pDataXYZ[1]) / 100.0f,
			static_cast<float>(pDataXYZ[2]) / 100.0f);
		HAL_Delay(20);

  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = 0;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 40;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  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_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief SPI1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_SPI1_Init(void)
{

  /* USER CODE BEGIN SPI1_Init 0 */

  /* USER CODE END SPI1_Init 0 */

  /* USER CODE BEGIN SPI1_Init 1 */

  /* USER CODE END SPI1_Init 1 */
  /* SPI1 parameter configuration*/
  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
  hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 7;
  hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI1_Init 2 */

  /* USER CODE END SPI1_Init 2 */

}

/**
  * @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 = 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;
  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 GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(NSS0_GPIO_Port, NSS0_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin : NSS0_Pin */
  GPIO_InitStruct.Pin = NSS0_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(NSS0_GPIO_Port, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* 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****/

Credits

Norik Badalyan
5 projects • 12 followers
Co-founder and Embedded software/machine learning engineer at Tiny and Microelectronics engineer in Hi-Tech Gateway. 5+ years of experience.
Contact

Comments

Please log in or sign up to comment.