Datacake and Inbound Messages

Greetings.

I’m considering a new design using Blues and Datacake. Curious if anyone has experience generating inbound message from Datacake towards Notehub? To date I’ve only seen examples of outbound messages to Datacake.

-AD

Hey @Autodog,

We’ve had people use Datacake’s Node-RED support to make this happen Datacake Cake Red | Cloud Hosted and Managed Node-RED.

Personally though I find Node-RED to be a bit confusing, and I’ve come up with my own approach (hack) that I love to use for this. My approach requires some basic web development skills to make happen, but I’ll spell it out below in case you’re interested in giving it a try.

  1. Add an iframe widget to your dashboard.

  1. Create a simple web page that uses the Notehub API. Here’s the one I created that sends a signal to a device with the signal API when you press a button. You could alter this pretty easily to send Notes with the note.add request.
<!DOCTYPE html>
<html>
  <style>
    body,
    html {
      padding: 0;
      margin: 0;
    }
    div {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      height: 100vh;
    }
    button {
      background: red;
      color: white;
      border: 0;
      border-radius: 150px;
      height: 70vh;
      width: 90vw;
      border-width: 5px;
      border-style: solid;
      border-color: black;
    }
    button:hover {
      cursor: pointer;
    }
  </style>

  <div>
    <button>Press Me</button>
  </div>

  <script>
    function onSubmit() {
      const API_KEY = "your_key_here";
      const APP_UID = "your_app_uid_here";
      const DEVICE_UID = "your_device_uid_here";

      fetch(
        `https://api.notefile.net/v1/projects/${APP_UID}/devices/${DEVICE_UID}/signal`,
        {
          method: "POST",
          headers: {
            "Content-Type": "text/plain",
            "X-SESSION-TOKEN": API_KEY,
          },
          body: JSON.stringify({ body: {} }),
        }
      )
        .then((response) => response.text())
        .then((data) => {
          console.log(data);
        })
        .catch((error) => {
          console.error("Error:", error);
        });
    }

    document.querySelector("button").addEventListener("click", onSubmit);
  </script>
</html>
  1. Host the HTML page somewhere. For Datacake to use your webpage it has to be available on the public internet. Any web hosting platform will work—it just needs to be up at a public URL.

  2. In the settings of your Notehub project, add your public URL in the “CORS Allowed Origins” textbox. This will allows your new public URL to access the Notehub API for your project.

  1. Return to Datacake and add that same URL as the source of your iframe widget.

And if all went well—your iframe should just work. You’ll know it works if you see your inbound Notes in your Events list in Notehub.

I used this approach for the demo we did at CES for Blues. We had the Notecard wired up to a scooter, and every time the Notecard got a signal in a certain format it would honk the horn. Below is a screenshot of the dashboard itself in Datacake. The button in the bottom right is the iframe, which has a single button that sends the signal when you click it. It worked flawlessly for us.

TJ

4 Likes

Very nice solution. We should make it a standard for the interaction with Notehub.

Regards
Rob Oudendijk

1 Like

@tjvantoll Thanks for the quick response - very creative!

Probably more cost-effective than hosting a Node-RED instance on Datacake as well…

While I can see this would be straightforward for a single management dashboard associated with a single Notecard, curious how this might work in a multi-user, multi-Nodecard environment?

-AD

1 Like

While I can see this would be straightforward for a single management dashboard associated with a single Notecard, curious how this might work in a multi-user, multi-Nodecard environment?

The approach I showed is definitely intended for smaller scale deployments. You can get a bit creative in the iframe, and do things like use the Notehub API to pull down a list of all your devices and let the user pick one—but I’d argue that once you start adding non-trivial logic like that you’re likely better off just building an entirely custom app for your project.

TJ

1 Like