数据上传
前言
最近做项目需要将32的数据上传到手机app,很明显需要用ESP8266,使用串口2遇到了一些问题,就将它记录下来。
一、STM32串口配置
这里是串口一的配置,用的APB2, 串口二用的APB1,这个没有注意,一直不能发数据。使用的时候要注意这个地方。 贴出STM32发送端的代码。
usart.c
#include "sys.h"
#include "usart.h"
#if SYSTEM_SUPPORT_OS
#include "includes.h"
#endif
#if 1
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
};
FILE __stdout;
_sys_exit(int x)
{
x = x;
}
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);
USART1->DR = (u8) ch;
return ch;
}
#endif
#if EN_USART1_RX
u8 USART_RX_BUF[USART_REC_LEN];
u16 USART_RX_STA=0;
u16 USART2_RX_STA=0;
u8 USART2_RX_BUF[USART2_REC_LEN];
void uart_init(u32 bound){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
void USART1_IRQHandler(void)
{
u8 Res;
#if SYSTEM_SUPPORT_OS
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(USART1);
if((USART_RX_STA&0x8000)==0)
{
if(USART_RX_STA&0x4000)
{
if(Res!=0x0a)USART_RX_STA=0;
else USART_RX_STA|=0x8000;
}
else
{
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
OSIntExit();
#endif
}
void uart2_init(u32 bound){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
void USART2_IRQHandler(void)
{
u8 Res,t;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(USART2);
if((USART2_RX_STA&0x8000)==0)
{
if(USART2_RX_STA&0x4000)
{
if(Res!=0x0a)USART2_RX_STA=0;
else USART2_RX_STA|=0x8000;
}
else
{
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
#define EN_USART1_RX 1
#define USART2_REC_LEN 200
#define EN_USART2_RX 1
extern u8 USART_RX_BUF[USART_REC_LEN];
extern u16 USART_RX_STA;
extern u8 USART2_RX_BUF[USART2_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);
uart2_init(115200);
LED_Init();
KEY_Init();
while(1)
{
LED1=1;
delay_ms(500);
LED1=0;
delay_ms(500);
if(KEY0==0)
{
USART_SendData(USART2, *U[0]);
}
}
}
二、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);
char auth[] = "**********";
char ssid[] = "Mr.Robot";
char pswd[] = "**********";
#define TEXTE_6 "tex-lf6"
BlinkerText Text6(TEXTE_6);
BlinkerButton Button2("btn-wfu");
BlinkerButton Button3("btn-pk8");
BlinkerButton Button1("btn-j1q");
BlinkerButton Button4("btn-tbk");
BlinkerButton Button5("btn-2us");
BlinkerButton Button6("btn-thr");
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
#define DHTTYPE DHT11
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)
{
if (state=="on") {
Serial.println("kai");
}
}
void button5_callback(const String & state)
{
if (state=="on") {
Serial.println("kaimen");
}
}
void button6_callback(const String & state)
{
if (state=="on") {
Serial.println("guanmen");
}
}
void button4_callback(const String & state)
{
if (state=="on") {
Serial.println("kaiqi");
}
}
void button3_callback(const String & 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);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Blinker.attachHeartbeat(heartbeat);
}
void loop()
{
int p;
Blinker.run();
}
三、现象
按下按键KEY0,发送H H的ASCII码值是72,可以看到esp接收到了数据。
手机端
总结
其实串口2与串口1并没有太多的区别,只需要注意细节就可以了,手机app端前面提过好多次了,就不介绍了。
|