So, im using the example that comes with the arduino software, but with this example, i have to manually input the phone number that i want to send to everytime.
I've tried to put the phone number as an int = "mynumber" and replaced remoteNum, but i doesnt seem to work..
what else can I try?
#include <GSM.h>
#define PINNUMBER ""
// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
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");
}
void loop() {
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++;
}
}
}
}
How can I write the phone number into the code, so that I dont have to input in manually everytime?
Your code reads the information from Serial and stores it as a character string an a char array:
Serial.print("Enter a mobile number: ");
char remoteNum[20]; // telephone number to send sms
readSerial(remoteNum);
Serial.println(remoteNum);
All you need to do is put the number in a character string in the code instead of getting it from serial:
Serial.print("Enter a mobile number: ");
char remoteNum[20] = "15558675309"; // telephone number to send sms
Serial.println(remoteNum);
I don't know for sure that is how your phone number should be formatted, but you can look at what it has been printing out to see if it should have any dashes or anything in it.
Related
I am currently making an Arduino project with GSM900 GSM GPRS. In this project, I have to receive data sent from a phone. I could easily receive data with a single character, but I can`t join does character to obtain a full word (String). I have to use this full word inside an If statement if this word equals to that other word (string), make something...
#include <SoftwareSerial.h>
// Configure software serial port
SoftwareSerial SIM900(7, 8);
//Variable to save incoming SMS characters
char incoming_char=0;
String newchar = "";
void setup() {
// Arduino communicates with SIM900 GSM shield at a baud rate of 19200
SIM900.begin(19200);
Serial.begin(19200);
// Give time to your GSM shield log on to network
delay(20000);
// AT command to set SIM900 to SMS mode
SIM900.print("AT+CMGF=1\r");
delay(100);
// Set module to send SMS data to serial out upon receipt
SIM900.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
}
void loop() {
if(SIM900.available() >0) {
incoming_char=SIM900.read();
Serial.print(incoming_char);
}
}
I tried putting this command on the the if statement inside the loop, but after i tried comparing the words, it wouldnt work.
void loop() {
if(SIM900.available() >0) {
incoming_char=SIM900.read();
newString = incoming_char + "";
Serial.print(incoming_char);
}
if (newString == "Test"){
Serial.println("It worked");
}
}
The output that i get from the Monitor Serial is this:
+CMT: "+myNumber","","19/09/20,16:31:05-12"
Test
void loop() {
if (SIM900.available() >0) {
incoming_char=SIM900.read();
newString += incoming_char;
Serial.print(incoming_char);
}
if (newString.endsWith("Test")) {
Serial.println("It worked");
}
}
For does who are wondering how it finished:
Thanks to phoenixstudio...
void loop() {
if(SIM900.available() >0) {
incoming_char=SIM900.read();
newString += incoming_char;
Serial.print(incoming_char);
}
if (newString.endsWith("Test1")){
Serial.println("Worked1");
}
if (newString.endsWith("Test2")){
Serial.println("Worked2");
}
if (newString.endsWith("Test3"){
Serial.println("Worked3");
}
}
I am a beginner in Arduino programming. The objective of the script below is to turn on the pump on button press(d==0) wait for 8 seconds turn the pump off and send an SMS alert. When the button is released the pump stays off (emergency switch off).
Here I have managed to turn the pump on when the button is pressed and send a message. When the button is released the pump successfully turns off. However, the delay after 8 seconds turn off the pump doesn't really work which leaves me puzzled. Instead what happens is after 8 second delay the pump instantaneously jerks(toggles) then continues to stay on and send the message. I would appreciate any insight.
// Include the GSM library
#include <GSM.h>
#define PINNUMBER ""
// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;
int pinout = 3;
int ledout = 13;
int d=1;
int a = 1;
void setup() {
pinMode(4,INPUT); // Input Power
pinMode(pinout , OUTPUT); // Motor Output
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
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");
}
void loop() {
d=digitalRead(4);
// If digital state of pin 4 is zero start motor and power ON switch LED
if(d==0){
digitalWrite(pinout ,HIGH);
digitalWrite(ledout ,HIGH);
delay(8000); // This is the line that doesn't seem to work!
digitalWrite(pinout ,LOW); // This is the line that doesn't seem to work!
//for( a = 1; a < 2; a = a + 1 ){
if (a<2){
Serial.print("Enter a mobile number: ");
char remoteNum[20] = "4xxxxxxx22";
// sms text
Serial.print("Now, enter SMS content: ");
char txtMsg[200] = "Cow #57 SCC Abnormality Detected";
//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");
}
a = a+1;
Serial.println(a);
}else{
digitalWrite(pinout ,LOW);
digitalWrite(ledout ,LOW);
a=1;
}
//Serial.print(d); // Monitor state of Pin 4
}
I am trying to receive GPS coordinates on my number via GSM. My code sends them to me just once even though I have put it in loop but I want to receive them continuously after triggering my circuit by sending a character (e-g: '#') once only. Please let me know what's wrong.
#include <TinyGPS.h>
// create variable for latitude and longitude object
long lat,lon;
// create gps object
TinyGPS gps;
//for storing incoming character from sms
char inchar;
void setup()
{
Serial.begin(9600); // connect mega
Serial1.begin(9600); // connect GSM
Serial2.begin(9600); // connect gps
delay(1000);
Serial1.print("AT+CMGF=1\r"); //reads string instead of hexadecimal from incoming sms
Serial1.print("AT+CNMI=1,2,0,0,0"); //select storage to read sms from
Serial.println("Ready...");
delay(1000);
}
void loop()
{
while(true)
{
if(Serial1.available())
{
inchar = Serial1.read();
Serial.println(inchar);
}
if(inchar == '#')
{
getData();
}
}
}//finish loop
void getData()
{
while(true)
{
if(Serial2.available()>0) // check for gps data
{
if(gps.encode(Serial2.read())) // encode gps data
break;
}
}
gps.get_position(&lat,&lon); // get latitude and longitude
displayInfo();
sendInfo();
Serial.println("In getdata");
}
void displayInfo()
{
Serial.print("Position: ");
Serial.print("lat: "); Serial.print(lat); Serial.print(" ");// print latitude
Serial.print("lon: "); Serial.println(lon); // print longitude
} //end displayInfo()
void sendInfo()
{
Serial1.print("AA");
delay(1000); //delay of 1
Serial1.println("AT");
delay(1000);
Serial1.write("AT+CMGF=1\r\n"); //set GSM to text mode
delay(1500);
Serial1.write("AT+CPMS=\"SM\"\r\n"); //Preferred SMS Message Storage
delay(1000);
Serial1.write("AT+CMGS=\"03360234233\"\r"); //set GSM to text mode
delay(1500);
Serial1.print(lat); Serial1.print(" "); Serial1.print(lon);//set GSM to text mode
delay(1500);
Serial1.write(0x1A); // sends ctrl+z end of message
delay(1500);
Serial.println("sms sent ");
} //end sendInfo()
When you send '#' character it is probably followed by a newline character '\n', which replaces it in the inchar variable. Better approach would be to set a flag that will signal your code to send data:
bool sendData = false;
// setup etc.
void loop()
{
if(Serial1.available())
{
inchar = Serial1.read();
Serial.println(inchar);
if(inchar == '#') sendData = true;
}
if(sendData)
{
getData();
}
}
BTW: you don't need while(true) inside the loop() function. As the name suggests, it's already looping forever.
I can't connect and I don't know why, can somebody help me?
The console returns:
"/tmp/arduino_ca13faa5535f759e3ad5c18bb4094ecd/SendSMS.ino: In function 'void setup()':
/tmp/arduino_ca13faa5535f759e3ad5c18bb4094ecd/SendSMS.ino:48:34: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
if (gsmAccess.begin(PINNUMBER) == GSM_READY) { "
#include <GSM.h>
#define PINNUMBER ""
// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
;// wait for serial port to connect. Needed for native USB port only
}
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");
}
void loop() {
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++;
}
}
}
}
That's just a warning. The sketch is building fine. What do you see on the Serial Monitor window? (drag-select, copy and paste into a comment below.)
You haven't set the PIN to anything, like "1234". Do you have a PIN?
// libraries
#include <GSM.h>
// PIN Number
//#define PINNUMBER ""
// initialize the library instance
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;
char remoteNumber[20]; // Holds the emitting number
void setup()
{
// initialize serial communications
Serial.begin(9600);
Serial.println("SMS Messages Receiver");
// 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()==GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
Serial.println("Waiting for messages");
}
void loop()
{
char c;
// If there are any SMSs available()
if (sms.available())
{
Serial.println("Message received from:");
// Get remote number
sms.remoteNumber(remoteNumber, 20);
Serial.println(remoteNumber);
// This is just an example of message disposal
// Messages starting with # should be discarded
if(sms.peek()=='#')
{
Serial.println("Discarded SMS");
sms.flush();
}
// Read message bytes and print them
while(c=sms.read())
Serial.print(c);
Serial.println("\nEND OF MESSAGE");
// delete message from modem memory
sms.flush();
Serial.println("MESSAGE DELETED");
}
delay(1000);
}
Error: GSM_SMS does not a name type....
so i don't understand what actually error is......plz give me a proper answer.
exactly, i want to read SMS using arduino Gboard and doing led on or off through mobile.
Try to download the last arduino software arduino-1.0.4 and check again the code.
Please try 1.0.4 (http://arduino.cc/en/main/software) , 1.5.2 is beta. If i wrong sorry...