|
| 1 | +// The example uses an arduno sdk for DCD hub to connect and update |
| 2 | +// Force Sensitive Register (FSR) values on thing's property. |
| 3 | +// |
| 4 | +// In this example, We are using Adafruit Force Sensitive Register (FSR) to measure the presure on the FSR pad. |
| 5 | +// https://learn.adafruit.com/force-sensitive-resistor-fsr |
| 6 | +// |
| 7 | +// Once you uploaded the code, You can check live update on your dcd hub dashboard after a successful |
| 8 | +// connection here: https://dwd.tudelft.nl/subject. |
| 9 | +// |
| 10 | +// by Nirav Malsattar <[email protected]> |
| 11 | +// https://github.com/datacentricdesign/dcd-sdk-arduino |
| 12 | +// |
| 13 | +// Pin Configuration |
| 14 | +// FSR Pin (2nd) - Arduino 3.5v / 5v |
| 15 | +// FSR Pin (Any) - Arduino nano A0 with the 3.3KOhm Register connected to the ground (Check the schematic from the link below) |
| 16 | +// |
| 17 | +// Schematic: https://datacentricdesign.org/docs/2019/04/30/sensors-force |
| 18 | + |
| 19 | + |
| 20 | +#define PRESSURE_PIN A0 |
| 21 | + |
| 22 | +#include "arduino_secrets.h" |
| 23 | +#include <dcd_hub_arduino.h> |
| 24 | + |
| 25 | +dcd_hub_arduino dcdHub; //creates a class object from library |
| 26 | + |
| 27 | +int value, prev_value = - 10000; // int values (read from analog port, both the current and the previous) |
| 28 | +int deviation = 0; // setting the minimum deviation between the measurements (0 by default) |
| 29 | + // up to 512 (although that is pretty useless) |
| 30 | +double voltage_value, newton_value; // Converted to Voltage |
| 31 | + |
| 32 | +void setup() { |
| 33 | + //Initialize serial and wait for port to open: |
| 34 | + Serial.begin(115200); |
| 35 | + while (!Serial) { |
| 36 | + ; // wait for serial port to connect. Needed for native USB port only |
| 37 | + } |
| 38 | + |
| 39 | + // Connects to dcd hub using your secret credential from "arduino_secrets.h" |
| 40 | + //{class_object}.connect(SECRET_SSID, SECRET_PASS, THING_ID, THING_TOKEN) |
| 41 | + |
| 42 | + // Make sure you have stored your credentials on "arduino_secrets.h" before running this command |
| 43 | + |
| 44 | + dcdHub.connect(SECRET_SSID, SECRET_PASS, THING_ID, THING_TOKEN, "Arduino Nano 33 IoT"); |
| 45 | + Serial.println(); |
| 46 | + |
| 47 | + pinMode(PRESSURE_PIN, INPUT); // setting pinmode to read analog value |
| 48 | + |
| 49 | + deviation = 10; // since there's a bit of a drift in the values if you put the same pressure over a certain period |
| 50 | + // we ignore a divergence of around 1 percent around the previous value. |
| 51 | +} |
| 52 | + |
| 53 | +// This is based on a 3.3kΩ resistor being used, with weights on the center that do not linger more than 10 seconds |
| 54 | +// For situations that deviate from this, accuracy can be lost. |
| 55 | +double convert_to_newtons( double voltage) |
| 56 | +{ |
| 57 | + /* General fitting model Exp2: |
| 58 | + f(x) = a*exp(b*x) + c*exp(d*x) |
| 59 | + Coefficients (with 95% confidence bounds): |
| 60 | + a = 0.01419 (0.01163, 0.01676) |
| 61 | + b = 0.9523 (0.8922, 1.012) |
| 62 | + c = -0.01461 (-0.02317, -0.006043) |
| 63 | + d = -2.231 (-6.605, 2.142) |
| 64 | + Goodness of fit: |
| 65 | + SSE: 7.906e-06 |
| 66 | + R-square: 0.9999 |
| 67 | + Adjusted R-square: 0.9997 |
| 68 | + RMSE: 0.001988 |
| 69 | + */ |
| 70 | + double a = 0.01419 ; |
| 71 | + double b = 0.9523; |
| 72 | + double c = -0.01461; |
| 73 | + double d = -2.231; |
| 74 | + |
| 75 | + return( (a*exp(b*voltage) + c*exp(d* voltage)) * 9.81 ); // the result of the fit is in KgF to convert to newton we simply |
| 76 | + // multiply by 9.81, if you want data in KgF, remove the final multiplication! |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +void loop() { |
| 81 | + |
| 82 | + // call keep_alive_mqtt() regularly to avoids being disconnected by the MQTT broker |
| 83 | + dcdHub.keep_alive_mqtt(); |
| 84 | + |
| 85 | + delay(100); // Here we can set the sampling rate, right now 10 Hz |
| 86 | + value = analogRead(PRESSURE_PIN); // reading our analog voltage, careful we only have 10 bit resolution so each |
| 87 | + // measurement step is only 5V ÷ 1024, so our result will be 0 - 1023 |
| 88 | + |
| 89 | + // if value is within the range of [ previous - σ , previous + σ], ignore it (if value is relatively the same) |
| 90 | + // this will help with having data ocuppy your buffer that is not a significant deviation. |
| 91 | + if( value >= (prev_value - deviation) && value <= (prev_value + deviation) ) |
| 92 | + return; |
| 93 | + |
| 94 | + voltage_value = double((value*5)) / 1023; // converting to voltage [ 0, 5] v. |
| 95 | + newton_value = convert_to_newtons(voltage_value); // getting actual force value (careful using this, accuracy may not be ideal) |
| 96 | + // sensitivity after 1Kgf and before 0.06kgf is limited, you can lower the deviation |
| 97 | + // for some improvements |
| 98 | + Serial.print("Pressure: "); |
| 99 | + Serial.print(value); |
| 100 | + Serial.print(" (0 - 1023) steps, "); |
| 101 | + Serial.print(voltage_value); |
| 102 | + Serial.print(" (v), "); |
| 103 | + Serial.print(newton_value); |
| 104 | + Serial.println(" N."); |
| 105 | + |
| 106 | + prev_value = value; |
| 107 | + |
| 108 | + // Some random 1D, 2D, 3D values to upload on the hub. Later, you can replace these with your sensors value. |
| 109 | + float forceValue[] = {newton_value}; |
| 110 | + |
| 111 | + //call to an update_property object to update property value as an array according to it's "proeprty_id" |
| 112 | + //{class_object}.update property (property_id, value[], dimension) |
| 113 | + |
| 114 | + dcdHub.update_property("random-02d3",forceValue, 1); |
| 115 | +} |
0 commit comments