Nocan.receiveMessage(&msg) 250 ms delay

Started by jameswhiteman

jameswhiteman

Hi Alain,

So our board (node) is using the same STM32 chip, and maybe a higher pin count SAMD board, but other than that we have taken the compiled node code and dropped it on the STM32 chip (The hardware is not really my field)
The issue we are facing is we want two way comms to the board. This is so we can send commands from a browser application to the device. We are using the nocan server to achieve this. The problem we are facing is that on setup we subscribe to a channel and then once in the main program, every loop we subscribe to Nocan.receiveMessage(&msg).
Some rough recieveMessage code ::

void receiveMsg() {
    if(Nocan.receiveMessage(&msg)== 0){
        Serial.println("Nocan Message :: ");
        Serial.print("node ID and ChannelID : ");
        Serial.print(msg.node_id);
        Serial.println(msg.channel_id);
        Serial.print("Data Length : ");
        Serial.println(msg.data_len);

        Serial.print("Data : ");
        Serial.print(msg.data_len);
        for ( int a = 0; a < msg.data_len; a = a + 1 ){
            Serial.print(msg.data[a]);
        }
        Serial.println();
    }
}

Problem is this takes 250 ms to respond even if returning no message. Is this correct operation? is it something wrong with our board setup/my code, (everything else is working fine, uploads, sending nocan messages)
Our looptime with out this is at worst 4 ms.

Kind Regards,

James

Alain

Hi James,
Nocan.receiveMessage waits up to 250ms before timing out if no message is received (in fact it would make more sense if it blocks indefinitely). If a message is available it should return immediately.

The function Nocan.receivePending() can be used to check if a message is available before calling Nocan.receiveMessage, thereby avoiding a busy wait.
e.g.

 1void receiveMsg() {
 2    if (Nocan.receivePending())
 3    {
 4        if(Nocan.receiveMessage(&msg)== 0){
 5            Serial.println("Nocan Message :: ");
 6            Serial.print("node ID and ChannelID : ");
 7            Serial.print(msg.node_id);
 8            Serial.println(msg.channel_id);
 9            Serial.print("Data Length : ");
10            Serial.println(msg.data_len);
11
12            Serial.print("Data : ");
13            Serial.print(msg.data_len);
14            for ( int a = 0; a < msg.data_len; a = a + 1 ){
15                Serial.print(msg.data[a]);
16            }
17            Serial.println();
18        }
19    }
20}

It's really cool that you are making a custom node. Don't hesitate to share pics with us!

Alain

jameswhiteman

Hi Alain,

Implemented and it works a treat! back to 3-4 ms cycles. Thank you.
Have a picture of the board, but I don't know how to attach a image to your forum

Cheers,

James

jameswhiteman

This is our board, couple of cool things on it.

  • Nocan
  • Multiplexing auto tunning antennas

jameswhiteman

looking at running a https://www.balena.io/fin/ board and cm3+L for the nocan server and hat as well as aws greengrass, using aws greengrass and remotely deployed lambdas to interact with the nocan server to pubsub messages. Nocan is going to be an overwhelming success story as it will allow us to deploy on mass over the air updates to our product in the field. It is also going to allow us the ability to add extra functionality to our product by just by adding another node.
Personally I believe that nocan really is ground breaking for the era of IoT, and am grateful to both yourself and our man Justin at OSE who put this all together

Alain

Wow, what can I say. This is impressive work!
I'm very happy to see NoCAN used this way.