Transmit/Receive Datas

Let's create an extension using a specific plugin.
We will use Uart to create a program that sends data to obnizCloud only when there is an error.

By using the plugin, we can reduce the communication capacity, which has been difficult with obniz so far, and eliminate data loss at high communication speeds.

Sending and receiving data

The plugin includes

  • Send data to obnizCloud
  • Receiving data from obnizCloud

There are two functions.

On the plugin side, you can send and receive data as follows.

#include <obniz.h>

// on data received from obnizCloud.
void onCommand(uint8_t* data, uint16_t length){
  Serial.println("\nonCommand");
  Serial.write(data,length);
}

void setup() {
  Serial.begin(115200);
  obniz.start();

  // receive data from obnizCloud
  obniz.commandReceive(onCommand);
}

void loop() {
  if (obniz.isOnline()) {
    uint8_t *text = "Hello";
    obniz.commandSend(text, strlen(text)); // send data to obnizCloud
    delay(1 * 1000);
  }
}

You can also use obniz.js to send and receive data to and from plugins as follows.

// receive from target device
obniz.plugin.onreceive = data => {
  console.log(data);
};

// send to target device
obniz.plugin.send("obniz.js send data get device?")

Example of using UART

In this example, we use the obniz board as the communication partner for the Uart, but in actual use, it will be the respective device.

First, let's look at the plug-in code.
When the obniz board is online, it reads the Uart and sends a message as a for correct data and b for abnormal data.

The connection method is as follows.

ESP32-DevKitC obniz Board
16(Rx) 0(TX)
GND 3(GND)
#include <obniz.h>
void setup() {
  Serial.begin(115200);
  obniz.start();

  //RX:16 TX:17
  obniz.pinReserve(16);
  obniz.pinReserve(17);
  Serial2.begin(115200);
}

void loop() {
  if (obniz.isOnline()) {
    if (Serial2.available()) {
      uint8_t inByte = Serial2.read();
      Serial.write(inByte);
      if(inByte != 'a'){
        Serial.print(" Error");
        obniz.commandSend((uint8_t*)&inByte,1);
      }
      Serial.println("");
    }
  }
}

Next comes the obniz.js code.
There are two buttons, one sends the Uart correctly, and the other generates an error. When an error occurs, the console is notified that an error has occurred.

You can also check the serial monitor in the Arduino IDE to see the data received.

<div id="obniz-debug"></div>
<div class="uart">
<h3 class="text-center">Uart Serial</h3>
<button style="width:49%"class="btn btn-primary" id="send_ok">OK Uart A send</button>
<button style="width:49%" class="btn btn-outline-primary" id="send_error">Error Uart B send</button>
</div>

<script>
    //test obniz
    let obniz = new Obniz("obnizID");
    obniz.onconnect = async function () {
        obniz.uart0.start({tx: 0, rx: 1, gnd:2, baud:115200, drive:"3v" });

        $("#send_ok").click(function() {
            obniz.uart0.send("a");
        });

        $("#send_error").click(function() {
            obniz.uart0.send("b");
        });
    };

    let plugin = new Obniz("plugin_obnizID");
    plugin.onconnect = async function () {
        plugin.plugin.onreceive = data => {
            console.log(`uart error data : ${data}`);
        };
    };
</script>

This is a test using a button, but I hope you get the idea.
If we program the plugin, we can easily change the processing depending on the data and send a message to obniz.js.

In this way, communication capacity can be reduced and data can be exchanged at high speed. Please try using plugins.

関連の質問