hellomqttt to thingspeak mqtt and ifttt

Dependencies:   Servo MQTTPacket FP

Committer:
jasonberry
Date:
Wed May 05 14:48:01 2021 +0000
Revision:
25:ca1b1098c77f
TEST

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jasonberry 25:ca1b1098c77f 1 /* Copyright (c) <year> <copyright holders>, MIT License
jasonberry 25:ca1b1098c77f 2 *
jasonberry 25:ca1b1098c77f 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
jasonberry 25:ca1b1098c77f 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
jasonberry 25:ca1b1098c77f 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
jasonberry 25:ca1b1098c77f 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
jasonberry 25:ca1b1098c77f 7 * furnished to do so, subject to the following conditions:
jasonberry 25:ca1b1098c77f 8 *
jasonberry 25:ca1b1098c77f 9 * The above copyright notice and this permission notice shall be included in all copies or
jasonberry 25:ca1b1098c77f 10 * substantial portions of the Software.
jasonberry 25:ca1b1098c77f 11 *
jasonberry 25:ca1b1098c77f 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
jasonberry 25:ca1b1098c77f 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
jasonberry 25:ca1b1098c77f 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
jasonberry 25:ca1b1098c77f 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jasonberry 25:ca1b1098c77f 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jasonberry 25:ca1b1098c77f 17 */
jasonberry 25:ca1b1098c77f 18
jasonberry 25:ca1b1098c77f 19 #include "mbed.h"
jasonberry 25:ca1b1098c77f 20
jasonberry 25:ca1b1098c77f 21
jasonberry 25:ca1b1098c77f 22 #ifndef MMA7660_H
jasonberry 25:ca1b1098c77f 23 #define MMA7660_H
jasonberry 25:ca1b1098c77f 24
jasonberry 25:ca1b1098c77f 25 #define MMA7660_ADDRESS 0x98
jasonberry 25:ca1b1098c77f 26 #define MMA7660_SENSITIVITY 21.33
jasonberry 25:ca1b1098c77f 27
jasonberry 25:ca1b1098c77f 28 #define MMA7660_XOUT_R 0x00
jasonberry 25:ca1b1098c77f 29 #define MMA7660_YOUT_R 0x01
jasonberry 25:ca1b1098c77f 30 #define MMA7660_ZOUT_R 0x02
jasonberry 25:ca1b1098c77f 31 #define MMA7660_TILT_R 0x03
jasonberry 25:ca1b1098c77f 32 #define MMA7660_INT_R 0x06
jasonberry 25:ca1b1098c77f 33 #define MMA7660_MODE_R 0x07
jasonberry 25:ca1b1098c77f 34 #define MMA7660_SR_R 0x08
jasonberry 25:ca1b1098c77f 35
jasonberry 25:ca1b1098c77f 36
jasonberry 25:ca1b1098c77f 37 /** An interface for the MMA7660 triple axis accelerometer
jasonberry 25:ca1b1098c77f 38 *
jasonberry 25:ca1b1098c77f 39 * @code
jasonberry 25:ca1b1098c77f 40 * //Uses the measured z-acceleration to drive leds 2 and 3 of the mbed
jasonberry 25:ca1b1098c77f 41 *
jasonberry 25:ca1b1098c77f 42 * #include "mbed.h"
jasonberry 25:ca1b1098c77f 43 * #include "MMA7660.h"
jasonberry 25:ca1b1098c77f 44 *
jasonberry 25:ca1b1098c77f 45 * MMA7660 MMA(p28, p27);
jasonberry 25:ca1b1098c77f 46 *
jasonberry 25:ca1b1098c77f 47 * DigitalOut connectionLed(LED1);
jasonberry 25:ca1b1098c77f 48 * PwmOut Zaxis_p(LED2);
jasonberry 25:ca1b1098c77f 49 * PwmOut Zaxis_n(LED3);
jasonberry 25:ca1b1098c77f 50 *
jasonberry 25:ca1b1098c77f 51 * int main() {
jasonberry 25:ca1b1098c77f 52 * if (MMA.testConnection())
jasonberry 25:ca1b1098c77f 53 * connectionLed = 1;
jasonberry 25:ca1b1098c77f 54 *
jasonberry 25:ca1b1098c77f 55 * while(1) {
jasonberry 25:ca1b1098c77f 56 * Zaxis_p = MMA.z();
jasonberry 25:ca1b1098c77f 57 * Zaxis_n = -MMA.z();
jasonberry 25:ca1b1098c77f 58 * }
jasonberry 25:ca1b1098c77f 59 *
jasonberry 25:ca1b1098c77f 60 * }
jasonberry 25:ca1b1098c77f 61 * @endcode
jasonberry 25:ca1b1098c77f 62 */
jasonberry 25:ca1b1098c77f 63 class MMA7660
jasonberry 25:ca1b1098c77f 64 {
jasonberry 25:ca1b1098c77f 65 public:
jasonberry 25:ca1b1098c77f 66 /**
jasonberry 25:ca1b1098c77f 67 * The 6 different orientations and unknown
jasonberry 25:ca1b1098c77f 68 *
jasonberry 25:ca1b1098c77f 69 * Up & Down = X-axis
jasonberry 25:ca1b1098c77f 70 * Right & Left = Y-axis
jasonberry 25:ca1b1098c77f 71 * Back & Front = Z-axis
jasonberry 25:ca1b1098c77f 72 *
jasonberry 25:ca1b1098c77f 73 */
jasonberry 25:ca1b1098c77f 74 enum Orientation {Up, Down,
jasonberry 25:ca1b1098c77f 75 Right, Left,
jasonberry 25:ca1b1098c77f 76 Back, Front,
jasonberry 25:ca1b1098c77f 77 Unknown
jasonberry 25:ca1b1098c77f 78 };
jasonberry 25:ca1b1098c77f 79
jasonberry 25:ca1b1098c77f 80 /**
jasonberry 25:ca1b1098c77f 81 * Creates a new MMA7660 object
jasonberry 25:ca1b1098c77f 82 *
jasonberry 25:ca1b1098c77f 83 * @param sda - I2C data pin
jasonberry 25:ca1b1098c77f 84 * @param scl - I2C clock pin
jasonberry 25:ca1b1098c77f 85 * @param active - true (default) to enable the device, false to keep it standby
jasonberry 25:ca1b1098c77f 86 */
jasonberry 25:ca1b1098c77f 87 MMA7660(PinName sda, PinName scl, bool active = true);
jasonberry 25:ca1b1098c77f 88
jasonberry 25:ca1b1098c77f 89 /**
jasonberry 25:ca1b1098c77f 90 * Tests if communication is possible with the MMA7660
jasonberry 25:ca1b1098c77f 91 *
jasonberry 25:ca1b1098c77f 92 * Because the MMA7660 lacks a WHO_AM_I register, this function can only check
jasonberry 25:ca1b1098c77f 93 * if there is an I2C device that responds to the MMA7660 address
jasonberry 25:ca1b1098c77f 94 *
jasonberry 25:ca1b1098c77f 95 * @param return - true for successfull connection, false for no connection
jasonberry 25:ca1b1098c77f 96 */
jasonberry 25:ca1b1098c77f 97 bool testConnection( void );
jasonberry 25:ca1b1098c77f 98
jasonberry 25:ca1b1098c77f 99 /**
jasonberry 25:ca1b1098c77f 100 * Sets the active state of the MMA7660
jasonberry 25:ca1b1098c77f 101 *
jasonberry 25:ca1b1098c77f 102 * Note: This is unrelated to awake/sleep mode
jasonberry 25:ca1b1098c77f 103 *
jasonberry 25:ca1b1098c77f 104 * @param state - true for active, false for standby
jasonberry 25:ca1b1098c77f 105 */
jasonberry 25:ca1b1098c77f 106 void setActive( bool state);
jasonberry 25:ca1b1098c77f 107
jasonberry 25:ca1b1098c77f 108 /**
jasonberry 25:ca1b1098c77f 109 * Reads acceleration data from the sensor
jasonberry 25:ca1b1098c77f 110 *
jasonberry 25:ca1b1098c77f 111 * When the parameter is a pointer to an integer array it will be the raw data.
jasonberry 25:ca1b1098c77f 112 * When it is a pointer to a float array it will be the acceleration in g's
jasonberry 25:ca1b1098c77f 113 *
jasonberry 25:ca1b1098c77f 114 * @param data - pointer to array with length 3 where the acceleration data will be stored, X-Y-Z
jasonberry 25:ca1b1098c77f 115 */
jasonberry 25:ca1b1098c77f 116 void readData( int *data);
jasonberry 25:ca1b1098c77f 117 void readData( float *data);
jasonberry 25:ca1b1098c77f 118
jasonberry 25:ca1b1098c77f 119 /**
jasonberry 25:ca1b1098c77f 120 * Get X-data
jasonberry 25:ca1b1098c77f 121 *
jasonberry 25:ca1b1098c77f 122 * @param return - X-acceleration in g's
jasonberry 25:ca1b1098c77f 123 */
jasonberry 25:ca1b1098c77f 124 float x( void );
jasonberry 25:ca1b1098c77f 125
jasonberry 25:ca1b1098c77f 126 /**
jasonberry 25:ca1b1098c77f 127 * Get Y-data
jasonberry 25:ca1b1098c77f 128 *
jasonberry 25:ca1b1098c77f 129 * @param return - Y-acceleration in g's
jasonberry 25:ca1b1098c77f 130 */
jasonberry 25:ca1b1098c77f 131 float y( void );
jasonberry 25:ca1b1098c77f 132
jasonberry 25:ca1b1098c77f 133 /**
jasonberry 25:ca1b1098c77f 134 * Get Z-data
jasonberry 25:ca1b1098c77f 135 *
jasonberry 25:ca1b1098c77f 136 * @param return - Z-acceleration in g's
jasonberry 25:ca1b1098c77f 137 */
jasonberry 25:ca1b1098c77f 138 float z( void );
jasonberry 25:ca1b1098c77f 139
jasonberry 25:ca1b1098c77f 140 /**
jasonberry 25:ca1b1098c77f 141 * Sets the active samplerate
jasonberry 25:ca1b1098c77f 142 *
jasonberry 25:ca1b1098c77f 143 * The entered samplerate will be rounded to nearest supported samplerate.
jasonberry 25:ca1b1098c77f 144 * Supported samplerates are: 120 - 64 - 32 - 16 - 8 - 4 - 2 - 1 samples/second.
jasonberry 25:ca1b1098c77f 145 *
jasonberry 25:ca1b1098c77f 146 * @param samplerate - the samplerate that will be set
jasonberry 25:ca1b1098c77f 147 */
jasonberry 25:ca1b1098c77f 148 void setSampleRate(int samplerate);
jasonberry 25:ca1b1098c77f 149
jasonberry 25:ca1b1098c77f 150 /**
jasonberry 25:ca1b1098c77f 151 * Returns if it is on its front, back, or unknown side
jasonberry 25:ca1b1098c77f 152 *
jasonberry 25:ca1b1098c77f 153 * This is read from MMA7760s registers, page 12 of datasheet
jasonberry 25:ca1b1098c77f 154 *
jasonberry 25:ca1b1098c77f 155 * @param return - Front, Back or Unknown orientation
jasonberry 25:ca1b1098c77f 156 */
jasonberry 25:ca1b1098c77f 157 Orientation getSide( void );
jasonberry 25:ca1b1098c77f 158
jasonberry 25:ca1b1098c77f 159 /**
jasonberry 25:ca1b1098c77f 160 * Returns if it is on it left, right, down or up side
jasonberry 25:ca1b1098c77f 161 *
jasonberry 25:ca1b1098c77f 162 * This is read from MMA7760s registers, page 12 of datasheet
jasonberry 25:ca1b1098c77f 163 *
jasonberry 25:ca1b1098c77f 164 * @param return - Left, Right, Down, Up or Unknown orientation
jasonberry 25:ca1b1098c77f 165 */
jasonberry 25:ca1b1098c77f 166 Orientation getOrientation ( void );
jasonberry 25:ca1b1098c77f 167
jasonberry 25:ca1b1098c77f 168
jasonberry 25:ca1b1098c77f 169 private:
jasonberry 25:ca1b1098c77f 170
jasonberry 25:ca1b1098c77f 171 /**
jasonberry 25:ca1b1098c77f 172 * Writes data to the device
jasonberry 25:ca1b1098c77f 173 *
jasonberry 25:ca1b1098c77f 174 * @param adress - register address to write to
jasonberry 25:ca1b1098c77f 175 * @param data - data to write
jasonberry 25:ca1b1098c77f 176 */
jasonberry 25:ca1b1098c77f 177 void write( char address, char data);
jasonberry 25:ca1b1098c77f 178
jasonberry 25:ca1b1098c77f 179 /**
jasonberry 25:ca1b1098c77f 180 * Read data from the device
jasonberry 25:ca1b1098c77f 181 *
jasonberry 25:ca1b1098c77f 182 * @param adress - register address to write to
jasonberry 25:ca1b1098c77f 183 * @return - data from the register specified by RA
jasonberry 25:ca1b1098c77f 184 */
jasonberry 25:ca1b1098c77f 185 char read( char adress);
jasonberry 25:ca1b1098c77f 186
jasonberry 25:ca1b1098c77f 187 /**
jasonberry 25:ca1b1098c77f 188 * Read multiple regigsters from the device, more efficient than using multiple normal reads.
jasonberry 25:ca1b1098c77f 189 *
jasonberry 25:ca1b1098c77f 190 * @param adress - register address to write to
jasonberry 25:ca1b1098c77f 191 * @param length - number of bytes to read
jasonberry 25:ca1b1098c77f 192 * @param data - pointer where the data needs to be written to
jasonberry 25:ca1b1098c77f 193 */
jasonberry 25:ca1b1098c77f 194 void read( char adress, char *data, int length);
jasonberry 25:ca1b1098c77f 195
jasonberry 25:ca1b1098c77f 196 /**
jasonberry 25:ca1b1098c77f 197 * Reads single axis
jasonberry 25:ca1b1098c77f 198 */
jasonberry 25:ca1b1098c77f 199 float getSingle(int number);
jasonberry 25:ca1b1098c77f 200
jasonberry 25:ca1b1098c77f 201 I2C _i2c;
jasonberry 25:ca1b1098c77f 202 bool active;
jasonberry 25:ca1b1098c77f 203 float samplerate;
jasonberry 25:ca1b1098c77f 204 };
jasonberry 25:ca1b1098c77f 205
jasonberry 25:ca1b1098c77f 206
jasonberry 25:ca1b1098c77f 207 #endif