LED wireless control realized by 51 single chip microcomputer and esp8266 routine

First, on the wiring aspects of 51 single-chip and wireless WIFI module

RXD, TXD, GND and 51 single-chip TXD, RXD, GND are connected, the other pins of the module are high level, the power supply VCC is about 3.3V (two 1.5v dry batteries), I am lazy because there is no voltage regulator module At that time, using a 5V DC source, the test results are currently no problem (but not recommended!)

Second, on the Android phone

It can be used during the test. The network debugging assistant (download this app installed on the mobile phone) can send the number 1 to indicate that the small light is on, and 0 to indicate that the light is off. If you want to go deeper, you can develop your own Android app.

Third, the parameter settings

Due to the parameter setting method and the stepping online information is very complete, here is the relevant website to provide your own reference.

Uart-WiFi module ESP8266 puts the game http://

The STA and AP modes of the Internet of Things wifi module work in detail http://

Fourth, the information may have deficiencies, just for reference!

/************************************************* ********************

Title: Based on STC12C560S2 MCU Wireless WIFI Module Control

Content: Control and set the ESP8266wifi module through the MCU host computer to realize the mobile terminal control LED light

The light is off.

Note: This type of MCU is 12 times faster than the ordinary 89C51 single chip. It contains 1280 bytes of RAM, 60K of ROM, PCA counter, PWM generation module, ADC conversion module, etc. The crystal oscillator must be 11.0592MHz.

************************************************** ******************/

#include "reg52.h" //Include header files

#define uint unsigned int

#define uchar unsigned char

#define RELOAD_COUNT 0xFA //The macro defines the load value of the baud rate generator

//define RELOAD_COUNT (256-(((11520000/16))))

/*****************LED light corresponds to 1 port of P0 port*************/

Sbit LED0=P3^2;

/************Baud rate generator related function register definition ******************/

Sfr AUXR=0x8E;

Sfr BRT=0x9C;

Sfr AUXR1=0xA2;

/*****************Related variables **************/

Uchar Receive,i;

Uint n;

Uchar Recive_table[15]; / / used to receive data from the wifi module feedback to the MCU

/************************************************* ******************

Name: Delay function

Function: millisecond delay, subtle delay function, waiting for data transmission and completion. . . . . . . . .

************************************************** ******************/

Void ms_delay(uint t)

{

Uint i,j;

For(i=t;i"0;i--)

For(j=110;j”0;j--);

}

Void us_delay(uchar t)

{

While(t--);

}

/************************************************* *******************

Name: Baud Rate Generator Function

Function: The baud rate generator can be implemented by the T1 timer or it can be an independent baud rate generator inside the MCU.

Different load value calculation formulas, refer to the calculation according to the register related settings, to achieve

Asynchronous serial communication. (After testing, both settings are available, you can choose one.)

************************************************** ******************/

Void Uart_Init()//Use timer 1 as the baud rate generator (STC89C52, STC89C51, AT89C51 or STC12C560S2, etc.)

{

SCON=0x50; //Set to serial port to work in mode 1, 8-bit asynchronous communication, allowing reception interrupt.

// One frame of information is 10 bits, 1 bit start bit, 8 data bits (low bit first), 1 stop bit.

PCON=0x80; //SMOD baud rate selection bit is 1, SMOD=1.

TMOD=0x21; //Set timer 1 to baud rate generator, work in mode 2, 8-bit auto load

TH1=RELOAD_COUNT;//baud rate 9600, TH1=256-FOSC/16/12/ baud rate

TL1=TH1;

EA=1; //Total interrupt open

ES=0; //Close the serial port interrupt

TR1=1; //Start timer 1

}

/*

Void Uart_Init()//Use a separate baud rate generator (STC12C560S2 or single-chip microcomputer with independent baud rate generator //, / /)

{

SCON=0x50; //Set to serial port to work in mode 1, 8-bit asynchronous communication, allowing reception interrupt.

PCON=0x80; //SMOD baud rate selection bit is 1, SMOD=1.

BRT=RELOAD_COUNT;//baud rate 9600 256-crystal frequency/baud rate x16=BRT

AUXR=0xD5; //T0, T1 are not divided by 12, the speed is 12 times of 89C51, the independent baud rate//generator is started, and each clock is counted once.

/ / Set the port 1 of the baud rate generator as an independent baud rate generator,

AUXR1=0x80; //Switch to P1 port

ES=1; //Open serial port interrupt

EA=1; //turn on the total interrupt

}

*/

/************************************************* *******************

Name: Serial port send function

Function: The MCU sends data to other connected devices (here is the wireless WIFI module ESP8266)

************************************************** ******************/

Void Send_Uart(uchar value)

{

ES=0; //Close the serial port interrupt

TI=0; //clear the completion of the interrupt request flag

SBUF=value; //send

While(TI==0); //waiting to send

TI=0; //clear the completion of the interrupt request flag

ES=1; //Allow serial port interrupt

}

/************************************************* *******************

Name: WIFI module setting function

Role: Start the module so that wireless access and control can be achieved

************************************************** ******************/

Void ESP8266_Set(uchar *puf) // array pointer *puf points to a string array

{

While(*puf!='\0') // encountered a space to jump out of the loop

{

Send_Uart(*puf); //Send control instructions to the WIFI module.

Us_delay(5);

Puf++;

}

Us_delay(5);

Send_Uart('');//Enter

Us_delay(5);

Send_Uart(''); //Line break

Ms_delay(1000);

}

/************************************************* ***

Name: ESP8266 send data function

Function: used to send data to the terminal connected to the wifi module

************************************************** ***/

Void ESP8266_Sent(uchar *puf) // array pointer *puf points to a string array

{

ESP8266_Set("AT+CIPSEND=0,4");

While(*puf!='\0') // encountered a space to jump out of the loop

{

Send_Uart(*puf); //Send control instructions to the WIFI module.

Us_delay(5);

Puf++;

}

Us_delay(5);

Send_Uart(''); //Line break

Ms_delay(10);

}

/************************************************* *******************

Name: main function

Role: the execution portal of the program

************************************************** ******************/

Void main()

{

LED0=0; / / turn off the LED light

Uart_Init();//Use a separate baud rate generator

ESP8266_Set("AT+CWMODE=2"); //Set router mode 1 staTIon mode 2 AP

/ / Point router mode 3 station + AP mixed mode

ESP8266_Set ("AT + RST"); / / restart the wifi module

ESP8266_Set("AT+CWSAP=\"WIFI\",\"1234567890\",11,4");

/ / Set the module SSID: WIFI, PWD: password and security type encryption mode (WPA2-PSK)

ESP8266_Set ("AT+CIPMUX=1"); / / enable multi-connection mode, allowing multiple clients to access

ESP8266_Set("AT+CIPSERVER=1,5000"); //Start the TCP/IP port to 8080 to implement network-based//control

ES=1; //Allow serial port interrupt

While(1)

{

If((Recive_table[0]=='+')&&(Recive_table[1]=='I')&&(Recive_table[2]=='P'))//When the data received by the MCU is +IPD, enter Judge control 0\1 to make the small light on and off

{

If((Recive_table[3]=='D')&&(Recive_table[6]==','))

{

If(Recive_table[9]=='0')

{

LED0=0; //0 light off

ESP8266_Sent ("light off");

// wifi module sends "lights off" to the pc or mobile phone

}

Else if (Recive_table[9]=='1')

{

LED0=1; //1 light is on

ESP8266_Sent ("lights on");

// wifi module sends "lights on" to pc or mobile phone

}

}

}

}

}

/************************************************* ********************

Name: Serial communication interrupt

Function: enter the function after the end of sending or receiving, clear the corresponding flag bit software, realize the module logarithm

According to normal transmission and reception.

************************************************** ******************/

Void Uart_Interrupt() interrupt 4

{

Static uchari=0;

If(RI==1)

{

RI=0;

Receive=SBUF; //MCU receives the data returned by the wifi module

Recive_table=Receive;

i++;

If((Recive_table[i-1]==''))i=0; //receive a newline reload value

}

Else TI=0;

}

Windshield Cell Phone Mount

Windshield Cell Phone Mount,Car Windscreen Mobile Phone Holder,Car Windshield Cell Phone Holder,Car Windscreen Cell Phone Holder

Ningbo Luke Automotive Supplies Ltd. , https://www.car-phone-holder.com

Posted on