Hello all, my project is using sensors to capture data and calculate the data, then sending the result via wireless module to the database and also storing the result to a SD card. The problem is that I can’t send the data to the database but can store the data to a SD card. After several modifications, now I can send the data to the database but can’t store the data to a SD card… Here is the code:
#include <SPI.h>
#include "SdFat.h"
SdFat SD;
File mySensorData;
//SD card is the only module using the SPI.
const int chipSelect = 10; //SD Card's CS pin
/* DHT11 */
#include "DHT.h"
#define dhtType DHT11 //DHT11
int humidity = 0.0;
int celsius = 0.0;
/* Module addresses */
const int DS1307Address = 0x68;
const int DHT11ModuleAddress = 8;
const int TRAICAddress = 15;
const int vibrationAddress = 17;
const int PIRAddress = 18;
int EP = 6;
int addressList[8] = {0};
int addressChannelMapping[8] = {8,17,0,0,0,0,0,0}; //mapping address
int counter = 0;
float DataMapping[10] = {};
float sum = 0;
float average;
float maxval;
float PAPR;
float Xpeak;
float Yrms;
int vibcont = 0;
int Y;
/*Confirm TRAIC exist */
//Due to the reasons that I need to map the setting of the database(.js file),
//I use “pin” to define the pins of sensors.
struct isTRAICModuleExist {
bool isExist;
int pin;
};
isTRAICModuleExist checkTraicExist = {false , -1};
//Digital Data
int pirCount = 0, vibrationCount = 0;
int pirSensorData = 0;
unsigned long vibrationSensorData;
unsigned long nextSensorData;
/* I2C */
#include "Wire.h"
/* I2C Scan */
extern "C"
{
#include "utility/twi.h" // from Wire library, so we can do bus scanning
}
/* I2C LiquidCrystal */
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
/* variable for sending Json data */
#include <ArduinoJson.h>
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(chipSelect, OUTPUT); //SD
digitalWrite(chipSelect, HIGH);
SD.begin(chipSelect);
printMUX();
}
void loop() {
functionSelection();
delay(1000);
checkLTE();
delay(1000);
SDcard();
delay(1000);
}
void printMUX(){
for(int i = 0 ; i < 8 ; i++){
Serial.print(" Channel ");
Serial.print(i);
Serial.print(" / ");
Serial.println(addressChannelMapping[i]);
}
}
void functionSelection(){
for(int i = 0 ; i <= 7 ; i++)
{
if(addressChannelMapping[i] == 8)
{
//readDHT11();
if(i >= 0 && i <= 4)
readDHT11(i+5);
else
readDHT11(i+9);
}
else if(addressChannelMapping[i] == 17)
{
if(i >= 0 && i <= 4)
readVibration(i+5);
else
readVibration(i+9);
}
//delay(20);
}
}
void readDHT11(int pin) {
pinMode(pin, INPUT);
DHT dht(pin, dhtType); // Initialize DHT sensor
dht.begin();
humidity = dht.readHumidity();
celsius = dht.readTemperature();
}
void readVibration(int pin){
pinMode(pin, INPUT);
vibrationSensorData = pulseIn(pin, HIGH);
DataMapping[counter]=vibrationSensorData;
counter++;
if(counter == 10){
for(int vib = 0; vib < 10; vib++){
sum = sum + DataMapping[vib];
Serial.print(DataMapping[vib]);
Serial.print(" ");
if(DataMapping[vib] > maxval){ //find maximum value
maxval = DataMapping[vib];
Xpeak = maxval * maxval;
}
Yrms = ((DataMapping[0] * DataMapping[0]) + (DataMapping[1] * DataMapping[1]) + (DataMapping[2] * DataMapping[2]) + (DataMapping[3] * DataMapping[3]) + (DataMapping[4] * DataMapping[4]) + (DataMapping[5] * DataMapping[5]) + (DataMapping[6] * DataMapping[6]) + (DataMapping[7] * DataMapping[7]) + (DataMapping[8] * DataMapping[8]) + (DataMapping[9] * DataMapping[9])) / 10;
PAPR = Xpeak / Yrms;
Y = round(PAPR);
}
Serial.println("");
Serial.print("sum = ");
Serial.println(sum);
average = sum / 10;
Serial.print("Ave = ");
Serial.println(average, 2);
Serial.print("Max = ");
Serial.println(maxval);
Serial.print("Xpeak = ");
Serial.println(Xpeak);
Serial.print("Yrms = ");
Serial.println(Yrms);
Serial.print("PAPR = ");
Serial.println(PAPR, 2);
Serial.println("===============================");
counter = 0;
sum = 0;
maxval = 0;
Xpeak = 0;
Yrms = 0;
}
delay(1000);
}
void SDcard(){
nextSensorData = pulseIn(EP, HIGH);
mySensorData = SD.open("CopyData.txt", FILE_WRITE);
if(mySensorData){
nextSensorData = pulseIn(EP, HIGH);
mySensorData.print("Move:");
mySensorData.println(nextSensorData);
}
mySensorData.close();
}
/* LTE module function */
void checkLTE(){
String command = "usr.cnAT";
command += "\r\n";
Serial.print(command);
delay(1000);
if(Serial.find("OK"))
{
lcd.print("OK");
packDataIntoJson();
}
else
{
lcd.print("Not Working");
}
}
//ArduinoJson ver.5.0
void packDataIntoJson(){
StaticJsonBuffer<500> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["ArduinoID"] = 1;
root["time"] = 123456789;
JsonObject& dataObj = root.createNestedObject("data");
for(int i = 0 ; i <= 7 ; i++)
{
JsonArray& sensor = dataObj.createNestedArray(String(i+1));
if(addressChannelMapping[i] == 8)
{
sensor.add(addressChannelMapping[i]); //moduleType
sensor.add(celsius); //data1
sensor.add(0); //data2
}
else if(addressChannelMapping[i] == 17)
{
sensor.add(addressChannelMapping[i]);
sensor.add(Y); //PAPR
//sensor.add(vibrationSensorData); //SensorData
sensor.add(0);
}
else
{
sensor.add(addressChannelMapping[i]);
sensor.add(0);
sensor.add(0);
}
}
//send Data
root.printTo(Serial);
}
I had tested the functions of sending data to the database and storing data to a SD card separately.
I use Arduino IDE 1.8.8, microcontroller is the ATmega328p.
Additional Board Manager URLs:
https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json
https://raw.githubusercontent.com/digistump/arduino-boards-index/master/package_digistump_index.json
http://arduino.esp8266.com/stable/package_esp8266com_index.json
The pin position of the circuit is shown in the figure.
SD card module:
https://protosupplies.com/product/microsd-card-module-with-level-shifter/
LTE module: USR LTE-7s4
https://www.amazon.in/USR-IOT-USR-LTE-7S4-Transparent-Transmission/dp/B01N5XIOVP
Any suggestion is help, thanks a lot
B.R.
WeiHsin