推荐使用本文之前有一定的嵌入式基础,请先确保了解以下名词(提前百度):cJSON,EDP,TCP透传
首先在ONENET平台上创建一个TCP透传的产品,这段在ONENET开发者中心上有很多教程,就不再赘述。
下载一个EdpProtoDebugger
先使用这个工具与ONENET建立连接,具体教程
可以看到,实际上建立与平台的连接就是通过一个简单的EDP数据包实现的,我们在嵌入式应用中,只需要先和ONENET的服务器连接,然后发送这一串设备认证的数据包,你的设备就上线了,之后就可以通过发送JSON格式的数据或者是发送分号间隔字符串的方式来进行数据传输
tip:本文只介绍以分号间隔字符串的方式发送数据,ONENET服务器下发的命令是JSON 格式的,如果想给设备下发命令,需要移植cJSON解码。
使用的芯片是stm32f103c8t6,正点原子的esp8266模块
开始讲解程序
#include "esp8266.h"
uint8_t rxbuffer[200];
uint8_t its2=2;
uint8_t checksd[]={0X10,0X31,0X00,0X03,0X45,0X44,0X50,0X01,0X40,0X00,0X80,0X00,0X08,0X32,0X33,0X37,0X37,0X36,0X31,0X34,0X37,0X00,0X1C,
0X62,0X68,0X35,0X42,0X34,0X63,0X4A,0X53,0X4F,0X52,0X79,0X31,0X59,0X56,0X6F, 0X4E ,0X45, 0X30, 0X4B, 0X7A, 0X6C, 0X54, 0X6F, 0X64,
0X6D, 0X67, 0X34, 0X3D }; //Log in Message Get From EDPDEBUGGER
//--------------------DO NOT USE THIS FUNCTION WHILE SENDING DATA !-------------------------------
void Uart_To_Oled(void) //read uart rxbuffer and print it in OLED
{
// OLED_CLS();
USART1->DR=0x0000;
HAL_UART_Receive(&huart1,rxbuffer,200,1000);
OLED_ShowStr(0,0,rxbuffer,1);
memset(rxbuffer,0,sizeof(uint8_t)*200);
}
//------------------------------------------------------------------------------------------------
/*********************Initialize Esp8266*******************************************
****** AT ************** return OK *************************************
****** AT+CWMODE=3****** AP+STATION MODE *************************************
****** AT+RST*********** RESET TO INIT CHANGE ***********************************
****** AT+CWJAP********* JOIN AP ************************************
****** AT+CIFSR********* return AP INFORMATION **********************************
****** AT+CIPSTART x 5** CONNECT TO SERVER **********************************
****** AT+CIPMODE=1***** EDP MODE ***********************************
**********************************************************************************/
void esp8266_Init(void)
{
HAL_UART_Transmit(&huart1, CMD_AT,strlen(CMD_AT),1000);
Uart_To_Oled();
HAL_Delay(200);
HAL_UART_Transmit(&huart1, CMD_CWMODE,strlen(CMD_CWMODE),1000);
Uart_To_Oled();
HAL_Delay(200);
HAL_UART_Transmit(&huart1, CMD_RST,strlen(CMD_RST),1000);
Uart_To_Oled();
HAL_Delay(200);
// HAL_UART_Transmit(&huart1, CMD_CWJAP,strlen(CMD_CWJAP),1000);
// Uart_To_Oled();
// HAL_Delay(1000);
HAL_UART_Transmit(&huart1, CMD_CIFSR,strlen(CMD_CIFSR),1000);
Uart_To_Oled();
HAL_Delay(5000);
// OLED_CLS();
// static uint8_t is=5;
// while(is!=0)
// {
HAL_UART_Transmit(&huart1, CMD_CIPSTART,strlen(CMD_CIPSTART),1000);
Uart_To_Oled();
HAL_Delay(200);
// OLED_CLS();
// is--;
// }
HAL_UART_Transmit(&huart1, CMD_CIPMODE,strlen(CMD_CIPMODE),1000);
Uart_To_Oled();
HAL_Delay(200);
HAL_UART_Transmit(&huart1, CMD_CIPSEND,strlen(CMD_CIPSEND),1000);
Uart_To_Oled();
HAL_Delay(200);
// while(its2!=0)
// {
HAL_Delay(100);
HAL_UART_Transmit(&huart1, checksd,sizeof(checksd),1000);
// Uart_To_Oled();
//OLED_CLS();
// its2--;
// }
// OLED_CLS();
}
因为只是一个简单的demo,这里没有移植完整的EDP数据包生成和解包函数,仅仅是用EdpPrototypeDebugger生成了一个固定的数据包,这个数据包连接到的是我预先创建好的测试设备,请自行更改数据包
测试部分,使用了stm32中自带的温度传感器,用freertos建立了两个个任务,
任务一读取接收到的信息(因为这个工程中没有移植cJSON,读到的数据是cJSON格式,请自行解码)
任务二不断采集传感器信息并发送到服务器(因为这个工程中没有移植EDPkit,是用EdpPrototypeDebugger生成了一个定长数据,并进行替换)
/**
******************************************************************************
* File Name : freertos.c
* Description : Code for freertos applications
******************************************************************************
* This notice applies to any and all portions of this file
* that are not between comment pairs USER CODE BEGIN and
* USER CODE END. Other portions of this file, whether
* inserted by the user or by software development tools
* are owned by their respective copyright owners.
*
* Copyright (c) 2017 STMicroelectronics International N.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted, provided that the following conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of other
* contributors to this software may be used to endorse or promote products
* derived from this software without specific written permission.
* 4. This software, including modifications and/or derivative works of this
* software, must execute solely and exclusively on microcontroller or
* microprocessor devices manufactured by or for STMicroelectronics.
* 5. Redistribution and use of this software other than as permitted under
* this license is void and will automatically terminate your rights under
* this license.
*
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "cmsis_os.h"
/* USER CODE BEGIN Includes */
#include "gpio.h"
//#include "OLED_I2C.h"
#include "usart.h"
#include "esp8266.h"
#include "adc.h"
/* USER CODE END Includes */
/* Variables -----------------------------------------------------------------*/
osThreadId defaultTaskHandle;
osThreadId SendTempHandle;
osMessageQId myQueue01Handle;
/* USER CODE BEGIN Variables */
extern uint8_t rxbuffer[];
uint8_t message[27]={0X80,0X19,0X80,0X00,0X08,0X32,0X33,0X37,0X37,0X36,0X31,0X34,0X37,0X05,
0X00,0X0B,0X2C,0X3B,0X74,0X65,0X6D,0X70,0X2C,0X31,0X32,0X33,0X34 }; //Data Format Form EDPDEBUGGER
/* USER CODE END Variables */
/* Function prototypes -------------------------------------------------------*/
void StartDefaultTask(void const * argument);
void Tempsend(void const * argument);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
/* USER CODE BEGIN FunctionPrototypes */
/* USER CODE END FunctionPrototypes */
/* Hook prototypes */
/* Init FreeRTOS */
void MX_FREERTOS_Init(void) {
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* USER CODE BEGIN RTOS_MUTEX */
/* add mutexes, ... */
/* USER CODE END RTOS_MUTEX */
/* USER CODE BEGIN RTOS_SEMAPHORES */
/* add semaphores, ... */
/* USER CODE END RTOS_SEMAPHORES */
/* USER CODE BEGIN RTOS_TIMERS */
/* start timers, add new ones, ... */
/* USER CODE END RTOS_TIMERS */
/* Create the thread(s) */
/* definition and creation of defaultTask */
osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
/* definition and creation of SendTemp */
osThreadDef(SendTemp, Tempsend, osPriorityNormal, 0, 128);
SendTempHandle = osThreadCreate(osThread(SendTemp), NULL);
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
/* USER CODE END RTOS_THREADS */
/* Create the queue(s) */
/* definition and creation of myQueue01 */
osMessageQDef(myQueue01, 16, uint8_t);
myQueue01Handle = osMessageCreate(osMessageQ(myQueue01), NULL);
/* USER CODE BEGIN RTOS_QUEUES */
/* add queues, ... */
/* USER CODE END RTOS_QUEUES */
}
/* StartDefaultTask function */
void StartDefaultTask(void const * argument)
{
/* USER CODE BEGIN StartDefaultTask */
/* Infinite loop */
for(;;)
{
osDelay(100);
HAL_UART_Receive(&huart1,rxbuffer,200,1000);
OLED_ShowStr(0,0,rxbuffer,2);
memset(rxbuffer,0,sizeof(uint8_t)*200);
// OLED_CLS();
}
/* USER CODE END StartDefaultTask */
}
/* Tempsend function */
void Tempsend(void const * argument)
上一篇:用stm32Cube生成103c8t6的usb驱动
下一篇:STM32F103内部FLASH及地址表
推荐阅读最新更新时间:2024-11-16 21:47
设计资源 培训 开发板 精华推荐
- IS31AP4991-GRLS2-EB,IS31AP4991A 演示板,具有低电平有效关断模式的 1.1W AB 类音频功率放大器
- esp32 并口墨水屏
- 【ART-Pi】初学者扩展板+584242A
- LTC3412、2.5V/2.5A 降压型稳压器
- LTC3621IMS8E-2 1.2Vout、强制连续模式、1MHz 同步降压稳压器的典型应用
- NCV2575降压-升压转换器欠压锁定电路典型应用电路
- DC1250A-BA,用于 LTM9001-BA CMOS 输出的演示板,16 位,160Msps,DC-300MHz LPF,8dB 增益
- SPX2954 250mA 低压降稳压器的典型应用,用于低电池断开
- AM1S-1215SH30Z 1W DC-DC转换器典型应用
- uln2003锡膏分配器