Dear All,
i've a case where i can't join the function of sending sms with SD card data logger.
I have a system that contain with control relay, user notification with gsm shield to send sms (inculde RTC) and data logging with sd card. The problem is if i use data logger function at setup to set header. I can't get the RTC time to be put in my dataString(which i'll send and log).
Here's my code. It would be a great help if i can solve this one.
int buttonInput = 2;
int lightOutput = 3;
int lightOutput2 = 5;
int lightOutput3 = 6;
//int supply = 5;
int buttonState = 0;
int lastState = 0; // previous state of the button
unsigned long previousMillis = 0;
String x;
int id;
String dataString;
#include <Wire.h>
//#include "RTClib.h"
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
//buat dapetin waktu
SoftwareSerial SIM900(7, 8); // configure software serial port
char inchar;
char incoming_char = 0;
int char_count;
long previousMillis_keepGSM = 0; //last trigger time
long interval_keepGSM = 600000; // 600 sec (10 minutes) interval for checking SIM900 activity
String stringOne;
String stringTime;
String sim900time = "AT+CCLK=\"YY/MM/DD,hh:mm:ss+12\"";
String val_from_time;
String string_from_serial;
String string_from_serial_time;
int value_EEPROM;
int address_EEPROM;
int numposition;
String in_sms_text;
int symbols_read;
//SoftwareSerial SIM900(7,8);
void setup()
{
SIM900.begin(19200);
Serial.begin(9600);
delay(1500);
// Serial.print("Initializing SD card...");
// // see if the card is present and can be initialized:
// if (!SD.begin(chipSelect)) {
// Serial.println("Card failed, or not present");
// // don't do anything more:
// return;
// }
//
//
//
// Serial.println("card initialized.");
////
// delay(1500);
pinMode(lightOutput, OUTPUT);
pinMode(lightOutput2, OUTPUT);
pinMode(lightOutput3, OUTPUT);
// pinMode(supply, OUTPUT);
pinMode(buttonInput, INPUT);
digitalWrite(buttonInput, HIGH);
// digitalWrite(supply, HIGH);
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
File dataFile = SD.open("ALADIN.csv", FILE_WRITE);
if (dataFile) {
String header = "ID,NO,Tanggal,Jam,Status";
dataFile.println(header);
dataFile.close();
}
//
delay(1500);
}
void loop()
{
/* check if SMS or data is coming from SIM900: */
if (SIM900.available() > 0)
{
inchar = SIM900.read();
Serial.print(inchar); //Print the incoming character to the terminal
if (inchar == '#')
{
delay(10);
inchar = SIM900.read(); Serial.print(inchar); //Print the incoming character to the terminal
if (inchar == '_') // SIM900 has received a message which starts with #_ so this is assumed as
// a command. We read next 7 characters and process it.
{
in_sms_text = "";
for (int k = 0; k < 8; k++) {
inchar = SIM900.read(); Serial.print(inchar); in_sms_text.concat(inchar); delay(10);
}
in_sms_text.trim(); // trim last spaces
//processing of the incoming message
if (in_sms_text == "gsminfo")
{
Serial.println("Got 'gsminfo' command");
// digitalWrite(led1, HIGH); // or do some other action
}
if (in_sms_text == "sensoff")
{
Serial.println("Got 'sensoff' command");
// digitalWrite(led2, LOW); // or do some other action
}
if (in_sms_text == "sensonn")
{
Serial.println("Got 'sensonn' command");
// digitalWrite(led2, HIGH); // or do some other action
}
}
}
}
stringOne = "";
stringTime = "";
SIM900.println("AT+CCLK?");
delay(100);
while (SIM900.available()) {
inchar = SIM900.read();
stringOne.concat(inchar);
}
dataString = "";
// DateTime now = rtc.now();
buttonState = digitalRead(buttonInput);
unsigned long currentMillis = millis();
if ((buttonState == HIGH) && (lastState == LOW))
{ id++;
for (int s = 0; s < (stringOne.length() - 6); s++) {
if (stringOne.substring(s, s + 6) == "+CCLK:")
{
stringTime = (stringOne.substring(s + 8, s + 25));
// dataString += String(id) + "," + String(stringTime) + "," + "OLS Closed";
}
}
// dataString += String(id) + "," + String(now.day()) + "/" + String(now.month()) + "/" + String(now.year()) + "," + String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second()) + "," + "OLS Closed";
dataString += String(id) + "," + String(stringTime) + "," + "OLS Closed";
x = "Nyala";
previousMillis = currentMillis;
delay(100);
SendTextMessageOn();
Serial.println(dataString);
lastState = buttonState;
}
if ((buttonState == HIGH) && (lastState == HIGH))
{
if (currentMillis - previousMillis < 2000) {
x = "Menunggu Nyala";
// digitalWrite(lightOutput, HIGH);
lastState = buttonState;
}
if (currentMillis - previousMillis >= 2000)
{
x = "Tahap 1";
digitalWrite(lightOutput, HIGH);
}
if (currentMillis - previousMillis >= 3000)
{
x = "Tahap 2";
digitalWrite(lightOutput2, HIGH);
}
if (currentMillis - previousMillis >= 4000)
{
x = "Tahap 3";
digitalWrite(lightOutput3, HIGH);
}
lastState = buttonState;
}
if ((buttonState == LOW) && (lastState == HIGH))
{ id++;
for (int s = 0; s < (stringOne.length() - 6); s++) {
if (stringOne.substring(s, s + 6) == "+CCLK:")
{
stringTime = (stringOne.substring(s + 8, s + 25));
// dataString += String(id) + "," + String(stringTime) + "," + "OLS Closed";
}
}
dataString += String(id) + "," + String(stringTime) + "," + "OLS Open";
x = "Mati";
digitalWrite(lightOutput, LOW);
digitalWrite(lightOutput2, LOW);
digitalWrite(lightOutput3, LOW);
// digitalWrite(lightOutput4, LOW);
SendTextMessageOff();
Serial.println(dataString);
lastState = buttonState;
}
if ((buttonState == LOW) && (lastState == LOW))
{
x = "Tetap Mati";
digitalWrite(lightOutput, LOW);
digitalWrite(lightOutput2, LOW);
digitalWrite(lightOutput3, LOW);
// digitalWrite(lightOutput4, LOW);
previousMillis = currentMillis;
lastState = buttonState;
}
}
void SendTextMessageOn()
{
Serial.println("Sending Text...");
SIM900.print("AT+CMGF=1\r"); // Set the shield to SMS mode
delay(100);
// send sms message, the phone number needs to include the country code e.g. if a U.S. phone number such as (540) 898-5543 then the string must be:
// +15408985543
SIM900.println("AT+CMGS = \"+6281233920541\"");
// SIM900.println("AT+CMGS = \"+6285717712393\"");
delay(100);
// SIM900.println("AT+CLTS = 1");
// delay(100);
// SIM900.println("AT+CCLK =?");
// delay(100);
SIM900.println(dataString); //the content of the message
delay(100);
SIM900.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)
delay(100);
SIM900.println();
Serial.println("Text Sent.");
}
void SendTextMessageOff()
{
Serial.println("Sending Text...");
SIM900.print("AT+CMGF=1\r"); // Set the shield to SMS mode
delay(100);
// send sms message, the phone number needs to include the country code e.g. if a U.S. phone number such as (540) 898-5543 then the string must be:
// +15408985543
SIM900.println("AT+CMGS = \"+6281233920541\"");
// SIM900.println("AT+CMGS = \"+6285717712393\"");
// SIM900.println("AT+CMGS = \"+628111338800\"");
delay(100);
SIM900.println(dataString); //the content of the message
delay(100);
SIM900.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)
delay(100);
SIM900.println();
Serial.println("Text Sent.");
}
I'm using duinopeak GSM shield 900A and SD card logger.
Many thanks