0

I have a sdcard connected to arduino uno with chipselect 10, and sim7600EI connected to sofware serial pins (6,5) of arduino. When I trying to upload data to thingspeak with an HTTP request it works seamlessly, however When I begin to add sdcard code and try to store values of my MPU6050 sensor , the values are stored in sd card but my sim7600 stopped sending data through HTTP. What do I do. I am so confused on how to proceed.Below is my code

#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 = "apikey";
bool sdOK = false;


// MPU6050 object
MPU6050 mpu;

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;
 
#define DEBUG true
#define LTE_RESET_PIN 6
#define LTE_PWRKEY_PIN 5
#define LTE_FLIGHT_PIN 8
//#define Sensor_PIN 3  //D3-DHT11
 
//DHT  dht(Sensor_PIN,DHT11);
 
void setup()
{
    mySerial.begin(115200);
    while (!mySerial)
    {
      ; // wait for Arduino serial Monitor port to connect
    }
    Serial.begin(115200);
    Wire.begin();
    SD.begin(10);
    pinMode(7, OUTPUT);
    digitalWrite(7, HIGH);
    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(2000);
    digitalWrite(LTE_PWRKEY_PIN, LOW);
    
    pinMode(LTE_FLIGHT_PIN, OUTPUT);
    digitalWrite(LTE_FLIGHT_PIN, LOW);//Normal Mode*/
    
    delay(5000);
 
    
    /*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()
{//File dataFile = SD.open("gyro.csv", FILE_WRITE);
//dataFile.close();
getdata();
saveDataToSD();
 
    delay(1000);   
}
void getdata(){    //--------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();
     Serial.print("ACCx: ");
     Serial.print(accX);
     Serial.println("%");
     Serial.print("accy: ");
     Serial.print(accY);
     Serial.println("*C");
     delay(1000);
 
    //-----------HTTP---------------------
    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);}
 
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;
}
void saveDataToSD() {
File dataFile = SD.open("gyro.csv", FILE_WRITE);
    if (dataFile) {
        dataFile.print(",");
        dataFile.print(mpu.getAccelerationX());
        dataFile.print(",");
        dataFile.print(mpu.getAccelerationY());
        dataFile.print(",");
        dataFile.print(mpu.getAccelerationZ());
        dataFile.print(",");
        dataFile.print(mpu.getRotationX());
        dataFile.print(",");
        dataFile.print(mpu.getRotationY());
        dataFile.print(",");
        dataFile.println(mpu.getRotationZ());
        dataFile.close();
        Serial.println("saved");
        delay(500);
    }
}
  • getdata() also calls sendData() so it's doing two things despite saying it only does one thing. Then immediately saveDataToSD() is being called. Could this be interfering with the sendData activity in the sim module? Try adding a long delay between getdata() and saveDataToSD() to see if that fixes it. If not, take the mcu code in getData() and put it in getMCUData() and create a new function sendDataToSim() passing it the data from getMCUData(). You can then comment out the call to getMCUData() to see if that was the problem. In the past I've had problems with SPI which transactions fixed. – codebrane Sep 01 '23 at 15:02
  • This is not related to your problem but to your design. What's the point of sending `AT+CREG?` without parsing what is returned? How do you know that your module is even registered to the network successfully? Every one of your AT command sent could failed. My advise is to use a library like [tinyGSM](https://github.com/vshymanskyy/TinyGSM), it will save you a lot of time for writing your own AT command sender and parser. – hcheung Sep 02 '23 at 01:59

0 Answers0