ADXL345 test on L476
Revision 2:2a57e2a50796, committed 2018-01-24
- Comitter:
- tifo
- Date:
- Wed Jan 24 14:56:54 2018 +0000
- Parent:
- 1:2098adebc6da
- Child:
- 3:618d78c7c53a
- Commit message:
- lte module added
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sat Dec 16 11:22:15 2017 +0000
+++ b/main.cpp Wed Jan 24 14:56:54 2018 +0000
@@ -1,6 +1,13 @@
#include "mbed.h"
#include "adxl345.h"
+#include <string>
+// skywire -------------------
+/* --CHANGE THIS FOR YOUR SETUP-- */
+#define DeviceID "yourDeviceIDhere" //Freeboard DweetIO unique ID
+/* --CHANGE THIS FOR YORU SETUP (IF APPLICABLE)-- */
+string APN = "yourAPNhere";
+// ===========================
ADXL345 adxl;
@@ -12,113 +19,40 @@
volatile char message_counter = 0;
volatile char input_flag = false;
-volatile char latitude[13] = {' ','1',' ','3',' ',' ',' ',' ',' ',' ',' ',' ',' '};
-volatile char longtitude[13] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};;
+volatile char latitude[15] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
+volatile char longtitude[15] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
volatile char coorFlag = 0;
-Serial mySerial(SERIAL_TX, SERIAL_RX, 9600); // tx, rx
-Serial gpsSerial(A0, A1, 9600); // tx, rx
+// skywire ------------------------
+DigitalOut skywire_en(PA_6); // Skywire Enable
+DigitalOut skywire_rts(PA_7); // Skywire Send
+Serial skywire(PA_9,PA_10); // Serial comms to Skywire
+// Variables for UART comms
+volatile int rx_in=0;
+volatile int rx_out=0;
+const int buffer_size = 600;
+char rx_buffer[buffer_size+1];
+char rx_line[buffer_size];
+// char array for reading from Skywire
+char str[255];
+// ===============================
+
+Serial pcSerial(SERIAL_TX, SERIAL_RX, 9600); // tx, rx // pc
+Serial gpsSerial(A0, A1, 9600); // tx, rx // gps
I2C i2cAcc(I2C_SDA, I2C_SCL);
+DigitalOut led(LED1);
+
void ADXL_ISR();
void rxHandler();
-int main() {
-
- i2cAcc.frequency(100000);
-
- adxl.powerOn(); // Power on the ADXL345
-
- adxl.setRangeSetting(8); // Give the range settings
- // Accepted values are 2g, 4g, 8g or 16g
- // Higher Values = Wider Measurement Range
- // Lower Values = Greater Sensitivity
-
-
- adxl.setActivityXYZ(1, 0, 0); // Set to activate movement detection in the axes "adxl.setActivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF)
- adxl.setActivityThreshold(75); // 62.5mg per increment // Set activity // Inactivity thresholds (0-255)
-
- adxl.setInactivityXYZ(1, 0, 0); // Set to detect inactivity in all the axes "adxl.setInactivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF)
- adxl.setInactivityThreshold(75); // 62.5mg per increment // Set inactivity // Inactivity thresholds (0-255)
- adxl.setTimeInactivity(10); // How many seconds of no activity is inactive?
-
- adxl.setTapDetectionOnXYZ(0, 0, 1); // Detect taps in the directions turned ON "adxl.setTapDetectionOnX(X, Y, Z);" (1 == ON, 0 == OFF)
-
- // Set values for what is considered a TAP and what is a DOUBLE TAP (0-255)
- adxl.setTapThreshold(50); // 62.5 mg per increment
- adxl.setTapDuration(15); // 625 μs per increment
- adxl.setDoubleTapLatency(80); // 1.25 ms per increment
- adxl.setDoubleTapWindow(200); // 1.25 ms per increment
-
- // Set values for what is considered FREE FALL (0-255)
- adxl.setFreeFallThreshold(10); // (5 - 9) recommended - 62.5mg per increment
- adxl.setFreeFallDuration(10); // (20 - 70) recommended - 5ms per increment
-
- // Setting all interupts to take place on INT1 pin
- adxl.setImportantInterruptMapping(1, 1, 1, 1, 1); // Sets "adxl.setEveryInterruptMapping(single tap, double tap, free fall, activity, inactivity);"
- // Accepts only 1 or 2 values for pins INT1 and INT2. This chooses the pin on the ADXL345 to use for Interrupts.
- // This library may have a problem using INT2 pin. Default to INT1 pin.
-
- // Turn on Interrupts for each mode (1 == ON, 0 == OFF)
- adxl.InactivityINT(1);
- adxl.ActivityINT(1);
- adxl.FreeFallINT(1);
- adxl.doubleTapINT(1);
- adxl.singleTapINT(1);
-
-
- mySerial.printf("SparkFun ADXL345 Accelerometer Hook Up Guide Example\n");
-
- gpsSerial.attach(rxHandler);
-
-
-
- while(1) {
-
- // Accelerometer Readings
- //int x,y,z;
- //adxl.readAccel(&x, &y, &z); // Read the accelerometer values and store them in variables declared above x,y,z
-
- // Output Results to Serial
- /* UNCOMMENT TO VIEW X Y Z ACCELEROMETER VALUES */
- //mySerial.printf("X: %i\nY: %i\nZ: %i\n================\n", x, y, z);
- //wait(2);
-
- //ADXL_ISR();
- // You may also choose to avoid using interrupts and simply run the functions within ADXL_ISR();
- // and place it within the loop instead.
- // This may come in handy when it doesn't matter when the action occurs.
- /*
- if(input_flag) // if message received
- {
- // check type of message and either send it directly, or convert to another type and send
- //
- // GGA input, send directly =============================================================
- if(input_buffer[1] == 'G')
- {
- for(char i=0; i<message_counter; i++) // copy message stored to output_buffer;
- message_buffer[i] = input_buffer[i];
-
- message_counter = input_buffer_counter; // set output pointer/counter value to input message counter value
- }
- }
- */
-
- if(coorFlag) // show coordinates
- {
- mySerial.printf("latitude: %s\n", latitude);
- mySerial.printf("longtitude: %s\n", longtitude);
-
- coorFlag = 0;
- }
-
-
- }
-}
+//===================================================================
+//FUNCTIONS
+//===================================================================
/********************* ISR *********************/
/* Look for Interrupts and Triggered Action */
void ADXL_ISR() {
@@ -129,36 +63,15 @@
// Free Fall Detection
if(adxl.triggered(interrupts, ADXL345_FREE_FALL)){
- mySerial.printf("*** FREE FALL ***\n");
- //add code here to do when free fall is sensed
- }
-
- // Inactivity
- if(adxl.triggered(interrupts, ADXL345_INACTIVITY)){
- mySerial.printf("*** INACTIVITY ***\n");
- //add code here to do when inactivity is sensed
- }
-
- // Activity
- if(adxl.triggered(interrupts, ADXL345_ACTIVITY)){
- mySerial.printf("*** ACTIVITY ***\n");
- //add code here to do when activity is sensed
- }
-
- // Double Tap Detection
- if(adxl.triggered(interrupts, ADXL345_DOUBLE_TAP)){
- mySerial.printf("*** DOUBLE TAP ***\n");
- //add code here to do when a 2X tap is sensed
- }
-
- // Tap Detection
- if(adxl.triggered(interrupts, ADXL345_SINGLE_TAP)){
- mySerial.printf("*** TAP ***\n");
- //add code here to do when a tap is sensed
+ pcSerial.printf("*** FREE FALL ***\n");
+
+ led = 1;
+ wait(2);
+ led = 0;
}
}
-
+//===================================================================
// RX interrupt handler
// stores everything into input_buffer
void rxHandler()
@@ -176,14 +89,16 @@
}
if(tmp == '*') // if end of message( every nmea message ends with *+CRC)
- {
+ {
+ input_buffer[input_buffer_counter] = tmp;
+ input_buffer_counter++;
if(input_buffer[3] == 'L') // if nmea string type is GPGLL
{
int t = 0;
int lat = 0;
int lon = 0;
- for(int i=0; i<13; i++) // clear latitude and longtitude
+ for(int i=0; i<15; i++) // clear latitude and longtitude
{
latitude[lat++] = ' ';
longtitude[lon++] = ' ';
@@ -204,9 +119,12 @@
t++; // t points coma after latitude
}
latitude[lat] = input_buffer[t]; // copy coma
+ lat++;
t++;
latitude[lat] = input_buffer[t]; // copy N or S direction
+ lat++;
t++;
+ latitude[lat] = '\0'; // write null character
t++; // set t to first character of longtitude
@@ -220,21 +138,197 @@
t++; // t points coma after longtitude
}
longtitude[lon] = input_buffer[t]; // copy coma
+ lon++;
t++;
longtitude[lon] = input_buffer[t]; // copy W or E direction
-
- coorFlag = 1;
+ lon++;
+ longtitude[lon] = '\0'; // write null character
- gpsSerial.printf("message: %s", input_buffer);
-
+ coorFlag = 1;
}
return;
}
-
input_buffer[input_buffer_counter] = tmp;
input_buffer_counter++;
}
while(gpsSerial.readable());
}
+//===================================================================
+// Read line from the UART
+void read_line()
+{
+ int i;
+ i = 0;
+// Start Critical Section - don't interrupt while changing global buffer variables
+ __disable_irq();
+// Loop reading rx buffer characters until end of line character
+ while ((i==0) || (rx_line[i-1] != '\n')) {
+// Wait if buffer empty
+ if (rx_in == rx_out) {
+// End Critical Section - need to allow rx interrupt to get new characters for buffer
+ __enable_irq();
+ while (rx_in == rx_out) {
+ }
+// Start Critical Section - don't interrupt while changing global buffer variables
+ __disable_irq();
+ }
+ rx_line[i] = rx_buffer[rx_out];
+ i++;
+ rx_out = (rx_out + 1) % buffer_size;
+ }
+// End Critical Section
+ __enable_irq();
+ rx_line[i-1] = 0;
+ return;
+}
+//=======================================================================
+// Wait for specific response
+int WaitForResponse(char* response, int num)
+{
+ do {
+ read_line();
+ pcSerial.printf("Waiting for: %s, Recieved: %s\r\n", response, rx_line);
+ } while (strncmp(rx_line, response, num));
+ return 0;
+}
+
+//==========================================================================
+// Interrupt for the Skywire
+void Skywire_Rx_interrupt()
+{
+// Loop just in case more than one character is in UART's receive FIFO buffer
+// Stop if buffer full
+ while ((skywire.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
+ rx_buffer[rx_in] = skywire.getc();
+ rx_in = (rx_in + 1) % buffer_size;
+ }
+ return;
+}
+
+void data_send()
+{
+ skywire.printf("AT+SQNSD=3,0,80,\"dweet.io\"\r\n");
+ WaitForResponse("CONNECT", 7);
+
+ pcSerial.printf("Sending information...\r\n");
+ // Report the sensor data to dweet.io
+ skywire.printf("POST /dweet/for/%s?latitude=%s&longtitude=%s HTTP/1.0\r\n\r\n", DeviceID, latitude, longtitude);
+
+ // Wait for response from dweet.io
+ WaitForResponse("OK", 2);
+ wait(1);
+}
+
+int main() {
+
+ skywire.baud(115200);
+ skywire.attach(&Skywire_Rx_interrupt, Serial::RxIrq);
+
+ i2cAcc.frequency(100000);
+
+ adxl.powerOn(); // Power on the ADXL345
+
+ adxl.setRangeSetting(8); // Give the range settings
+ // Accepted values are 2g, 4g, 8g or 16g
+ // Higher Values = Wider Measurement Range
+ // Lower Values = Greater Sensitivity
+
+ // Set values for what is considered FREE FALL (0-255)
+ adxl.setFreeFallThreshold(9); // (5 - 9) recommended - 62.5mg per increment
+ adxl.setFreeFallDuration(20); // (20 - 70) recommended - 5ms per increment
+
+ // Setting all interupts to take place on INT1 pin
+ adxl.setImportantInterruptMapping(0, 0, 1, 0, 0); // Sets "adxl.setEveryInterruptMapping(single tap, double tap, free fall, activity, inactivity);"
+ // Accepts only 1 or 2 values for pins INT1 and INT2. This chooses the pin on the ADXL345 to use for Interrupts.
+ // This library may have a problem using INT2 pin. Default to INT1 pin.
+
+ // Turn on Interrupts for each mode (1 == ON, 0 == OFF)
+ adxl.FreeFallINT(1);
+
+
+
+ pcSerial.printf("SparkFun ADXL345 Accelerometer Hook Up Guide Example\n");
+
+ gpsSerial.attach(rxHandler);
+
+ pcSerial.printf("Starting Demo...\r\n");
+ pcSerial.printf("Waiting for Skywire to Boot...\r\n");
+
+ //Enable Skywire
+ skywire_en=0;
+ wait(2);
+ skywire_en=1;
+ wait(2);
+ skywire_en=0;
+
+ // Wait for modem to initialize
+ wait(60);
+
+ //Turn off echo
+ // Helps with checking responses from Skywire
+ pcSerial.printf("Turning off echo...\r\n");
+ skywire.printf("ATE0\r\n");
+ WaitForResponse("OK", 2);
+
+ // Turn on DNS Response Caching
+ // Used on the Telit-based Skywires
+ pcSerial.printf("Turning on DNS Cacheing to improve speed...");
+ skywire.printf("AT#CACHEDNS=1\r\n");
+ WaitForResponse("OK", 2);
+
+ pcSerial.printf("Connecting to Network...\r\n");
+ // get IP address
+ // The last parameter in AT+SQNSCFG sets the timeout if transmit buffer is not full
+ // Time is in hundreds of ms: so, a value of 5 = 500ms
+ pcSerial.printf("Configuring context part 1...\r\n");
+ skywire.printf("AT+SQNSCFG=3,3,300,90,600,5\r\n");
+ WaitForResponse("OK", 2);
+ wait(1);
+ pcSerial.printf("Configuring context part 2...\r\n");
+ skywire.printf("AT+CGDCONT=3,\"IP\",\"vzwinternet\"\r\n");
+ WaitForResponse("OK", 2);
+ wait(1);
+ pcSerial.printf("Activating context...\r\n");
+ skywire.printf("AT+CGACT=1,3\r\n");
+ WaitForResponse("OK", 2);
+
+
+
+ while(1) {
+
+ // Accelerometer Readings
+ //int x,y,z;
+ //adxl.readAccel(&x, &y, &z); // Read the accelerometer values and store them in variables declared above x,y,z
+
+ // Output Results to Serial
+ /* UNCOMMENT TO VIEW X Y Z ACCELEROMETER VALUES */
+ //mySerial.printf("X: %i\nY: %i\nZ: %i\n================\n", x, y, z);
+ //wait(2);
+
+ ADXL_ISR();
+ // You may also choose to avoid using interrupts and simply run the functions within ADXL_ISR();
+ // and place it within the loop instead.
+ // This may come in handy when it doesn't matter when the action occurs.
+
+
+ if(coorFlag) // show coordinates
+ {
+ pcSerial.printf("message: %s\n", input_buffer);
+ pcSerial.printf("latitude: %s\n", latitude);
+ pcSerial.printf("longtitude: %s\n", longtitude);
+
+ data_send();
+
+ coorFlag = 0;
+ }
+
+
+
+ }
+}
+
+
+
+