Introducción
En este post, se explicará cómo configurar el sistema de comunicación entre un Ardbox WiFi & BT Family PLC Arduino y el Módulo WiFi. Para ello, utilizaremos un protocolo de comunicación llamado SimpleComm. Hay un ejemplo práctico al final de este post.
Tenemos otro post donde explicamos cómo funciona el módulo ESP-32 según su Datasheet:
Post: Wifi / Bluetooth Module ESP32 Devkit v1
Aquí tienes un enlace a cada ficha técnica:
Ficha técnica de Ardbox Relay WiFi
Software:
Debes tener la Placas Industrial Shields instaladas (minimo version 1.1.17 )
Explicación SimpleComm
SimpleComm is available here: Aquí puedes ver cómo descargar esa librería.
Para ver cómo funciona SimpleComm, echa un vistazo al siguienteblog post.
Ejemplos Arduino IDE
En primer lugar, tenemos un código de ejemplo para M-Duino shield donde inicializamos los datos, creamos y enviamos un paquete, haciendo la función de Tx (Master).
En esta, se utiliza la librería WifiModule.h para acceder al puerto serie para la comunicación interna entre el PLC industrial Arduino y el módulo WiFi. Esta librería se descarga una vez que se han descargado las placas Industrial shields.
Aquí tienes el código del PLC Wifi y BLE (Para compilar este sketch a tu módulo PLC, por favor conecta el PLC a tu ordenador conectando el cable micro USB al puerto micro USB de la zona superior):
/* 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> typedef struct { char name[15]; uint8_t input; uint16_t value; } data_t; SimplePacket packet; data_t data; //////////////////////////////////////////////////////////////////////////////////////////////////// void setup() { Serial.begin(9600L); WifiModule.begin(115200UL); SimpleComm.begin(); Serial.println("Wifi Module started"); } //////////////////////////////////////////////////////////////////////////////////////////////////// void loop() { static uint32_t lastSent = 0UL; if (millis() - lastSent > 1000) { lastSent = millis(); // Init data sprintf(data.name, "ABCD"); data.input = 7; data.value = analogRead(I0_7); // Create packet from data packet.setData(&data, sizeof(data)); // Send packet if (!SimpleComm.send(WifiModule, packet, 0)) { Serial.println("Send packet error"); } } }
1.-Selecciona Herramientas > Placa > Familia Ardbox WiFi/BT.
2.-Selecciona Herramientas > Industrial shields > Modelo: "Selecciona tu propio PLC WiFi/BT M-Fuino":
3.-Selecciona Herramientas > Puerto > El puerto donde está conectado tu dispositivo (en este caso es el COM22).
4.- Subir el programa. Pulsa sobre la flecha situada en la parte superior izquierda de la ventana.
Placa ESP-32
Centrándonos en este shield, tenemos otro código de ejemplo llamado 'esp32' donde recibimos el paquete previamente creado en el código 'mega', haciendo la función Rx (Slave). En este se utiliza el Serial2 como puerto para la comunicación (Serial2 del Módulo WiFi).
Aquí tienes el código 'esp32', para cargar este sketch en tu módulo WiFi, por favor conecta el cable micro USB a la toma micro USB del lado izquierdo:
/* 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> typedef struct { char name[15]; uint8_t input; uint16_t value; } data_t; SimplePacket packet; char json[100]; //////////////////////////////////////////////////////////////////////////////////////////////////// void setup() { Serial.begin(115200UL); Serial2.begin(115200UL); SimpleComm.begin(); Serial.println("esp32 started"); } //////////////////////////////////////////////////////////////////////////////////////////////////// void loop() { if (SimpleComm.receive(Serial2, packet)) { const data_t *data = (const data_t *) packet.getData(); if (data != nullptr) { // Print received data Serial.print("Received data: "); Serial.print(data->name); Serial.print("; "); Serial.print(data->input); Serial.print("; "); Serial.print(data->value); Serial.println(); // Create JSON from data values sprintf(json, "{\"name\":\"%s\",\"input\":%d,\"value\":%d}", data->name, data->input, data->value); Serial.print("JSON: "); Serial.println(json); } } }
Si quieres cargar este código en tu Ardbox tienes que seguir estos pasos:
1.-Selecciona Herramientas > Placa > Módulo WiFi.
1.-Selecciona Herramientas > Placa > Módulo WiFi.
Sistema de comunicación para PLC industrial Ardbox WiFi & BT