数据上传
前言
最近做项目需要将32的数据上传到手机app,很明显需要用ESP8266,使用串口2遇到了一些问题,就将它记录下来。
一、STM32串口配置
这里是串口一的配置,用的APB2,
串口二用的APB1,这个没有注意,一直不能发数据。使用的时候要注意这个地方。
贴出STM32发送端的代码。
usart.c
#include "sys.h"
#include "usart.h"
//
//Èç¹ûʹÓÃucos,Ôò°üÀ¨ÏÂÃæµÄÍ·Îļþ¼´¿É.
#if SYSTEM_SUPPORT_OS
#include "includes.h" //ucos ʹÓÃ
#endif
//¼ÓÈëÒÔÏ´úÂë,Ö§³Öprintfº¯Êý,¶ø²»ÐèҪѡÔñuse MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//±ê×¼¿âÐèÒªµÄÖ§³Öº¯Êý
struct __FILE
{
int handle;
};
FILE __stdout;
//¶¨Òå_sys_exit()ÒÔ±ÜÃâʹÓðëÖ÷»úģʽ
_sys_exit(int x)
{
x = x;
}
//ÖØ¶¨Òåfputcº¯Êý
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//Ñ»··¢ËÍ,Ö±µ½·¢ËÍÍê±Ï
USART1->DR = (u8) ch;
return ch;
}
#endif
#if EN_USART1_RX //Èç¹ûʹÄÜÁ˽ÓÊÕ
//´®¿Ú1ÖжϷþÎñ³ÌÐò
//×¢Òâ,¶ÁÈ¡USARTx->SRÄܱÜÃâĪÃûÆäÃîµÄ´íÎó
u8 USART_RX_BUF[USART_REC_LEN]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.
u16 USART_RX_STA=0; //½ÓÊÕ״̬±ê¼Ç
u16 USART2_RX_STA=0; //½ÓÊÕ״̬±ê¼Ç
u8 USART2_RX_BUF[USART2_REC_LEN]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.
void uart_init(u32 bound){
//GPIO¶Ë¿ÚÉèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //ʹÄÜUSART1£¬GPIOAʱÖÓ
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.9
//USART1_RX GPIOA.10³õʼ»¯
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.10
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//ÇÀÕ¼ÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
//USART ³õʼ»¯ÉèÖÃ
USART_InitStructure.USART_BaudRate = bound;//´®¿Ú²¨ÌØÂÊ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ
USART_InitStructure.USART_StopBits = USART_StopBits_1;//Ò»¸öֹͣλ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎÞÆæÅ¼Ð£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎÞÓ²¼þÊý¾ÝÁ÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //ÊÕ·¢Ä£Ê½
USART_Init(USART1, &USART_InitStructure); //³õʼ»¯´®¿Ú1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿ªÆô´®¿Ú½ÓÊÜÖжÏ
USART_Cmd(USART1, ENABLE); //ʹÄÜ´®¿Ú1
}
void USART1_IRQHandler(void) //´®¿Ú1ÖжϷþÎñ³ÌÐò
{
u8 Res;
#if SYSTEM_SUPPORT_OS //Èç¹ûSYSTEM_SUPPORT_OSÎªÕæ£¬ÔòÐèÒªÖ§³ÖOS.
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //½ÓÊÕÖжÏ(½ÓÊÕµ½µÄÊý¾Ý±ØÐëÊÇ0x0d 0x0a½áβ)
{
Res =USART_ReceiveData(USART1); //¶ÁÈ¡½ÓÊÕµ½µÄÊý¾Ý
if((USART_RX_STA&0x8000)==0)//½ÓÊÕδÍê³É
{
if(USART_RX_STA&0x4000)//½ÓÊÕµ½ÁË0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//½ÓÊÕ´íÎó,ÖØÐ¿ªÊ¼
else USART_RX_STA|=0x8000; //½ÓÊÕÍê³ÉÁË
}
else //»¹Ã»ÊÕµ½0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//½ÓÊÕÊý¾Ý´íÎó,ÖØÐ¿ªÊ¼½ÓÊÕ
}
}
}
}
#if SYSTEM_SUPPORT_OS //Èç¹ûSYSTEM_SUPPORT_OSÎªÕæ£¬ÔòÐèÒªÖ§³ÖOS.
OSIntExit();
#endif
}
void uart2_init(u32 bound){
//GPIO¶Ë¿ÚÉèÖÃ
//GPIO¶Ë¿ÚÉèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ʹÄÜGPIOAʱÖÓ
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //ʹÄÜUSART2
//USART2_TX GPIOA.2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.2
//USART1_RX GPIOA.3³õʼ»¯
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.3
//Usart2 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//ÇÀÕ¼ÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
//USART ³õʼ»¯ÉèÖÃ
USART_InitStructure.USART_BaudRate = bound;//´®¿Ú²¨ÌØÂÊ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ
USART_InitStructure.USART_StopBits = USART_StopBits_1;//Ò»¸öֹͣλ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎÞÆæÅ¼Ð£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎÞÓ²¼þÊý¾ÝÁ÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //ÊÕ·¢Ä£Ê½
USART_Init(USART2, &USART_InitStructure); //³õʼ»¯´®¿Ú2
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//¿ªÆô´®¿Ú½ÓÊܺͷ¢ËÍÖжÏ
USART_Cmd(USART2, ENABLE);
}
void USART2_IRQHandler(void) //??2??????
{
u8 Res,t;
/
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //½ÓÊÕÖжÏ(½ÓÊÕµ½µÄÊý¾Ý±ØÐëÊÇ0x0d 0x0a½áβ)
{
Res =USART_ReceiveData(USART2); //¶ÁÈ¡½ÓÊÕµ½µÄÊý¾Ý
if((USART2_RX_STA&0x8000)==0)//½ÓÊÕδÍê³É
{
if(USART2_RX_STA&0x4000)//½ÓÊÕµ½ÁË0x0d
{
if(Res!=0x0a)USART2_RX_STA=0;//½ÓÊÕ´íÎó,ÖØÐ¿ªÊ¼
else USART2_RX_STA|=0x8000; //½ÓÊÕÍê³ÉÁË
}
else //»¹Ã»ÊÕµ½0X0D
{
if(Res==0x0d)USART2_RX_STA|=0x4000;
else
{
USART2_RX_BUF[USART2_RX_STA&0X3FFF]=Res ;
USART2_RX_STA++;
if(USART2_RX_STA>(USART2_REC_LEN-1))USART2_RX_STA=0;//½ÓÊÕÊý¾Ý´íÎó,ÖØÐ¿ªÊ¼½ÓÊÕ
}
}
}
}
}
#endif
usart.h
#ifndef __USART_H
#define __USART_H
#include "stdio.h"
#include "sys.h"
#define USART_REC_LEN 200 //¶¨Òå×î´ó½ÓÊÕ×Ö½ÚÊý 200
#define EN_USART1_RX 1 //ʹÄÜ£¨1£©/½ûÖ¹£¨0£©´®¿Ú1½ÓÊÕ
#define USART2_REC_LEN 200 //¶¨Òå×î´ó½ÓÊÕ×Ö½ÚÊý 200
#define EN_USART2_RX 1 //ʹÄÜ£¨1£©/½ûÖ¹£¨0£©´®¿Ú1½ÓÊÕ
extern u8 USART_RX_BUF[USART_REC_LEN]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.Ä©×Ö½ÚΪ»»Ðзû
extern u16 USART_RX_STA; //½ÓÊÕ״̬±ê¼Ç
extern u8 USART2_RX_BUF[USART2_REC_LEN]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.Ä©×Ö½ÚΪ»»Ðзû
extern u16 USART2_RX_STA;
//Èç¹ûÏë´®¿ÚÖжϽÓÊÕ£¬Ç벻ҪעÊÍÒÔϺ궨Òå
void uart_init(u32 bound);
void uart2_Init(u32 bound);
void USART2_IRQHandler(void);
#endif
main.c
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
u8 *U[20]={"H","Q","W","E","R","T","Y","U"};
int main(void)
{
u16 t;
u16 len;
u16 times=0;
delay_init(); //ÑÓʱº¯Êý³õʼ»¯
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //ÉèÖÃNVICÖжϷÖ×é2:2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶
uart2_init(115200); //´®¿Ú³õʼ»¯Îª115200
LED_Init(); //LED¶Ë¿Ú³õʼ»¯
KEY_Init(); //³õʼ»¯Óë°´¼üÁ¬½ÓµÄÓ²¼þ½Ó¿Ú
while(1)
{
LED1=1;
delay_ms(500);
LED1=0;
delay_ms(500);
if(KEY0==0)
{
USART_SendData(USART2, *U[0]);//Ïò´®¿Ú2·¢ËÍÊý¾Ý
}
}
}
二、ESP8266代码
if (Serial.available() > 0)//串口接收到数据
{
incomedate = Serial.read();//获取串口接收到的数据
if (incomedate == 'H')
{
Text6.print("苏E·82L84");
Serial.print("收到数据");
Serial.println(incomedate);
}
}
ESP8266有效的代码其实就这么多,串口波特率设置对就可以了。贴出完整的代码
#define BLINKER_WIFI
#include <Blinker.h>
#include <DHT.h>·
#include <SoftwareSerial.h>
int incomedate=0;
SoftwareSerial mySerial(2,3);//RX=2,TX=3
char auth[] = "**********"; // blinker app提供的秘钥
char ssid[] = "Mr.Robot";
char pswd[] = "**********";
#define TEXTE_6 "tex-lf6"
BlinkerText Text6(TEXTE_6);
BlinkerButton Button2("btn-wfu");//注意:要和APP组件’数据键名’一致
BlinkerButton Button3("btn-pk8");//注意:要和APP组件’数据键名’一致
BlinkerButton Button1("btn-j1q");//注意:要和APP组件’数据键名’一致
BlinkerButton Button4("btn-tbk");//注意:要和APP组件’数据键名’一致
BlinkerButton Button5("btn-2us");//注意:要和APP组件’数据键名’一致
BlinkerButton Button6("btn-thr");//注意:要和APP组件’数据键名’一致
BlinkerNumber HUMI("num-dax");
BlinkerNumber TEMP("num-2ev");
#define TEXTE_2 "tex-zbf"
BlinkerText Text2(TEXTE_2);
#define TEXTE_5 "tex-cnz"
BlinkerText Text5(TEXTE_5);
#define TEXTE_10 "tex-orn"
BlinkerText Text10(TEXTE_10);
BlinkerNumber chewei("num-xub");
#define LED 4 // 板子上的灯
#define DHTPIN 2 // what digital pin we're connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
float humi_read = 0, temp_read = 0;
int i=0,a;
void heartbeat()
{
HUMI.print(humi_read);
//反馈湿度数据
TEMP.print(temp_read);
}
void button2_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
digitalWrite(LED, !digitalRead(LED));
}
void button1_callback(const String & state)
{
// BLINKER_LOG("get button state: ", state);
if (state=="on") {
Serial.println("kai");
}
}
void button5_callback(const String & state)
{
// BLINKER_LOG("get button state: ", state);
if (state=="on") {
Serial.println("kaimen");
}
}
void button6_callback(const String & state)
{
// BLINKER_LOG("get button state: ", state);
if (state=="on") {
Serial.println("guanmen");
}
}
void button4_callback(const String & state)
{
// BLINKER_LOG("get button state: ", state);
if (state=="on") {
Serial.println("kaiqi");
}
}
void button3_callback(const String & state)
{
// BLINKER_LOG("get button state: ", state);
if (state=="on") {
Serial.println("guan");
}
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
a=12-i;
chewei.print(a);
if (Serial.available() > 0)//串口接收到数据
{
incomedate = Serial.read();//获取串口接收到的数据
if (incomedate == 'H')
{
Text6.print("苏E·82L84");
Serial.print("收到数据");
Serial.println(incomedate);
++i;
a=12-i;
chewei.print(a);
}
if (incomedate == 'Q')
{
Text2.print("苏E·42J68");
++i;
a=12-i;
chewei.print(a);
}
if (incomedate == 'W')
{
Text5.print("京K·98410");
++i;
a=12-i;
chewei.print(a);
}
if (incomedate == 'E')
{
Text10.print("京N·2B945");
++i;
a=12-i;
chewei.print(a);
}
if (incomedate == 'R')
{
Text2.print("2");
--i;
a=12-i;
chewei.print(a);
}
if (incomedate == 'T')
{
Text6.print("6");
--i;
a=12-i;
chewei.print(a);
}
if (incomedate == 'Y')
{
Text5.print("5");
--i;
a=12-i;
chewei.print(a);
}
if (incomedate == 'U')
{
Text10.print("10");
--i;
a=12-i;
chewei.print(a);
}
}
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Button1.attach(button1_callback);
Button4.attach(button4_callback);
Button5.attach(button5_callback);
Button6.attach(button6_callback);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW); // 初始化 led 高电平 ,则灯熄灭状态
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Blinker.attachHeartbeat(heartbeat);
}
void loop()
{
int p;
Blinker.run();
// read_data();
}
/*void read_data()
{
float read_h = dht.readHumidity();//湿度
float read_t = dht.readTemperature();//温度
humi_read = read_h;
temp_read = read_t;
}*/
三、现象
按下按键KEY0,发送H
H的ASCII码值是72,可以看到esp接收到了数据。
手机端
总结
其实串口2与串口1并没有太多的区别,只需要注意细节就可以了,手机app端前面提过好多次了,就不介绍了。