Monday, April 18, 2016

37: Raising an "Exception" in a Particle Photon or Electron

Let's say that that you have a remote Photon that is monitoring a temperature sensor and it reads a value that is out of range (too hot, too cold, whatever). I have a Raspberry Pi Linux system (could be any other Unix or Linux computer) that needs to be notified. One way would be for the Linux system to "ask" (i.e., "poll") the Photon every few minutes for the current temperature value and then to test for out of range for itself. A plausible action would be to send a text message to a (hopefully) responsible human.

A better procedure would be for the Photon to decide what is out of range and to send an alert message directly to the Linux on its own. The Particle programming interface offers a neat way to do this between that Photon and another Particle device. The sending device "publishes" a uniquely named message (up to the "cloud") and the receiving device can "subscribe" to it. Note that Particle has gone and gotten metaphorical on us: overloading the words "publish" and "subscribe" with new specialized meanings.

This is good for Particle Photons or Electrons but the interface to a Linux doesn't seem so straightforward. Linux/Unix systems like to receive this kind of message by reading a named device. So, until Particle provides such an interface, the simple solution is to publish/subscribe between 2 Particle devices and have the receiving computer pass the data on through the USB cable. In Linux shell this statement could record the message;

$ my-pgm </dev/ttyACM0 # program "hangs" waiting for data

Note that the single subscribing device could be used to receive messages from several publishing devices. And these could be either Photons or Electrons. The Photon receiver would cost a negligible $20.

No comments:

Post a Comment