Hello,
I am facing some problems achieving a project's objective.
I am developing a low-cost accelerometer to measure seismic activity in an area.I have used Arduino Uno, MEMS ADXL 345, Micro SD card Module with a 16 GB SD card, SIM 900 GPRS Arduino Shield with a sim.
I have a thingspeak accout, with an API key and 3 channels to take data from the ADXL 345. My goal is that, the setup will take data in each 0.005 seconds and store that data into the sd card. Every hour, the data in sd card will be uploaded to the Thingspeak IoT. The SD card will be cleared after 30 days ensuring all the data is on the Thingspeak platform.
There will be a trigger mechanism. If resultant acceleration in the XY plane exceeds a certain value and if the acceleration in Z axis exceeds a certain value, the accelerometer will send a message to, say 5 phone numbers, that "The acceleration value has exceeded, Take Precaution". This process has to be instantaneous as the warning will save lives and sooner the message is delivered, the better.
I have already wired the whole connections.
My connections are
ADXL345 GND -> Arduino GND
ADXL345 VCC -> Arduino 5V
ADXL345 SDA -> Arduino A4 (SDA)
ADXL345 SCL -> Arduino A5 (SCL)
MicroSD VCC -> Arduino 5V
MicroSD GND -> Arduino GND
MicroSD MISO -> Arduino 12 (MISO)
MicroSD MOSI -> Arduino 11 (MOSI)
MicroSD SCK -> Arduino 13 (SCK)
MicroSD CS -> Arduino 4
- SIM900 RX to Arduino digital pin 8
- SIM900 TX to Arduino digital pin 7
- SIM900 GND to Arduino GND
- SIM900 VCC to external power supply
I am using the code attached below. But I am not getting any results. Is the data sampling rate 200 samples/ second achievable? And what was wrong with my code?
..............
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <SPI.h>
#include <SD.h>
#include <SoftwareSerial.h>
// Constants for accelerometer
#define ADXL345_ADDRESS (0x53)
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
// Constants for SD card
const int chipSelect = 4;
// Constants for SIM900
SoftwareSerial sim900(7, 8); // RX, TX
// Constants for Thingspeak
const char* thingspeakServer = "api.thingspeak.com";
const char* apiKey = "YOUR_API_KEY";
const int channelID = 12345;
// Variables for data and timing
unsigned long lastUploadTime = 0;
unsigned long uploadInterval = 3600000; // 1 hour
unsigned long currentTime;
float accelerationX, accelerationY, accelerationZ;
bool triggerSent = false;
float triggerThresholdXY = 10.0; // Set your desired threshold
float triggerThresholdZ = 5.0; // Set your desired threshold
void setup() {
Serial.begin(9600);
sim900.begin(9600);
// Initialize accelerometer
if (!accel.begin()) {
Serial.println("Could not find a valid ADXL345 sensor, check wiring!");
while (1);
}
// Initialize SD card
if (!SD.begin(chipSelect)) {
Serial.println("SD card initialization failed!");
while (1);
}
// Connect to GPRS network
sim900.print("AT+CGATT?\r");
delay(1000);
sim900.println("AT+SAPBR=3,1,"APN","your_apn"");
delay(2000);
sim900.println("AT+SAPBR=1,1");
delay(2000);
sim900.println("AT+HTTPINIT");
delay(2000);
}
void loop() {
currentTime = millis();
// Read accelerometer data
sensors_event_t event;
accel.getEvent(&event);
accelerationX = event.acceleration.x;
accelerationY = event.acceleration.y;
accelerationZ = event.acceleration.z;
// Store data on SD card
String dataString = String(currentTime) + "," + String(accelerationX) + "," + String(accelerationY) + "," + String(accelerationZ);
File dataFile = SD.open("data.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
}
// Check for trigger conditions
if (abs(accelerationX) > triggerThresholdXY && abs(accelerationY) > triggerThresholdXY && abs(accelerationZ) > triggerThresholdZ && !triggerSent) {
sendTriggerSMS();
triggerSent = true;
}
// Upload data to Thingspeak every hour
if (currentTime - lastUploadTime >= uploadInterval) {
uploadDataToThingspeak();
lastUploadTime = currentTime;
}
}
void sendTriggerSMS() {
sim900.println("AT+CMGF=1");
delay(1000);
sim900.println("AT+CMGS="+1234567890""); // Replace with the recipient phone numbers
delay(1000);
sim900.println("The acceleration value has exceeded, Take Precaution");
delay(100);
sim900.println((char)26);
delay(1000);
sim900.println();
delay(1000);
}
void uploadDataToThingspeak() {
File dataFile = SD.open("data.txt");
if (dataFile) {
sim900.println("AT+HTTPPARA="CID",1");
delay(2000);
sim900.println("AT+HTTPPARA="URL","http://api.thingspeak.com/update\"");
delay(2000);
sim900.print("AT+HTTPDATA=");
sim900.println(dataFile.size() + 20);
delay(1000);
sim900.println("Content-Type: application/x-www-form-urlencoded");
delay(1000);
sim900.println();
delay(1000);
sim900.print("field1=");
while (dataFile.available()) {
sim900.write(dataFile.read());
}
dataFile.close();
delay(2000);
sim900.println();
delay(5000);
sim900.println("AT+HTTPACTION=1");
delay(10000);
sim900.println("AT+HTTPTERM");
delay(2000);
}
}
<CODE>
..............