I found a peculiar problem where I have a MPU6050, sim7600ei,and SDcard module, Connected to arduinio. When I try to send data to thingspeak using the AT command by calling only sim7600, then the data gets uploaded seamlessly, However When i try to add saveDataToSD() the It doesnt upload at all. I get AT+HTTPACTION= error all the time. I am stuck What should I do?
#include <stdio.h>
#include <string.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <MPU6050.h>
#include <SD.h>
SoftwareSerial mySerial(6,5); // RX, TX
//Change the API key to yours
String Apikey = "your_api_key";
// MPU6050 object
MPU6050 mpu;
boolean condition = false; // Initialize the condition to false
int16_t accx, accy, accz;
int16_t acc_cx, acc_cy, acc_cz;
int16_t gyx , gyy, gyz;
int16_t gy_cx, gy_cy, gy_cz;
const int numReadings = 10;
int16_t accXReadings[numReadings];
int16_t accYReadings[numReadings];
int16_t accZReadings[numReadings];
int16_t gyroXReadings[numReadings];
int16_t gyroYReadings[numReadings];
int16_t gyroZReadings[numReadings];
int readingIndex = 0;
const int ssPin = 10; // Slave Select (SS) pin
#define DEBUG true
#define LTE_RESET_PIN 3
#define LTE_PWRKEY_PIN 4
#define LTE_FLIGHT_PIN 9
//#define Sensor_PIN 3 //D3-DHT11
// Set SPI pins to OUTPUT mode
void setup()
{
mySerial.begin(115200);
//while (!mySerial)
Serial.begin(115200);
Wire.begin();
pinMode(7, OUTPUT);
digitalWrite(7, HIGH);
// pinMode(ssPin, OUTPUT); // SS (Slave Select)
// pinMode(11, OUTPUT); // MOSI (Master Out Slave In)
// pinMode(12, OUTPUT); // MISO (Master In Slave Out)
// pinMode(13, OUTPUT); // SCK (Serial Clock)
//SD.begin(10);
mpu.initialize();
//Serial.begin(UART_BAUD, SERIAL_8N1, MODEM_RXD, MODEM_TXD);
pinMode(LTE_RESET_PIN, OUTPUT);
digitalWrite(LTE_RESET_PIN, LOW);
pinMode(LTE_PWRKEY_PIN, OUTPUT);
digitalWrite(LTE_RESET_PIN, LOW);
delay(100);
digitalWrite(LTE_PWRKEY_PIN, HIGH);
delay(500);
digitalWrite(LTE_PWRKEY_PIN, LOW);
pinMode(LTE_FLIGHT_PIN, OUTPUT);
digitalWrite(LTE_FLIGHT_PIN, LOW);//Normal Mode*/
delay(1000);
/*ModuleState = moduleStateCheck();
if (ModuleState == false) //if it's off, turn on it.
{
digitalWrite(PWR_KEY, LOW);
delay(3000);
digitalWrite(PWR_KEY, HIGH);
delay(10000);
mySerial.println("Now turnning the SIM7600 on.");
}*/
sendData("AT+CCID", 3000, DEBUG);
sendData("AT+CREG?", 3000, DEBUG);
sendData("AT+CGATT=1", 1000, DEBUG);
sendData("AT+CGACT=1,1", 1000, DEBUG);
sendData("AT+CGDCONT=1,\"IP\",\"airtelgprs.com\"", 1000, DEBUG);
//sendData("AT+CIPSTART=\"TCP\",\"www.mirocast.com\",80", 2000, DEBUG);
Serial.println("4G HTTP Test Begin!");
//dht.begin();
delay(1000);
}
void loop()
{
//--------Get temperature and humidity-------------
int16_t accX = mpu.getAccelerationX();
int16_t accY = mpu.getAccelerationY();
int16_t accZ = mpu.getAccelerationZ();
int16_t gyroX = mpu.getRotationX();
int16_t gyroY = mpu.getRotationY();
int16_t gyroZ = mpu.getRotationZ();
unsigned long currentTime = millis();
// Store readings in arrays
accXReadings[readingIndex] = accX;
accYReadings[readingIndex] = accY;
accZReadings[readingIndex] = accZ;
gyroXReadings[readingIndex] = gyroX;
gyroYReadings[readingIndex] = gyroY;
gyroZReadings[readingIndex] = gyroZ;
readingIndex = (readingIndex + 1) % numReadings; // Move to the next index
if (readingIndex == 0) {
// Calculate and print the average of the last 10 readings
float accXAvg = calculateAverage(accXReadings, numReadings);
float accYAvg = calculateAverage(accYReadings, numReadings);
float accZAvg = calculateAverage(accZReadings, numReadings);
float gyroXAvg = calculateAverage(gyroXReadings, numReadings);
float gyroYAvg = calculateAverage(gyroYReadings, numReadings);
float gyroZAvg = calculateAverage(gyroZReadings, numReadings);
Serial.print("ACCx: ");
Serial.print(accX);
Serial.println("%");
Serial.print("accy: ");
Serial.print(accY);
Serial.println("*C");
delay(1000);
// enableSPI();
saveDataToSD();//( accX,accY,accZ,gyroX,gyroY,gyroZ);
// disableSPI();
//condition = !condition;
//-----------HTTP---------------------
// if(condition){
// Serial.print("true on");
String http_str = "AT+HTTPPARA=\"URL\",\"https://api.thingspeak.com/update?api_key=" + Apikey + "&field1=" + (String)accX + "&field2=" + (String)accY + "&field3=" + (String)accZ + "&field4=" +(String)gyroX + "&field5=" + (String)gyroY + "&field6=" + (String)gyroZ +"\"\r\n";
Serial.println(http_str);
sendData("AT+HTTPINIT\r\n", 2000, DEBUG);
sendData(http_str, 2000, DEBUG);
sendData("AT+HTTPACTION=0\r\n", 3000, DEBUG);
sendData("AT+HTTPTERM\r\n", 3000, DEBUG);
// }
// else
// {
// Serial.print("false on");
// String http_str = "AT+HTTPPARA=\"URL\",\"https://api.thingspeak.com/update?api_key=" + Apikey + "&field4=" + (String)gyroX + "&field5=" + (String)gyroY + "&field6=" + (String)gyroZ + "\"\r\n";//"&field4=" +(String)gyroX + "&field5=" + (String)gyroY + "&field6=" + (String)gyroZ +"\"\r\n";
// Serial.println(http_str);
// sendData("AT+HTTPINIT\r\n", 2000, DEBUG);
// sendData(http_str, 2000, DEBUG);
// sendData("AT+HTTPACTION=0\r\n", 3000, DEBUG);
// sendData("AT+HTTPTERM\r\n", 3000, DEBUG);
// }
delay(3000);
}}
bool moduleStateCheck()
{
int i = 0;
bool moduleState = false;
for (i = 0; i < 5; i++)
{
String msg = String("");
msg = sendData("AT", 1000, DEBUG);
if (msg.indexOf("OK") >= 0)
{
mySerial.println("SIM7600 Module had turned on.");
moduleState = true;
return moduleState;
}
delay(1000);
}
return moduleState;
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
mySerial.println(command);
long int time = millis();
while ( (time + timeout) > millis())
{
while (mySerial.available())
{
char c = mySerial.read();
response += c;
}
}
if (debug)
{
Serial.print(response);
}
return response;
}
float calculateAverage(int16_t readings[], int numReadings) {
long sum = 0;
for (int i = 0; i < numReadings; i++) {
sum += readings[i];
}
return static_cast<float>(sum) / numReadings;
}
void saveDataToSD(){//(int accX, int accY, int accZ, int gyroX, int gyroY, int gyroZ) {
File dataFile = SD.open("te.txt", FILE_WRITE);
if (dataFile) {
//dataFile.print(formattedTime);
//dataFile.print(",");
dataFile.print("test");
// dataFile.print(",");
// dataFile.print(accY);
// dataFile.print(",");
// dataFile.print(accZ);
// dataFile.print(",");
// dataFile.print(gyroX);
// dataFile.print(",");
// dataFile.print(gyroY);
// dataFile.print(",");
// dataFile.println(gyroZ);
Serial.println("saved");
dataFile.close();
}}