|
| 1 | +# Relay Control Feature |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The Arduino Opta has 4 built-in relay outputs (D0-D3) that can be controlled remotely via the Blues Notecard cellular connection. This feature enables: |
| 6 | + |
| 7 | +1. **Manual Control** - Toggle relays from the server web dashboard |
| 8 | +2. **Alarm-Triggered Control** - Automatically trigger relays on another client when an alarm occurs |
| 9 | +3. **Server-Triggered Control** - Trigger relays programmatically from the server |
| 10 | + |
| 11 | +## Hardware |
| 12 | + |
| 13 | +- **Arduino Opta Lite** - Has 4 built-in mechanical relays |
| 14 | +- **Relay Outputs**: D0, D1, D2, D3 (controlled via LED_D0-LED_D3 constants) |
| 15 | +- **Blues Notecard** - Provides cellular communication for relay commands |
| 16 | + |
| 17 | +## Configuration |
| 18 | + |
| 19 | +### Client Configuration |
| 20 | + |
| 21 | +Add relay trigger settings to individual tanks in the client configuration: |
| 22 | + |
| 23 | +```json |
| 24 | +{ |
| 25 | + "tanks": [ |
| 26 | + { |
| 27 | + "id": "A", |
| 28 | + "name": "Tank A", |
| 29 | + "highAlarm": 100.0, |
| 30 | + "lowAlarm": 20.0, |
| 31 | + "relayTargetClient": "dev:864475044012345", |
| 32 | + "relayMask": 3 |
| 33 | + } |
| 34 | + ] |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +#### Configuration Fields |
| 39 | + |
| 40 | +- **`relayTargetClient`** (string): UID of the target client whose relays to control |
| 41 | + - Leave empty ("") to disable relay triggering |
| 42 | + - Get the UID from the Blues Notehub device list or server dashboard |
| 43 | + - Format: `dev:IMEI` (e.g., "dev:864475044012345") |
| 44 | + |
| 45 | +- **`relayMask`** (integer): Bitmask indicating which relays to trigger |
| 46 | + - Bit 0 (value 1) = Relay 1 (D0) |
| 47 | + - Bit 1 (value 2) = Relay 2 (D1) |
| 48 | + - Bit 2 (value 4) = Relay 3 (D2) |
| 49 | + - Bit 3 (value 8) = Relay 4 (D3) |
| 50 | + - Combine bits to trigger multiple relays |
| 51 | + |
| 52 | +#### Relay Mask Examples |
| 53 | + |
| 54 | +| Decimal | Binary | Relays Triggered | |
| 55 | +|---------|--------|------------------| |
| 56 | +| 1 | 0001 | Relay 1 only | |
| 57 | +| 2 | 0010 | Relay 2 only | |
| 58 | +| 3 | 0011 | Relays 1 and 2 | |
| 59 | +| 5 | 0101 | Relays 1 and 3 | |
| 60 | +| 15 | 1111 | All 4 relays | |
| 61 | + |
| 62 | +## Usage |
| 63 | + |
| 64 | +### 1. Manual Control from Server Web UI |
| 65 | + |
| 66 | +1. Open the server dashboard at `http://<server-ip>/` |
| 67 | +2. Locate the client in the telemetry table |
| 68 | +3. Click the relay buttons (R1, R2, R3, R4) to toggle relays |
| 69 | +4. Active relays are highlighted in blue |
| 70 | +5. Commands are sent immediately via Blues Notecard |
| 71 | + |
| 72 | +### 2. Automatic Control on Client Alarms |
| 73 | + |
| 74 | +When a tank alarm is triggered (high or low threshold): |
| 75 | + |
| 76 | +1. Client sends alarm notification to server (as usual) |
| 77 | +2. If `relayTargetClient` is configured and `relayMask` is non-zero: |
| 78 | + - Client sends relay commands to the target client |
| 79 | + - Specified relays are activated on the target client |
| 80 | + - Commands are tagged with source "client-alarm" |
| 81 | + |
| 82 | +**Example Scenario:** |
| 83 | +- Tank A on Client 1 goes into high alarm |
| 84 | +- Tank A is configured to trigger relays 1 and 2 on Client 2 |
| 85 | +- Client 1 automatically sends commands to activate Client 2's relays 1 and 2 |
| 86 | +- Client 2 can control pumps, valves, or alarms connected to those relays |
| 87 | + |
| 88 | +### 3. Programmatic Control via API |
| 89 | + |
| 90 | +Send relay commands from external systems: |
| 91 | + |
| 92 | +```bash |
| 93 | +curl -X POST http://<server-ip>/api/relay \ |
| 94 | + -H "Content-Type: application/json" \ |
| 95 | + -d '{ |
| 96 | + "clientUid": "dev:864475044012345", |
| 97 | + "relay": 1, |
| 98 | + "state": true |
| 99 | + }' |
| 100 | +``` |
| 101 | + |
| 102 | +**Request Parameters:** |
| 103 | +- `clientUid` (string): Target client's device UID |
| 104 | +- `relay` (integer): Relay number 1-4 |
| 105 | +- `state` (boolean): true = ON, false = OFF |
| 106 | + |
| 107 | +**Response:** |
| 108 | +- `200 OK` - Command sent successfully |
| 109 | +- `400 Bad Request` - Invalid parameters |
| 110 | +- `500 Internal Server Error` - Failed to send command |
| 111 | + |
| 112 | +## Communication Protocol |
| 113 | + |
| 114 | +### Relay Command Message |
| 115 | + |
| 116 | +Commands are sent via Blues Notecard to `device:<clientUID>:relay.qi`: |
| 117 | + |
| 118 | +```json |
| 119 | +{ |
| 120 | + "relay": 1, |
| 121 | + "state": true, |
| 122 | + "source": "server", |
| 123 | + "duration": 0 |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +**Fields:** |
| 128 | +- `relay` (integer): Relay number 1-4 |
| 129 | +- `state` (boolean): true = ON, false = OFF |
| 130 | +- `source` (string): Origin of command ("server", "client-alarm", "manual") |
| 131 | +- `duration` (integer): Auto-off timer in seconds (0 = manual, feature not yet implemented) |
| 132 | + |
| 133 | +### Client Polling |
| 134 | + |
| 135 | +- Client polls `relay.qi` every 10 minutes |
| 136 | +- Commands are deleted after processing |
| 137 | +- Invalid commands are logged and ignored |
| 138 | + |
| 139 | +## Use Cases |
| 140 | + |
| 141 | +### Example 1: Cross-Site Alarm Response |
| 142 | + |
| 143 | +**Scenario:** When Tank A at Site 1 goes into high alarm, activate a strobe light at Site 2 |
| 144 | + |
| 145 | +**Configuration:** |
| 146 | +```json |
| 147 | +{ |
| 148 | + "tanks": [{ |
| 149 | + "id": "A", |
| 150 | + "name": "Site 1 Tank A", |
| 151 | + "relayTargetClient": "dev:864475044056789", |
| 152 | + "relayMask": 1 |
| 153 | + }] |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +**Result:** Relay 1 on Client 2 (Site 2) activates when Tank A alarm triggers |
| 158 | + |
| 159 | +### Example 2: Pump Control |
| 160 | + |
| 161 | +**Scenario:** Manually control a pump connected to Relay 2 from the server dashboard |
| 162 | + |
| 163 | +**Steps:** |
| 164 | +1. Open server web UI |
| 165 | +2. Find the client controlling the pump |
| 166 | +3. Click "R2" button to toggle the pump |
| 167 | +4. Button turns blue when pump is ON |
| 168 | + |
| 169 | +### Example 3: Multi-Relay Coordination |
| 170 | + |
| 171 | +**Scenario:** When Tank B goes low, activate both a pump (Relay 1) and warning light (Relay 3) |
| 172 | + |
| 173 | +**Configuration:** |
| 174 | +```json |
| 175 | +{ |
| 176 | + "tanks": [{ |
| 177 | + "id": "B", |
| 178 | + "name": "Tank B", |
| 179 | + "lowAlarm": 20.0, |
| 180 | + "relayTargetClient": "dev:864475044056789", |
| 181 | + "relayMask": 5 |
| 182 | + }] |
| 183 | +} |
| 184 | +``` |
| 185 | + |
| 186 | +**Result:** Relays 1 and 3 activate together (relayMask = 5 = binary 0101) |
| 187 | + |
| 188 | +## Troubleshooting |
| 189 | + |
| 190 | +### Relay Commands Not Working |
| 191 | + |
| 192 | +1. **Check Blues Notecard Connection** |
| 193 | + - Verify client has cellular signal |
| 194 | + - Check Blues Notehub shows recent sync |
| 195 | + - Look for "notecard offline" messages in serial output |
| 196 | + |
| 197 | +2. **Verify Configuration** |
| 198 | + - Confirm `relayTargetClient` UID is correct |
| 199 | + - Check `relayMask` is non-zero |
| 200 | + - Ensure target client is online and polling |
| 201 | + |
| 202 | +3. **Check Serial Output** |
| 203 | + - Client: "Relay command received from..." |
| 204 | + - Server: "Queued relay command for client..." |
| 205 | + - Look for error messages about failed commands |
| 206 | + |
| 207 | +### Relays Not Activating Physically |
| 208 | + |
| 209 | +1. **Verify Hardware** |
| 210 | + - Check relay LED indicators on Arduino Opta |
| 211 | + - Test with manual control from web UI |
| 212 | + - Ensure external devices are properly connected |
| 213 | + |
| 214 | +2. **Check Power** |
| 215 | + - Relays may not activate if power supply is insufficient |
| 216 | + - Verify external load is within relay specifications |
| 217 | + |
| 218 | +3. **Review Serial Output** |
| 219 | + - Look for "Relay X (DX) set to ON/OFF" messages |
| 220 | + - Check for platform compatibility warnings |
| 221 | + |
| 222 | +### Web UI Buttons Not Responding |
| 223 | + |
| 224 | +1. **Check Browser Console** |
| 225 | + - Look for JavaScript errors |
| 226 | + - Verify `/api/relay` endpoint is accessible |
| 227 | + - Check network tab for failed requests |
| 228 | + |
| 229 | +2. **Verify Server Configuration** |
| 230 | + - Server must have valid Blues Notecard connection |
| 231 | + - Check that client UIDs are correct in the table |
| 232 | + |
| 233 | +## Security Considerations |
| 234 | + |
| 235 | +- Relay commands use device-specific targeting (device:UID:relay.qi) |
| 236 | +- Client UIDs are HTML-escaped in the web UI to prevent XSS |
| 237 | +- Only authenticated users with server access can control relays |
| 238 | +- Consider physical security for devices controlling critical equipment |
| 239 | + |
| 240 | +## Limitations |
| 241 | + |
| 242 | +- Maximum 4 relays per Arduino Opta client |
| 243 | +- No timed auto-off feature yet (documented in code with TODO) |
| 244 | +- Relay state is not persisted across power cycles |
| 245 | +- No feedback mechanism to confirm relay state to server |
| 246 | +- Commands depend on cellular connectivity |
| 247 | + |
| 248 | +## Future Enhancements |
| 249 | + |
| 250 | +Potential improvements for future versions: |
| 251 | + |
| 252 | +1. **Auto-Off Timers** - Automatically turn off relays after a specified duration |
| 253 | +2. **Relay State Feedback** - Report relay states back to server dashboard |
| 254 | +3. **Scheduling** - Time-based relay control (on at 8am, off at 5pm) |
| 255 | +4. **Interlock Logic** - Prevent certain relay combinations |
| 256 | +5. **Persistent State** - Remember relay states across power cycles |
| 257 | +6. **Relay Groups** - Control multiple relays across multiple clients as a group |
| 258 | + |
| 259 | +## References |
| 260 | + |
| 261 | +- [Arduino Opta Documentation](https://docs.arduino.cc/hardware/opta) |
| 262 | +- [Blues Notecard Device-to-Device Communication](https://dev.blues.io/guides-and-tutorials/routing-data-to-cloud/device-to-device/) |
| 263 | +- [TankAlarm Fleet Implementation](FLEET_IMPLEMENTATION_SUMMARY.md) |
| 264 | +- [Blues Notehub Fleet Management](https://dev.blues.io/reference/notehub-api/fleet-api/) |
0 commit comments