ADXL345 test on L476

Dependencies:   mbed

Committer:
tifo
Date:
Wed Jan 24 14:56:54 2018 +0000
Revision:
2:2a57e2a50796
Parent:
1:2098adebc6da
Child:
3:618d78c7c53a
lte module added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tifo 0:a0f7c6807a3a 1 #include "mbed.h"
tifo 0:a0f7c6807a3a 2 #include "adxl345.h"
tifo 2:2a57e2a50796 3 #include <string>
tifo 0:a0f7c6807a3a 4
tifo 2:2a57e2a50796 5 // skywire -------------------
tifo 2:2a57e2a50796 6 /* --CHANGE THIS FOR YOUR SETUP-- */
tifo 2:2a57e2a50796 7 #define DeviceID "yourDeviceIDhere" //Freeboard DweetIO unique ID
tifo 2:2a57e2a50796 8 /* --CHANGE THIS FOR YORU SETUP (IF APPLICABLE)-- */
tifo 2:2a57e2a50796 9 string APN = "yourAPNhere";
tifo 2:2a57e2a50796 10 // ===========================
tifo 0:a0f7c6807a3a 11
tifo 0:a0f7c6807a3a 12 ADXL345 adxl;
tifo 0:a0f7c6807a3a 13
tifo 0:a0f7c6807a3a 14 int x, y, z;
tifo 0:a0f7c6807a3a 15
tifo 1:2098adebc6da 16 volatile char input_buffer[90]; // store everything received
tifo 1:2098adebc6da 17 volatile char message_buffer[90]; // store message
tifo 1:2098adebc6da 18 volatile char input_buffer_counter = 0;
tifo 1:2098adebc6da 19 volatile char message_counter = 0;
tifo 1:2098adebc6da 20 volatile char input_flag = false;
tifo 1:2098adebc6da 21
tifo 2:2a57e2a50796 22 volatile char latitude[15] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
tifo 2:2a57e2a50796 23 volatile char longtitude[15] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
tifo 1:2098adebc6da 24 volatile char coorFlag = 0;
tifo 1:2098adebc6da 25
tifo 1:2098adebc6da 26
tifo 2:2a57e2a50796 27 // skywire ------------------------
tifo 2:2a57e2a50796 28 DigitalOut skywire_en(PA_6); // Skywire Enable
tifo 2:2a57e2a50796 29 DigitalOut skywire_rts(PA_7); // Skywire Send
tifo 2:2a57e2a50796 30 Serial skywire(PA_9,PA_10); // Serial comms to Skywire
tifo 2:2a57e2a50796 31 // Variables for UART comms
tifo 2:2a57e2a50796 32 volatile int rx_in=0;
tifo 2:2a57e2a50796 33 volatile int rx_out=0;
tifo 2:2a57e2a50796 34 const int buffer_size = 600;
tifo 2:2a57e2a50796 35 char rx_buffer[buffer_size+1];
tifo 2:2a57e2a50796 36 char rx_line[buffer_size];
tifo 2:2a57e2a50796 37 // char array for reading from Skywire
tifo 2:2a57e2a50796 38 char str[255];
tifo 2:2a57e2a50796 39 // ===============================
tifo 2:2a57e2a50796 40
tifo 2:2a57e2a50796 41 Serial pcSerial(SERIAL_TX, SERIAL_RX, 9600); // tx, rx // pc
tifo 2:2a57e2a50796 42 Serial gpsSerial(A0, A1, 9600); // tx, rx // gps
tifo 0:a0f7c6807a3a 43
tifo 0:a0f7c6807a3a 44 I2C i2cAcc(I2C_SDA, I2C_SCL);
tifo 0:a0f7c6807a3a 45
tifo 2:2a57e2a50796 46 DigitalOut led(LED1);
tifo 2:2a57e2a50796 47
tifo 0:a0f7c6807a3a 48 void ADXL_ISR();
tifo 1:2098adebc6da 49 void rxHandler();
tifo 0:a0f7c6807a3a 50
tifo 0:a0f7c6807a3a 51
tifo 0:a0f7c6807a3a 52
tifo 2:2a57e2a50796 53 //===================================================================
tifo 2:2a57e2a50796 54 //FUNCTIONS
tifo 2:2a57e2a50796 55 //===================================================================
tifo 0:a0f7c6807a3a 56 /********************* ISR *********************/
tifo 0:a0f7c6807a3a 57 /* Look for Interrupts and Triggered Action */
tifo 0:a0f7c6807a3a 58 void ADXL_ISR() {
tifo 0:a0f7c6807a3a 59
tifo 0:a0f7c6807a3a 60 // getInterruptSource clears all triggered actions after returning value
tifo 0:a0f7c6807a3a 61 // Do not call again until you need to recheck for triggered actions
tifo 0:a0f7c6807a3a 62 int interrupts = adxl.getInterruptSource();
tifo 0:a0f7c6807a3a 63
tifo 0:a0f7c6807a3a 64 // Free Fall Detection
tifo 0:a0f7c6807a3a 65 if(adxl.triggered(interrupts, ADXL345_FREE_FALL)){
tifo 2:2a57e2a50796 66 pcSerial.printf("*** FREE FALL ***\n");
tifo 2:2a57e2a50796 67
tifo 2:2a57e2a50796 68 led = 1;
tifo 2:2a57e2a50796 69 wait(2);
tifo 2:2a57e2a50796 70 led = 0;
tifo 0:a0f7c6807a3a 71 }
tifo 0:a0f7c6807a3a 72 }
tifo 0:a0f7c6807a3a 73
tifo 2:2a57e2a50796 74 //===================================================================
tifo 1:2098adebc6da 75 // RX interrupt handler
tifo 1:2098adebc6da 76 // stores everything into input_buffer
tifo 1:2098adebc6da 77 void rxHandler()
tifo 1:2098adebc6da 78 {
tifo 1:2098adebc6da 79 char tmp;
tifo 1:2098adebc6da 80
tifo 1:2098adebc6da 81 do
tifo 1:2098adebc6da 82 {
tifo 1:2098adebc6da 83 tmp = gpsSerial.getc(); // read serial data
tifo 1:2098adebc6da 84
tifo 1:2098adebc6da 85 if(tmp == '$') // if message start character( every nmea message starts with $)
tifo 1:2098adebc6da 86 {
tifo 1:2098adebc6da 87 input_buffer_counter = 0; // reset inut buffer counter
tifo 1:2098adebc6da 88 return;
tifo 1:2098adebc6da 89 }
tifo 1:2098adebc6da 90
tifo 1:2098adebc6da 91 if(tmp == '*') // if end of message( every nmea message ends with *+CRC)
tifo 2:2a57e2a50796 92 {
tifo 2:2a57e2a50796 93 input_buffer[input_buffer_counter] = tmp;
tifo 2:2a57e2a50796 94 input_buffer_counter++;
tifo 1:2098adebc6da 95 if(input_buffer[3] == 'L') // if nmea string type is GPGLL
tifo 1:2098adebc6da 96 {
tifo 1:2098adebc6da 97 int t = 0;
tifo 1:2098adebc6da 98 int lat = 0;
tifo 1:2098adebc6da 99 int lon = 0;
tifo 1:2098adebc6da 100
tifo 2:2a57e2a50796 101 for(int i=0; i<15; i++) // clear latitude and longtitude
tifo 1:2098adebc6da 102 {
tifo 1:2098adebc6da 103 latitude[lat++] = ' ';
tifo 1:2098adebc6da 104 longtitude[lon++] = ' ';
tifo 1:2098adebc6da 105 }
tifo 1:2098adebc6da 106
tifo 1:2098adebc6da 107 while(input_buffer[t] != ',') // find coma after GPGLL
tifo 1:2098adebc6da 108 t++; // t points coma after GPGLL
tifo 1:2098adebc6da 109
tifo 1:2098adebc6da 110 t++; // set to to first latitude character
tifo 1:2098adebc6da 111
tifo 1:2098adebc6da 112
tifo 1:2098adebc6da 113 lat = 0;
tifo 1:2098adebc6da 114
tifo 1:2098adebc6da 115 while(input_buffer[t] != ',') // copy latitude value
tifo 1:2098adebc6da 116 {
tifo 1:2098adebc6da 117 latitude[lat] = input_buffer[t];
tifo 1:2098adebc6da 118 lat++;
tifo 1:2098adebc6da 119 t++; // t points coma after latitude
tifo 1:2098adebc6da 120 }
tifo 1:2098adebc6da 121 latitude[lat] = input_buffer[t]; // copy coma
tifo 2:2a57e2a50796 122 lat++;
tifo 1:2098adebc6da 123 t++;
tifo 1:2098adebc6da 124 latitude[lat] = input_buffer[t]; // copy N or S direction
tifo 2:2a57e2a50796 125 lat++;
tifo 1:2098adebc6da 126 t++;
tifo 2:2a57e2a50796 127 latitude[lat] = '\0'; // write null character
tifo 1:2098adebc6da 128
tifo 1:2098adebc6da 129 t++; // set t to first character of longtitude
tifo 1:2098adebc6da 130
tifo 1:2098adebc6da 131
tifo 1:2098adebc6da 132 lon = 0;
tifo 1:2098adebc6da 133
tifo 1:2098adebc6da 134 while(input_buffer[t] != ',') // copy longtitude value
tifo 1:2098adebc6da 135 {
tifo 1:2098adebc6da 136 longtitude[lon] = input_buffer[t];
tifo 1:2098adebc6da 137 lon++;
tifo 1:2098adebc6da 138 t++; // t points coma after longtitude
tifo 1:2098adebc6da 139 }
tifo 1:2098adebc6da 140 longtitude[lon] = input_buffer[t]; // copy coma
tifo 2:2a57e2a50796 141 lon++;
tifo 1:2098adebc6da 142 t++;
tifo 1:2098adebc6da 143 longtitude[lon] = input_buffer[t]; // copy W or E direction
tifo 2:2a57e2a50796 144 lon++;
tifo 2:2a57e2a50796 145 longtitude[lon] = '\0'; // write null character
tifo 1:2098adebc6da 146
tifo 2:2a57e2a50796 147 coorFlag = 1;
tifo 1:2098adebc6da 148 }
tifo 1:2098adebc6da 149 return;
tifo 1:2098adebc6da 150 }
tifo 1:2098adebc6da 151 input_buffer[input_buffer_counter] = tmp;
tifo 1:2098adebc6da 152 input_buffer_counter++;
tifo 1:2098adebc6da 153 }
tifo 1:2098adebc6da 154 while(gpsSerial.readable());
tifo 1:2098adebc6da 155 }
tifo 1:2098adebc6da 156
tifo 2:2a57e2a50796 157 //===================================================================
tifo 2:2a57e2a50796 158 // Read line from the UART
tifo 2:2a57e2a50796 159 void read_line()
tifo 2:2a57e2a50796 160 {
tifo 2:2a57e2a50796 161 int i;
tifo 2:2a57e2a50796 162 i = 0;
tifo 2:2a57e2a50796 163 // Start Critical Section - don't interrupt while changing global buffer variables
tifo 2:2a57e2a50796 164 __disable_irq();
tifo 2:2a57e2a50796 165 // Loop reading rx buffer characters until end of line character
tifo 2:2a57e2a50796 166 while ((i==0) || (rx_line[i-1] != '\n')) {
tifo 2:2a57e2a50796 167 // Wait if buffer empty
tifo 2:2a57e2a50796 168 if (rx_in == rx_out) {
tifo 2:2a57e2a50796 169 // End Critical Section - need to allow rx interrupt to get new characters for buffer
tifo 2:2a57e2a50796 170 __enable_irq();
tifo 2:2a57e2a50796 171 while (rx_in == rx_out) {
tifo 2:2a57e2a50796 172 }
tifo 2:2a57e2a50796 173 // Start Critical Section - don't interrupt while changing global buffer variables
tifo 2:2a57e2a50796 174 __disable_irq();
tifo 2:2a57e2a50796 175 }
tifo 2:2a57e2a50796 176 rx_line[i] = rx_buffer[rx_out];
tifo 2:2a57e2a50796 177 i++;
tifo 2:2a57e2a50796 178 rx_out = (rx_out + 1) % buffer_size;
tifo 2:2a57e2a50796 179 }
tifo 2:2a57e2a50796 180 // End Critical Section
tifo 2:2a57e2a50796 181 __enable_irq();
tifo 2:2a57e2a50796 182 rx_line[i-1] = 0;
tifo 2:2a57e2a50796 183 return;
tifo 2:2a57e2a50796 184 }
tifo 1:2098adebc6da 185
tifo 2:2a57e2a50796 186 //=======================================================================
tifo 2:2a57e2a50796 187 // Wait for specific response
tifo 2:2a57e2a50796 188 int WaitForResponse(char* response, int num)
tifo 2:2a57e2a50796 189 {
tifo 2:2a57e2a50796 190 do {
tifo 2:2a57e2a50796 191 read_line();
tifo 2:2a57e2a50796 192 pcSerial.printf("Waiting for: %s, Recieved: %s\r\n", response, rx_line);
tifo 2:2a57e2a50796 193 } while (strncmp(rx_line, response, num));
tifo 2:2a57e2a50796 194 return 0;
tifo 2:2a57e2a50796 195 }
tifo 2:2a57e2a50796 196
tifo 2:2a57e2a50796 197 //==========================================================================
tifo 2:2a57e2a50796 198 // Interrupt for the Skywire
tifo 2:2a57e2a50796 199 void Skywire_Rx_interrupt()
tifo 2:2a57e2a50796 200 {
tifo 2:2a57e2a50796 201 // Loop just in case more than one character is in UART's receive FIFO buffer
tifo 2:2a57e2a50796 202 // Stop if buffer full
tifo 2:2a57e2a50796 203 while ((skywire.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
tifo 2:2a57e2a50796 204 rx_buffer[rx_in] = skywire.getc();
tifo 2:2a57e2a50796 205 rx_in = (rx_in + 1) % buffer_size;
tifo 2:2a57e2a50796 206 }
tifo 2:2a57e2a50796 207 return;
tifo 2:2a57e2a50796 208 }
tifo 2:2a57e2a50796 209
tifo 2:2a57e2a50796 210 void data_send()
tifo 2:2a57e2a50796 211 {
tifo 2:2a57e2a50796 212 skywire.printf("AT+SQNSD=3,0,80,\"dweet.io\"\r\n");
tifo 2:2a57e2a50796 213 WaitForResponse("CONNECT", 7);
tifo 2:2a57e2a50796 214
tifo 2:2a57e2a50796 215 pcSerial.printf("Sending information...\r\n");
tifo 2:2a57e2a50796 216 // Report the sensor data to dweet.io
tifo 2:2a57e2a50796 217 skywire.printf("POST /dweet/for/%s?latitude=%s&longtitude=%s HTTP/1.0\r\n\r\n", DeviceID, latitude, longtitude);
tifo 2:2a57e2a50796 218
tifo 2:2a57e2a50796 219 // Wait for response from dweet.io
tifo 2:2a57e2a50796 220 WaitForResponse("OK", 2);
tifo 2:2a57e2a50796 221 wait(1);
tifo 2:2a57e2a50796 222 }
tifo 2:2a57e2a50796 223
tifo 2:2a57e2a50796 224 int main() {
tifo 2:2a57e2a50796 225
tifo 2:2a57e2a50796 226 skywire.baud(115200);
tifo 2:2a57e2a50796 227 skywire.attach(&Skywire_Rx_interrupt, Serial::RxIrq);
tifo 2:2a57e2a50796 228
tifo 2:2a57e2a50796 229 i2cAcc.frequency(100000);
tifo 2:2a57e2a50796 230
tifo 2:2a57e2a50796 231 adxl.powerOn(); // Power on the ADXL345
tifo 2:2a57e2a50796 232
tifo 2:2a57e2a50796 233 adxl.setRangeSetting(8); // Give the range settings
tifo 2:2a57e2a50796 234 // Accepted values are 2g, 4g, 8g or 16g
tifo 2:2a57e2a50796 235 // Higher Values = Wider Measurement Range
tifo 2:2a57e2a50796 236 // Lower Values = Greater Sensitivity
tifo 2:2a57e2a50796 237
tifo 2:2a57e2a50796 238 // Set values for what is considered FREE FALL (0-255)
tifo 2:2a57e2a50796 239 adxl.setFreeFallThreshold(9); // (5 - 9) recommended - 62.5mg per increment
tifo 2:2a57e2a50796 240 adxl.setFreeFallDuration(20); // (20 - 70) recommended - 5ms per increment
tifo 2:2a57e2a50796 241
tifo 2:2a57e2a50796 242 // Setting all interupts to take place on INT1 pin
tifo 2:2a57e2a50796 243 adxl.setImportantInterruptMapping(0, 0, 1, 0, 0); // Sets "adxl.setEveryInterruptMapping(single tap, double tap, free fall, activity, inactivity);"
tifo 2:2a57e2a50796 244 // Accepts only 1 or 2 values for pins INT1 and INT2. This chooses the pin on the ADXL345 to use for Interrupts.
tifo 2:2a57e2a50796 245 // This library may have a problem using INT2 pin. Default to INT1 pin.
tifo 2:2a57e2a50796 246
tifo 2:2a57e2a50796 247 // Turn on Interrupts for each mode (1 == ON, 0 == OFF)
tifo 2:2a57e2a50796 248 adxl.FreeFallINT(1);
tifo 2:2a57e2a50796 249
tifo 2:2a57e2a50796 250
tifo 2:2a57e2a50796 251
tifo 2:2a57e2a50796 252 pcSerial.printf("SparkFun ADXL345 Accelerometer Hook Up Guide Example\n");
tifo 2:2a57e2a50796 253
tifo 2:2a57e2a50796 254 gpsSerial.attach(rxHandler);
tifo 2:2a57e2a50796 255
tifo 2:2a57e2a50796 256 pcSerial.printf("Starting Demo...\r\n");
tifo 2:2a57e2a50796 257 pcSerial.printf("Waiting for Skywire to Boot...\r\n");
tifo 2:2a57e2a50796 258
tifo 2:2a57e2a50796 259 //Enable Skywire
tifo 2:2a57e2a50796 260 skywire_en=0;
tifo 2:2a57e2a50796 261 wait(2);
tifo 2:2a57e2a50796 262 skywire_en=1;
tifo 2:2a57e2a50796 263 wait(2);
tifo 2:2a57e2a50796 264 skywire_en=0;
tifo 2:2a57e2a50796 265
tifo 2:2a57e2a50796 266 // Wait for modem to initialize
tifo 2:2a57e2a50796 267 wait(60);
tifo 2:2a57e2a50796 268
tifo 2:2a57e2a50796 269 //Turn off echo
tifo 2:2a57e2a50796 270 // Helps with checking responses from Skywire
tifo 2:2a57e2a50796 271 pcSerial.printf("Turning off echo...\r\n");
tifo 2:2a57e2a50796 272 skywire.printf("ATE0\r\n");
tifo 2:2a57e2a50796 273 WaitForResponse("OK", 2);
tifo 2:2a57e2a50796 274
tifo 2:2a57e2a50796 275 // Turn on DNS Response Caching
tifo 2:2a57e2a50796 276 // Used on the Telit-based Skywires
tifo 2:2a57e2a50796 277 pcSerial.printf("Turning on DNS Cacheing to improve speed...");
tifo 2:2a57e2a50796 278 skywire.printf("AT#CACHEDNS=1\r\n");
tifo 2:2a57e2a50796 279 WaitForResponse("OK", 2);
tifo 2:2a57e2a50796 280
tifo 2:2a57e2a50796 281 pcSerial.printf("Connecting to Network...\r\n");
tifo 2:2a57e2a50796 282 // get IP address
tifo 2:2a57e2a50796 283 // The last parameter in AT+SQNSCFG sets the timeout if transmit buffer is not full
tifo 2:2a57e2a50796 284 // Time is in hundreds of ms: so, a value of 5 = 500ms
tifo 2:2a57e2a50796 285 pcSerial.printf("Configuring context part 1...\r\n");
tifo 2:2a57e2a50796 286 skywire.printf("AT+SQNSCFG=3,3,300,90,600,5\r\n");
tifo 2:2a57e2a50796 287 WaitForResponse("OK", 2);
tifo 2:2a57e2a50796 288 wait(1);
tifo 2:2a57e2a50796 289 pcSerial.printf("Configuring context part 2...\r\n");
tifo 2:2a57e2a50796 290 skywire.printf("AT+CGDCONT=3,\"IP\",\"vzwinternet\"\r\n");
tifo 2:2a57e2a50796 291 WaitForResponse("OK", 2);
tifo 2:2a57e2a50796 292 wait(1);
tifo 2:2a57e2a50796 293 pcSerial.printf("Activating context...\r\n");
tifo 2:2a57e2a50796 294 skywire.printf("AT+CGACT=1,3\r\n");
tifo 2:2a57e2a50796 295 WaitForResponse("OK", 2);
tifo 2:2a57e2a50796 296
tifo 2:2a57e2a50796 297
tifo 2:2a57e2a50796 298
tifo 2:2a57e2a50796 299 while(1) {
tifo 2:2a57e2a50796 300
tifo 2:2a57e2a50796 301 // Accelerometer Readings
tifo 2:2a57e2a50796 302 //int x,y,z;
tifo 2:2a57e2a50796 303 //adxl.readAccel(&x, &y, &z); // Read the accelerometer values and store them in variables declared above x,y,z
tifo 2:2a57e2a50796 304
tifo 2:2a57e2a50796 305 // Output Results to Serial
tifo 2:2a57e2a50796 306 /* UNCOMMENT TO VIEW X Y Z ACCELEROMETER VALUES */
tifo 2:2a57e2a50796 307 //mySerial.printf("X: %i\nY: %i\nZ: %i\n================\n", x, y, z);
tifo 2:2a57e2a50796 308 //wait(2);
tifo 2:2a57e2a50796 309
tifo 2:2a57e2a50796 310 ADXL_ISR();
tifo 2:2a57e2a50796 311 // You may also choose to avoid using interrupts and simply run the functions within ADXL_ISR();
tifo 2:2a57e2a50796 312 // and place it within the loop instead.
tifo 2:2a57e2a50796 313 // This may come in handy when it doesn't matter when the action occurs.
tifo 2:2a57e2a50796 314
tifo 2:2a57e2a50796 315
tifo 2:2a57e2a50796 316 if(coorFlag) // show coordinates
tifo 2:2a57e2a50796 317 {
tifo 2:2a57e2a50796 318 pcSerial.printf("message: %s\n", input_buffer);
tifo 2:2a57e2a50796 319 pcSerial.printf("latitude: %s\n", latitude);
tifo 2:2a57e2a50796 320 pcSerial.printf("longtitude: %s\n", longtitude);
tifo 2:2a57e2a50796 321
tifo 2:2a57e2a50796 322 data_send();
tifo 2:2a57e2a50796 323
tifo 2:2a57e2a50796 324 coorFlag = 0;
tifo 2:2a57e2a50796 325 }
tifo 2:2a57e2a50796 326
tifo 2:2a57e2a50796 327
tifo 2:2a57e2a50796 328
tifo 2:2a57e2a50796 329 }
tifo 2:2a57e2a50796 330 }
tifo 2:2a57e2a50796 331
tifo 2:2a57e2a50796 332
tifo 2:2a57e2a50796 333
tifo 2:2a57e2a50796 334