Protocol TCP

Create a TCP connection from a device throught the network the device is currently connected to.

8 TCP can be used.
tcp0 to tcp7 can be used.

// Javascript Example

var tcp = obniz.getFreeTcp();
await tcp.connectWait(80, "obniz.io");
const request = ""
    + "GET / HTTP/1.1\r\n"
    + "Host: obniz.io\r\n"
    + "Accept: */*\r\n"
    + "Connection: close\r\n\r\n";
tcp.write(request)

obniz.repeat(async()=>{
    let data = await tcp.readWait();
    console.log(dataArray2string(data));
}, 1)

function dataArray2string(data) {
    let result = '';
    for (let n of data) {
        result += String.fromCharCode(n);
    }
    return result;
}

obniz.getFreeTcp()

Get TCP modules not used by obniz Board.
TCP can use 8 from tcp0 to tcp7,
You can get a TCP that is not in use by calling this function.

// Example
var tcp = obniz.getFreeTcp();

If no TCP is available, an exception is raised and the program stops.

// Example
var tcp0 = obniz.getFreeTcp();
var tcp1 = obniz.getFreeTcp();
var tcp2 = obniz.getFreeTcp();
var tcp3 = obniz.getFreeTcp();
var tcp4 = obniz.getFreeTcp();
var tcp5 = obniz.getFreeTcp();
var tcp6 = obniz.getFreeTcp();
var tcp7 = obniz.getFreeTcp();
var tcp8 = obniz.getFreeTcp(); // Error

connectWait(port, domain)

Starts a connection on the port and domain for which TCP is specified.。

// Javascript Example
var tcp = obniz.getFreeTcp();
tcp.connectWait(80,"obniz.io");

write(data)

The argument data is sent by TCP.

If you pass a string or Array type argument, the data will be sent.

// Javascript Example
var tcp = obniz.getFreeTcp();
tcp.connectWait(80,"obniz.io");

// Array
tcp.write([0,1,2,3,4]);

// Text
tcp.write('hello');

readWait

Wait for TCP reception.

// Javascript Example
var tcp = obniz.getFreeTcp();
tcp.connectWait(80,"obniz.io");

let data = await tcp.readWait();
console.log(data);

onreceive

Callback function is called when TCP is received.

// Javascript Example
var tcp = obniz.getFreeTcp();
tcp.connectWait(80,"obniz.io");

tcp.onreceive = data => {
    console.log(data);
};

onconnection

Callback function is called when there is a change in TCP connection status.

  • true : Connect
  • false : Disconnect
// Javascript Example
var tcp = obniz.getFreeTcp();

tcp.onconnection = data => {
    console.log(data);
};
tcp.connectWait(80,"obniz.io");

onerror

You can get the error message that occurred when connecting.

// Javascript Example
var tcp = obniz.getFreeTcp();
tcp.connectWait(80,"obniz.io");

tcp.onerror = state => {
    console.log(state);
};

end();

Terminates the TCP session.

// Javascript Example
var tcp = obniz.getFreeTcp();
tcp.end();

You will Get in Few Days

Circuit for Starter “obniz Board” is available on Amazon and other online stores.
You can get it at below

Our products and resellers

Forum

Visit our developer’s forum to discuss and discover technologies.

Forum

Contact

Feel free to contact out support and technical team.

Contact us