Hi Guys,
I'm here to ask how to combine both GSM code with receiver code together. There's something wrong with receiver code inside the void loop function.
Kindly help me thank you.
// Include the GSM library
#include <GSM.h>
#include <VirtualWire.h>
#define PINNUMBER ""
// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;
char remoteNumber[20]= "+86xxxxxxxx"; // number that you want to send a SMS
// Pins definition
const int led_pin = 7;
const int receive_pin = 6;
int pinSpeaker = 5;
int bb;
char txtMsg[200]="Hi COOL press the switch";
void setup()
{
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
Serial.begin(9600); // Debugging only
// Initialise the IO and ISR
vw_set_rx_pin(receive_pin);
vw_setup(4000); // Transmission rate
// Start the receiver PLL
vw_rx_start();
// Set LED pin and Buzzer
pinMode(led_pin, OUTPUT);
pinMode(pinSpeaker, OUTPUT);
}
Serial.println("SMS Messages Sender");
// connection state
boolean notConnected = true;
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while (notConnected)
{
if (gsmAccess.begin(PINNUMBER) == GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
//SendSMS(); //SendSMS
}
void loop()
{
//send the message
bb=digitalRead(6);
if(bb==LOW) {
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
// Check if a message was received
if (vw_get_message(buf, &buflen))
{
if(buf[0]=='1')
{
Serial.println("Motion detected!");
digitalWrite(led_pin,1);
playTone(300, 160);
delay(150);
Serial.println("the message will be send"); //
delay(2000); //
sms.beginSMS(remoteNumber); //
sms.print(txtMsg); //
sms.endSMS(); //
delay(10000); //
//SendSMS(); //SendSMS
}
else
{
Serial.println("no message");
delay(2000);
if(buf[0]=='0')
{
Serial.println("Motion ended!");
digitalWrite(led_pin,0);
playTone(0, 0);
delay(300);
}
}
}
}
}
void SendSMS()
{
Serial.print("Enter a mobile number: ");
char remoteNum[20]; // telephone number to send sms
readSerial(remoteNum);
Serial.println(remoteNum);
// sms text
Serial.print("Now, enter SMS content: ");
char txtMsg[200];
readSerial(txtMsg);
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the message
sms.beginSMS(remoteNum);
sms.print(txtMsg);
sms.endSMS();
Serial.println("\nCOMPLETE!\n");
}
/*
Read input serial
*/
int readSerial(char result[])
{
int i = 0;
while (1)
{
while (Serial.available() > 0)
{
char inChar = Serial.read();
if (inChar == '\n')
{
result[i] = '\0';
Serial.flush();
return 0;
}
if (inChar != '\r')
{
result[i] = inChar;
i++;
}
}
}
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq)
{
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration)
{
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}