Trigger a one-time event on a device using Env Variables?

Hi All,

I would like a way to trigger a one-time event on a device by using a Env variable in notehub. For example, suppose I have env variable called user_flag that is set to “0” in the notehub project. If I change it to a “1” in my notehub environment page and hit the apply changes button, my esp32 reads the variable, sees the change to 1 and executes some kind of event. The problem is now I want the esp32 to change it back to 0 in my notehub dashboard when its finished. The only way I have done this is to use env.set, which overrides the project variable of the same name, then I delete the variable with another env.set without “text” defined. This does work, but seems convoluted.

I’m guessing there is better way to get this done.

Thanks

Jay

Hi @micromet,

This is a good question and there are a couple of ways you could accomplish this:

Using Inbound and Outbound Notefiles

  1. Create an inbound .qi Notefile (from Notehub to device) with the notification.
  2. Once the Notecard receives the Notefile, you can read the contents with the note.get API and then delete it by using the delete:true argument.
  3. When it’s done processing, send an outbound .qo Notefile back to Notehub.

Using a DB Notefile

DB Notefiles let you share state between a device and Notehub, similar to environment variables.

  1. Using a .db Notefile, add two named Notes. One whose note argument is “request” and the other whose note is “response”.
  2. In the body of the “request”, have one string field that is the “request type” and one field that is the “request time”. We recommend using Unix epoch time (seconds) for request time.
  3. In the body of the “response”, have one string field that is “status” and one field that is “response time”.
  4. When the service wants to make a request of the device, set the request using note.update. When the Notecard-connected host detects it via note.get, it can process it and use note.update to set the “response” and re-sync with Notehub.
1 Like

Thanks Rob, this DB Notefile approach seems like it has a lot of potential uses for monitoring the status or controlling cyber-physical systems (we do a lot of irrigation system control - valve, pumps, etc. )

I’ll give this a try.

Would love to see a simple DB notefile example.

Jay

The best examples for using DB Notefiles are in that referenced documentation section: Inbound Requests & Shared Data - Blues Developers

1 Like