I've modified my code to make it read an analogInput then send a message. I've it debugged and no errors at all. As I try it several times and do many modification as I can it still doesn't wanna work. I wonder if anyone can help me figure it out.
When the module senses a signal, it will turn the Arduino GSM Shield on within a certain time that is enough to send a message. Then it will automatically turn off.
The problem here is when I plug everything in place, nothing happens even there is a signal at the input pin 0
Thanks
// Example 55.2
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
const int analogPin = A0; // pin that the sensor is attached to
const int threshold = 797; // an arbitrary threshold level that's in the range of the analog input
void setup()
{
pinMode(9, OUTPUT);
SIM900.begin(19200);
//SIM900power();
delay(10000); // give time to log on to network.
}
void SIM900power()
//software equivalent of pressing the GSM shield "power" button
{
int analogValue = analogRead(analogPin);
if(analogValue >= threshold){
sendSMS();
}
digitalWrite(9,LOW);
delay(1000);
digitalWrite(9, HIGH);
delay(2000);
digitalWrite(9, LOW);
delay(3000);
}
void sendSMS()
{
SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(100);
SIM900.println("AT + CMGS = \"+xxxxxxxxxxx\""); // recipient's mobile number, in international format E.123
delay(100);
SIM900.println("Message to send here!"); // message to send
delay(100);
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(100);
SIM900.println();
delay(5000); // give module time to send SMS
SIM900power(); // turn off module
}
void loop()
{
sendSMS();
// SIM900power(); //power off GSM SHIELD
do {} while (1);
}
Is this the portion of code that turns the GSM module on and off ?
If so then you need to realise that during the delays nothing else will happen. Should you not be turning the module on, sending the SMS then turning it off ?
I did the sound detection code and it worked just fine
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
int threshold = 306;
int sensePin= A0;
void setup()
{
Serial.begin(9600);
pinMode(9, OUTPUT);
}
void loop()
{
int analogValue=analogRead(sensePin);
Serial.println(sensePin);
if(analogValue > threshold){
digitalWrite(9, HIGH);
delay(100);
}
}
but when I put the sendSMS piece together, the module still turned on when it detected a sound but it didn't send any message.
Here is my full program.
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
int threshold = 306; //threshold voltage to turn on/off the GSM module
int sensePin= A0; /analoginput pin A0
void setup()
{
Serial.begin(9600);
pinMode(9, OUTPUT); //power pin for the GSM module
}
void loop()
{
int analogValue=analogRead(sensePin);
Serial.println(sensePin);
if(analogValue > threshold){ //compare the analog value from pin A0 to the given threshold
digitalWrite(9, HIGH); //power up the GSM module
delay(100);
}
sendSMS(); //after the GSM module is powered up, it will go to this sendsms section to send a message
}
void sendSMS()
{
SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(100);
SIM900.println("AT + CMGS = \"+64212377933\""); // recipient's mobile number, in international format E.123
delay(100);
SIM900.println("Your Alarm has gone off. You got a visitor"); // message to send
delay(100);
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(100);
SIM900.println();
delay(5000); // give module time to send SMS
}
Anyway, the program stuck at the turn on/off the GSM module. When I compile the program there is no problem.
I mentioned it on my very first post on this thread but you're still not calling that function SIM900power() In fact, now you seem to have even deleted it completely.
As the timing of turn on/off , a >1s pulse was need to trigger the turning, and a >3.2s delay was need to get the timing stable. Add the following code in your firmware to turn on / off the shield without pressing the button: