Communication System between M-Duino and WiFi Module

July 8, 2019 by
Communication System between M-Duino and WiFi Module
Boot & Work Corp. S.L., Quesada Dani Salvans

Introduction

This post will be explained how to set the communication system between M-Duino and Module WiFi. To achieve this, we will use a communication protocol called SimpleComm. There is a practical example at the end of the post.

We have another post where we explain how the module ESP-32 works according to its Datasheet:

Post: Wifi / Bluetooth Module ESP32 Devkit v1

Here you have a link to each Datasheet:

M-Duino Datasheet

ESP32 Series Datasheet

Software:

You must have the Industrial Shields Boards installed (minimum 1.1.17 version)


SimpleComm Explanation

SimpleComm is available here: Here you can see how to download that library. 

To see how SimpleComm works take a look at the next blog post. 

Arduino IDE examples

WiFi & BLE PLC Board

First of all, we have an example code for M-Duino shield where we initialize the data, create and send a packet, making the Tx function (Master).

In this one is used the WifiModule.h library to access the serial port for the internal communication between the PLC and the WiFi module.

Here you have the Wifi & BLE PLC code (To compile this sketch to your PLC module, please connect the PLC to your computer by connecting the micro USB cable to the top zone micro USB port):


/*
   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"); } } }

If you want to upload this code on your M-Duino you have to follow these steps:

 
 1.-Select Tools > Board > M-Duino WiFi/BT Family.


2.-Select Tools > Industrial Shields > Model: "Select your own WiFi/BT M-Duino PLC":


3.-Select Tools > Port > The port where you have your device connected (in this case is COM72).

4.-Upload the program. Click on the arrow located on the left top of the window.


ESP-32 Board

Focusing on this shield, we have another example code called 'esp32' where we receive the packet previously created in the 'mega' code, making the Rx function (Slave). This one is used Serial2 as a port for communication (Serial2 of the WiFi Module).

Here you have the 'esp32' code, in order to upload this sketch on your WiFi Module, please connect the micro USB wire to the left-side micro USB socket:

/*
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); } } }

If you want to upload this code on your M-Duino you have to follow these steps: 

1.-Select Tools > Board > WiFi module.
 

2.-Select Tools > Port > The port where you have your device connected (In this case COM43).


3.-Upload the program. Click on the arrow located on the left top of the window.


If you want to check if the communication is working you can follow the next steps:

1.-click on the Serial Monitor icon located in the right top corner of the window. Make sure that you have configurated the port to which you have connected the ESP32. 



2.-Make sure to configure the baudrate to 115200 to sync up with the serial port. If everything is working right, you will see the recieved data, name, input and value as you can see in the image below:




​Search in our Blog

Communication System between M-Duino and WiFi Module
Boot & Work Corp. S.L., Quesada Dani Salvans July 8, 2019

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 >>>