Hi, Yes, relatively new to arduino, not completely new to programming albeit a little rusty. That being said I've been working on my first, seemingly ambitious project to have an arduino equipped with a GPRS shield send me an SMS each time a rain gauge trips. I've got it logging to an SD card as a backup, all good, and the GPRS shield has an active SIM card in it, also all good. I just can't seem to communicate or get the serial connection to work.
The GPRS shield is configured for software serial, I'm using as seeeduino stalker v2.3, I think I have it set up correctly at the hardware level.
The method "sendMessage(String message)" seems to the issue. I check for Serial.available() and have yet to find it to be available.
Any pointers are much appreciated and thanks in advance for the time and consideration to read this post.
B
This is my code:
#include <Wire.h>
#include <SD.h>
#include "DS3231.h"
#include "DS1307RTC.h"
#include "Time.h"
#include "TimeAlarms.h"
#include <SoftwareSerial.h>
#include <String.h>
SoftwareSerial GPRS(7, 8);
unsigned char buffer[64];
int count = 0;
const int chipSelect = 10;
int switchPin = 2; // choose the Sensor_Pin
int counter0 = 0;
long lastDebounce = 0;
long debounceDelay = 500;
int val = 0;// variable for reading the Sensor_Pin status
DS3231 RTC2; //Create the R8025 object
int32_t temp = 0;
char amsg;
void setup(){
GPRS.begin(19200);
Serial.begin(19200);
Wire.begin();
setTime(12,39,45,21,04,2013);
pinMode(switchPin, INPUT);
attachInterrupt(0, trigger0, FALLING);
SD.remove("datalog.txt");
setupsdcard();
initializelogfile();
Alarm.alarmRepeat(12,41,0, DailyLog);
delay(500);
}
void loop()
{
Alarm.delay(1000);
temp = RTC2.getTemperature();
}
void trigger0(){
if((millis() - lastDebounce) > debounceDelay){
counter0++;
File myFile = SD.open("datalog.txt", FILE_WRITE);
if (myFile) {
time_t t = now();
String aDate = String(year(t), DEC) + "/" + String(month(t), DEC) + "/" + String(day(t), DEC) + ":" + String(hour(t),DEC) + ":" + String(minute(t), DEC) + ":"+ String(second(t),DEC)+ " " + temp + "'c";
myFile.println(aDate);
sendMessage(aDate);
myFile.close();
delay(200);
}
lastDebounce = millis();
}
}
void sendMessage(String amessage){
if (Serial.available()){
GPRS.print("AT+CMGF=1\r");
//Because we want to send the SMS in text mode
delay(100);
GPRS.println("AT + CMGS = \"+14195551212\"");
//send sms message, be careful need to add a country code before the cellphone number
delay(100);
GPRS.print(amessage);
//the content of the message
delay(100);
GPRS.println((char)26);
//the ASCII code of the ctrl+z is 26
delay(100);
GPRS.println();
} else {
Serial.println("no serial " + amessage);
}
}
void DailyLog(){
Serial.println("Alarm: - sending log file");
if (Serial.available()){
File dataFile = SD.open("datalog.txt");
// if the file is available, write to it:
if (dataFile) {
Serial.write(dataFile.read());
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
}
void initializelogfile(){
char* filename = "datalog.txt";
if(!SD.exists(filename)){
Serial.println("Creating new datalog.txt...");
File NewdataFile = SD.open(filename, FILE_WRITE);
NewdataFile.close();
}
}
void setupsdcard(){
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
return;
}
}