![](/media/cache/group/MI_Logo_Small_Pos_RGB.gif.50x50_q85.png)
MAXREFDES143#: DeepCover Embedded Security in IoT Authenticated Sensing & Notification
Dependencies: MaximInterface mbed
The MAXREFDES143# is an Internet of Things (IoT) embedded security reference design, built to protect an industrial sensing node by means of authentication and notification to a web server. The hardware includes a peripheral module representing a protected sensor node monitoring operating temperature and remaining life of a filter (simulated through ambient light sensing) and an mbed shield representing a controller node responsible for monitoring one or more sensor nodes. The design is hierarchical with each controller node communicating data from connected sensor nodes to a web server that maintains a centralized log and dispatches notifications as necessary. The mbed shield contains a Wi-Fi module, a DS2465 coprocessor with 1-Wire® master function, an LCD, LEDs, and pushbuttons. The protected sensor node contains a DS28E15 authenticator, a DS7505 temperature sensor, and a MAX44009 light sensor. The mbed shield communicates to a web server by the onboard Wi-Fi module and to the protected sensor node with I2C and 1-Wire. The MAXREFDES143# is equipped with a standard shield connector for immediate testing using an mbed board such as the MAX32600MBED#. The simplicity of this design enables rapid integration into any star-topology IoT network requiring the heightened security with low overhead provided by the SHA-256 symmetric-key algorithm.
More information about the MAXREFDES143# is available on the Maxim Integrated website.
main.cpp@35:3d414ba9ab6c, 2018-01-19 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Fri Jan 19 10:33:16 2018 -0600
- Revision:
- 35:3d414ba9ab6c
- Parent:
- 32:0a09505a656d
Updated MaximInterface revision.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IanBenzMaxim | 1:e1c7c1c636af | 1 | /******************************************************************************* |
IanBenzMaxim | 1:e1c7c1c636af | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
IanBenzMaxim | 1:e1c7c1c636af | 3 | * |
IanBenzMaxim | 1:e1c7c1c636af | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
IanBenzMaxim | 1:e1c7c1c636af | 5 | * copy of this software and associated documentation files (the "Software"), |
IanBenzMaxim | 1:e1c7c1c636af | 6 | * to deal in the Software without restriction, including without limitation |
IanBenzMaxim | 1:e1c7c1c636af | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
IanBenzMaxim | 1:e1c7c1c636af | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
IanBenzMaxim | 1:e1c7c1c636af | 9 | * Software is furnished to do so, subject to the following conditions: |
IanBenzMaxim | 1:e1c7c1c636af | 10 | * |
IanBenzMaxim | 1:e1c7c1c636af | 11 | * The above copyright notice and this permission notice shall be included |
IanBenzMaxim | 1:e1c7c1c636af | 12 | * in all copies or substantial portions of the Software. |
IanBenzMaxim | 1:e1c7c1c636af | 13 | * |
IanBenzMaxim | 1:e1c7c1c636af | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
IanBenzMaxim | 1:e1c7c1c636af | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
IanBenzMaxim | 1:e1c7c1c636af | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
IanBenzMaxim | 1:e1c7c1c636af | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
IanBenzMaxim | 1:e1c7c1c636af | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
IanBenzMaxim | 1:e1c7c1c636af | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
IanBenzMaxim | 1:e1c7c1c636af | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
IanBenzMaxim | 1:e1c7c1c636af | 21 | * |
IanBenzMaxim | 1:e1c7c1c636af | 22 | * Except as contained in this notice, the name of Maxim Integrated |
IanBenzMaxim | 1:e1c7c1c636af | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
IanBenzMaxim | 1:e1c7c1c636af | 24 | * Products, Inc. Branding Policy. |
IanBenzMaxim | 1:e1c7c1c636af | 25 | * |
IanBenzMaxim | 1:e1c7c1c636af | 26 | * The mere transfer of this software does not imply any licenses |
IanBenzMaxim | 1:e1c7c1c636af | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
IanBenzMaxim | 1:e1c7c1c636af | 28 | * trademarks, maskwork rights, or any other form of intellectual |
IanBenzMaxim | 1:e1c7c1c636af | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
IanBenzMaxim | 1:e1c7c1c636af | 30 | * ownership rights. |
IanBenzMaxim | 32:0a09505a656d | 31 | *******************************************************************************/ |
IanBenzMaxim | 1:e1c7c1c636af | 32 | |
IanBenzMaxim | 1:e1c7c1c636af | 33 | #include <sstream> |
IanBenzMaxim | 32:0a09505a656d | 34 | #include <mbed.h> |
IanBenzMaxim | 32:0a09505a656d | 35 | #include <MaximInterface/Devices/DS2465.hpp> |
IanBenzMaxim | 32:0a09505a656d | 36 | #include <MaximInterface/Platforms/mbed/I2CMaster.hpp> |
IanBenzMaxim | 32:0a09505a656d | 37 | #include <MaximInterface/Platforms/mbed/Sleep.hpp> |
IanBenzMaxim | 32:0a09505a656d | 38 | #include <MaximInterface/Utilities/RomId.hpp> |
IanBenzMaxim | 25:37ea43ff81be | 39 | #include "SensorData.hpp" |
IanBenzMaxim | 1:e1c7c1c636af | 40 | #include "WebServerInterface.hpp" |
IanBenzMaxim | 1:e1c7c1c636af | 41 | #include "Factory.hpp" |
IanBenzMaxim | 1:e1c7c1c636af | 42 | #include "SensorNode.hpp" |
IanBenzMaxim | 1:e1c7c1c636af | 43 | #include "Display.hpp" |
IanBenzMaxim | 29:590a7561318b | 44 | #include "ESP8266.hpp" |
IanBenzMaxim | 1:e1c7c1c636af | 45 | |
IanBenzMaxim | 32:0a09505a656d | 46 | using namespace MaximInterface; |
IanBenzMaxim | 6:b6bafd0a7013 | 47 | |
IanBenzMaxim | 1:e1c7c1c636af | 48 | /// Main status for the program. |
IanBenzMaxim | 32:0a09505a656d | 49 | enum Status { |
IanBenzMaxim | 32:0a09505a656d | 50 | InitializingController, ///< Configure DS2465 and connect to network. |
IanBenzMaxim | 32:0a09505a656d | 51 | DisplaySessionId, ///< Display ID for use with website. |
IanBenzMaxim | 1:e1c7c1c636af | 52 | SensorNodeNeedsDetection, ///< Prompt user to insert Sensor Node. |
IanBenzMaxim | 32:0a09505a656d | 53 | DetectingSensorNode, ///< Check if Sensor Node present. |
IanBenzMaxim | 1:e1c7c1c636af | 54 | SensorNodeNeedsProvision, ///< Sensor Node needs to be provisioned. |
IanBenzMaxim | 32:0a09505a656d | 55 | ProvisioningSensorNode, ///< Provisioning Sensor Node to factory defaults. |
IanBenzMaxim | 32:0a09505a656d | 56 | NormalOperation, ///< The normal demo operation state. |
IanBenzMaxim | 32:0a09505a656d | 57 | SensorNodeNotAuthentic, ///< Sensor Node failed authentication check. |
IanBenzMaxim | 1:e1c7c1c636af | 58 | ControllerInitializationError, ///< Failed to initialize Controller. |
IanBenzMaxim | 32:0a09505a656d | 59 | ControllerHardwareError, ///< Controller hardware failed unexpectedly. |
IanBenzMaxim | 32:0a09505a656d | 60 | SensorNodeHardwareError ///< Sensor Node hardware failed unexpectedly. |
IanBenzMaxim | 1:e1c7c1c636af | 61 | }; |
IanBenzMaxim | 1:e1c7c1c636af | 62 | |
IanBenzMaxim | 1:e1c7c1c636af | 63 | /// @{ |
IanBenzMaxim | 1:e1c7c1c636af | 64 | /// Configuration options. |
IanBenzMaxim | 32:0a09505a656d | 65 | static const int webPostIntervalMs = 10000; |
IanBenzMaxim | 32:0a09505a656d | 66 | static const int webPostRetryIntervalMs = 1000; |
IanBenzMaxim | 32:0a09505a656d | 67 | static const int maxConsecutiveWebPostErrors = 3; |
IanBenzMaxim | 1:e1c7c1c636af | 68 | /// @} |
IanBenzMaxim | 1:e1c7c1c636af | 69 | |
IanBenzMaxim | 1:e1c7c1c636af | 70 | /// @{ |
IanBenzMaxim | 1:e1c7c1c636af | 71 | /// LCD display colors. |
IanBenzMaxim | 32:0a09505a656d | 72 | static const Display::Color Teal = {0x00, 0xB2, 0xA9}; |
IanBenzMaxim | 32:0a09505a656d | 73 | static const Display::Color Red = {0xFF, 0x00, 0x00}; |
IanBenzMaxim | 32:0a09505a656d | 74 | static const Display::Color Green = {0x00, 0xFF, 0x00}; |
IanBenzMaxim | 32:0a09505a656d | 75 | static const Display::Color Purple = {0x6E, 0x25, 0x85}; |
IanBenzMaxim | 1:e1c7c1c636af | 76 | /// @} |
IanBenzMaxim | 1:e1c7c1c636af | 77 | |
IanBenzMaxim | 1:e1c7c1c636af | 78 | /// @{ |
IanBenzMaxim | 1:e1c7c1c636af | 79 | /// Peripheral and pin definitions |
IanBenzMaxim | 19:b8b0cd35f7b4 | 80 | static DigitalIn provisionButton(D13); |
IanBenzMaxim | 19:b8b0cd35f7b4 | 81 | static DigitalIn invalidateButton(D5); |
IanBenzMaxim | 19:b8b0cd35f7b4 | 82 | static DigitalOut tempAlarmLed(D11, 1); |
IanBenzMaxim | 19:b8b0cd35f7b4 | 83 | static DigitalOut filterLifeAlarmLed(D10, 1); |
IanBenzMaxim | 19:b8b0cd35f7b4 | 84 | static I2C i2c(D14, D15); |
IanBenzMaxim | 1:e1c7c1c636af | 85 | static Display lcd(i2c, 0x78, 0x98); |
IanBenzMaxim | 32:0a09505a656d | 86 | static MaximInterface::mbed::I2CMaster i2cWrapper(i2c); |
IanBenzMaxim | 32:0a09505a656d | 87 | static DS2465 ds2465(MaximInterface::mbed::Sleep::instance(), i2cWrapper, 0x30); |
IanBenzMaxim | 1:e1c7c1c636af | 88 | static SensorNode sensorNode(i2c, 0x90, 0x94, ds2465); |
IanBenzMaxim | 29:590a7561318b | 89 | static ESP8266 esp8266(D1, D0, D2, D3, 38400); |
IanBenzMaxim | 28:e5cdaf13d299 | 90 | static WebServerInterface webIntf(esp8266); |
IanBenzMaxim | 1:e1c7c1c636af | 91 | /// @} |
IanBenzMaxim | 1:e1c7c1c636af | 92 | |
IanBenzMaxim | 32:0a09505a656d | 93 | /// Imitate an invalid controller when posting to web server. |
IanBenzMaxim | 32:0a09505a656d | 94 | static bool useInvalidSecret = false; |
IanBenzMaxim | 1:e1c7c1c636af | 95 | static unsigned int randomSeed = 0; ///< Create extra entropy for challenge. |
IanBenzMaxim | 1:e1c7c1c636af | 96 | static Status currentStatus = InitializingController; |
IanBenzMaxim | 1:e1c7c1c636af | 97 | static bool result = false; |
IanBenzMaxim | 32:0a09505a656d | 98 | /// Account for a few network errors in case of flaky connection. |
IanBenzMaxim | 32:0a09505a656d | 99 | static uint8_t consecutiveWebPostErrors = 0; |
IanBenzMaxim | 1:e1c7c1c636af | 100 | static Timer webPostTimer; ///< Software timer to track web posting interval. |
IanBenzMaxim | 32:0a09505a656d | 101 | static Timer retryTimer; ///< Software timer to track authentication retries. |
IanBenzMaxim | 1:e1c7c1c636af | 102 | |
IanBenzMaxim | 32:0a09505a656d | 103 | /// Invert LEDs for a given amount of time. |
IanBenzMaxim | 32:0a09505a656d | 104 | static void blinkLeds(int time_ms); |
IanBenzMaxim | 32:0a09505a656d | 105 | /// Checks if button is pressed (returns true) and waits for release. |
IanBenzMaxim | 32:0a09505a656d | 106 | static bool buttonPressed(DigitalIn & button); |
IanBenzMaxim | 32:0a09505a656d | 107 | /// Display status message on LCD. |
IanBenzMaxim | 32:0a09505a656d | 108 | static void displayStatus(Status status); |
IanBenzMaxim | 32:0a09505a656d | 109 | /// Display sensor data on the LCD. |
IanBenzMaxim | 32:0a09505a656d | 110 | static void displaySensorData(const SensorData & sensorData); |
IanBenzMaxim | 32:0a09505a656d | 111 | /// Read device's web session ID from it's nonvolatile storage. |
IanBenzMaxim | 32:0a09505a656d | 112 | static bool readWebSessionId(RomId & sessionId); |
IanBenzMaxim | 1:e1c7c1c636af | 113 | |
IanBenzMaxim | 1:e1c7c1c636af | 114 | #ifdef ASSEMBLY_TEST |
IanBenzMaxim | 32:0a09505a656d | 115 | #include "AssemblyTest.hpp" |
IanBenzMaxim | 1:e1c7c1c636af | 116 | #endif |
IanBenzMaxim | 1:e1c7c1c636af | 117 | |
IanBenzMaxim | 32:0a09505a656d | 118 | int main() { |
IanBenzMaxim | 1:e1c7c1c636af | 119 | blinkLeds(500); |
IanBenzMaxim | 32:0a09505a656d | 120 | |
IanBenzMaxim | 1:e1c7c1c636af | 121 | #ifdef ASSEMBLY_TEST |
IanBenzMaxim | 1:e1c7c1c636af | 122 | assemblyTest(); |
IanBenzMaxim | 1:e1c7c1c636af | 123 | #endif |
IanBenzMaxim | 32:0a09505a656d | 124 | |
IanBenzMaxim | 32:0a09505a656d | 125 | while (true) { |
IanBenzMaxim | 1:e1c7c1c636af | 126 | Status nextStatus = currentStatus; |
IanBenzMaxim | 32:0a09505a656d | 127 | switch (currentStatus) { |
IanBenzMaxim | 1:e1c7c1c636af | 128 | case InitializingController: |
IanBenzMaxim | 1:e1c7c1c636af | 129 | i2c.frequency(100000); |
IanBenzMaxim | 1:e1c7c1c636af | 130 | webPostTimer.start(); |
IanBenzMaxim | 32:0a09505a656d | 131 | |
IanBenzMaxim | 1:e1c7c1c636af | 132 | // Set initial LCD state |
IanBenzMaxim | 1:e1c7c1c636af | 133 | lcd.initialize(); |
IanBenzMaxim | 1:e1c7c1c636af | 134 | displayStatus(currentStatus); |
IanBenzMaxim | 32:0a09505a656d | 135 | |
IanBenzMaxim | 1:e1c7c1c636af | 136 | // Connect to Wifi network |
IanBenzMaxim | 29:590a7561318b | 137 | result = webIntf.initialize(); |
IanBenzMaxim | 32:0a09505a656d | 138 | |
IanBenzMaxim | 1:e1c7c1c636af | 139 | // Read session ID |
IanBenzMaxim | 32:0a09505a656d | 140 | if (result) { |
IanBenzMaxim | 32:0a09505a656d | 141 | RomId sessionId; |
IanBenzMaxim | 25:37ea43ff81be | 142 | result = readWebSessionId(sessionId); |
IanBenzMaxim | 32:0a09505a656d | 143 | if (result) { |
IanBenzMaxim | 32:0a09505a656d | 144 | webIntf.setSessionId(sessionId); |
IanBenzMaxim | 25:37ea43ff81be | 145 | } |
IanBenzMaxim | 1:e1c7c1c636af | 146 | } |
IanBenzMaxim | 32:0a09505a656d | 147 | |
IanBenzMaxim | 1:e1c7c1c636af | 148 | // Provision DS2465 with master secret and page data |
IanBenzMaxim | 32:0a09505a656d | 149 | if (result) { |
IanBenzMaxim | 25:37ea43ff81be | 150 | result = provisionCoprocessor(ds2465); |
IanBenzMaxim | 1:e1c7c1c636af | 151 | } |
IanBenzMaxim | 32:0a09505a656d | 152 | |
IanBenzMaxim | 32:0a09505a656d | 153 | if (result) { |
IanBenzMaxim | 1:e1c7c1c636af | 154 | nextStatus = DisplaySessionId; |
IanBenzMaxim | 32:0a09505a656d | 155 | } else { |
IanBenzMaxim | 1:e1c7c1c636af | 156 | nextStatus = ControllerInitializationError; |
IanBenzMaxim | 1:e1c7c1c636af | 157 | } |
IanBenzMaxim | 1:e1c7c1c636af | 158 | break; |
IanBenzMaxim | 32:0a09505a656d | 159 | |
IanBenzMaxim | 1:e1c7c1c636af | 160 | case DisplaySessionId: |
IanBenzMaxim | 1:e1c7c1c636af | 161 | // Wait for user to press Provision button |
IanBenzMaxim | 32:0a09505a656d | 162 | if (buttonPressed(provisionButton)) { |
IanBenzMaxim | 1:e1c7c1c636af | 163 | nextStatus = SensorNodeNeedsDetection; |
IanBenzMaxim | 1:e1c7c1c636af | 164 | } |
IanBenzMaxim | 1:e1c7c1c636af | 165 | break; |
IanBenzMaxim | 32:0a09505a656d | 166 | |
IanBenzMaxim | 1:e1c7c1c636af | 167 | case SensorNodeNeedsDetection: |
IanBenzMaxim | 1:e1c7c1c636af | 168 | // Wait for user to press Provision button |
IanBenzMaxim | 32:0a09505a656d | 169 | if (buttonPressed(provisionButton)) { |
IanBenzMaxim | 1:e1c7c1c636af | 170 | nextStatus = DetectingSensorNode; |
IanBenzMaxim | 1:e1c7c1c636af | 171 | } |
IanBenzMaxim | 1:e1c7c1c636af | 172 | break; |
IanBenzMaxim | 32:0a09505a656d | 173 | |
IanBenzMaxim | 1:e1c7c1c636af | 174 | case DetectingSensorNode: |
IanBenzMaxim | 1:e1c7c1c636af | 175 | // Perform Sensor Node detection sequence |
IanBenzMaxim | 32:0a09505a656d | 176 | switch (sensorNode.detect(randomSeed)) { |
IanBenzMaxim | 1:e1c7c1c636af | 177 | case SensorNode::UnableToCommunicate: |
IanBenzMaxim | 1:e1c7c1c636af | 178 | default: |
IanBenzMaxim | 1:e1c7c1c636af | 179 | nextStatus = SensorNodeHardwareError; |
IanBenzMaxim | 1:e1c7c1c636af | 180 | break; |
IanBenzMaxim | 32:0a09505a656d | 181 | |
IanBenzMaxim | 1:e1c7c1c636af | 182 | case SensorNode::NotProvisioned: |
IanBenzMaxim | 1:e1c7c1c636af | 183 | nextStatus = SensorNodeNeedsProvision; |
IanBenzMaxim | 1:e1c7c1c636af | 184 | break; |
IanBenzMaxim | 32:0a09505a656d | 185 | |
IanBenzMaxim | 1:e1c7c1c636af | 186 | case SensorNode::NotAuthentic: |
IanBenzMaxim | 1:e1c7c1c636af | 187 | nextStatus = SensorNodeNotAuthentic; |
IanBenzMaxim | 1:e1c7c1c636af | 188 | break; |
IanBenzMaxim | 32:0a09505a656d | 189 | |
IanBenzMaxim | 1:e1c7c1c636af | 190 | case SensorNode::Authentic: |
IanBenzMaxim | 1:e1c7c1c636af | 191 | nextStatus = NormalOperation; |
IanBenzMaxim | 1:e1c7c1c636af | 192 | break; |
IanBenzMaxim | 1:e1c7c1c636af | 193 | } |
IanBenzMaxim | 1:e1c7c1c636af | 194 | break; |
IanBenzMaxim | 32:0a09505a656d | 195 | |
IanBenzMaxim | 1:e1c7c1c636af | 196 | case SensorNodeNeedsProvision: |
IanBenzMaxim | 1:e1c7c1c636af | 197 | // Wait for user to press Provision button |
IanBenzMaxim | 32:0a09505a656d | 198 | if (buttonPressed(provisionButton)) { |
IanBenzMaxim | 1:e1c7c1c636af | 199 | nextStatus = ProvisioningSensorNode; |
IanBenzMaxim | 1:e1c7c1c636af | 200 | } |
IanBenzMaxim | 1:e1c7c1c636af | 201 | break; |
IanBenzMaxim | 32:0a09505a656d | 202 | |
IanBenzMaxim | 32:0a09505a656d | 203 | case ProvisioningSensorNode: |
IanBenzMaxim | 32:0a09505a656d | 204 | if (!buttonPressed(invalidateButton)) { // Provision normally |
IanBenzMaxim | 32:0a09505a656d | 205 | if (provisionSensorNode(sensorNode, true)) { |
IanBenzMaxim | 1:e1c7c1c636af | 206 | nextStatus = NormalOperation; |
IanBenzMaxim | 32:0a09505a656d | 207 | } else { |
IanBenzMaxim | 1:e1c7c1c636af | 208 | nextStatus = SensorNodeNotAuthentic; |
IanBenzMaxim | 1:e1c7c1c636af | 209 | } |
IanBenzMaxim | 32:0a09505a656d | 210 | } else { // Invalidate button also pressed; Load invalid secret |
IanBenzMaxim | 1:e1c7c1c636af | 211 | // Provision with invalid secret |
IanBenzMaxim | 32:0a09505a656d | 212 | if (provisionSensorNode(sensorNode, false)) { |
IanBenzMaxim | 1:e1c7c1c636af | 213 | nextStatus = NormalOperation; |
IanBenzMaxim | 32:0a09505a656d | 214 | } else { |
IanBenzMaxim | 1:e1c7c1c636af | 215 | nextStatus = SensorNodeHardwareError; |
IanBenzMaxim | 1:e1c7c1c636af | 216 | } |
IanBenzMaxim | 1:e1c7c1c636af | 217 | } |
IanBenzMaxim | 1:e1c7c1c636af | 218 | break; |
IanBenzMaxim | 32:0a09505a656d | 219 | |
IanBenzMaxim | 32:0a09505a656d | 220 | case NormalOperation: |
IanBenzMaxim | 1:e1c7c1c636af | 221 | // Check if user pressed Provision button |
IanBenzMaxim | 32:0a09505a656d | 222 | if (buttonPressed(provisionButton)) { |
IanBenzMaxim | 1:e1c7c1c636af | 223 | // Re-provision Sensor Node |
IanBenzMaxim | 1:e1c7c1c636af | 224 | nextStatus = ProvisioningSensorNode; |
IanBenzMaxim | 1:e1c7c1c636af | 225 | } |
IanBenzMaxim | 1:e1c7c1c636af | 226 | // Check if user pressed Invalidate button |
IanBenzMaxim | 32:0a09505a656d | 227 | else if (buttonPressed(invalidateButton)) { |
IanBenzMaxim | 1:e1c7c1c636af | 228 | // Toggle between using valid and invalid secret |
IanBenzMaxim | 1:e1c7c1c636af | 229 | // 1 blink = invalid; 2 blinks = valid |
IanBenzMaxim | 1:e1c7c1c636af | 230 | useInvalidSecret = !useInvalidSecret; |
IanBenzMaxim | 1:e1c7c1c636af | 231 | blinkLeds(100); |
IanBenzMaxim | 32:0a09505a656d | 232 | if (!useInvalidSecret) { |
IanBenzMaxim | 1:e1c7c1c636af | 233 | wait_ms(100); |
IanBenzMaxim | 1:e1c7c1c636af | 234 | blinkLeds(100); |
IanBenzMaxim | 1:e1c7c1c636af | 235 | } |
IanBenzMaxim | 1:e1c7c1c636af | 236 | } |
IanBenzMaxim | 1:e1c7c1c636af | 237 | // Check node and display measurements |
IanBenzMaxim | 32:0a09505a656d | 238 | else { |
IanBenzMaxim | 1:e1c7c1c636af | 239 | SensorData sensorData; |
IanBenzMaxim | 1:e1c7c1c636af | 240 | // Read sensor data with authentication |
IanBenzMaxim | 32:0a09505a656d | 241 | switch ( |
IanBenzMaxim | 32:0a09505a656d | 242 | sensorNode.authenticatedReadSensorData(randomSeed, sensorData)) { |
IanBenzMaxim | 1:e1c7c1c636af | 243 | case SensorNode::Authentic: |
IanBenzMaxim | 1:e1c7c1c636af | 244 | // Update measurements on LCD |
IanBenzMaxim | 1:e1c7c1c636af | 245 | displaySensorData(sensorData); |
IanBenzMaxim | 32:0a09505a656d | 246 | |
IanBenzMaxim | 1:e1c7c1c636af | 247 | // Update alarm LEDs |
IanBenzMaxim | 32:0a09505a656d | 248 | tempAlarmLed = !sensorData.tempAlarm(); // Active Low |
IanBenzMaxim | 1:e1c7c1c636af | 249 | filterLifeAlarmLed = !sensorData.filterLifeAlarm(); // Active Low |
IanBenzMaxim | 32:0a09505a656d | 250 | |
IanBenzMaxim | 1:e1c7c1c636af | 251 | // Send measurements to web if time interval reached |
IanBenzMaxim | 32:0a09505a656d | 252 | if (webPostTimer.read_ms() >= webPostIntervalMs) { |
IanBenzMaxim | 1:e1c7c1c636af | 253 | // Format, sign, and transmit data to web server |
IanBenzMaxim | 32:0a09505a656d | 254 | result = webIntf.authPostHttpEvent( |
IanBenzMaxim | 32:0a09505a656d | 255 | ds2465, SensorDataEvent, |
IanBenzMaxim | 32:0a09505a656d | 256 | WebServerInterface::formatSensorDataPostBody(sensorData), |
IanBenzMaxim | 32:0a09505a656d | 257 | !useInvalidSecret); |
IanBenzMaxim | 32:0a09505a656d | 258 | if (result) { |
IanBenzMaxim | 1:e1c7c1c636af | 259 | // Reset timer count after logging sample complete |
IanBenzMaxim | 1:e1c7c1c636af | 260 | webPostTimer.reset(); |
IanBenzMaxim | 1:e1c7c1c636af | 261 | consecutiveWebPostErrors = 0; |
IanBenzMaxim | 1:e1c7c1c636af | 262 | } |
IanBenzMaxim | 1:e1c7c1c636af | 263 | // There was likely an error establishing a web connection |
IanBenzMaxim | 32:0a09505a656d | 264 | else if (++consecutiveWebPostErrors < maxConsecutiveWebPostErrors) { |
IanBenzMaxim | 1:e1c7c1c636af | 265 | // Wait and try again |
IanBenzMaxim | 1:e1c7c1c636af | 266 | wait_ms(webPostRetryIntervalMs); |
IanBenzMaxim | 1:e1c7c1c636af | 267 | } |
IanBenzMaxim | 1:e1c7c1c636af | 268 | // Too many retry attempts |
IanBenzMaxim | 32:0a09505a656d | 269 | else { |
IanBenzMaxim | 1:e1c7c1c636af | 270 | // Assume we have lost network connection |
IanBenzMaxim | 1:e1c7c1c636af | 271 | nextStatus = ControllerHardwareError; |
IanBenzMaxim | 1:e1c7c1c636af | 272 | } |
IanBenzMaxim | 1:e1c7c1c636af | 273 | } |
IanBenzMaxim | 1:e1c7c1c636af | 274 | break; |
IanBenzMaxim | 32:0a09505a656d | 275 | |
IanBenzMaxim | 1:e1c7c1c636af | 276 | case SensorNode::NotAuthentic: |
IanBenzMaxim | 1:e1c7c1c636af | 277 | nextStatus = SensorNodeNotAuthentic; |
IanBenzMaxim | 1:e1c7c1c636af | 278 | break; |
IanBenzMaxim | 32:0a09505a656d | 279 | |
IanBenzMaxim | 1:e1c7c1c636af | 280 | case SensorNode::UnableToCommunicate: |
IanBenzMaxim | 1:e1c7c1c636af | 281 | default: |
IanBenzMaxim | 1:e1c7c1c636af | 282 | nextStatus = SensorNodeHardwareError; |
IanBenzMaxim | 1:e1c7c1c636af | 283 | break; |
IanBenzMaxim | 1:e1c7c1c636af | 284 | } |
IanBenzMaxim | 1:e1c7c1c636af | 285 | } |
IanBenzMaxim | 1:e1c7c1c636af | 286 | break; |
IanBenzMaxim | 32:0a09505a656d | 287 | |
IanBenzMaxim | 1:e1c7c1c636af | 288 | case SensorNodeNotAuthentic: |
IanBenzMaxim | 5:63232ef22abe | 289 | // Wait for some time before retrying authentication |
IanBenzMaxim | 5:63232ef22abe | 290 | retryTimer.reset(); |
IanBenzMaxim | 5:63232ef22abe | 291 | retryTimer.start(); |
IanBenzMaxim | 32:0a09505a656d | 292 | do { |
IanBenzMaxim | 5:63232ef22abe | 293 | // Wait for user to press Provision button |
IanBenzMaxim | 32:0a09505a656d | 294 | if (buttonPressed(provisionButton)) { |
IanBenzMaxim | 5:63232ef22abe | 295 | nextStatus = ProvisioningSensorNode; |
IanBenzMaxim | 5:63232ef22abe | 296 | break; |
IanBenzMaxim | 5:63232ef22abe | 297 | } |
IanBenzMaxim | 5:63232ef22abe | 298 | // Try to authenticate and return to normal operation |
IanBenzMaxim | 32:0a09505a656d | 299 | else if (webPostTimer.read_ms() >= webPostIntervalMs) { |
IanBenzMaxim | 5:63232ef22abe | 300 | // Send event message to server |
IanBenzMaxim | 32:0a09505a656d | 301 | result = webIntf.authPostHttpEvent(ds2465, InvalidSensorEvent, "", |
IanBenzMaxim | 32:0a09505a656d | 302 | !useInvalidSecret); |
IanBenzMaxim | 32:0a09505a656d | 303 | if (result) { |
IanBenzMaxim | 5:63232ef22abe | 304 | // Reset timer count after logging complete |
IanBenzMaxim | 5:63232ef22abe | 305 | webPostTimer.reset(); |
IanBenzMaxim | 5:63232ef22abe | 306 | consecutiveWebPostErrors = 0; |
IanBenzMaxim | 32:0a09505a656d | 307 | |
IanBenzMaxim | 5:63232ef22abe | 308 | // Try to authenticate again |
IanBenzMaxim | 5:63232ef22abe | 309 | nextStatus = SensorNodeNeedsDetection; |
IanBenzMaxim | 32:0a09505a656d | 310 | } else if (++consecutiveWebPostErrors < maxConsecutiveWebPostErrors) { |
IanBenzMaxim | 5:63232ef22abe | 311 | // There was likely an error establishing a web connection |
IanBenzMaxim | 5:63232ef22abe | 312 | // Wait and try again |
IanBenzMaxim | 5:63232ef22abe | 313 | wait_ms(webPostRetryIntervalMs); |
IanBenzMaxim | 5:63232ef22abe | 314 | } |
IanBenzMaxim | 15:8cc4cdea59da | 315 | // Too many retry attempts |
IanBenzMaxim | 32:0a09505a656d | 316 | else { |
IanBenzMaxim | 15:8cc4cdea59da | 317 | // Assume we have lost network connection |
IanBenzMaxim | 5:63232ef22abe | 318 | nextStatus = ControllerHardwareError; |
IanBenzMaxim | 5:63232ef22abe | 319 | break; |
IanBenzMaxim | 5:63232ef22abe | 320 | } |
IanBenzMaxim | 1:e1c7c1c636af | 321 | } |
IanBenzMaxim | 32:0a09505a656d | 322 | } while (retryTimer.read_ms() < webPostIntervalMs); |
IanBenzMaxim | 5:63232ef22abe | 323 | retryTimer.stop(); |
IanBenzMaxim | 1:e1c7c1c636af | 324 | break; |
IanBenzMaxim | 32:0a09505a656d | 325 | |
IanBenzMaxim | 1:e1c7c1c636af | 326 | case ControllerInitializationError: |
IanBenzMaxim | 1:e1c7c1c636af | 327 | case ControllerHardwareError: |
IanBenzMaxim | 1:e1c7c1c636af | 328 | case SensorNodeHardwareError: |
IanBenzMaxim | 1:e1c7c1c636af | 329 | default: |
IanBenzMaxim | 1:e1c7c1c636af | 330 | // Do nothing until user resets |
IanBenzMaxim | 1:e1c7c1c636af | 331 | break; |
IanBenzMaxim | 1:e1c7c1c636af | 332 | } |
IanBenzMaxim | 1:e1c7c1c636af | 333 | // Check if status changed |
IanBenzMaxim | 32:0a09505a656d | 334 | if (currentStatus != nextStatus) { |
IanBenzMaxim | 1:e1c7c1c636af | 335 | currentStatus = nextStatus; |
IanBenzMaxim | 1:e1c7c1c636af | 336 | displayStatus(currentStatus); // Display status message on LCD |
IanBenzMaxim | 1:e1c7c1c636af | 337 | } |
IanBenzMaxim | 32:0a09505a656d | 338 | |
IanBenzMaxim | 1:e1c7c1c636af | 339 | // Change seed value on every loop pass |
IanBenzMaxim | 1:e1c7c1c636af | 340 | randomSeed++; |
IanBenzMaxim | 1:e1c7c1c636af | 341 | } |
IanBenzMaxim | 1:e1c7c1c636af | 342 | } |
IanBenzMaxim | 1:e1c7c1c636af | 343 | |
IanBenzMaxim | 1:e1c7c1c636af | 344 | /// Blink all LEDs for a certain amount of time. |
IanBenzMaxim | 1:e1c7c1c636af | 345 | /// @param time_ms Time in ms to blink for. |
IanBenzMaxim | 32:0a09505a656d | 346 | static void blinkLeds(int time_ms) { |
IanBenzMaxim | 1:e1c7c1c636af | 347 | tempAlarmLed = !tempAlarmLed; |
IanBenzMaxim | 1:e1c7c1c636af | 348 | filterLifeAlarmLed = !filterLifeAlarmLed; |
IanBenzMaxim | 1:e1c7c1c636af | 349 | wait_ms(time_ms); |
IanBenzMaxim | 1:e1c7c1c636af | 350 | tempAlarmLed = !tempAlarmLed; |
IanBenzMaxim | 1:e1c7c1c636af | 351 | filterLifeAlarmLed = !filterLifeAlarmLed; |
IanBenzMaxim | 1:e1c7c1c636af | 352 | } |
IanBenzMaxim | 1:e1c7c1c636af | 353 | |
IanBenzMaxim | 1:e1c7c1c636af | 354 | /// Check if a button is pressed and wait for it to be release. |
IanBenzMaxim | 1:e1c7c1c636af | 355 | /// @param button Active low button to check. |
IanBenzMaxim | 1:e1c7c1c636af | 356 | /// @returns True if pressed. |
IanBenzMaxim | 32:0a09505a656d | 357 | static bool buttonPressed(DigitalIn & button) { |
IanBenzMaxim | 1:e1c7c1c636af | 358 | const int buttonPressed = 0; // Active low |
IanBenzMaxim | 32:0a09505a656d | 359 | if (button == buttonPressed) { |
IanBenzMaxim | 32:0a09505a656d | 360 | while (button == buttonPressed) |
IanBenzMaxim | 32:0a09505a656d | 361 | ; |
IanBenzMaxim | 1:e1c7c1c636af | 362 | return true; |
IanBenzMaxim | 1:e1c7c1c636af | 363 | } |
IanBenzMaxim | 1:e1c7c1c636af | 364 | // else |
IanBenzMaxim | 1:e1c7c1c636af | 365 | return false; |
IanBenzMaxim | 1:e1c7c1c636af | 366 | } |
IanBenzMaxim | 1:e1c7c1c636af | 367 | |
IanBenzMaxim | 1:e1c7c1c636af | 368 | /// Display the current status of the Controller on the LCD display. |
IanBenzMaxim | 32:0a09505a656d | 369 | static void displayStatus(Status status) { |
IanBenzMaxim | 32:0a09505a656d | 370 | switch (status) { |
IanBenzMaxim | 1:e1c7c1c636af | 371 | case InitializingController: |
IanBenzMaxim | 1:e1c7c1c636af | 372 | lcd.writeMessage("Initializing Controller..."); |
IanBenzMaxim | 1:e1c7c1c636af | 373 | lcd.setBackLightColor(Teal); |
IanBenzMaxim | 1:e1c7c1c636af | 374 | break; |
IanBenzMaxim | 32:0a09505a656d | 375 | |
IanBenzMaxim | 1:e1c7c1c636af | 376 | case DisplaySessionId: |
IanBenzMaxim | 25:37ea43ff81be | 377 | lcd.writeLine("ID: " + webIntf.sessionIdString(), Display::FirstLine); |
IanBenzMaxim | 1:e1c7c1c636af | 378 | lcd.writeLine("Provision to begin", Display::SecondLine); |
IanBenzMaxim | 1:e1c7c1c636af | 379 | lcd.setBackLightColor(Teal); |
IanBenzMaxim | 1:e1c7c1c636af | 380 | break; |
IanBenzMaxim | 32:0a09505a656d | 381 | |
IanBenzMaxim | 1:e1c7c1c636af | 382 | case SensorNodeNeedsDetection: |
IanBenzMaxim | 1:e1c7c1c636af | 383 | lcd.writeMessage("Insert Sensor Node and press Provision"); |
IanBenzMaxim | 1:e1c7c1c636af | 384 | lcd.setBackLightColor(Teal); |
IanBenzMaxim | 1:e1c7c1c636af | 385 | break; |
IanBenzMaxim | 32:0a09505a656d | 386 | |
IanBenzMaxim | 1:e1c7c1c636af | 387 | case DetectingSensorNode: |
IanBenzMaxim | 1:e1c7c1c636af | 388 | lcd.writeMessage("Detecting Sensor Node..."); |
IanBenzMaxim | 1:e1c7c1c636af | 389 | lcd.setBackLightColor(Teal); |
IanBenzMaxim | 1:e1c7c1c636af | 390 | break; |
IanBenzMaxim | 32:0a09505a656d | 391 | |
IanBenzMaxim | 1:e1c7c1c636af | 392 | case SensorNodeNeedsProvision: |
IanBenzMaxim | 1:e1c7c1c636af | 393 | lcd.writeMessage("Sensor Node Needs Provision"); |
IanBenzMaxim | 1:e1c7c1c636af | 394 | lcd.setBackLightColor(Teal); |
IanBenzMaxim | 1:e1c7c1c636af | 395 | break; |
IanBenzMaxim | 32:0a09505a656d | 396 | |
IanBenzMaxim | 1:e1c7c1c636af | 397 | case ProvisioningSensorNode: |
IanBenzMaxim | 1:e1c7c1c636af | 398 | lcd.writeMessage("Provisioning Sensor Node"); |
IanBenzMaxim | 1:e1c7c1c636af | 399 | lcd.setBackLightColor(Teal); |
IanBenzMaxim | 1:e1c7c1c636af | 400 | break; |
IanBenzMaxim | 32:0a09505a656d | 401 | |
IanBenzMaxim | 1:e1c7c1c636af | 402 | case NormalOperation: |
IanBenzMaxim | 1:e1c7c1c636af | 403 | // Everything handled in displaySensorData() |
IanBenzMaxim | 1:e1c7c1c636af | 404 | break; |
IanBenzMaxim | 32:0a09505a656d | 405 | |
IanBenzMaxim | 1:e1c7c1c636af | 406 | case SensorNodeNotAuthentic: |
IanBenzMaxim | 1:e1c7c1c636af | 407 | lcd.writeMessage("Sensor Node Not Authentic"); |
IanBenzMaxim | 25:37ea43ff81be | 408 | lcd.setBackLightColor(Purple); |
IanBenzMaxim | 1:e1c7c1c636af | 409 | break; |
IanBenzMaxim | 32:0a09505a656d | 410 | |
IanBenzMaxim | 1:e1c7c1c636af | 411 | case ControllerInitializationError: |
IanBenzMaxim | 1:e1c7c1c636af | 412 | lcd.writeMessage("Initialization Error Check Wi-Fi"); |
IanBenzMaxim | 1:e1c7c1c636af | 413 | lcd.setBackLightColor(Red); |
IanBenzMaxim | 1:e1c7c1c636af | 414 | break; |
IanBenzMaxim | 32:0a09505a656d | 415 | |
IanBenzMaxim | 1:e1c7c1c636af | 416 | case ControllerHardwareError: |
IanBenzMaxim | 15:8cc4cdea59da | 417 | lcd.writeMessage("Controller Hardware Error: Check Wi-Fi"); |
IanBenzMaxim | 1:e1c7c1c636af | 418 | lcd.setBackLightColor(Red); |
IanBenzMaxim | 1:e1c7c1c636af | 419 | break; |
IanBenzMaxim | 32:0a09505a656d | 420 | |
IanBenzMaxim | 1:e1c7c1c636af | 421 | case SensorNodeHardwareError: |
IanBenzMaxim | 1:e1c7c1c636af | 422 | lcd.writeMessage("Sensor Node Hardware Error"); |
IanBenzMaxim | 1:e1c7c1c636af | 423 | lcd.setBackLightColor(Red); |
IanBenzMaxim | 1:e1c7c1c636af | 424 | break; |
IanBenzMaxim | 1:e1c7c1c636af | 425 | } |
IanBenzMaxim | 1:e1c7c1c636af | 426 | } |
IanBenzMaxim | 1:e1c7c1c636af | 427 | |
IanBenzMaxim | 1:e1c7c1c636af | 428 | /// Display sensor data on the LCD display during normal operation. |
IanBenzMaxim | 32:0a09505a656d | 429 | static void displaySensorData(const SensorData & sensorData) { |
IanBenzMaxim | 1:e1c7c1c636af | 430 | std::ostringstream stream; |
IanBenzMaxim | 25:37ea43ff81be | 431 | stream << "Chiller Temp: " << static_cast<int>(sensorData.temp) << "C"; |
IanBenzMaxim | 1:e1c7c1c636af | 432 | lcd.writeCompleteLine(stream.str(), Display::FirstLine); |
IanBenzMaxim | 1:e1c7c1c636af | 433 | stream.str(""); // Clear stream |
IanBenzMaxim | 32:0a09505a656d | 434 | stream << "Filter Life: " << static_cast<unsigned int>(sensorData.filterLife) |
IanBenzMaxim | 32:0a09505a656d | 435 | << "%"; |
IanBenzMaxim | 1:e1c7c1c636af | 436 | lcd.writeCompleteLine(stream.str(), Display::SecondLine); |
IanBenzMaxim | 25:37ea43ff81be | 437 | lcd.setBackLightColor(Green); |
IanBenzMaxim | 1:e1c7c1c636af | 438 | } |
IanBenzMaxim | 1:e1c7c1c636af | 439 | |
IanBenzMaxim | 1:e1c7c1c636af | 440 | /// Read the Session ID to use with the web server from ROM. |
IanBenzMaxim | 1:e1c7c1c636af | 441 | /// @note Session ID is taken from the ROM ID of the MAX66242. |
IanBenzMaxim | 1:e1c7c1c636af | 442 | /// @param[out] Session ID string. |
IanBenzMaxim | 1:e1c7c1c636af | 443 | /// @returns True on success. |
IanBenzMaxim | 32:0a09505a656d | 444 | static bool readWebSessionId(RomId & sessionId) { |
IanBenzMaxim | 1:e1c7c1c636af | 445 | const uint8_t I2C_address = 0x32; |
IanBenzMaxim | 1:e1c7c1c636af | 446 | const uint8_t ROM_address = 0x68; |
IanBenzMaxim | 1:e1c7c1c636af | 447 | RomId romId; |
IanBenzMaxim | 32:0a09505a656d | 448 | |
IanBenzMaxim | 1:e1c7c1c636af | 449 | // Set register pointer |
IanBenzMaxim | 4:71d578d3af22 | 450 | if (i2c.write(I2C_address, reinterpret_cast<const char *>(&ROM_address), 1) != 0) |
IanBenzMaxim | 1:e1c7c1c636af | 451 | return false; |
IanBenzMaxim | 1:e1c7c1c636af | 452 | // Read ROM ID |
IanBenzMaxim | 32:0a09505a656d | 453 | if (i2c.read(I2C_address, reinterpret_cast<char *>(romId.data()), |
IanBenzMaxim | 32:0a09505a656d | 454 | romId.size()) != 0) |
IanBenzMaxim | 1:e1c7c1c636af | 455 | return false; |
IanBenzMaxim | 1:e1c7c1c636af | 456 | // Check if CRC valid |
IanBenzMaxim | 32:0a09505a656d | 457 | if (!valid(romId)) |
IanBenzMaxim | 1:e1c7c1c636af | 458 | return false; |
IanBenzMaxim | 25:37ea43ff81be | 459 | sessionId = romId; |
IanBenzMaxim | 1:e1c7c1c636af | 460 | return true; |
IanBenzMaxim | 1:e1c7c1c636af | 461 | } |