Skip to content
You Start with one site
Amounts in Start with one site
IoT & AIoT · Course

Act on it: your first alert and your first command

Lesson 4 of 4 Beginner 11 min read
Found this useful?

Your device now sends numbers, and you can read them from anywhere. That is half of the job. A tank that overflows at 2am does not care that the chart was correct. This lesson is about the other half: what happens when a number goes wrong, and who or what does something about it.

#IoT #Alerts #Commands

The deviceIt is beside the pump and needs nothing else.Acts in milliseconds
A rule on the platformIt sees the number arrive and sends a command back.Acts in seconds, if the network is up
A personThe phone rings. They wake, read, decide, drive.Acts in minutes or hours

Three places an action can live

Every action you want belongs in one of three places. Choosing the right one is the whole skill.

FirmwarePlatform ruleA person needs no networkneeds the networkneeds to be awake millisecondssecondsminutes to hours
How long each one takes to act. The bars are not to scale: a person is thousands of times slower than firmware.
  • Firmware is for anything that must work when the network is down: a safety cutoff, a limit, a control loop.
  • A rule on the platform is for what one device cannot know: another device's reading, the time of day across a site, a limit you want to change without a reflash.
  • A person is for a judgement: is this a fault or a visitor, do we send a technician tonight or tomorrow.
Predict first

The tank is at 94% and rising. The Wi-Fi router loses power. Which protection still stops the pump?

The rule to keep. If a failure of the network would turn a late action into damage, the action belongs in firmware. The platform is then the second line and the witness, never the only line.

An alert a person will answer

An alert is the simplest action: when a number crosses a limit, tell someone. It is also the easiest to get wrong, because an alert that fires every night is ignored on the night that matters. Four choices decide whether yours is answered.

The question

Your tank sends its level every 30 seconds. You want to be told when it is about to run dry. What exactly do you set?

What you know: the pump must not run below 10%. A refill takes about 20 minutes. Water sloshes, so one reading can jump by a few percent.

Show the worked solution, 4 steps
  1. Choose the field and the limit

    The field is the level. The limit is not 10%: at 10% it is already too late to act. With a 20 minute refill, alert at 25%, so there is time to do something.

  2. Say how long it must last

    One low reading is a wave, not a problem. Ask for the level to stay below 25% for 2 minutes, which is 4 readings in a row. This one choice removes most false alarms.

  3. Say who is told, and who is told next

    The person who can refill the tank is first. If they do not confirm, someone else must be told. An alert that reaches nobody is the same as no alert.

  4. Test it on purpose

    Lower the level, or publish a low value by hand, and watch the alert arrive. An alert that has never fired is not known to work.

Any monitoring setup asks for these four choices. On a server you run, a rule engine such as Node-RED holds them; the alerts and rule engines lesson builds one. On siliconwit.io, our monitoring service and the worked example of this course, you set them on the device's Automation tab and on the Alerts page.

A command the device obeys and confirms

You already know that your device publishes to d/<id>/t. Commands travel the other way, on d/<id>/c. The device subscribes to that topic, does what it is asked, and then says so.

Your deviceServer or service pump, tank, sensorrule, button or alert d/<id>/t the readingd/<id>/c the commandd/<id>/k/<session> done
Three topics, one conversation. The reply is what turns "a command was sent" into "the pump is off".

The command arrives as JSON with a command name, an optional value, and a session that identifies this one command. siliconwit.io waits 30 seconds for the reply on d/<id>/k/<session>; a server of your own can use the same three topics. No reply means nobody can say the action happened, and the command shows as not confirmed.

// 1. The safety cutoff lives here, in firmware. It needs no network.
void loop() {
  float level = readTankLevel();              // percent
  if (level >= 95.0) pumpOff();               // always works, online or not
  mqtt.loop();                                // then let commands arrive
}

// 2. Listen for commands on your own command topic.
void onConnect() {
  String cmdTopic = String("d/") + DEVICE_ID + "/c";
  mqtt.subscribe(cmdTopic.c_str());
}

// 3. Obey, then confirm on the reply topic within 30 seconds.
void onMessage(char* topic, byte* body, unsigned int len) {
  JsonDocument cmd;  deserializeJson(cmd, body, len);
  const char* name    = cmd["command"];       // for example "pump"
  const char* session = cmd["session"];       // identifies this one command
  if (strcmp(name, "pump") == 0) {
    if (cmd["value"] == "off") pumpOff(); else if (readTankLevel() < 95.0) pumpOn();
  }
  String reply = String("d/") + DEVICE_ID + "/k/" + session;
  mqtt.publish(reply.c_str(), "{\"ok\":true}");
}

Read the code in the order of its three comments. The cutoff is first and does not depend on anything below it. The command handler refuses to start the pump when the tank is already full: a device should never trust a command more than its own sensor. The reply fields siliconwit.io accepts are listed in the siliconwit.io documentation; the sketch above sends the shortest one.

Your turn: the silent device

Your device stops sending at 3am because its power supply failed. No limit was crossed, so no alert fired. What would have told you, and where does that check have to live?

Show the answer, 2 steps
  1. It cannot live on the device

    A device that has lost power cannot report that it has lost power. This is the one check firmware can never do for itself.

  2. The platform checks for silence

    Set an alert on "no data for 5 minutes". With a reading every 30 seconds, 5 minutes is ten missed readings, which is not a short Wi-Fi fault. This is the clearest case where the cloud earns its place.

Check yourself

Question 1

A heater must switch off at 80 °C or the element is destroyed. Where does that limit go?

Question 2

Your alert fires five times a night because the reading jumps for a few seconds. What do you change first?

Question 3

The platform shows a "pump off" command as sent, and no reply came back. What do you know?

Key points
  • An action lives in firmware, in a platform rule, or with a person. Anything that must work with the network down lives in firmware.
  • An alert needs four choices: the field, a limit that leaves time to act, how long it must last, and who is told next.
  • Commands come down on d/<id>/c. The device replies on d/<id>/k/<session> within 30 seconds.
  • A command with no reply is "sent", not "done". A silent device can only be caught from outside it.

You can now measure, and act on what you measure. What remains is the record: showing, at the end of a month, what happened and what it saved. That is what the monthly record is for.

Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.