Hardware components | ||||||
![]() |
| × | 1 | |||
| × | 1 | ||||
Software apps and online services | ||||||
| ||||||
This projectis about controlling an LED by sending command from TCP server on a local area network. An ESP8266 wireless module is connected to USART6 of STM32 F407. This wireless module serves as station connected to a wireless router, and it will achieve wireless communication with my laptop, which is another station connected to same wireless router. A TCP server is built up on my laptop by using a network debugging tool called “TCP/UDP socket debugging tool”, and command will be sent to ESP8266 through this socket tool.
The ESP8266 is manipulated through AT instruction set, thus the MCU will send specific string through serial interface, which is USART6, to initialize ESP8266. After ESP8266 being initialized, user can send command from TCP server. “ON” is the command turn on the LED, and “OFF” is the command turn off the LED. ESP8266 will transmit whatever received to MCU through USART6 as well, and MCU will process command and perform the corresponding action.
//this file is open-source technical support from Alientek
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);
}
//this file is open-source technical support from Alientek
#ifndef __LED_H
#define __LED_H
#include "sys.h"
#define LED0 PFout(9)
#define LED1 PFout(10)
void LED_Init(void);
#endif
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
int main(void)
{
u8 len;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
LED_Init(); //LED1 is connected to PF9 and LED2 is connected to PF10
usart6_init();// usart6 is using PC6 and PC7
while(1)
{
if(usart6_switch==2){
for (len=0;len<usart6_cnt;len++){
USART_GetFlagStatus(USART6,USART_FLAG_TC);
USART_SendData(USART6, usart6_buff[len]);
while(USART_GetFlagStatus(USART6,USART_FLAG_TC)!=SET);
}
GPIO_ResetBits(GPIOF,GPIO_Pin_10);
delay_ms(1000);
GPIO_SetBits(GPIOF,GPIO_Pin_10);
//ESP8266 send ture information received from ninth byte
if (usart6_buff[8]==*"O"&&usart6_buff[9]==*"N"){
GPIO_ResetBits(GPIOF,GPIO_Pin_9);
}
if(usart6_buff[8]==*"O"&&usart6_buff[9]==*"F"&&usart6_buff[10]==*"F"){
GPIO_SetBits(GPIOF,GPIO_Pin_9);
}
usart6_switch=0;
usart6_cnt=0;
firstline=0;
}
}
}
void usart6_init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6,ENABLE);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_USART6);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_USART6);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = ESP8266_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(USART6, &USART_InitStructure);
USART_Cmd(USART6, ENABLE);
USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
esp_init();
}
void esp_init(void) {
int t;
u8 start[]="AT+CIPSTART=\"TCP\",\"192.168.1.6\",8500\r\n";//connect to TCP server
u8 send[]="AT+CIPSEND=5\r\n";//define length of string to send
u8 flag[]="READY\r\n";//confirm connection established
for(t=0;t<39;t++){
USART_GetFlagStatus(USART6,USART_FLAG_TC);
USART_SendData(USART6, start[t]);
while(USART_GetFlagStatus(USART6,USART_FLAG_TC)!=SET);
printf("%c",start[t]);
}
delay_ms(1000);
for(t=0;t<15;t++){
USART_GetFlagStatus(USART6,USART_FLAG_TC);
USART_SendData(USART6, send[t]);
while(USART_GetFlagStatus(USART6,USART_FLAG_TC)!=SET);
}
delay_ms(1000);
for(t=0;t<5;t++){
USART_GetFlagStatus(USART6,USART_FLAG_TC);
USART_SendData(USART6, flag[t]);
while(USART_GetFlagStatus(USART6,USART_FLAG_TC)!=SET);
}
}
uint16_t usart6_buff[30];// record information received via usart 6
int usart6_switch=0;
int usart6_cnt=0;//count string length
int firstline=0;//esp8266 will start to transmit information with a new line
void USART6_IRQHandler(void) {
uint16_t temp=0;
if(USART_GetITStatus(USART6, USART_IT_RXNE) == SET){
temp=USART_ReceiveData(USART6);
if(usart6_switch==1){
if(firstline==1){usart6_switch=0;}
else{
if(temp==0x0a){
usart6_switch=2;
}
}
}
if(temp ==0x0d){
usart6_switch=1;
firstline++;
}
if(usart6_switch==0){
usart6_buff[usart6_cnt]=temp;
usart6_cnt++;
}
}
}
#ifndef __USART_H
#define __USART_H
#include "stdio.h"
#include "stm32f4xx_conf.h"
#include "sys.h"
#define ESP8266_bound 115200//bound rate
extern uint16_t usart6_buff[30];
extern int usart6_switch;
extern int usart6_cnt;
extern int firstline;
void usart6_init(void);
void esp_init(void);
#endif
#include "delay.h"
#include "sys.h"
//this file is open-source technical support from Alientek
//the function this file is providing delay function
#if SYSTEM_SUPPORT_OS
#include "includes.h"
#endif
static u32 fac_us=0;
#if SYSTEM_SUPPORT_OS
static u16 fac_ms=0;
#endif
#if SYSTEM_SUPPORT_OS
#ifdef OS_CRITICAL_METHOD
#define delay_osrunning OSRunning
#define delay_ostickspersec OS_TICKS_PER_SEC
#define delay_osintnesting OSIntNesting
#endif
#ifdef CPU_CFG_CRITICAL_METHOD
#define delay_osrunning OSRunning
#define delay_ostickspersec OSCfg_TickRate_Hz
#define delay_osintnesting OSIntNestingCtr
#endif
void delay_osschedlock(void)
{
#ifdef CPU_CFG_CRITICAL_METHOD
OS_ERR err;
OSSchedLock(&err);
#else
OSSchedLock();
#endif
}
void delay_osschedunlock(void)
{
#ifdef CPU_CFG_CRITICAL_METHOD
OS_ERR err;
OSSchedUnlock(&err);
#else
OSSchedUnlock();
#endif
}
void delay_ostimedly(u32 ticks)
{
#ifdef CPU_CFG_CRITICAL_METHOD
OS_ERR err;
OSTimeDly(ticks,OS_OPT_TIME_PERIODIC,&err);
#else
OSTimeDly(ticks);
#endif
}
void SysTick_Handler(void)
{
HAL_IncTick();
if(delay_osrunning==1)
{
OSIntEnter();
OSTimeTick();
OSIntExit();
}
}
#endif
void delay_init(u8 SYSCLK)
{
#if SYSTEM_SUPPORT_OS
u32 reload;
#endif
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
fac_us=SYSCLK;
#if SYSTEM_SUPPORT_OS
reload=SYSCLK;
reload*=1000000/delay_ostickspersec;
fac_ms=1000/delay_ostickspersec;
SysTick->CTRL|=SysTick_CTRL_TICKINT_Msk;
SysTick->LOAD=reload;
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk;
#endif
}
#if SYSTEM_SUPPORT_OS
void delay_us(u32 nus)
{
u32 ticks;
u32 told,tnow,tcnt=0;
u32 reload=SysTick->LOAD;
ticks=nus*fac_us;
delay_osschedlock();
told=SysTick->VAL;
while(1)
{
tnow=SysTick->VAL;
if(tnow!=told)
{
if(tnow<told)tcnt+=told-tnow;
else tcnt+=reload-tnow+told;
told=tnow;
if(tcnt>=ticks)break;
}
};
delay_osschedunlock();
}
void delay_ms(u16 nms)
{
if(delay_osrunning&&delay_osintnesting==0)
{
if(nms>=fac_ms)
{
delay_ostimedly(nms/fac_ms);
}
nms%=fac_ms;
}
delay_us((u32)(nms*1000));
}
#else
void delay_us(u32 nus)
{
u32 ticks;
u32 told,tnow,tcnt=0;
u32 reload=SysTick->LOAD;
ticks=nus*fac_us;
told=SysTick->VAL;
while(1)
{
tnow=SysTick->VAL;
if(tnow!=told)
{
if(tnow<told)tcnt+=told-tnow;
else tcnt+=reload-tnow+told;
told=tnow;
if(tcnt>=ticks)break;
}
};
}
void delay_ms(u16 nms)
{
u32 i;
for(i=0;i<nms;i++) delay_us(1000);
}
#endif
#ifndef _DELAY_H
#define _DELAY_H
#include <sys.h>
//this file is open-source technical support from Alientek
//the function this file is providing delay function
void delay_init(u8 SYSCLK);
void delay_ms(u16 nms);
void delay_us(u32 nus);
#endif
#include "sys.h"
//this is open-source technical support from Alientek
//the main function of this file is initialize system clock and set ram address
__asm void WFI_SET(void)
{
WFI;
}
__asm void INTX_DISABLE(void)
{
CPSID I
BX LR
}
__asm void INTX_ENABLE(void)
{
CPSIE I
BX LR
}
__asm void MSR_MSP(u32 addr)
{
MSR MSP, r0 //set Main Stack value
BX r14
}
#ifndef __SYS_H
#define __SYS_H
#include "stm32f4xx.h"
//this is open-source technical support from Alientek
//the main function of this file is initialize system clock and set ram address
#define SYSTEM_SUPPORT_OS 0
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
#define GPIOA_ODR_Addr (GPIOA_BASE+20) //0x40020014
#define GPIOB_ODR_Addr (GPIOB_BASE+20) //0x40020414
#define GPIOC_ODR_Addr (GPIOC_BASE+20) //0x40020814
#define GPIOD_ODR_Addr (GPIOD_BASE+20) //0x40020C14
#define GPIOE_ODR_Addr (GPIOE_BASE+20) //0x40021014
#define GPIOF_ODR_Addr (GPIOF_BASE+20) //0x40021414
#define GPIOG_ODR_Addr (GPIOG_BASE+20) //0x40021814
#define GPIOH_ODR_Addr (GPIOH_BASE+20) //0x40021C14
#define GPIOI_ODR_Addr (GPIOI_BASE+20) //0x40022014
#define GPIOA_IDR_Addr (GPIOA_BASE+16) //0x40020010
#define GPIOB_IDR_Addr (GPIOB_BASE+16) //0x40020410
#define GPIOC_IDR_Addr (GPIOC_BASE+16) //0x40020810
#define GPIOD_IDR_Addr (GPIOD_BASE+16) //0x40020C10
#define GPIOE_IDR_Addr (GPIOE_BASE+16) //0x40021010
#define GPIOF_IDR_Addr (GPIOF_BASE+16) //0x40021410
#define GPIOG_IDR_Addr (GPIOG_BASE+16) //0x40021810
#define GPIOH_IDR_Addr (GPIOH_BASE+16) //0x40021C10
#define GPIOI_IDR_Addr (GPIOI_BASE+16) //0x40022010
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n)
#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n)
#define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n)
#define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n)
#define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n)
#define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n)
#define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n)
#define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n)
#define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n)
#define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n)
#define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n)
#define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n)
#define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n)
#define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n)
#define PHout(n) BIT_ADDR(GPIOH_ODR_Addr,n)
#define PHin(n) BIT_ADDR(GPIOH_IDR_Addr,n)
#define PIout(n) BIT_ADDR(GPIOI_ODR_Addr,n)
#define PIin(n) BIT_ADDR(GPIOI_IDR_Addr,n)
void WFI_SET(void);
void INTX_DISABLE(void);
void INTX_ENABLE(void);
void MSR_MSP(u32 addr);
#endif







Comments