How to send sms only every 30minutes since the last sms

Hi all I want to ask about my program's issue.

I want to send an SMS but minimum only every 30minutes since the last SMS sent. Here's the sms command:

gprs.sendSMS("081320972055", "Moving object detected");

The sms is triggered every PIR Sensor detects and then send bit 1 for the trigger. But I only want the SMS only sent every 30minutes since its last sending. Do you have suggestion?

Thank you..

Do you have suggestion?

Yes. Record when you last sent a message. It it is desired to send a message, see how long it has been since the last one was sent. If it has not been long enough, don't send one.

millis() is useful.

You might need a Real time clock, when arudino resets or starts he will not know when he sent the last sms.

if you are not really bothered about checking the last sent time when Arduino restarts then you can ignore RTC and go with PaulS approach.

Would an RTC help in the case of a reset unless you saved the time sent to non volatile memory ?

The requirement for periodically doing something whilst monitoring inputs is a good fit with the BlinkWithoutDelay principle. In this case perhaps it should be known as SMSWithoutDelay

The microcontroller on the Arduino and Genuino AVR based board has on board EEPROM - a memory whose values are kept when the board is turned off. This EEPROM Library enables you to read and write those bytes

What you can do, every time you send an SMS store the date in the built in EEPROM.

Bytes in EEPROM will loose their ability to be written to or read back correctly after about 100,000 erase cycles. if you use the same location in memory, that gives you about 5 years if you write every 30 minutes there. if you alternate between multiple location, say 5, then you get ~25 years worth of usage... in theory. probably good enough, your SIM card will be dead long before that :slight_smile:

if you want to deal with cases where you arduino reboots and looses world clock time, you need to have a consistent date / time.

many options:

  • don't care about it. Just record a timestamp from Arduino in EEPROM - worse case scenario is that your arduino crashes and in that case you lost the time and thus might send an SMS before 30 min after the previous event.. if you have a good pgm, your arduino should not reboot often... May be that's acceptable.

  • attach a RTC clock to your project. they are coin operated. coin will last probably 9 years. easy, cheap. but only 9 years where the rest of your project can last longer. You can envision powering the RTC in some other way, I suppose you have power anyway to get the PIR and Arduino going... so may be some sort of rechargeable system.... seems over-engineered.. depends how long you want this to live...

  • ideally - as you have gprs capability, get the time from the network. The network time feature (aka NITZ) needs to be supported by your network provider. set up using AT+CTZU=1 (that enables automatic time zone update via NITZ) and when you need time you do AT+CCLK? and you'll get back +CCLK: "16/06/23,16:533:26+04" (format is "yy/MM/dd,hh:mm:ss+zz" where zz indicates the difference, expressed in quarters of an hour, between the local time and GMT and ranges between -47...+48 and the other values are I guess obvious from their acronym )

BTW - if you store in 5 places you won't know where to store it upfront or which one to read, so the way you deal with this is you read the 5 locations (reads don't damage the memory), identify the min and max date. test if you are 30 min after the max date and if so replace the min date location by your new date (now). this way you keep a log of the 5 most recent sends.

Keep in mind EEPROM has some n number of write cycles. Not sure what will happen after that.