ISKBoard example project
guowenxue
7 days ago 6c7b6c910be1dcdc0bb786e02be648b1a56faa5e
Core/Src/main.c
@@ -26,9 +26,11 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>
#include "miscdev.h"
#include "sht20.h"
#include "w25q.h"
#include "core_json.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -60,7 +62,68 @@
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t     g_rxch; /* HAL_UART_Receive_IT() receive 1 byte buffer */
uint8_t     g_rxbuf[256]; /* UART1 receive data buffer */
uint8_t     g_rxbytes;    /* UART1 receive data bytes */
uint32_t    g_rxTick;
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if( huart == &huart1 )
    {
        /* clear buffer if it's full */
        if( g_rxbytes >= sizeof(g_rxbuf) )
            g_rxbytes = 0;
        /* Receive a byte data by interrupter */
        HAL_UART_Receive_IT(huart, &g_rxch, 1);
        /* Put this data into receive buffer */
        g_rxbuf[g_rxbytes++] = g_rxch;
        /* Get current receive data tick */
        g_rxTick = HAL_GetTick();
    }
}
/* parser and control the Leds and relay */
int parser_command(char *json_str)
{
    const char *fields[] = {"RedLed", "GreenLed", "BlueLed", "Relay"};
    char *value;
    size_t value_len = 0;
    int status;
    if (JSON_Validate(json_str, strlen(json_str) ) != JSONSuccess)
    {
        printf("Invalid JSON: %s\r\n", json_str);
        return -1;
    }
    for (int i = 0; i < sizeof(fields) / sizeof(fields[0]); i++)
    {
        if (JSONSuccess == JSON_Search(json_str, strlen(json_str), fields[i], strlen(fields[i]), &value, &value_len))
        {
            if ( !strncasecmp(value, "On", value_len) )
                status = 1;
            else if ( !strncasecmp(value, "Off", value_len) )
                status = 0;
            if ( !strcmp(fields[i], "RedLed") ) {
                turn_led(Led_R, status);
            } else if ( !strcmp(fields[i], "GreenLed") ) {
                turn_led(Led_G, status);
            } else if ( !strcmp(fields[i], "BlueLed") ) {
                turn_led(Led_B, status);
            } else if ( !strcmp(fields[i], "Relay") ) {
                turn_relay(Relay1, status);
            }
        }
    }
    return 0;
}
/* USER CODE END 0 */
/**
@@ -73,6 +136,7 @@
  /* USER CODE BEGIN 1 */
  int      rv;
  float    temperature, humidity;
  char     pack_buf[128];
  /* USER CODE END 1 */
  /* MCU Configuration--------------------------------------------------------*/
@@ -101,25 +165,40 @@
  /* USER CODE BEGIN 2 */
  beep_start(2, 300);
  spinor_test();
  /* Must Enable UART3 receive interrupter here */
  HAL_UART_Receive_IT(&huart1 , &g_rxch, 1);
  do {} while(1);
  printf("Remote monitor and control program\r\n");
  /* USER CODE END 2 */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
      const uint32_t sample_interval = 5000; /* sample interval time 5000ms */
      static uint32_t last_tick = 0; /* last sample time */
      /* check sample time arrive and start report  */
      if ((HAL_GetTick() - last_tick) >= sample_interval)
      {
   rv = sht20_sample_TrH(&temperature, &humidity);
   if( rv )
            if( !rv )
   {
      printf("ERROR: SHT20 sample data failure, rv=%d\r\n", rv);
                last_tick = HAL_GetTick();
                memset(pack_buf, 0, sizeof(pack_buf));
                snprintf(pack_buf, sizeof(pack_buf), "{\"Temperature\":\"%.02f\", \"Humidity\":\"%.02f\"}\r\n", temperature, humidity);
                HAL_UART_Transmit(&huart1, (uint8_t *)pack_buf, strlen(pack_buf), 0xFFFF);
   }
   else
      }
      /* UART receive data and receive over */
      if( g_rxbytes > 0 && (HAL_GetTick()-g_rxTick)>=2)
   {
      printf("SHT20 sample temperature: %.2f relative humidity: %.1f%%\r\n", temperature, humidity);
          parser_command((char *)g_rxbuf);
          memset(g_rxbuf, 0, sizeof(g_rxbuf));
          g_rxbytes = 0;
   }
   HAL_Delay(1000);
    /* USER CODE END WHILE */