Accelerometer mqtt

Hey, I tried to get data from an accelerometer MP6050 to the cayenne dashboard but the widget that shows up is expeting an x,y and z value.

What is the function to write accelerometer data to the widget?

I’ve been using virtualWrite(1,acceleration, “accel”, “g”) but that only transmits 1 value and created the said 3 value widget.

It would also be great if I could add 1 value for the total acceleration to the dashboard instead of 3.

If you need more context to help I’d be glad to upload some screenshots or anything else that might help.

Hi @julian,

So I’ve been playing with this with fake data for the last hour since I saw your post and I think I can get you up and running with this widget. This might be good enough depending on what you want to do with the data, but first, a few caveats:

  • The widget as it stands was designed for a LoRa partner so it has not really been tested in the context of MQTT devices. This is actually a pretty minor caveat since most of the LoRa stuff boils down to MQTT under the hood.

  • It doesn’t currently have data history, or a proper tie in with our Triggers system (you can add it to a trigger ‘IF’ statement, but the UI doesn’t know how to handle the 3 values).

  • Since we don’t have any official multi-value widgets yet for MQTT, the virtualWrite() statement wasn’t designed for it either. What I’ve got below should work great, but if you use too many decimal places or very large numbers for all 3 values, you could overrun the maximum for sending data and see issues. Probably not a big deal with real-world accelerometer data unless you have crazy precision, and you could always round things in code. A library update that will handle this caveat is coming soon.

With all that said, the format to send this data is pretty straightforward:

Cayenne.virtualWrite(channel, "[x,y,z]", "accel", "type");
so, a real example:
Cayenne.virtualWrite(1, "[2,1,3]", "accel", "g");

You can note that type is just a text label, so if your data is in meters per second squared or whatever other unit, you can label however you’d like. It’s not involved in any calculation or conversion.

Finally I should note that the "[2,1,3]" part with the actual data is being passed as a single string to compensate for virtualWrite() not being ready for multi variables like this as mentioned above. So you’ll have to use string manipulation functions to construct a text string in that format before passing it into the virtualWrite() statement – you won’t be able to pass it the direct sensor data as int/float variables or anything like that. This will also be resolved when we update the library for multivariable support.

1 Like

Thanks a bunch. That’s all very helpful and interesting. Do you think in the future there will be a widget for net acceleration that uses only 1 data input? So that if I calculate the net acceleration I can display it on the dashboard?

We’ll be improving this widget at some point to make it not such a pain to send data to, you can see a little more of what we’re planning here.

As far as a single acceleration value, would the Value widget we already have work for you if it had data/unit labels for net acceleration, or allowed you to customize them on your end?

I’ve been trying to use that but I ran into issues with Triggering. If that worked then it would definitely work for me.

What trouble did you have with triggering the value widget, out of curiosity?

It just doesn’t trigger. Maybe it’s the way I set up the widget. I use virtualWrite(6, total_accel, “Accel”, “g”).

Is that giving you an accelerometer widget like this that you’re trying to trigger?

If that’s the case, it’s expected, per the caveats I posted above.
But you should just be able to pass generic data to get a value widget that should work with triggers, if you try something like:

virtualWrite(13, 145, “null”, “myunit”)

That worked to generate trigger notifications for above/below X value for me. (and you can of course customize the ‘myunit’ label to whatever your actual data is.)

No it is giving me a widget like the second one. But even trying “null” instead of “Accel” it still doesn’t trigger.

The value is updating correctly and displaying correctly.

Could you show me the detail of the trigger statement? I’ll try to reproduce on my end. Is it possible it could be hitting the trigger condition but the ‘then’ part is not happening? A good test to make sure it’s not the ‘Then’ part is to change it to notify you via email.

It says the trigger ran 0 times.

Thanks, I’ll try that setup. In the meantime, can you refresh your browser on the page where it shows the ‘Ran X times’ count? We have a known issue right now where this count isn’t updating without refresh even though the trigger is actually firing. That might not be the case here of course, but worth checking. I’d still expect the ‘then’ action to occur in this case.

Trigger is running for me with the setup in your screenshot (I do need to refresh to see the run counter increment, but the ‘Then’ action of email is happening).

If it’s still not working for you, do you mind if I take a peek in your account (and temporarily change the target email address for that trigger)?

Go ahead. Still not working. Could it be something from my code?

I don’t think it would be in your code if the widget is updating our our side, but we can check there next. Could you PM me your login/password so I can have a look?

1 Like