2201 views|3 replies

2

Posts

0

Resources
The OP
 

esp8266 cannot transfer data to APP [Copy link]

I hope to use esp8266 to display data on the APP. The current situation is that WIFI can establish a connection with the APP, and the serial port data can be received through the computer, but the data cannot be sent to the mobile phone through wifi. How can I solve this problem? (The computer's transmission line and esp8266 use the same serial port)
The relevant programs are as follows:

MCU main function:

#include<reg52.h>
#include"dht11.h"
#include "string.h"
#define uchar unsigned char
#define uint unsigned int
unsigned char idata Rxbuff[50],Rxnum;
extern unsigned char F16T,F16RH; //Temperature and humidity data
sbit LED1=P2^2;
sbit LED2=P2^4;
unsigned char getflag;//Get flag
void InitUART(void);
void delayms(unsigned int x);
void UART_send_byte(char dat);
void ESP8266_SERVER(void);
void Send_Str(unsigned char *buf);

void main()
{
unsigned char Tx_Buf[10];
delayms(500);
delayms(1000); //Delay for a while to let the WIFI module stabilizeInitUART
(); //Initialize the serial portESP8266_SERVER
(); //Initialize ESP8266
LED1=0;
while(1)
{
getDHT11(); //Get the temperature and humidity
valuesTx_Buf[0]=F16T/10%10+0x30; //Frame
headerTx_Buf[1]=F16T%10+0x30; //Send the temperature and humidity data to the sending array and send it to the module for display on the mobile phone
APPTx_Buf[2]= ';';
Tx_Buf[3]=F16RH/10%10+0x30;
Tx_Buf[4]=F16RH%10+0x30;
Send_Str("AT+CIPSEND=0,5\r\n");
delayms(300);
Send_Str(Tx_Buf);//Send dataLED2
=0;

}
}
void InitUART(void) //This is the basic configuration of the serial port, and configures its baud rate to 9600. These parameters are standard.
{
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xFD;
TL1 = TH1;
PCON = 0x00;
EA = 1;
ES = 1;
TR1 = 1;
}
void delayms(unsigned int x)
{
unsigned int i;
while(x--)
for(i=125;i>0;i--);
}
//UART sends one byte
void UART_send_byte(char dat)
{
SBUF = dat; //Send
while (TI == 0); //Wait for sending to be completed
TI = 0; //Clear the interrupt request flag when sending is completed
}
//UART sends a string
void Send_Str(unsigned char *buf)
{
while (*buf != '\0')
{
UART_send_byte(*buf++);
}
}

void ESP8266_SERVER(void)//Establish server
{
ES = 0;
TI = 1;
Send_Str("AT\r\n");
delayms(500);
Send_Str("AT\r\n");
delayms(500);
Send_Str("AT+CWMODE=2\r\n"); //The server is built on the WIFI module
delayms(500);
Send_Str("AT+CWSAP=\"WiFi_mushu\",\"12345678\",5,3\r\n"); //Set the display name: wifi, password: 12345678
delayms(500);
Send_Str("AT+CIPMUX=1\r\n");//Start multiple connections, both servers need to be configured
delayms(500);
Send_Str("AT+CIPSERVER=1,5000\r\n");//Establish server
delayms(500);
Send_Str("AT+CIPSTO=0\r\n");
TI = 0;
ES = 1;
}


APP related program
package com.example.wifi_mushu;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint ; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.EditText; roid.widget.Toast ; import java.io.BufferedReader; import java.io.IOException; importimportjava.io.InputStreamReaderimport java.io.OutputStream ; import java.net.InetAddress ; import java.net.Socket; import java.net.UnknownHostException; public class MainActivity extends AppCompatActivity { //Definethecontrol EditText edGateIp , edGateSn ; Button btnSearch , btnLogin ; //Message mechanismprivateHandler messageHandler ; // OpenasocketSocket socket = null ; OutputStream OutputStream = null ; //Define data output stream for sending dataBufferedReader bufferedReader ; //Declare input stream objectInputStream InputStream = null ; // Definedata input stream for receiving data//Definelogical variables for judging server connectionstatusbooleanisConnected = false ;


































// Used to control whether the data reading thread is executed boolean RD = false ; // Test area Button fs ; EditText sjfs , sjjs ; @SuppressLint ( "HandlerLeak" ) @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout. activity_main ); // Control binding edGateIp = findViewById(R.id. edGateIP ); edGateSn = findViewById(R.id. edGateSn ); btnSearch = findViewById(R.id. btnSearch ); btnLogin = findViewById(R.id. btnLogin ); // Display control final TextView wendu = (TextView)findViewById(R.id. wendu ); final TextView shidi = (TextView)findViewById(R.id. shidu ); final TextView kongqi = (TextView)findViewById(R.id. kongqi ); // Auto-fill button event btnSearch .setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // Automatically fill in the server's fixed IP address and port edGateIp .setText( "192.168.4.1" ); edGateSn .setText( "5000" ); } }); // Connect to server button event btnLogin .setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // Fault tolerance mechanism // Take out the text box content to determine whether the input box is empty String ip = edGateIp .getText().toString(); String sn = edGateSn .getText().toString(); if ((ip == null || ip.length() == 0 ) && (sn == null || sn.length() == 0 )) { Toast.makeText (MainActivity.this , " IP address and port number cannot be empty " , Toast.LENGTH_SHORT ).show(); } else if (ip == null || ip.length () == 0 ) {












































Toast.makeText (MainActivity.this , " IPaddress cannot be empty", Toast.LENGTH_SHORT ).show(); } else if (sn == null || sn.length() == 0 ) { Toast.makeText (MainActivity.this , " Portnumber cannot be empty" , Toast.LENGTH_SHORT ) .show(); } else { //Judge server connection statusif(isConnected != true ) { //Createa newSocket ( in thread mode) connectionnewClientThread ().start(); //Button text changebtnLogin.setText ( "Disconnect" ); //Page messageToast.makeText ( MainActivity.this , " Serverconnection successful" , Toast.LENGTH_SHORT ) .show(); //Secondjudgment, whether the server is connectedif ( socket == null ) { //Ifnot connected, the button text is changed to connect to the server, and the page message promptsbtnLogin.setText( " Connectto server" ); Toast.makeText (MainActivity.this , " Connectionerror, please checkthe WiFiis connected, andwhetherthe IP and port are entered correctly!",Toast.LENGTH_SHORT ) .show(); } } else { //The server is disconnected when the button is pressedif( socket ! = null ) { try { //Exit the serversocket.close (); //Server status is emptysocket= null ; // Serverconnection state changes to emptyisConnected= false ; // Readdata thread does not executeRD = false ; btnLogin .setText( "Connect to server" ); //Pagetext displayToast.makeText ( MainActivity.this , " Disconnectedfrom the server!",Toast.LENGTH_SHORT ) .show();

































} catch (IOException e) {
e.printStackTrace();
}
// } } } } }); //Message processing mechanismmessageHandler = new Handler() { //Wait forsocketconnection to succeedpublic void handleMessage(android.os.Message msgMessage) { String receiveString = "" ; switch (msgMessage. what ) { case1 : receiveString = msgMessage. obj .toString(); String temp[] = DataString. split ( ";" ); wendu .setText(temp[ 0 ]); shidi .setText(temp[ 1 ]); kongqi .setText(temp[ 2 ]); } } }; } //Use threads to createSocketconnections. Threads are not allowed to updateUI(implemented usingHandler)public class ClientThread extends Thread { public void run() { //Define two variables to storeIPand port number InetAddress GateIp; int GateSn; try { //Judgesockedto prevent repeated executionif( socket == null ) { //Get the inputIPaddressGateIp = InetAddress.getByName ( edGateIp .getText ().toString()); //Get the inputportGateSn = Integer.parseInt ( edGateSntoString ()); //Create a newsocketconnectionsocket = new Socket(GateIp, GateSn); //Getthe socketand receive dataInputStream= socket .getInputStream (); //Get the input stream and outputstreambufferedReader = new BufferedReader( new InputStreamReader( socket .getInputStream())); OutputStream = socket .getOutputStream(); //
















































The received data can be used in the main thread or directly operated in this thread char [] buffer = new char [ 265 ]; // Define an array to receive input stream data String bufferString = "" ; // Define a character to receive string array data int count = 0 ; // Initialize the length of the buffer array to 0 int tag = 0 ; // The initial write position of the array // Indicates that the server has been established and the connection isConnected = true ; // Infinite loop to repeatedly receive input stream data and process it while ( true ) { // When the length of the input stream written to the buffer array is greater than 0 , the data has been received while ((count = bufferedReader .read(buffer)) > 0 ) { // Write all the data in the buffer array into the bufferString character type while (tag < buffer.length ) { bufferString = bufferString + buffer[ tag ]; tag++; } // Give the data to messageHandler to refresh the UI interface Message msgMessagr = new Message(); msgMessagr.obj = bufferString; msgMessagr.what = 1 ; messageHandler .sendMessage(msgMessagr); // Initialize data in order to process the next input stream information tag = 0 ; bufferString = "" ; } } } // Error prompt //UnknownHostExceptionDNS resolution error //IOException read and write file exception } catch (UnknownHostException e) { // Print the exception information on the command line, the location and cause of the error in the program e.printStackTrace(); } catch (IOException e) { // Print the exception information on the command line, the location and cause of the error in the program e.printStackTrace(); } } } }










































chuankou.jpg (143.7 KB, downloads: 0)

chuankou.jpg

danpianj.jpg (168.9 KB, downloads: 0)

danpianj.jpg

Screenshot_20200413-084926.png (93.8 KB, downloads: 0)

Screenshot_20200413-084926.png
This post is from 51mcu

Latest reply

Hello! 1. First, select the correct baud rate and complete WIFI initialization 2. The hardware crystal oscillator uses 11.0592Mhz, and the serial port error is small 3. The number of bytes sent to the APP must be correct to send all the data 4. This is an APP. Take a look at the effect. [attach]471280[/attach] can receive data through APP and display data curve at the same time   Details Published on 2020-4-18 22:38
 

1w

Posts

25

Resources
2
 

Please post the key points of the problem.

This post is from 51mcu
 
 
 

2

Posts

0

Resources
3
 
dcexpert posted on 2020-4-13 11:34 Please post the key points of the problem

The data sent by wifi cannot be received by APP, but the AT commands sent by wifi can be received

This post is from 51mcu
 
 
 

11

Posts

0

Resources
4
 

Hello!

1. First, select the correct baud rate and complete WIFI initialization

2. The hardware crystal oscillator uses 11.0592Mhz, and the serial port error is small

3. The number of bytes sent to the APP must be correct to send all the data

4. This is an APP. Take a look at the effect.

can receive data through APP and display data curve at the same time

This post is from 51mcu
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list