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, 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!
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
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- [National Technology N32 MCU Development Package] --N32G432 Series
- MSP430 MCU Example 18 - Timer A generates 4-channel periodic signals
- Low-cost wireless RF communication chip selection
- Does it cost a lot to buy an on-load tapchanger tester?
- Requires 3-way hardware SPI
- Matter and Wi-Fi 6: An ideal combination
- 【XMC4800 Relax EtherCAT Kit Review】+ Getting started with DAVE, simple application of ADC module
- Today's Live Broadcast | Cytech: Hidden Costs of Isolation System Design
- Half a year has passed, let’s share what we have gained in EEWORLD!
- MSP430F5529 generates PWM waves with CCS