51 MCU controls four relays through WIFI module ESP8266

Publisher:太和清音Latest update time:2021-07-27 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. System Solution

The mobile phone APP controls four relays through the ESP8266 WIFI module and the 51 single-chip microcomputer. The lower computer consists of the single-chip microcomputer, ESP8266 module and relay module, and the upper computer is the Android mobile phone APP. We send the switch control command of the relay on the APP, and ESP8266 sends the received data to the single-chip microcomputer to realize the switch control of the relay.

2. Hardware Design

The ESP8266 module is used as a transparent transmission module. RXD and TXD are connected to the TXD and RXD of the 51 microcontroller respectively. The VCC and EN pins are connected to 3.3V voltage, and GND is grounded. Only these pins need to be connected, and the ESP8266 module can work normally.


The P2^0, P2^1, P2^2, and P2^3 outputs of the microcontroller are four relays for high and low battery control. The relay module is a pre-soldered module purchased from the Internet, and the other places are manually welded on a multi-purpose board.

3. MCU Software Design

The MCU code mainly includes serial port initialization, ESP8266 initialization and serial port interruption.


1. Serial port and ESP8266 initialization:


/**

* Send a single character

*/

void sendChar(uchar a)

{

SBUF = a;

while(TI==0);

TI=0;

}

 

/**

* Send a string

*/

void sendString(uchar *s)

{

while(*s!='')

{

sendChar(*s);

s++;

}

}

 

/**

* Initialize the ESP8266 module

*/

void initEsp()

{

TMOD=0x20; //Timer 1 works in mode 2

TH1 = 0xfd; //Baud rate 9600

TL1 = 0xfd;

SM0=0; //Serial port works in mode 1

SM1=1;

EA = 1; // Enable general interrupt

REN = 1; // Enable the serial port

TR1 = 1; //Timer 1 starts timing

delayms(200);

sendString("AT+CWMODE=2rn"); //AP mode

delayms(200);

sendString("AT+CIPMUX=1rn"); //Allow multiple connections

delayms(200);

sendString("AT+CIPSERVER=1rn"); //Establish TCP Server

delayms(200);

ES = 1; //Open serial port interrupt

}

sendString("AT+CWMODE=2rn") ----- The MCU sends AT commands to the ESP8266 module. AT+CWMODE=2 sets the ESP8266 to AP mode. rn is a line break, because AT commands must be followed by a line break to take effect.

sendString("AT+CIPMUX=1rn") ---- Allow multiple connections

sendString("AT+CIPSERVER=1rn") ---- Establish TCP Server


2.Serial port interrupt function, responsible for processing the instructions sent by App to the microcontroller:


/**

* Serial port interrupt function, responsible for processing the instructions sent by App to the microcontroller

*/

void uart() interrupt 4

{

if(RI == 1)   

  {

    RI = 0; // Clear the serial port receiving flag

receiveTable[i]=SBUF;

if(receiveTable[0]=='+')

{

i++;

}

else

{

i=0;

}

if(i==10)

{

i=0;

switch(receiveTable[9])

{

case '1': //Turn on the relay

JDQ4=0;

break;

case '2': //turn off the relay

JDQ4=1;

break;

case '3':

JDQ3=0;

break;

case '4':

JDQ3=1;

break;

case '5':

JDQ2=0;

break;

case '6':

JDQ2=1;

break;

case '7':

JDQ1=0;

break;

case '8':

JDQ1=1;

break;

}

}

  }

}

The data format when esp8266 receives data and forwards it to the microcontroller is: +IPD, , : received character, such as +IPD, 0, 5: hello, where +PID is fixed; 0 represents the TCP client number, esp8266 supports up to 5 clients connected at the same time, that is, the client number is 0 to 4. In this design, since there is only one client connected to esp8266, the client number is 0; 5 represents the received character length; hello is the received character. In this example, the data sent by esp8266 to the microcontroller is +IPD, 0, 1: 1. We cache the received string into the character array, so in the logic of processing the received data, first determine whether it starts with '+', otherwise it is regarded as invalid data, and then determine the tenth data in the array, because the tenth data is the data sent by the host computer.


4. Android APP software design

The Android APP was developed with the help of Android Studio, and the interface is relatively fresh. The default IP address of esp8266 is 192.168.4.1, and the port number is 333. Four switches control four relays, and you can edit the switch name by long pressing the switch name. The screenshot of the APP interface is shown below:

 The button click callback method responsible for connecting to ESP8266:


/**

 * Connection button click event callback method

 * @param v

 */

@Override

public void onClick(View v) {

    if(v.getId()==R.id.btn_connect){

        if (mSocket == null || !mSocket.isConnected()) {

            new Thread(){

                @Override

                public void run() {

                    try {

                        mSocket = new Socket("192.168.4.1", 333);

                        out = new PrintStream(mSocket.getOutputStream());

                        runOnUiThread(new Runnable() {

                            @Override

                            public void run() {

                                mBtnConnect.setText("disconnect");

                            }

                        });

                        new HeartBeatThread().start();

                    } catch (IOException e) {

                        e.printStackTrace();

                        runOnUiThread(new Runnable() {

                            @Override

                            public void run() {

                                Toast.makeText(MainActivity.this, "Connection failed", Toast.LENGTH_SHORT).show();

                            }

                        });

                    }

                }

            }.start();

        }

        if (mSocket != null && mSocket.isConnected()) {

            try {

                mSocket.close();

                mBtnConnect.setText("connect");

                mSocket = null;

            } catch (IOException e) {

                e.printStackTrace();

                mSocket = null;

            }

        }

    }

}

The slide switch click callback method sends instructions to the microcontroller to control the switch of the relay:


/**

 * The slide button monitors events and sends instructions to the microcontroller to control the relay switch

 * @param buttonView

 * @param isChecked

 */

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    switch (buttonView.getId()) {

        case R.id.switch1:

            if (isChecked) {

                //turn on

                Log.d(TAG, "onCheckedChanged: send1");

                sendData("1");

            } else {

                //turn off

                Log.d(TAG, "onCheckedChanged: send2");

                sendData("2");

            }

            break;

        case R.id.switch2:

            if (isChecked) {

                //turn on

                Log.d(TAG, "onCheckedChanged: send3");

                sendData("3");

            } else {

                //turn off

                Log.d(TAG, "onCheckedChanged: send4");

                sendData("4");

            }

            break;

        ....

....

....

           

    }

}


End of this article!

Reference address:51 MCU controls four relays through WIFI module ESP8266

Previous article:51 MCU controls LED lights through WIFI module ESP8266
Next article:51 single chip microcomputer DHT11 temperature and humidity detection mobile phone Bluetooth APP display design

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号