this is my code.
Master:
/* GSM Shield example
created 2011
by Boris Landoni
This example code is in the public domain.
http://www.open-electronics.org
http://www.futurashop.it
*/
#include <GSM_Shield.h>
#include<string.h>
#include<stdlib.h>
#include "RS485_protocol.h"
#include <SoftwareSerial.h>
SoftwareSerial myserial(2, 3);
SoftwareSerial rs485(5, 6);
int l1=8;
//**************************************************************************
char number[]="+919490974707"; //Destination number
char text[]="hello world"; //SMS to send
byte type_sms=SMS_UNREAD; //Type of SMS
byte del_sms=0; //0: No deleting sms - 1: Deleting SMS
//**************************************************************************
GSM gsm;
char sms_rx[122]; //Received text SMS
//int inByte=0; //Number of byte received on serial port
char number_incoming[20];
//int call;
int error;
/************************************************************/
void fWrite (const byte what)
{
//Serial.write (what);
rs485.write (what);
//Serial.print(" ");
delay(1);
}
int fAvailable ()
{
//return Serial.available ();
return rs485.available ();
}
int fRead ()
{
//return Serial.read ();
return rs485.read ();
}
const byte ENABLE_PIN = 4;
void clearbuf(byte *b)// clearing the buffer
{
while(*b)
*b++='\0';
}
void setup()
{
Serial.begin(9600);
rs485.begin(9600);
Serial.println("system startup");
gsm.TurnOn(9600); //module power on
gsm.InitParam(PARAM_SET_1);//configure the module
gsm.Echo(0); //enable AT echo
pinMode(l1,OUTPUT);
digitalWrite(l1,HIGH);
pinMode(ENABLE_PIN,OUTPUT);
}
void Send_SMS()
{
Serial.print("Send SMS to ");
Serial.println(number);
error=gsm.SendSMS(number,text);
if (error==0) //Check status
{
Serial.println("SMS ERROR \n");
}
else
{
Serial.println("SMS OK \n");
}
}
void Check_SMS() //Check if there is an sms 'type_sms'
{
char pos_sms_rx; //Received SMS position
pos_sms_rx=gsm.IsSMSPresent(type_sms);
if (pos_sms_rx!=0)
{
//Read text/number/position of sms
gsm.GetSMS(pos_sms_rx,number_incoming,sms_rx,120);
Serial.print("Received SMS from ");
Serial.print(number_incoming);
Serial.print("(sim position: ");
Serial.print(word(pos_sms_rx));
Serial.println(")");
Serial.println(sms_rx);
//if (del_sms==1) //If 'del_sms' is 1, i delete sms
{
error=gsm.DeleteSMS(pos_sms_rx);
if (error==1)Serial.println("SMS deleted");
else Serial.println("SMS not deleted");
}
}
else
{
Serial.println("SMS NOT RECEVIED");
}
return;
}
void loop()
{
char inSerial[5];
int i=0,m;
gsm.listen1();
Check_SMS(); //Check if there is SMS
m=atoi(sms_rx);
byte msg[] = {
1, // device id
2, // no of rotations
3 // direction
};
// if(m==2)
{
Serial.println("m=2");
// send to slave
digitalWrite (ENABLE_PIN, HIGH); // enable sending
delay(1);
sendMsg (fWrite, msg, sizeof msg);
delay(1);
digitalWrite (ENABLE_PIN, LOW); // disable sending
//Send_SMS();
}
gsm.flush1();
//gsm.end1();
delay(100);
// rs485.listen();
// rs485.listen();
byte buf [10];
byte received123 = recvMsg (fAvailable, fRead, buf, sizeof buf);
if(received123)
{
Serial.println("receved");
Serial.println(buf[0]);
Serial.println(buf[1]);
Serial.println(buf[2]);
digitalWrite(l1,LOW);
//delay(10000);
digitalWrite(l1,HIGH);
}
delay(1000);
// gsm.end1();
// rs485.flush();
// gsm.flush1();
//rs485.end();
clearbuf(buf);
}
slave:
#include <SoftwareSerial.h>
#include "RS485_protocol.h"
SoftwareSerial rs485 (2, 3);
// receive pin, transmit pin
const byte ENABLE_PIN = 2;
/***** Devices ********/
const byte l1=8;
const byte l2=9;
const byte l3=10;
const byte l4=11;
const byte l5=12;
const byte l6=13;
byte n=0;
byte s;
void fWrite (const byte what)
{
//Serial.write (what);
rs485.write (what);
delay(10);
}
int fAvailable ()
{
//return Serial.available ();
return rs485.available ();
}
int fRead ()
{
//return Serial.read ();
return rs485.read ();
}
void clearbuf(byte *b)// clearing the buffer
{
while(*b)
*b++='\0';
}
void setup()
{
rs485.begin (19200);
pinMode (ENABLE_PIN, OUTPUT);
pinMode (l1, OUTPUT);
pinMode (l2, OUTPUT);
pinMode (l3, OUTPUT);
pinMode (l4, OUTPUT);
pinMode (l5, OUTPUT);
pinMode (l6, OUTPUT);
digitalWrite (l1, HIGH); // disable 1
digitalWrite (l2, HIGH); // disable 1
digitalWrite (l3, HIGH); // disable 1
digitalWrite (l4, HIGH); // disable 1
digitalWrite (l5, HIGH); // disable 1
digitalWrite (l6, HIGH); // disable 1
Serial.begin(9600);
Serial.println("SLAVE 1");
}
void loop()
{
byte buf [10];
//rs485.listen();
byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf));
if (received)
{
switch(buf[0])
{
case 0: Serial.println("I am MASTER");
Serial.print("SLAVE ID: ");
Serial.print(buf[1]);
Serial.println(" OPERATION COMPLETED");
break;
case 1: Serial.println("I am Slave 1");
Serial.print("msg: ");
Serial.println(buf[1]);
Serial.print("Dir: ");
Serial.println(buf[2]);
digitalWrite (l1, LOW); // enable 1
delay(buf[1]);
delay(5000);
digitalWrite (l1, HIGH); // disable 1
n=1;
s=1;
break;
case 2: Serial.println("I am Slave 2");
Serial.print("msg: ");
Serial.println(buf[1]);
Serial.print("Dir: ");
Serial.println(buf[2]);
digitalWrite (l2, LOW); // enable 1
delay(buf[1]);
delay(5000);
digitalWrite (l2, HIGH); // disable 1
n=1;
s=2;
break;
case 3: Serial.println("I am Slave 3");
Serial.print("msg: ");
Serial.println(buf[1]);
Serial.print("Dir: ");
Serial.println(buf[2]);
digitalWrite (l3, LOW); // enable 1
delay(buf[1]);
delay(5000);
digitalWrite (l3, HIGH); // disable 1
n=1;
s=3;
break;
case 4: Serial.println("I am Slave 4");
Serial.print("msg: ");
Serial.println(buf[1]);
Serial.print("Dir: ");
Serial.println(buf[2]);
digitalWrite (l4, LOW); // enable 1
delay(buf[1]);
delay(5000);
digitalWrite (l4, HIGH); // disable 1
n=1;
s=4;
break;
case 5: Serial.println("I am Slave 5");
Serial.print("msg: ");
Serial.println(buf[1]);
Serial.print("Dir: ");
Serial.println(buf[2]);
digitalWrite (l5, LOW); // enable 1
delay(buf[1]);
delay(5000);
digitalWrite (l5, HIGH); // disable 1
n=1;
s=5;
break;
case 6: Serial.println("I am Slave 6");
Serial.print("msg: ");
Serial.println(buf[1]);
Serial.print("Dir: ");
Serial.println(buf[2]);
digitalWrite (l6, LOW); // enable 1
delay(buf[1]);
delay(5000);
digitalWrite (l6, HIGH); // disable 1
n=1;
s=6;
break;
case 7: Serial.println("I am Slave 7");
Serial.print("msg: ");
Serial.println(buf[1]);
Serial.print("Dir: ");
Serial.println(buf[2]);
break;
}//switch(buf[0])*/
//Serial.print("Device: ");
//Serial.println(buf[0]);
byte msg [] = {
1, // device 0 (master)
2, // slave id
3, // operation command
};
delay (10); // give the master a moment to prepare to receive
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, sizeof msg);
delay(100);
digitalWrite (ENABLE_PIN, LOW); // disable sending
delay(2000);
// end if something received
clearbuf(buf);
n=0;
//rs485.end();
}// if(recevied)
}// end of loop
And output , RS485 wiring , library’s I am using as shown below.
GSM_Shield.zip (30.6 KB)
RS485_protocol.zip (2.95 KB)