Docs

Micro-controllers, wireless transmission and database

Head

ESP01 with STM32 & thingspeak using STM32CubeIDE

Prerequisites

This project assumes you have already installed STM32CubeIDE. You need to have previously done a basic blink sketch with blue-pill using STM32CubeIDE. I have made a complete video from installing STM32CubeIDE to LED blink program. You can watch it by clicking this link. https://www.youtube.com/watch?v=kXg467nVd_A

Wiring Diagram

Diagram

STM32CubeIDE Settings

Enable USART1 asynchronous

Parameter Settings --> Basic Parameters --> Baud rate 115200

Additional code on top of STM32CubeIDE generated code

/* USER CODE BEGIN Includes */
#include "string.h"
#include "stdio.h"
/* USER CODE END Includes */

  /* USER CODE BEGIN 2 */
  char api[58] = "https://api.thingspeak.com/update?api_key=PUAP0YK403IYN5RZ";
  // Change PUAP0YK403IYN5RZ to your key and length 58 to new length
  uint8_t f1;
  uint32_t f2;
  char ATcommand[100];
  char toPost[150];
  uint8_t rxBuffer[150] = {0};
  uint8_t ATisOK;

  sprintf(ATcommand,"AT+RST\r\n");
  memset(rxBuffer,0,sizeof(rxBuffer));
  HAL_UART_Transmit(&huart1,(uint8_t *)ATcommand,strlen(ATcommand),1000);
  HAL_UART_Receive (&huart1, rxBuffer, 512, 100);
  HAL_Delay(500);

  ATisOK = 0;
  while(!ATisOK){
    sprintf(ATcommand,"AT+CWMODE_CUR=1\r\n");
    memset(rxBuffer,0,sizeof(rxBuffer));
    HAL_UART_Transmit(&huart1,(uint8_t *)ATcommand,strlen(ATcommand),1000);
    HAL_UART_Receive (&huart1, rxBuffer, 512, 1000);
    if(strstr((char *)rxBuffer,"OK")){
      ATisOK = 1;
    }
    HAL_Delay(500);
  }

  ATisOK = 0;
  while(!ATisOK){
    sprintf(ATcommand,"AT+CWJAP_CUR=\"TPU4G_8NRL\",\"31408902\"\r\n");
    memset(rxBuffer,0,sizeof(rxBuffer));
    HAL_UART_Transmit(&huart1,(uint8_t *)ATcommand,strlen(ATcommand),1000);
    HAL_UART_Receive (&huart1, rxBuffer, 512, 20000);
    if(strstr((char *)rxBuffer,"OK")){
      ATisOK = 1;
    }
    HAL_Delay(500);
  }

  ATisOK = 0;
  while(!ATisOK){
    sprintf(ATcommand,"AT+CIPMUX=0\r\n");
    memset(rxBuffer,0,sizeof(rxBuffer));
    HAL_UART_Transmit(&huart1,(uint8_t *)ATcommand,strlen(ATcommand),1000);
    HAL_UART_Receive (&huart1, rxBuffer, 512, 1000);
    if(strstr((char *)rxBuffer,"OK")){
      ATisOK = 1;
    }
    HAL_Delay(500);
  }
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    for(f1=0; f1<101; f1=f1+20)
    {
      sprintf(ATcommand,"AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",80\r\n");
      memset(rxBuffer,0,sizeof(rxBuffer));
      HAL_UART_Transmit(&huart1,(uint8_t *)ATcommand,strlen(ATcommand),1000);
      HAL_UART_Receive (&huart1, rxBuffer, 512, 1000);
      HAL_Delay(500);

      f2 = HAL_GetTick();
      sprintf(toPost,"GET %s&field1=%d&field2=%lu\r\n",api,f1,f2);
      sprintf(ATcommand,"AT+CIPSEND=%d\r\n",strlen(toPost));
      memset(rxBuffer,0,sizeof(rxBuffer));
      HAL_UART_Transmit(&huart1,(uint8_t *)ATcommand,strlen(ATcommand),1000);
      HAL_UART_Receive (&huart1, rxBuffer, 512, 1000);
      if(strstr((char *)rxBuffer,">"))
      {
        memset(rxBuffer,0,sizeof(rxBuffer));
        HAL_UART_Transmit(&huart1,(uint8_t *)toPost,strlen(toPost),1000);
        HAL_UART_Receive (&huart1, rxBuffer, 512, 1000);
      }
      HAL_Delay(15000);
    }
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */