How to send inbound notes via notehub API

@bsatrom @RobLauer @gwolff @pavan Dumb question…how do I send an inbound note to a specific notecard using the notehub API? I have used the Notehub’s Add Note to Device feature. So now I need to make it work for the real world application: to send a command (requested from the end user’s mobile app) to the notehub and on to the notecard that the mcu host will then execute. Without this (or, even better, having the notehub directly command the notecard without the need of the mcu…which may be supported at some point?), my project is seriously handicapped.

Hi @PostThis,

This capability doesn’t exist with the current options available in the Notehub API, but I’ve added it as a feature request for the engineering team to look at!

In the meantime, while you probably already have thought of a workaround, the way I would approach this is to send the DeviceUID as part of the inbound note and then have the MCU only process those inbound notes relevant to that particular device.

Rob

1 Like

Thanks @RobLauer I do not yet have a workaround…but you’re giving me ideas. Also, your reply seems to imply that there is still a way to send an inboud note without doing it manually in notehub. lf yes, please enlighten me.

Sure! The note.add API in a curl request allows you to add an inbound note, like so:

curl -L 'https://api.notefile.net/req?product=com.product.name&device=dev:000000000000000' -d \
        '{"req":"note.add","file":"sensors.qi", "body": {"temp": 72.22 }}'

And actually as I read this code, we do require a deviceUID to be sent, which probably solves the first issue you asked about :slight_smile:

Rob

1 Like

Perfect. That’s what I was missing. Thank you, sir.

Just FYI - I used the following NodeJS code to create inbound data for a notecard - it may help someone - note that in this example, specific data is pulled from a separate config.js file

const axios = require('axios');
const config = require('./config')

const user = config.user
const passwd = config.passwd
const product = config.product
const device = config.device
const project = config.project

const filename = "my_inbound.qi"
const my_key = "my_key"
const my_val = "my_val"

// Build url to communicate with notehub/notecard API
const ncUrl = `https://api.notefile.net/req?product=${product}&device=${device}&project=${project}`;

// First, retrieve one-time token based on user account
axios({
    method: "post",
    url:"https://api.notefile.net/auth/login",
    data: {
      username: user,
      password: passwd
    }
  })
  // Now we have token,  
  .then(response => {
    var token = response.data.session_token;
    console.log({token});
    // add inbound note to our notecard
    axios ({
      method: "post",
      url: ncUrl,
      data: {
        "req": "note.add",
        "file": filename,
        "body":{
          my_key:my_val
        }
      },
      headers: {
        "X-SESSION-TOKEN": token
      }
    })
    .then(response => {
      console.log("result",response.data);
    })
    .catch(error => {
      console.log(error);
    })
  })
  .catch(error => {
    console.log(error);
  });
4 Likes

Hi, my Notecard is version 1.5.6.13807 - should I be able to use Curl to add notes (as described above) or is there an update I need to install for that feature?

regards, Rich …

@richf
The curl is for when you need to make HTTP requests to Notehub from another computer that is connected to the internet.

curl requests made directly to the Notecard over a serial or I2C connection will not work.

You can add data indirectly to Notecard by making curl requests to the Notehub. Then when those changes get synced to the Notecard, you can access those changes over the Notecard JSON API (via serial or I2C)

For more information about curl see https://curl.se/

Hi,

Thx for the quick reply – The above line "The curl is for when you need to make HTTP requests to Notehub from another computer that is connected to the internet. is my interest. I’m making ‘curl’ requests from my Linux (Debian) PC … Having a ROUTE connected to WebHook, I can see the incoming message on that site but the ‘add.note’ doesn’t appear to make to my Raspberry PI/Notecard. The ‘Add Note’ dialog works fine.

Here’s the ‘curl’ example …

{
  "req": "note.add",
  "file": "rpi.qi",
  "body": {
    "command": "curl2"
  }
}

regards, Rich …

Hmmmm. I’m not sure I follow entirely. Hopefully I can clarify a bit.

Do you mean from the Linux bash terminal, you are entering something like

curl POST -L 'https://api.notefile.net/req?project=app:xxxxxxxxxxxxxxxxx&device=dev:xxxxxxxxxxx' -H 'X-SESSION-TOKEN: xxxxxxxxxxxxxxx' -d '{"req":"note.add","file":"rpi.qi","body":{"command":"curl2"}}'

(similar to: How to send inbound notes via notehub API - #4 by RobLauer)

then, after syncing with Notehub, if you issue the following JSON request to the Notecard from the Raspberry Pi over the serial connection

{"req":"note.changes","file":"rpi.qi"}

the Note you added via the curl command does not appear?
That is, you do not see something like

{"changes":1,"total":1,"notes":{"uuid":{"body":{"command":"curl2"}}}}

am I understanding correctly?

Hi,

Yes … your comments match my observations. This morning I adapted the ‘curl’ example in your last message, filled in my Webhook URL, Product and Device ID and observed that the Raspberry PI did NOT show any file “changes”. The Webhook’s “Raw Content” field did show the note’s contents.

I then used the Notehub’s Devices/Add-Note dialog to add this note (“command”:“stat7”) – here’s the Raspberry PI’s output …

{“req”: “note.changes”, “file”: “rpi.qi”, “delete”: true}
{“notes”:{“1:6632”:{“body”:{“command”:“stat7”},“time”:1652872813}},“total”:1}

An earlier comment in this thread appears to indicate that a fix may be in progress to allow ‘curl’ to match the Notehub’s Device/Add-Note dialog behavior …

regards, Rich …