B4R Question How to comunicate with Node Red?

ivan.tellez

Active Member
Licensed User
Longtime User
It is easy to use B4RSerializator for comunicating with another B4X apps, but, for home automation (as other many things), there is already a good back end. In this case, why to reinvent the wheel with B4J when Node Red is easy to use, powerful and a free option.

The best protocol to comunicate is with MQTT but, for the message, we have no B4RSerializator in Node Red (nor other non B4X sofware). Node Red has a great support for Json, but we have no Json lib for B4R.

Have someone els implement json in B4R (send and receive)? Are there other optios?


@Erel are there plans to release an official Json Lib for B4R? Can you provide implementations of B4RSerializator for other tools like Node Red (Javascript)? https://nodered.org/

Thanks
 

ivan.tellez

Active Member
Licensed User
Longtime User
Parsing or creating simple JSON strings is quite simple with ByteConverter features. Post an example of the json that you need to parse and I'll write an example.

Generally speaking, JSON is not a very good protocol for devices with low memory.


Hi

I intend to make a Json Like this:

B4X:
{
  "id": "84F3EBE3621A",
  "name": "Kitchen",
  "get_status": "0,1,1,1,0,1,1,0",
  "set_status": "0,1,1,1,0,1,1,0",
  "get": {
    "index": 8,
    "value": 0
  },
  "set": {
    "index": 8,
    "value": 0
  }
}

I intend to read 8 switches and set 8 outputs, so, the esp82666 should receive a Json via MQTT, to get or set the status of one or all the outputs, and also send a Json when a switch is presed and another when is released.



Yes, maybe Json is not the best for the device's resources, but, it is a standard.
Since NodeRed is becoming a standard for IOT, also, consider to launch a B4RSerializator for it.

Thanks
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Yes, maybe Json is not the best for the device's resources, but, it is a standard.
Since NodeRed is becoming a standard for IOT, also, consider to launch a B4RSerializator for it.
I'm out of my territory here, but reading up on Node-RED, JSON is used on the platforms that actually can run Node.js and Node-RED. Communications with Arduino on the other hand happens via Firmata, a binary based protocol. This would align with @Erel's statement
Generally speaking, JSON is not a very good protocol for devices with low memory.
Looks like various transports can be used for Firmata (Serial, Raw TCP, MQTT), etc. Source for the client library of Firmata exist in various languages (none for B4R). For your purposes, you would not need to implement the complete library, just the parts you need (to set/read pins). In order to decode / encode the Firmata packets, you would use B4RSerializator. Now that sounds like a lot of work, but that would make it the most compatible with Node-RED when it comes to communicating with Arduino (with JSON not being the protocol for Arduino communications in regards to Node-RED).

Sources:
https://nodered.org/docs/faq/interacting-with-arduino
https://github.com/firmata/protocol
https://github.com/firmata/arduino#cloning-firmata
https://flows.nodered.org/node/node-red-contrib-gpio
 
Upvote 0

ivan.tellez

Active Member
Licensed User
Longtime User
I'm out of my territory here, but reading up on Node-RED, JSON is used on the platforms that actually can run Node.js and Node-RED. Communications with Arduino on the other hand happens via Firmata, a binary based protocol. This would align with @Erel's statement

Firmata is not part of Node-Red, there are contributions that add compatibility. Firmata is not really intended as a comunication protocol (in the sense of exanging information beetween apps), it is intended to be the entire firmare of the board just to give direct control of the IO pins to an external app, so the microcontroller has no logic. Shure, you can also implement it partially and have logic in the microcontroller and have comunication with the external app with Firmata.sendString(stringToSend), but, you end with the exact same thing as Json.

On other hand, Just read the first lines of your sources: "There are several ways to interact with an Arduino using Node-RED. They all assume the Arduino is connected to the host computer via a USB serial connection." The TCP and MQTT optios are done with a serial emulation.

So, for multiple devices with its own logic, conected over the network, the best option is JSON, unless @Erel makes a B4RSerializator for Node Red.
 
Upvote 0
Top