5PRIPriyanshu Saini, home
Plain site

DigitalPaani · 2022–25 · Personal best

Remote control of treatment plants, one command at a time

A live diagram of each water treatment plant, with remote control of its equipment. Manual changes no longer needed a phone call to an operator, and conflicting commands could not reach a plant.

commands per plant, until the result is known
1 at a time
Role
Software Engineer
Stack
TypeScript · React · React Flow · Node.js · MongoDB

DigitalPaani’s plants already ran on automation scripts. When something had to be changed by hand, someone phoned the operator at the plant, or the IoT team edited the plant’s Node-RED flows. Some clients ran their own SCADA systems. We built a live diagram of each plant in the browser, with remote control of its equipment, for the internal team and senior clients.

Problem

Manual changes depended on people being available: an operator ready to pick up the phone, or someone from the IoT team. Late at night, that was the weak point.

Remote control carried real risk. Two commands racing each other on the same plant could damage a pump or overflow a tank, so the feature was only worth building if that could not happen.

I built the frontend with my engineering manager and two colleagues, and later maintained the backend for it as well: the live-data stream and the control routes.

Decisions

How readings reach the screen and commands reach the plantReadings: PLCs, HMIs and Raspberry Pi gateways at the plant publish over MQTT to AWS IoT; a Lambda writes them to a MongoDB time-series collection every minute; the backend queries each sensor and streams the values to the browser over Server-Sent Events, where React Flow draws the plant. Commands: the user picks equipment and a duration; the backend accepts one command per plant and records it in a command history; a Lambda publishes it over MQTT to Node-RED at the plant; the next minute's readings show whether it succeeded or failed, and only then do the other controls unlock. When the duration runs out, a scheduled job restores the previous control state.Readings, plant to screenPlantPLC, HMI, PiAWS IoTMQTTLambdaevery minuteMongoDBtime seriesBackendqueries sensorsBrowserSSE, React FlowCommands, screen to plantBrowserpicks a durationBackendone per plantMongoDBcommand historyLambdapublishesNode-REDvia MQTTPlantapplies itnext minute's readings: success or failure, then the other controls unlockUntil the result is known, every other control on that plant stays locked, and a banner tells everyone viewing it.When the chosen time runs out, a scheduled job restores the previous control state.

Draw the plant from configuration

Designers drew each kind of equipment (pumps, blowers, valves, tanks and pipes) as an SVG. Customer success managers described each plant on a configuration page: which equipment’s outlet feeds which equipment’s inlet. The frontend turned that description into a React Flow graph, so a new plant needed configuration rather than code. A large plant came to around 30 nodes.

The examples below are generic illustrations, not production code.

// Configuration in, React Flow graph out
type Item = { id: string; kind: string; x: number; y: number };
// The output of `from` feeds the input of `to`
type Link = { from: string; to: string };

function useGraph(items: Item[], links: Link[]) {
  return useMemo(
    () => ({
      nodes: items.map((item) => ({
        id: item.id,
        type: item.kind,
        position: { x: item.x, y: item.y },
        data: {},
      })),
      edges: links.map((link) => ({
        id: `${link.from}->${link.to}`,
        source: link.from,
        sourceHandle: 'out',
        target: link.to,
        targetHandle: 'in',
      })),
    }),
    [items, links],
  );
}

Stream readings one way, over SSE

The view is mostly one-way: the server sends readings and the browser draws them. Remote control was used perhaps once a day. So rather than WebSockets, the page opens a Server-Sent Events stream to our Node backend, which queries the time-series data for each sensor and sends it down. SSE runs over plain HTTP and the browser reconnects on its own; commands went over ordinary requests.

Readings reached the database once a minute. PLCs, HMIs and Raspberry Pi gateways at the plant publish over MQTT to AWS IoT, and a Lambda wrote them into a MongoDB time-series collection.

Each message changes only a few sensors, so the diagram leans on memoisation: custom nodes are wrapped in memo, the node types are defined once, and only nodes with new readings get a new object. A reading re-renders the equipment it belongs to, not the whole plant.

const StatusNode = memo(function StatusNode({ data }: NodeProps<Status>) {
  return <StatusIcon active={data.active} value={data.value} />;
});

// Defined once, at module level. A new object on every render
// would make React Flow remount every node.
const nodeTypes = { status: StatusNode, gauge: GaugeNode };

type Updates = Record<string, Partial<Status>>;

function useLiveUpdates(url: string, setNodes: SetNodes) {
  useEffect(() => {
    const stream = new EventSource(url);
    stream.onmessage = (event) => {
      const updates: Updates = JSON.parse(event.data);
      // Untouched nodes keep their identity, so memo skips them.
      setNodes((nodes) =>
        nodes.map((n) => {
          const update = updates[n.id];
          return update ? { ...n, data: { ...n.data, ...update } } : n;
        }),
      );
    };
    return () => stream.close();
  }, [url, setNodes]);
}

One command per plant, until the result is known

To take control, a user picks the equipment, the action and how long manual control should last. The backend records the command in a command history and refuses another for that plant while one is in progress. A Lambda picked the command up and published it over MQTT to Node-RED at the plant. The user saw an acknowledgement as soon as the command was registered and being processed. There was no separate reply from the plant: the next minute’s data from the HMI or Node-RED showed whether the change had taken effect, and the command was marked as a success or a failure. Until then, every other control on that plant stayed disabled.

While control is taken, a persistent banner across the top of the screen tells everyone viewing that plant. Because every command carries a duration, manual control cannot be left on by accident: when the time runs out, a scheduled job restores the previous control state.

After the access-control rollout, sending commands became a permission of its own, so only the people allowed to could take control at all.

Result

  • Late-night monitoring no longer depended only on an operator being ready to answer the phone.
  • The team could take control of a plant directly, without going through the IoT team.
  • The live view rolled out across all clients, with remote control used about once a day.