Go Down

Topic: Arduino Uno with (RS485 Interface) + (GSM Modem) ? (Read 2192 times) previous topic - next topic

muhannad17

Hi,

      I'm trying to connect two devices with my Ardunio Uno , the first device is a meter with RS485 interface .. the second device is SM5100B GSM modem.

      I'm new with those devices and with some researches i found that they are both use RX and TX pin (Is that right?) , in Arduino board there are 1 RX and 1 TX .. so how can i connect those two devices with the Arduino Board ?

     I just need headlines for how do i connect those devices to my board and which librarys is should use ?
     Also how can i communicate with both of them  ?

Thanks,

PeterH

I only provide help via the forum - please do not contact me for private consultancy.

Manidhar

Hi muhannad,

Have you done your project rs485+gsm.

I am also suffering the same problem .

If you are rectify that problem please send me the code, schematics, library's, 

Thanks in advance.

Robin2

Hi muhannad,

Have you done your project rs485+gsm.

I am also suffering the same problem .

If you are rectify that problem please send me the code, schematics, library's, 

Thanks in advance.
You are just 4 years late to the party.  :)

If you want extra serial ports on an Uno use SoftwareSerial. A better solution would be to use a Mega which has four HardwareSerial ports.

...R

Manidhar

hi Robin,

I tried Software serial also, but i can success with differently.

that is it is work rs485, gsm.

but when i am using both it does not work,(multiple software serial)

why,

present i have only uno boards, so i want to implement uno only.

could you help me to complete this project,

thanks in advance.

 

Robin2

but when i am using both it does not work,(multiple software serial)

why,
As you have not posted your code I have no idea. Please use the code button </>
Code: [Select]
so code looks like this

...R

Manidhar

this is my code.

Master:

Code: [Select]



/* 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:

Code: [Select]


#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.

Robin2

I wasn't expecting two programs. You will have to explain what each of them is supposed to do and what it actually does.

Have both programs got problems?

If you are using the Arduino IDE please use autoFormat to make your code more readable.

...R

Manidhar

Thanks for the reply,

Actually I am controlling devices using GSM.

separately both are working excellent.

but when I am clubbing the both GSM and RS485 by using multi port software serial, Rs485 doesn't work.(Show the output window in my previous post).

and also I am using listen(), end() functions also but no use.

I can't find out where is the problem occurred.

And I am using Arduino IDE V 1-6-4. I can't understand auto formate.

Thanks.....
 

Robin2

If you won't explain the programs you have posted I can't help.

...R

Go Up