|
| 1 | +// Adafruit IO - Analog Devices ADT7410 + ADXL343 Example |
| 2 | +// |
| 3 | +// Adafruit invests time and resources providing this open source code. |
| 4 | +// Please support Adafruit and open source hardware by purchasing |
| 5 | +// products from Adafruit! |
| 6 | +// |
| 7 | +// Written by Brent Rubell for Adafruit Industries |
| 8 | +// Copyright (c) 2019 Adafruit Industries |
| 9 | +// Licensed under the MIT license. |
| 10 | +// |
| 11 | +// All text above must be included in any redistribution. |
| 12 | + |
| 13 | +/************************ Adafruit IO Config *******************************/ |
| 14 | +// visit io.adafruit.com if you need to create an account, |
| 15 | +// or if you need your Adafruit IO key. |
| 16 | +#define IO_USERNAME "YOUR_IO_USERNAME" |
| 17 | +#define IO_KEY "YOUR_IO_KEY" |
| 18 | + |
| 19 | +/******************************* WiFi Config ********************************/ |
| 20 | +#define WIFI_SSID "WIFI_NAME" |
| 21 | +#define WIFI_PASS "WIFI_PASS" |
| 22 | + |
| 23 | +// comment out the following two lines if you are using fona or ethernet |
| 24 | +#include "AdafruitIO_WiFi.h" |
| 25 | +AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS); |
| 26 | + |
| 27 | +/************************** Configuration ***********************************/ |
| 28 | +// time between sending data to adafruit io, in seconds. |
| 29 | +#define IO_DELAY 5 |
| 30 | +/************************ Example Starts Here *******************************/ |
| 31 | +#include <Wire.h> |
| 32 | +#include <Adafruit_Sensor.h> |
| 33 | +#include "Adafruit_ADT7410.h" |
| 34 | +#include <Adafruit_ADXL343.h> |
| 35 | + |
| 36 | +float tempC, accelX, accelY, accelZ; |
| 37 | + |
| 38 | +// Create the ADT7410 temperature sensor object |
| 39 | +Adafruit_ADT7410 tempsensor = Adafruit_ADT7410(); |
| 40 | + |
| 41 | +// Create the ADXL343 accelerometer sensor object |
| 42 | +Adafruit_ADXL343 accel = Adafruit_ADXL343(12345); |
| 43 | + |
| 44 | +// set up the 'temperature' feed |
| 45 | +AdafruitIO_Feed *huzzah_temperature = io.feed("temperature"); |
| 46 | + |
| 47 | +// set up the 'accelX' feed |
| 48 | +AdafruitIO_Feed *huzzah_accel_x = io.feed("accelX"); |
| 49 | + |
| 50 | +// set up the 'accelY' feed |
| 51 | +AdafruitIO_Feed *huzzah_accel_y = io.feed("accelY"); |
| 52 | + |
| 53 | +// set up the 'accelZ' feed |
| 54 | +AdafruitIO_Feed *huzzah_accel_z= io.feed("accelZ"); |
| 55 | + |
| 56 | +void setup() |
| 57 | +{ |
| 58 | + // start the serial connection |
| 59 | + Serial.begin(115200); |
| 60 | + |
| 61 | + // wait for serial monitor to open |
| 62 | + while (!Serial) |
| 63 | + ; |
| 64 | + |
| 65 | + Serial.println("Adafruit IO - ADT7410 + ADX343"); |
| 66 | + |
| 67 | + /* Initialise the ADXL343 */ |
| 68 | + if(!accel.begin()) |
| 69 | + { |
| 70 | + /* There was a problem detecting the ADXL343 ... check your connections */ |
| 71 | + Serial.println("Ooops, no ADXL343 detected ... Check your wiring!"); |
| 72 | + while(1); |
| 73 | + } |
| 74 | + |
| 75 | + /* Set the range to whatever is appropriate for your project */ |
| 76 | + accel.setRange(ADXL343_RANGE_16_G); |
| 77 | + |
| 78 | + /* Initialise the ADT7410 */ |
| 79 | + if (!tempsensor.begin()) |
| 80 | + { |
| 81 | + Serial.println("Couldn't find ADT7410!"); |
| 82 | + while (1) |
| 83 | + ; |
| 84 | + } |
| 85 | + |
| 86 | + // sensor takes 250 ms to get first readings |
| 87 | + delay(250); |
| 88 | + |
| 89 | + // connect to io.adafruit.com |
| 90 | + Serial.print("Connecting to Adafruit IO"); |
| 91 | + io.connect(); |
| 92 | + |
| 93 | + // wait for a connection |
| 94 | + while (io.status() < AIO_CONNECTED) |
| 95 | + { |
| 96 | + Serial.print("."); |
| 97 | + delay(500); |
| 98 | + } |
| 99 | + |
| 100 | + // we are connected |
| 101 | + Serial.println(); |
| 102 | + Serial.println(io.statusText()); |
| 103 | +} |
| 104 | + |
| 105 | +void loop() |
| 106 | +{ |
| 107 | + // io.run(); is required for all sketches. |
| 108 | + // it should always be present at the top of your loop |
| 109 | + // function. it keeps the client connected to |
| 110 | + // io.adafruit.com, and processes any incoming data. |
| 111 | + io.run(); |
| 112 | + |
| 113 | + /* Get a new accel. sensor event */ |
| 114 | + sensors_event_t event; |
| 115 | + accel.getEvent(&event); |
| 116 | + |
| 117 | + accelX = event.acceleration.x; |
| 118 | + accelY = event.acceleration.y; |
| 119 | + accelZ = event.acceleration.z; |
| 120 | + |
| 121 | + /* Display the results (acceleration is measured in m/s^2) */ |
| 122 | + Serial.print("X: "); Serial.print(accelX); Serial.print(" "); |
| 123 | + Serial.print("Y: "); Serial.print(accelY); Serial.print(" "); |
| 124 | + Serial.print("Z: "); Serial.print(accelZ); Serial.print(" ");Serial.println("m/s^2 "); |
| 125 | + |
| 126 | + // Read and print out the temperature |
| 127 | + tempC = tempsensor.readTempC(); |
| 128 | + Serial.print("Temperature: "); Serial.print(tempC); Serial.println("C"); |
| 129 | + |
| 130 | + Serial.println("Sending to Adafruit IO..."); |
| 131 | + huzzah_temperature->save(tempC, 0, 0, 0, 2); |
| 132 | + huzzah_accel_x->save(accelX); |
| 133 | + huzzah_accel_y->save(accelY); |
| 134 | + huzzah_accel_z->save(accelZ); |
| 135 | + Serial.println("Data sent!"); |
| 136 | + |
| 137 | + Serial.print("Waiting ");Serial.print(IO_DELAY);Serial.println(" seconds..."); |
| 138 | + // wait IO_DELAY seconds between sends |
| 139 | + for (int i = 0; i < IO_DELAY; i++) |
| 140 | + { |
| 141 | + delay(1000); |
| 142 | + } |
| 143 | +} |
0 commit comments