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?
Related
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.
Hey I have a home automation project I've been working on recently, in which I have an Arduino Mega with an Ethernet shield.
The Mega is waiting for Telnet commands. When a command is received, it turns a relay on. I then have an auto-hotkey script that sends Telnet commands when I press specific keys on my Windows PC.
My problem is that I plan to use 4 relays and right now I have to assign two keys per relay(one for on and for off).
I researched and found about impulse relays, but due to the lockdown, I can't buy any. I tried to find/write code that implemented the same idea in a simple relay but failed.
So, my question is, How do you trigger a relay on/off with a single command?
The code I am using:
#include <SPI.h>
#include <Ethernet.h>
int backlight = 7;
int fan = 6;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,21,108);
IPAddress gateway(192,168,21,21);
IPAddress subnet(255, 255, 255, 0);
// telnet defaults to port 23
EthernetServer server(23);
boolean alreadyConnected = false; // whether or not the client was connected previously
String commandString;
void setup() {
pinMode(fan, OUTPUT);
pinMode(backlight, OUTPUT);
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Chat server address:");
Serial.println(Ethernet.localIP());
}
void loop() {
// wait for a new client:
EthernetClient client = server.available();
// when the client sends the first byte, say hello:
if (client) {
if (!alreadyConnected) {
// clear out the input buffer:
client.flush();
commandString = ""; //clear the commandString variable
server.println("--> Please type your command and hit Return...");
alreadyConnected = true;
}
while (client.available()) {
// read the bytes incoming from the client:
char newChar = client.read();
if (newChar == 0x0D) { //If a 0x0D is received, a Carriage Return, then evaluate the command
server.print("Received this command: ");
server.println(commandString);
processCommand(commandString);
} else {
Serial.println(newChar);
commandString += newChar;
}
}
}
}
void processCommand(String command)
{
server.print("Processing command ");
server.println(command);
if (command.indexOf("backlight1") > -1){
server.println("Backlight On command received");
digitalWrite(backlight, HIGH); // sets the LED on
server.println("Backlight was turned on");
commandString = "";
return;
}
if (command.indexOf("backlight0") > -1){
Serial.println("Backlight Off command received");
digitalWrite(backlight, LOW); // sets the LED off
server.println("Backlight was turned off");
commandString = "";
return;;
}
if (command.indexOf("fan1") > -1){
server.println("fan On command received");
digitalWrite(fan, HIGH); // sets the LED on
server.println("Fan was turned on");
commandString = "";
return;
}
if (command.indexOf("fan0") > -1 ){
Serial.println("fan Off command received");
digitalWrite(fan, LOW); // sets the LED off
server.println("Fan was turned off");
commandString = "";
return;
}
commandString = "";
instructions();
}
void instructions()
{
server.println("Please use one of these commands:");
server.println("* backlight1, to turn backlight on");
server.println("* backlight0, to turn off the backligt");
server.println("* fan1, to turn on the fan");
server.println("* fan0, to turn off the fan");
}
If you want to have a single command (= toggle) you need a global bool var for each relay:
bool fanIsOn = false;
// The toggle command is fan
if (command.indexOf("fan") > -1 && fanIsOn == false){
server.println("fan On command received");
digitalWrite(fan, HIGH); // sets the LED on
server.println("Fan was turned on");
fanIsOn = true;
commandString = "";
return;
}
if (command.indexOf("fan") > -1 && fanIsOn == true){
Serial.println("fan Off command received");
digitalWrite(fan, LOW); // sets the LED off
server.println("Fan was turned off");
fanIsOn = false;
commandString = "";
return;
}
Hope this was what you meant
I am new to Arduino and GSM shield. I have been working with SMS, and I have a problem to read the SMS's text contents from the modem.
Here is the setting for the Arduino and the GSM.
#include <SoftwareSerial.h>
SoftwareSerial sim900(7, 8);
#define BUFF_SIZE 400
char buffer[BUFF_SIZE];
bool GSMconnected = false;
//functions
bool copyResponse(int* size);
void printBuff(int size);
bool readlnbuffer(int* start, int* nextline, int* size);
bool isResponseOK(int start, int endofline);
bool checkConnection();
bool checkMessage(int index);
bool checkString(int start, int end, String command);
bool checkSMS(int start, int nextline);
bool newMessage();
void setup() {
sim900.begin(9600);
Serial.begin(9600);
delay(100);
pinMode(relayIN, OUTPUT);
delay(100);
while (!GSMconnected) {
checkConnection();
delay(10000);
}
int rsize;
//text mode
sim900.print("AT+CMGF=1\r");
delay(1000);
//store message in SIM card
sim900.print("AT+CPMS=\"SM\"\r");
delay(1000);
sim900.print("AT+CMGD=0,4\r\n");
delay(1000);
copyResponse(&rsize);
printBuff(rsize);
/*
0: do not show header values
1: show the values in result codes
*/
sim900.print("AT+CSDH=1\r");
delay(1000);
//receive mode +CMTI:"SM", 1
sim900.print("AT+CNMI=2,2,0,0,0 \r");
delay(1000);
copyResponse(&rsize);
printBuff(rsize);
sim900.print("AT+CSMP=17,167,0,0\r\n");
delay(10000);
copyResponse(&rsize);
printBuff(rsize);
Serial.println("GSM is ready");
I want to check the SMS message when there is a new SMS. So I simply call the function when the GSM is available.
void loop() {
if(sim900.available()) {
newMessage();
}
}
newMessage() checks the index of the new SMS stored in the SIM card.
bool newMessage() {
int responseSize;
if (!copyResponse(&responseSize)) {
return false;
}
//+CMTI: “SM”,1
//read a line and check if there is a SMS
int start = 0;
int nextline = 0;
int index;
while (readlnbuffer(&start, &nextline, &responseSize)) {
String command = "+CMTI: \"SM\",";
if (checkString(start, start + 12, command)) {
Serial.println("SMS found");
index = int(buffer[start + 12]);
break;
}
start = nextline;
}
//debug
printBuff(responseSize);
delay(10000);
if (checkMessage(index)) {
//delete message
sim900.print("AT+CMGD=");
sim900.print(char(index));
sim900.println("\r");
delay(100);
return true;
} else {
return false;
}
checkMessage() reads SMS message and should print SMS text data.
bool checkMessage(int index) {
//listing
//sim900.print("AT+CMGL=\"REC UNREAD\"\r\n");
//reading
sim900.print("AT+CMGR=");
sim900.print(char(index));
sim900.print("\r\n");
delay(5000);
int responseSize;
if (!copyResponse(&responseSize)) {
return false;
}
delay(100);
//debug, to check what is in the buffer
printBuff(responseSize);
//read a line and check if there is a SMS
int start = 0;
int nextline = 0;
bool isSMS = false;
while (!isSMS) {
start = nextline;
if (!readlnbuffer(&start, &nextline, &responseSize)) {
Serial.println("no SMS found");
return false;
}
String command = "+CMGR:";
if (checkString(nextline, nextline + 6, command)) {
Serial.println("SMS checking");
isSMS = true;
}
}
//debug
if (!readlnbuffer(&start, &nextline, &responseSize)) {
Serial.println("cannot read the sms info");
return false;
}
Serial.println("SMS message: ");
for (int i = start; i < nextline; i++) {
//debug
Serial.print(buffer[i]);
}
Serial.println("");
}
When I run the code, it looks like the response for AT+CMGR cannot be copied completely. Here is what the Serial monitor shows.
checking the network
AT+CREG?
+CREG: 0,1
OK
AT+CMGF=1
OK
AT+CPMS="SM"
+CPMS: 1,30,1,30,1,30
OK
AT+
AT+CSDH=1
OK
AT+CNMI=2,1,0,0,0
OK
AT+CSMP=17,167,0,0
OK
GSM is ready
SMS found
+CMTI: "SM",1
AT+CMGR=1
> +CMGR: "REC UNREAD","+123456789","","17/11/26,11
SMS checking
SMS message:
AT+CMGD=1
OK
I think I went wrong with copying the response in the buffer. I am also a beginner of writing code and I need some help. Can anyone advise me on why I am getting this problem?
Thank you!
I think the problem is that you are aborting the command by using delay which you should never, never, ever do as a substitute for reading and parsing the responses from the mopdem.
For hints on removing delay usage see the linked answer (and for details on aborting command you can see V.250).
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 am using SIM 900 GSM module, and I am using this code for GSM based data connection. But problem is I am not getting any feedback from my code. I am from Bangladesh, using a local sim provider's SIM card(Grameen Phone(Telenor)). It's APN is gpinternet/gpwap. But what is the username & password. If the module can connect then it will show some positive feedback otherwise negative. but it showing nothing. Where is my problem? can anyone help me.
#include <GSM.h>
#define PINNUMBER "1234"
// APN data
#define GPRS_APN "gpinternet" // replace your GPRS APN
#define GPRS_LOGIN "" // replace with your GPRS login
#define GPRS_PASSWORD "" // replace with your GPRS password
// initialize the library instance
GPRS gprs;
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSMServer server(80); // port 80 (http default)
// timeout
const unsigned long __TIMEOUT__ = 10 * 100;
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
}
// 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) & (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
notConnected = false;
} else {
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("Connected to GPRS network");
// start server
server.begin();
//Get IP.
IPAddress LocalIP = gprs.getIPAddress();
Serial.println("Server IP address=");
Serial.println(LocalIP);
}
void loop() {
// listen for incoming clients
GSMClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
Serial.println("Receiving request!");
bool sendResponse = false;
while (char c = client.read()) {
if (c == '\n') {
sendResponse = true;
}
}
// if you've gotten to the end of the line (received a newline
// character)
if (sendResponse) {
Serial.println("HI man");
}
}
}
}
}