Interact with M-Duino WiFi/BLE industrial PLC via Bluetooth

Learn how to work using M-Duino WiFi/BLE PLC and Bluetooth
September 30, 2022 by
Interact with M-Duino WiFi/BLE industrial PLC via Bluetooth
Boot & Work Corp. S.L., Bernat Brunet Pedra

Introduction

Bluetooth wireless technology is a short-range wireless technology that allows wireless data communication between digital devices, such as computers or digital cameras. This technology works within a range of 10 metres, and does not require users to orient devices face-to-face as in IR communications.

The Bluetooth standard is a supported international standard and is used by thousands of companies around the world.

Bluetooth wireless technology

Requirements

Programming the industrial PLC

In M-Duino WiFi/BLE PLCs there are 2 different boards in the controller:

  • Arduino Mega 2560
  • ESP32 module

Both boards need to be programmed in order to program the PLC for industrial automation correctly. The ESP32 will be the first one, so let's get into it.


✅ ESP32

To upload the code below to the ESP32, go to:

Tools --> boards --> Industrial Shields EPS32 board --> WiFi module 

Then choose the correct port and use the short-cut Ctrl+U to upload the code. Remember you will need a micro-USB cable.

/*
   Copyright (c) 2019 Boot&Work Corp., S.L. All rights reserved
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.
   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <SimpleComm.h>
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
String message = "";
char char_received; 
SimplePacket packet;
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(115200UL);
  SerialBT.begin("ESPMDUINOtest");
  Serial2.begin(115200UL);
  SimpleComm.begin();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  if (SerialBT.available()) {
    char_received = SerialBT.read();
  
    if (char_received != '\n'){
      message += String(char_received);
    }
    else {
      packet.setData(message);
      message = "";
      if (!SimpleComm.send(Serial2, packet, 0)) {
        Serial.println("Send packet error");
      }
    }
  }
  if (SimpleComm.receive(Serial2, packet)) {
    SerialBT.write(packet.getChar());
  }
  delay(20);
}

Thanks to Industrial Shields boards, SimpleComm allows the boards to communicate with each other. With this code, a Bluetooth device with the name "ESPMDUINOtest" is created so that you can connect to it with your mobile phone. Also, whenever a message is received through Bluetooth, the message will be stored in the message variable and then sent to the M-Duino board using SimpleComm.

In addition, every time a message is sent from the M-Duino to the ESP32, it will be forwarded to the phone.


✅ Arduino

The M-Duino code is as follows. As before, go to:

Tools --> boards --> Industrial Shields boards --> M-Duino WiFi/BT family

After that go to:

Model --> Your PLC model

Then upload the code using a B-USB type cable.

/*
   Copyright (c) 2019 Boot&Work Corp., S.L. All rights reserved
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.
   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <SimpleComm.h>
#include <WifiModule.h>
int leds[] = {Q0_0, Q0_1, Q0_2, Q0_3, Q0_4, Q0_5};
int select = 0;
void open_all(void){
  select = 0;
  while(select < 7){
    digitalWrite(leds[select], HIGH);
    select += 1;
  }
}
SimplePacket packet;
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(115200UL);
  WifiModule.begin(115200UL);
  SimpleComm.begin();
  Serial.println("Wifi Module started");
  pinMode(Q0_0, OUTPUT);
  pinMode(Q0_1, OUTPUT);
  pinMode(Q0_2, OUTPUT);
  pinMode(Q0_3, OUTPUT);
  pinMode(Q0_4, OUTPUT);
  pinMode(Q0_5, OUTPUT);
  
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  static uint32_t lastSent = 0UL;
  if (millis() - lastSent > 1000) {
    if (select<=6) {
      digitalWrite(leds[select-1], LOW);
      digitalWrite(leds[select], HIGH);
      select += 1;
    }
    else{
      select = 0;
    }
    lastSent = millis();
  }
  
  if (SimpleComm.receive(WifiModule, packet)) {
    const char *data = packet.getString();
    const char *match = 'STOP';
    Serial.println(data);
    
    if (strcmp(data, match) == 0 && digitalRead(Q0_3) == HIGH) {
      Serial.println("\nYOU HAVE WON");
      open_all();
      packet.setData("W");
      if (!SimpleComm.send(WifiModule, packet, 0)) {
        Serial.println("Send packet error");
      }
    }
  }
}

This other code does 2 different things at the same time:

  • The first one is to open all leds from the open source based PLC subsequently every second.
  • The second is to print using the Serial port all strings it receives from the ESP32 board with SimpleComm.

After receiving the string, it is compared with 'STOP'. If it matches and the 4th LED is HIGH, all LEDS are opened and the message "W" is sent back to the ESP32.

To sum up, both codes make a minigame to test some features from the M-Duino WiFi/BLE industrial controller.

How can I send messages from my phone to the PLC, you may be thinking. This task will be done using a Serial Bluetooth application, like the one in the picture on the left:

Send messages from phone to PLC

After installing the app, upload the codes to the industrial controller and establish a connection with the Bluetooth device from your phone. Then, open the app and establish a connection with "ESPMDUINOtest".

If everything has been done correctly, the screen should show the right picture. The blue messages are the ones you send from your phone and the green ones are those you receive.

Wifi module started

​Search in our Blog

Interact with M-Duino WiFi/BLE industrial PLC via Bluetooth
Boot & Work Corp. S.L., Bernat Brunet Pedra September 30, 2022
Share this post

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>