A way to upload data to onenet based on communication between stm32 and esp8266

Article directory

  • Preface
  • 1. Initialization configuration of stm32
  • 2. Usage steps
    • 1. Connect to esp8266
    • 2. Connect to the new version of onenet platform
  • Summarize

Foreword

The author recently discovered that the onenet platform has been updated. The new onenet seems to no longer have the same access method as the previous one, but has switched to a new device management method. However, the author tried the previous configuration and it can still be used if the device is not offline. Onenet has also started to implement narrowing functions and new charges, and it feels like it is the same as Alibaba Cloud…

1. Initialization configuration of stm32

It is relatively simple here to connect the serial port of esp8266. The author here uses serial port 3 and the hal library.

Remember that after the RX-TX TX-RX interface is confirmed to be OK, let’s look at the code.

2. Steps to use

1. Import the library

The code is as follows (example):

//Initialize IO serial port 3
//bound: baud rate
void uart3_init(u32 bound)
{
    //UART initialization settings
    UART3_Handler.Instance=USART3; //USART3
    UART3_Handler.Init.BaudRate=bound; //Baud rate
    UART3_Handler.Init.WordLength=UART_WORDLENGTH_8B; //Word length is 8-bit data format
    UART3_Handler.Init.StopBits=UART_STOPBITS_1; //A stop bit
    UART3_Handler.Init.Parity=UART_PARITY_NONE; //No parity bit
    UART3_Handler.Init.HwFlowCtl=UART_HWCONTROL_NONE; //No hardware flow control
    UART3_Handler.Init.Mode=UART_MODE_TX_RX; //Transceiver mode
    HAL_UART_Init( & amp;UART3_Handler); //HAL_UART_Init() will enable UART1
    
// HAL_UART_Receive_IT( & amp;UART3_Handler, (u8 *)USART3_RX_BUF, RXBUFFERSIZE);//This function will enable the receive interrupt: flag bit UART_IT_RXNE, and set the receive buffer and the maximum amount of data received by the receive buffer
  
}

Basic configuration of serial port 3

 if(huart->Instance==USART3)//If it is serial port 3, perform serial port 3 MSP initialization
    {
        __HAL_RCC_GPIOB_CLK_ENABLE(); //Enable GPIOb clock
        __HAL_RCC_USART3_CLK_ENABLE(); //Enable USART3 clock
        __HAL_RCC_AFIO_CLK_ENABLE();
    
        GPIO_Initure.Pin=GPIO_PIN_10; //PB10
        GPIO_Initure.Mode=GPIO_MODE_AF_PP; //Multiplex push-pull output
        GPIO_Initure.Pull=GPIO_PULLUP; //Pull-up
        GPIO_Initure.Speed=GPIO_SPEED_FREQ_HIGH;//High speed
        HAL_GPIO_Init(GPIOB, & amp;GPIO_Initure); //Initialize PB10

        GPIO_Initure.Pin=GPIO_PIN_11; //PB11
        GPIO_Initure.Mode=GPIO_MODE_AF_INPUT; //The mode must be set to multiplex input mode!
        HAL_GPIO_Init(GPIOB, & amp;GPIO_Initure); //Initialize PB11
        
        HAL_NVIC_EnableIRQ(USART3_IRQn); //Enable USART3 interrupt channel
        HAL_NVIC_SetPriority(USART3_IRQn,2,2);
    }

The author's operation here is relatively simple. I directly call the send function of serial port 3 to send the command without interrupting the reception. Generally speaking, in order to make the system more stable, you can add the ok message returned after receiving the corresponding command. It will be updated in the next step. Better, the author here is relatively simple. When the LCD is displayed, it is used in one of my projects and I keep it if I am too lazy to delete it. I can just delete it depending on the situation.

The format and content of the instruction can be found by searching the at instruction set online. Here are a few that may be vague, AT + CWJAP="xy","xiaoyang123 Here is the name and password of your own hotspot or wifi

AT + MQTTUSERCFG=0,1, here are the important parameters for connecting to the onenet platform’s own device, corresponding to the device name, soken code and timestamp.

For the soken code, you can go to the developer manual of onenet and there is a link to download and configure it yourself.

void ESP8266_Init(void)
{
    uint8_t aaa[]="AT\r\\
";
uint8_t abb[]="AT + RST\r\\
\r\\
";
uint8_t acc[]="AT + CWMODE=1\r\\
";
uint8_t add[]="AT + CWDHCP=1,1\r\\
";
uint8_t aee[]="AT + CWJAP="xy","xiaoyang123"\r\\
";
uint8_t aff[]="AT + MQTTUSERCFG=0,1,"basketball","ogdIZ57P1f","version=2018-10-31 &res =products/ogdIZ57P1f/devices/basketball & amp;et=2810295940 & amp;method=md5 & amp;sign=g0d4e+2W18mJcW8q4VRWCQ==",0,0,""\r \\
";
    uint8_t agg[]="AT + MQTTCONN=0,"mqtts.heclouds.com",1883,1\r\\
";

ESP8266_Clear();
LCD_ShowString(8,32,"system is init.",0x00,0xffff,16,0);
LCD_ShowString(8,48,"please wait.",0x00,0xffff,16,0);

printf("0. AT\r\\
");
    HAL_UART_Transmit( & amp;UART3_Handler,aaa,strlen(aaa),100); //Send data
// while(ESP8266_SendCmd("AT\r\\
", "OK"))
delay_ms(200);
    LCD_DrawLine(0,80,20,80,0X7D7C);
    LCD_DrawLine(0,81,20,81,0X7D7C);
    LCD_DrawLine(0,82,20,82,0X7D7C);

printf("1. AT + RST\r\\
");
    HAL_UART_Transmit( & amp;UART3_Handler,abb,strlen(abb),100); //Send data
// while(ESP8266_SendCmd("AT + RST\r\\
", ""))
delay_ms(200);
    LCD_DrawLine(20,80,40,80,0X7D7C);
    LCD_DrawLine(20,81,40,81,0X7D7C);
    LCD_DrawLine(20,82,40,82,0X7D7C);

\t
printf("2. CWMODE\r\\
");
    HAL_UART_Transmit( & amp;UART3_Handler,acc,strlen(acc),100); //Send data
// while(ESP8266_SendCmd("AT + CWMODE=1\r\\
", "OK"))
delay_ms(200);
    LCD_DrawLine(40,80,60,80,0X7D7C);
    LCD_DrawLine(40,81,60,81,0X7D7C);
    LCD_DrawLine(40,82,60,82,0X7D7C);

printf( "3. AT + CWDHCP\r\\
");
    HAL_UART_Transmit( & amp;UART3_Handler,add,strlen(add),100); //Send data
// while(ESP8266_SendCmd("AT + CWDHCP=1,1\r\\
", "OK"))
delay_ms(500);
    LCD_DrawLine(60,80,80,80,0X7D7C);
    LCD_DrawLine(60,81,80,81,0X7D7C);
    LCD_DrawLine(60,82,80,82,0X7D7C);

printf("4. CWJAP\r\\
");
     HAL_UART_Transmit( & amp;UART3_Handler,aee,strlen(aee),100); //Send data
// while(ESP8266_SendCmd(ESP8266_WIFI_INFO, "GOT IP"))
delay_ms(2000);
    LCD_DrawLine(80,80,100,80,0X7D7C);
    LCD_DrawLine(80,81,100,81,0X7D7C);
    LCD_DrawLine(80,82,100,82,0X7D7C);

printf( "5. MQTTUSERCFG\r\\
");
     HAL_UART_Transmit( & amp;UART3_Handler,aff,strlen(aff),100); //Send data
// while(ESP8266_SendCmd(ESP8266_USERCFG_INFO, "OK"))
delay_ms(2000);
    LCD_DrawLine(100,80,120,80,0X7D7C);
    LCD_DrawLine(100,81,120,81,0X7D7C);
    LCD_DrawLine(100,82,120,82,0X7D7C);

\t
printf( "6. MQTTCONN\r\\
");
    HAL_UART_Transmit( & amp;UART3_Handler,agg,strlen(agg),100); //Send data
// while(ESP8266_SendCmd(ESP8266_ONENET_INFO, "OK"))
delay_ms(2000);
    LCD_DrawLine(120,80,144,80,0X7D7C);
    LCD_DrawLine(120,81,144,81,0X7D7C);
    LCD_DrawLine(120,82,144,82,0X7D7C);

printf("7. ESP8266 Init OK\r\\
");
LCD_ShowString(8,64,"Init success!",0x00,0xffff,16,0);
delay_ms(500);

}
//============================================== =============
// Function name: ESP8266_SendData
//
// Function: Send data
//
// Entry parameters: temp: angle
// humi: angle 2
// adcx: angle 3
//
// Return parameters: None
//
//\tillustrate:\t\t
//================================================ ==========
void ESP8266_SendData(double angle,double angle_2,double angle_3)
{
char cmdBuf[512];
\t
ESP8266_Clear(); //Clear the receive buffer
\t
//First send the instruction to send data to prepare
\t
sprintf(cmdBuf, "AT + MQTTPUB=0,"%s","{\"id\":\ "123\"\,\"params\":{\"angle\ ":{\"value\":%f\}\,\"angle_2\\\ ":{\"value\":%f\}\,\"angle_3\":{ \"value\":%f\}}}",0,0\r\\
",pubtopic,angle,angle_2,angle_3) ;\t\t//send command
HAL_UART_Transmit( & amp;UART3_Handler,cmdBuf,strlen(cmdBuf),100); //Send data
    //while(ESP8266_SendCmd(cmdBuf, "OK"))
delay_ms(50);
memset(cmdBuf,0,sizeof(cmdBuf));
}

The above is the function reference format for sending data. Just modify the corresponding ID number and data content.

Here is the function that receives the command and returns ok. You can refer to it and modify it yourself.

//============================================== =============
// Function name: ESP8266_SendCmd
//
// Function: Send command
//
// Entry parameters: cmd: command
// res: return instruction that needs to be checked
//
//Return parameters: 0-success 1-failure
//
//\tillustrate:\t\t
//================================================ ==========
_Bool ESP8266_SendCmd(char *cmd, char *res)
{
\t
unsigned char timeOut = 200;

//Usart_SendString((unsigned char *)cmd, strlen((const char *)cmd));
HAL_UART_Transmit( & amp;UART3_Handler,(unsigned char *)cmd,strlen((const char *)cmd),100); //Send data

while(timeOut--)
{
if(ESP8266_WaitRecive() == REV_OK) //If data is received
{
if(strstr((const char *)esp8266_buf, res) != NULL) //If the keyword is retrieved
{
ESP8266_Clear(); //Clear cache
\t\t\t\t
return 0;
}
}
\t\t
delay_ms(10);
}
\t
return 1;

}

Summary

The introduction is relatively rough, mainly because there are a lot of codes posted. You can refer to and modify it yourself. It is relatively simple to use. If you are interested in how to access onenet, you can refer to the relevant tutorials on the new onenet access method and data platform. There are also many places in the code that can be modified, just refer to the modifications yourself.