Android and snap7, simple example for communication between S7 PLC and Android

 

A very simple example how to read Data from Siemens PLC (S7-300 / S7-400) with Android Device.
in this example you need Android Studio and moka7 (is snap7 in Java)
you can download snap7 (and moka7) libx here: snap7

here a short and simple video

 

short description of steps in video:

  1. create new Android project
  2. add button and textview to the activity
    • Android and snap7 simple example for communication between S7 PLC and Android_place text view and a button
  3. add moka7 to the project
    • Android and snap7 simple example for communication between S7 PLC and Android_add moka7 to project
  4. write a simple java code
    • Android and snap7 simple example for communication between S7 PLC and Android_add_code
  5. run the project (or upload it to your device)
    • Android and snap7 simple example for communication between S7 PLC and Android_and run

finish

34 comments on “Android and snap7 simple example for communication between S7 PLC and Android

  • Hello plc2k,
    your video is very interesting”Communication between Android and Siemens PLC (S7-300/S7-400) with moka7 (snap7)”.
    It’s the first time I work on Android Studio and wanted to develop an app for remote control of a home (on / off illumination, presence, etc.), how can I do this?
    To communicate with the plc S7-1200 that changes have to do in the TIA Portal?
    Help me
    thank you

      • Hello, yes I have a home automation project in the TIA Portal (S7-1200 PLC).
        For example, I wanted to remotely control the lighting of my house through a android application (android studio), carried a button to turn on or turn off the lights.
        How can I do this with Moka 7? Can you give me an example?

        PS: In TIA Portal have already defined that access DB3.DBX0.0 asset to Q0.0 output (turn on lights)

        Help me
        thank you

        • here a simple example to write “true” of D3.DBX0.0:
          byte[] dataWrite = new byte[1];
          S7.SetBitAt(dataWrite,0,0,true);
          client.WriteArea(S7.S7AreaDB,3,0,1,dataWrite);

          • I want to thank the availability to help, it is an excellent person.
            The CODIG I have in the studio android is:
            com.example.marisa.myapplication package;

            import android.os.AsyncTask;
            import android.os.Bundle;
            // import android.support.design.widget.FloatingActionButton;
            // import android.support.design.widget.Snackbar;
            import android.support.v7.app.AppCompatActivity;
            import android.support.v7.widget.Toolbar;
            import android.view.View;
            // import android.view.Menu;
            // import android.view.MenuItem;
            import android.widget.TextView;

            Moka7 import. *;

            public class MainActivity extends AppCompatActivity {

                Override
                protected void onCreate (Bundle savedInstanceState) {
                    super.onCreate (savedInstanceState);
                    setContentView (R.layout.content_main);
                }
                S7Client S7Client client = new ();

                public void readdb_val (View view) {
                    new PlcReader () execute (“.”);

                }
                private class PlcReader extends AsyncTask {
                    String ret = “”;
                    Override
                    doInBackground protected String (String … params) {
                        try {

                            client.SetConnectionType (S7.S7_BASIC);
                            int res = client.ConnectTo (“192.168.0.1”, 0, 1); // S7-1200
                            if (res == 0) {// ok connection

                                byte [] DATAWRITE = new byte [1];
                                S7.SetBitAt (DATAWRITE, 0,0, true);
                                client.WriteArea (S7.S7AreaDB, 1,0,1, DATAWRITE);

                            } else {
                                        ret = “ERR” + S7Client.ErrorText (res);
                            }

                            client.Disconnect ();
                        } catch (Exception e) {
                            ret = “Exc” + e.toString ();
                            Thread.interrupted ();
                        }
                            return “executed”;
                    }
                    Override
                    protected void OnPostExecute (String result) {
                    TextView txout = (TextView) findViewById (R.id.textView);
                        txout.setText (ret);

                  }
                }
            }

            However, when I want to communicate with the PC, an error appears, TCP Connetion failed. How can I eliminate this error? the IP are ok …

            % DB3.DBX0.0 % Q0.0
            TIA Portal have _________ | | ______________ ( ) and changed the properties of the DB in order to be accessed.

  • Android Studio, the layout, the button to put, for example turn on a light, you have to change the specification “one click”, that is, I have to put writedb, but for that I have to create, right?
    Sorry but it is the first time I see this,
    best regards

    • how do you managed to communicate with your PLC ? what do you put excactely in your tia programme
      i developpe the Android App but i don’t know what to put i Tia to make communication
      i appreciate any idea for help
      thnaks

  • Hello again, does not have an Android Studio explanatory movie with Libnodave library has with the moka 7?
    The problem is this, my supervisor wanted that used to Libnodave but do not know which file should I import into Studio Android and how to realize a small program to activate a PLC output with one button.
    The ip or address (my.adress.home.dynip.sapo.pt) should be placed on the mobile phone not in code in android Studio, that is, when I start the application should ask to enter it.
    Best regards,
    Marisa

  • Hello,

    When i try to run the program, then I get an Error.
    This error is: “TCP Connection failed”
    The PLC is ok, the IP is also ok,…
    How can i solve this error.

    Best regards,
    Tom VdV

    • Hello, try to ping your PLC.
      1. Dont use Optimized Block Access in TIA.
      2. And set Full access of PLC.
      3. Set permit access with PUT/GET communication from remote partner.

  • Hello, and really enjoyed the video about moka7. I can read and write separately in the PLC, however, the problem is to combine the two, that is, read the DB2.0 (Boolean) and is not active, put it (writing) to true if it is already “true “appears a message saying it is already active or connected.

    I also have another question, for example lack energy, and I have some of the active PLC outputs, then they are disabled, how do I realize me that?

    written code:

    import android.os.AsyncTask;
    import android.os.Bundle;
    //import android.support.design.widget.FloatingActionButton;
    //import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    //import android.view.Menu;
    //import android.view.MenuItem;
    import android.widget.TextView;

    import Moka7.*;

    public class MainActivity extends AppCompatActivity {

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

    S7Client client = new S7Client();

    public void writedb_val(View view) {
    new PlcReader().execute(“”);

    }

    private class PlcReader extends AsyncTask {
    String ret = “”;

    @Override
    protected String doInBackground(String… params) {
    try {

    client.SetConnectionType(S7.S7_BASIC);
    int res = client.ConnectTo(“xxx.xxx.x.x”, 0, 1); // s7-1200

    if (res == 0) { //ok

    ret = “OK”;

    // byte[] data = new byte[1];
    // res = client.ReadArea(S7.S7AreaDB,2,0,1,data);
    // ret = “value of db100.dbd0–“+S7.GetBitAt(data, 0, 0);

    byte[] dataWrite = new byte[1];
    S7.SetBitAt(dataWrite, 0, 0, true);
    client.WriteArea(S7.S7AreaDB, 2, 0, 1, dataWrite);

    } else {
    ret = “ERR: ” + S7Client.ErrorText(res);
    }

    client.Disconnect();
    } catch (Exception e) {
    ret = “Exc: ” + e.toString();
    Thread.interrupted();
    }
    return “executed”;
    }

    @Override
    protected void onPostExecute(String result) {
    TextView txout = (TextView) findViewById(R.id.textView);
    txout.setText(ret);

    }
    }
    }

    Help me please,
    best regards

  • To read the state of DB and when I click the button puts (writes) the DB to “true” (active output) and press the button again puts the DB as “false”? I have to make a loop if, but how do it?

    Best regards

  • hello i’m using android studio 2.0 but i can’t follow the exmples how i make the file moka7

    best regards

  • Hey , please help me, how can I import moka 7 into android studio, please help me, I can´t do what yo do in the video, how can I paste moka7?

  • I appreciate your effort about explaining Moka7 ,it s very interresting video;
    just two simple questions:Can I put a Dsl router to connect my tablet to a real plc s7,and is it possible to simulate my plc program on TIA portal for testing my android application without real plc ?

  • Hi,
    Could you please release your these files as txt files or something. I’ts very difficult to copy this of the video or pictures due to low res. Or zip as a Android Studio import 🙂 Pretty Please

  • Hello, this code does not work in “Android Studio 2.1.” Help me please.

    package com.example.wis.myapplication;

    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.TextView;

    import Moka7.*;

    public class MainActivity extends AppCompatActivity {

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

    S7Client client = new S7Client();
    public void readdb_val(View v){
    //new PlcReader().execute(“”);
    }

    private class PlcReader extends AsyncTask{
    String ret=””;

    @Override
    protected String doInBackground(String… params){
    try{
    client.SetConnectionType(S7.S7_BASIC);
    int res = client.ConnectTo(“192.168.6.205”,0,2);//S7-300

    if(res==0) {//connection ok

    byte[] data = new byte[4];
    res = client.ReadArea(S7.S7AreaDB, 7, 0, 4, data);//I want to read DB7.DBD0 (it is a real)
    ret = “value of DB.DBD0: ” + S7.GetFloatAt(data,0);
    }
    else{
    ret = “ERR: “+S7Client.ErrorText(res);
    }
    client.Disconnect();
    }catch (Exception e){
    ret=”EXC: “+e.toString();
    Thread.interrupted();
    }
    return “executed”;
    }

    @Override
    protected void OnPostExecute(String result){
    TextView txout = (TextView) findViewById(R.id.textView);
    txout.setText(ret);
    }
    }

    }

    best regards

  • Hey. I have difficulties with the transfer of data from Android to the controller I need to button deliver data to edit text in s7-300 controller db100 dbd12

  • Dear PLC2K / All,

    I have used the video for my project. Simply used the code to connect a Samsung smartphone with an 1516-3 PN/DP CPU. The following can be done with success:

    I can ping the PLC and the smartphone.
    1. Dont use Optimized Block Access in TIA.
    2. And set Full access of PLC.
    3. Set permit access with PUT/GET communication from remote partner.

    Still I am getting the ERR: TCP Connection failed message when pushing the READ VAL button.

    Can somebody help me with this issue?

      • If you give me an e-mail address then I can send you a wetransfer link with the project. The TIA project is made for a customer and contents knowledge that we want to protect. Therefor I will password protect the FC’s and FB’s in this project.

  • Unfortunately I did not receive an answer yet. Is the support of this project not available anymore?

  • hi buddy help me to connect 100 plc.s to android to know about each is turn on time process to turn off complete process and to know which one is off and why

Leave a Reply

Your email address will not be published. Required fields are marked *