Communication between host computer and mcu in the design of West Power Project

I'm a classmate of grade 19. Let me introduce our project: intelligent garbage sorting trash can.

Because the hardware issued by the school is only color sensor, garbage classification is actually color classification, and color sensor is a necessary device.

You should pay attention to several points: 1 First of all, the devices issued by the college should be used, and no points will be deducted.

2. You can add anything innovative. For example, our group used cameras to identify garbage, which is an innovation. However, because we forgot the requirements of the college and didn't use color sensors, the teacher deducted points, but the display effect was good at the later expert meeting and won the first prize.

3. Most of the students in the upper computer are old boards. The new boards obtained by individual groups are not easy to use. The version is too high (just can't use) and can communicate with the teacher.

I put the operation flow of my team below to facilitate you to understand the communication between the host computer and mcu

I create an activity. Post the new steps below:

project → app → src → main → Java, then right-click and select create Empty Activity according to your file name, and the layout corresponding to the java file will be directly generated together, as shown in the figure:

Layout in the layout in res under the main file is the selected xml file.

According to the project requirements, the function codes realized by the upper computer are mainly divided into:

1. Interface jump

2. Communication with mcu (user mode and maintenance mode)

I first watch the video of swc010 senior students in station B, have the sample, and then modify the content to meet the communication of our group. We must watch the video first, follow step by step, copy the sample to the java file that needs communication, and then understand how to change the code to meet the communication requirements according to the following comments.

Video link: https://www.bilibili.com/video/BV1Xb41177wR

My modified code is as follows:

package com.example.teamworks;//This is a newly created project. You can ignore the name

import android.content.Intent;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.friendlyarm.FriendlyThings.HardwareControler;

import java.util.Timer;
import java.util.TimerTask;//Don't move all the import s above

//The green one is the java file name. You should change the one in the sample to your own
public class UseChinese1Activity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_back;
    private Button btn_call;
    private Button btn_put, btn_radio;
    private TextView t1, t2, t3, t4;
    public static int a = 100, b = 100, c = 100, d = 100;
    private MediaPlayer mp = new MediaPlayer();
    private MediaPlayer mp1 = new MediaPlayer();
    private Button play;
    //The above private variables need to be defined by yourself

    private final int MAXLINES = 200;
    private StringBuilder remoteData = new StringBuilder(256 * MAXLINES);

    private String devName = "/dev/ttyAMA3"; //ttyAMA3-->UART3
    private int speed = 115200;
    private int dataBits = 8;
    private int stopBits = 1;
    private int devfd = -1;
    private String str = "";//This part has the definitions of baud rate, data bit, check bit and stop bit. This corresponds to the students in charge of mcu


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_use_chinese1);


        String winTitle = devName + "," + speed + "," + dataBits + "," + stopBits;
        setTitle(winTitle);

        btn_back = (Button) findViewById(R.id.back2);
        btn_back.setOnClickListener(this);
        btn_call = (Button) findViewById(R.id.call_btn2);
        btn_call.setOnClickListener(this);
        btn_put = (Button) findViewById(R.id.put_btn2);
        btn_put.setOnClickListener(this);
        btn_radio = (Button) findViewById(R.id.shuoming2);
        btn_radio.setOnClickListener(this);
        //Link the variables in the java file with the controls in the layout through ID
        devfd = HardwareControler.openSerialPort(devName, speed, dataBits, stopBits);
        if (devfd >= 0) {
            timer.schedule(task, 0, 100);
        } else {
            devfd = -1;
        }

    }


//if statement determines whether the control is clicked and triggers a response
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back2) {
            //Jump to the main interface
            Intent intent = new Intent(UseChinese1Activity.this,LoadingChineseActivity.class);
            startActivity(intent);
        }
//Interface jump, Intent is jump instruction, XXX this,XXX. Calss refers to jumping from this interface to class interface. XXX is the java file name


            if (v.getId() == R.id.put_btn2) {
                String str_0 = "b";
                if (str_0.length() > 0) {
                    int ret = HardwareControler.write(devfd, str_0.getBytes());
                    if (ret > 0) {
                        remoteData.append(str_0);
                    }
                }
                remoteData.append(str_0);
            }
//Click the button to send data to mcu. In the example, "b" is sent

        if (v.getId() == R.id.shuoming2) {
            Button play = findViewById(R.id.shuoming2);
            final MediaPlayer mp = MediaPlayer.create(this, R.raw.help);
            play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) { mp.start(); }
            });
        }
    }
//Click the button to play the voice

//The following is how the host computer receives the data sent by mcu and responds accordingly
    private final int BUFSIZE = 512;
    private byte[] buf = new byte[BUFSIZE];
    private Timer timer = new Timer();
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    if (HardwareControler.select(devfd, 0, 0) == 1) {
                        int retSize = HardwareControler.read(devfd, buf, BUFSIZE);
                        if (retSize > 0) {
                            String str = new String(buf, 0, retSize);
                            remoteData.append(str);//These do not need to be changed, which means that the data received from mcu is stored in the variable name str
                          switch (str) {
                            case "k"://If K is received, execute the following instructions
                                Toast.makeText(UseEnglishActivity.this, "recyclable garbage", Toast.LENGTH_SHORT).show();
                                //Toast is a pop-up window that displays "recyclable garbage"
                      
                                break;
                            case "i":
                                Toast.makeText(UseEnglishActivity.this, "hazardous waste", Toast.LENGTH_SHORT).show();
                            
                                break;
                            case "o":
                                Toast.makeText(UseEnglishActivity.this, "other rubbish", Toast.LENGTH_SHORT).show();
                             
                                break;
                            case "v":
                                Toast.makeText(UseEnglishActivity.this, "kitchen rubbish", Toast.LENGTH_SHORT).show();
                               
                                break;
                            case "l":
                                Toast.makeText(UseEnglishActivity.this, "Please keep a distance over 10 cm!",                                                 Toast.LENGTH_SHORT).show();
                                break;

                        }

                    }
                }
                break;
        }
        super.handleMessage(msg);
    }
};

    private TimerTask task = new TimerTask() {
        public void run() {
            Message message = new Message();
            message.what = 1;
            handler.sendMessage(message);
        }
    };




   public void onDestroy() {
        timer.cancel();
        if (devfd != -1) {
            HardwareControler.close(devfd);
            devfd = -1;
        }
        super.onDestroy();
        if (mp != null) {
            mp.stop();
            mp.release();
        }
        super.onDestroy();
        if (mp1 != null) {
            mp1.stop();
            mp1.release();
        }
    }
    public void onPause(){
        if (devfd != -1) {
            HardwareControler.close(devfd);
            devfd = -1;
        }
        super.onPause();
    }
    public void onStop(){
        if (devfd != -1) {
            HardwareControler.close(devfd);
            devfd = -1;
        }
        super.onStop();
    }
}//Just copy these

One thing to pay attention to is that my communication is to send and receive letters. I've tried numbers before. I don't know why it's not very good. You can try. If you can't, be honest and use letters.

package com.example.teamworks;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RepairLoadingEnglishActivity extends AppCompatActivity implements View.OnClickListener {

    private Button tv_back;
    private EditText et_user_name;
    private EditText et_psw;
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair_loading_english);

        tv_back = findViewById(R.id.tv_back2);
        tv_back.setOnClickListener(this);
        btn_login = findViewById(R.id.btn_login2);
        et_user_name = findViewById(R.id.et_user_name);
        et_psw = findViewById(R.id.et_psw);

        btn_login.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.btn_login2){
            String username = et_user_name.getText().toString();
            String password = et_psw.getText().toString();
            Intent intent = null;
            //Pop up content
            String ok = "successfully";
            String fail = "please try it again";


            if(username.equals("089")&&password.equals("1")){
               Intent intent1 = new Intent(RepairLoadingEnglishActivity.this, RepairActivity.class);
                startActivity(intent1);
                //eject
                Toast.makeText(getApplicationContext(),ok,Toast.LENGTH_SHORT).show();
            }else{
                //Incorrect toast
                Toast.makeText(getApplicationContext(),fail,Toast.LENGTH_SHORT).show();

            }
        }
               //Login is mainly the setting of user name and password
        if (v.getId()==R.id.tv_back2){
            Intent intent2 = new Intent(RepairLoadingEnglishActivity.this, LoadingEnglishActivity.class);
            startActivity(intent2);
        }


    }



}

Added by Fsoft on Thu, 20 Jan 2022 10:46:08 +0200