Introducción
La tecnología inalámbrica Bluetooth es una solución inalámbrica de corto alcance que permite la comunicación sin cables entre dispositivos digitales, como ordenadores o cámaras digitales. Esta tecnología funciona dentro de un rango de 10 metros, y no requiere que los usuarios orienten los dispositivos cara a cara como en las comunicaciones por infrarrojos.
El estándar Bluetooth es una norma internacional respaldada y utilizada por miles de empresas en todo el mundo.

Requisitos

BluetoothSerial.h
(incluido en las librerías de Industrial Shields)
Programación del PLC industrial
En los PLCs M-Duino WiFi/BLE hay 2 tarjetas diferentes en el controlador:
- Arduino Mega 2560
- móduo ESP32
Ambas placas necesitan ser programadas para poder programar correctamente el PLC para la automatización industrial. La ESP32 será la primera, así que vamos a entrar en ella.
✅ ESP32
Para cargar el código de abajo en el ESP32, ve a:
Tools --> boards --> Industrial Shields EPS32 board --> WiFi module
A continuación, elige el puerto correcto y utiliza el atajo Ctrl+U para cargar el código. Recuerda que necesitarás un cable micro-USB.
/*Copyright (c) 2019 Boot&Work Corp., S.L. All rights reservedThis program is free software: you can redistribute it and/or modifyit under the terms of the GNU Lesser General Public License as published bythe 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU Lesser General Public License for more details.You should have received a copy of the GNU Lesser General Public Licensealong 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#endifBluetoothSerial 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);}
Gracias a las placas de Industrial Shields, SimpleComm permite que las placas se comuniquen entre sí. Con este código, se crea un dispositivo Bluetooth con el nombre "ESPMDUINOtest" para que puedas conectarte a él con tu teléfono móvil. Además, cada vez que se reciba un mensaje a través de Bluetooth, el mensaje se almacenará en la variable de mensajes y luego se enviará a la placa M-Duino utilizando SimpleComm.
Además, cada vez que se envíe un mensaje desde el M-Duino al ESP32, se reenviará al teléfono.
✅ Arduino
El código de M-Duino es el siguiente. Como antes, vaya a:
Tools --> boards --> Industrial Shields boards --> M-Duino WiFi/BT family
Después de eso ve a:
Model --> Your PLC model
A continuación, carga el código utilizando un cable de tipo B-USB.
/*
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");
}
}
}
}
Este otro código hace 2 cosas diferentes al mismo tiempo:
- La primera es abrir todos los leds del PLC basado en código abierto posteriormente cada segundo.
- La segunda es imprimir mediante el puerto serie todas las cadenas que recibe de la placa ESP32 con SimpleComm.
Después de recibir la secuencia, se compara con 'STOP'. Si coincide y el 4º LED es ALTO, todos los LEDS se abren y el mensaje "W" se envía de vuelta al ESP32.
En resumen, ambos códigos hacen un minijuego para probar algunas características del controlador industrial M-Duino WiFi/BLE.
Pero, ¿cómo puedo enviar mensajes desde mi teléfono al PLC?, estarás pensando. Esta tarea se llevará a cabo mediante una aplicación Serial Bluetooth, como la que aparece en la imagen de la izquierda:
Después de instalar la aplicación, carga los códigos en el controlador industrial y establece una conexión con el dispositivo Bluetooth desde tu teléfono. A continuación, abre la app y establece una conexión con "ESPMDUINOtest".
Si lo has hecho todo bien, la pantalla debería mostrar la imagen correcta. Los mensajes azules son los que envías desde tu teléfono y los verdes son los que recibes.
Interactúa con un PLC industrial M-Duino WiFi/BLE a través de Bluetooth