text alarm

hi guys,
im new to all this programming stuff and after spending 3 days feeling like im right on the verge of cracking this i surrender. im trying to make an alarm to protect my kayaks when there on my roof rack when i am camping. i plan to have a 5v wire coming out of my device around my boats and back to an input pin (4) when the wire is pulled from its connector or cut i would like an alarm to sound (pin 12 LED for testing) and for a text message to be sent to my phone but i just cant get it right. in the program below the LED comes on when i pull the wire and the text is sent when i put it back in i have no idea why but this is the closest i have been in 3 days please help me out

Code:
#include <SoftwareSerial.h>  
char gsm_char=0;      //Stores character from the gsmSerial
 
SoftwareSerial gsmSerial(2,3);  //Creates a software serial port. (rx 2, tx 3)
const int buttonPin = 4;     // the number of the pushbutton pin
int ledPin =  12;      // the number of the LED pin
int buttonState = 0;         // variable for reading the pushbutton status

void setup()
{
  pinMode(ledPin, OUTPUT);  // initialize the LED pin as an output:
  pinMode(buttonPin, INPUT);   // initialize the pushbutton pin as an input:  
  Serial.begin(9600);//Initialize serial ports for communication. set baud rate
  gsmSerial.begin(9600);//Initialize gsm serial ports for communication.set baud rate
  Serial.println("Starting TC35 and debug Communication..."); //print to PC serial moniter display
}
 
void loop() {
 buttonState = digitalRead(buttonPin);
    if (buttonState == LOW) {// if wire breaks     
        digitalWrite(ledPin, HIGH); //then give output (high) to ledpin
        //delay(500);
        gsmSerial.print("AT+CMGF=1\r"); //and Set gsm module to text mode.
        delay(500); // thinking time for gsm module
        gsmSerial.print("AT+CMGS=+447788704543\r"); //AT command to send SMS to my number
        delay(100); // thinking time for gsm module
        gsmSerial.print("Hello world"); //Print the message
        delay(10); //thinking time for gsm module
        gsmSerial.print("\x1A"); //Send it ascii SUB
    }
  }

ultimately i would like to be able to arm and disarm the alarm by text if any one has any suggestions for this? but for now im happy using a key switch if i can just get this to work im sure its realy obvious when you know what your doing
thanks

I'm not sure why your code sends the message when the wire is connect. It appear that is should send when the wire is disconnected.

However, when you determine what is wrong with your wiring, I hope you have an unlimited text message plan as that code will send a message more than once per second when someone steals your kayak, I would have thought that one message would be sufficient.

Why would you want to receive a message that says "Hello world" when someone is stealing your kayak?

@dogfood: Please do not re-post this query in another part of the forum. That is called cross-posting and riles people up.

Your other thread deleted.

  • Moderator

Hi dogfood, Hope I understnad your problem correctly. Can think of two reasons for the observed odd behaviour: 1) while your loop() will complete repeatedly while the button is pressed, the GSM module may not complete its send process before the next iteration of the loop. I'm not familiar with this gsm library or your module, but do either allow for immediate re-send? Your code has no delay between the "\x1A" at the end of the loop and the "AT+CMGF=1\r" at the top of the next iteration. My SIM900 doesn't cope with that. A bit of SerialMonitor debugging and/or a loop counter could help to confirm this as the offending issue. 2) your service provider likely has routines to block repeated transmissions of identical SMS within very brief periods. Mine does (they notified me when my code got me into trouble - but were nice and didn't charge me!). Same suggestion - include the loop counter count in the txt message to see which msg you are receiving. Either effect may cause the appearance of the SMS being sent when the button is released, while in fact it is only the last message sent while the button is pressed that gets transmitted successfully

Also - I've used a plug as a switch in a previous project to build an alarm loop similar to yours. The plug required a lot more de-bouncing than a regular button switch. That wasn't an arduino project, so maybe someone can advise whether digitalRead() suitably debounces a 'plug' event.

Also, hope that's not your real number :open_mouth: Best of luck!

Hi,

Did you have some progress on your project?

I have some remarks: - For debugging, it is best to send all data that the arduino receives from the TC35 board to the Serial terminal. Then you know if you get e.g. an error. - Did you specify the SMS server before, is it set correctly? - Does the LED blink on the TC35 board? - I think that you should increase the delay between the AT+CMGS command and the text message. I had some problems with that myself - Officially, you should place the phonenumber between "". I'm not sure if it is necessary, but you can try nonetheless.

For some working example code, try this website: http://arjanvdbeek.nimio.info/electronics/sms-using-a-uc/siemens-tc35-gsm-board/