Look for in our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

Communication System between M-Duino and WiFi Module

Introduction

In 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 its Datasheet:

Post: Wifi / Bluetooth Module ESP32 Devkit v1

Here you have a link to each Datasheet:

M-Duino Datasheet

ESP32 Series Datasheet


SimpleComm Explanation


SimpleComm is available in the Tools40 library. Here you can see how to download that library. 

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

Arduino Examples

M-Duino Board

First of all, we have an example code for M-Duino shield called 'mega' where we inicialize the data, create and send a packet, making the Tx function (Master). In this one is used the Serial1 as a port for the communication.

Here you have the 'mega' code:

/*
   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;
data_t data;

////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(9600L);
  Serial1.begin(115200UL);
  SimpleComm.begin();

  Serial.println("mega 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(Serial1, 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 Family (or your working shield).

2.-Select Tools > Industrial Shields > M-Duino 21+ (or your working shield model).


3.-Select Tools > Port > COM64 (or the port where you have your device connected).

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 recieve the packet previously created in the 'mega' code, making the Rx function (Slave). In this one is used the Serial2 as a port for the communication.

Here you have the 'esp32' code:

/*
   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 > DOIT ESP32 DEVKIT V1 (or your working shield model).

 

2.-Select Tools > Port > COM43 (or the port where you have your device connected).


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 which you have connected the ESP32.