Remote controlled relays and appending strings

Hi.

I’m working on a project to remote control relays via SMS that turn on and off the heating cables in the waterpipes.

I have the following shields:

http://tronixlabs.com/arduino/shields/gsm/sim900-gsm-shield-for-arduino-australia/

http://tronixlabs.com/arduino/shields/relay/freetronics-relay8-8-channel-relay-driver-shield-for-arduino-australia/

Here is the code I currently have :

#include <SoftwareSerial.h> 
#include "Wire.h"
SoftwareSerial SIM900(2, 3);

#define I2C_ADDR 0x20 // 0x20 is the address with all jumpers removed
byte relayPort=B0;


char inchar;
String sendSMSContent[50];


void setup()
{
 Serial.begin(19200);
 Serial.println("GSM shield booting");
 Serial.println("Relay8 shield booting");
 
 Wire.begin(); // Wake up I2C bus
 
 SIM900power();// wake up the GSM shield
 SIM900.begin(19200);
 delay(5000);
 //delay(20000);  // give time to log on to network.
 SIM900.print("AT+CMGF=1\r");  // set SMS mode to text
 delay(100);
 SIM900.print("AT+CNMI=2,2,0,0,0\r"); 
 delay(100);
 
 Wire.beginTransmission(I2C_ADDR);  // Set addressing style
 Wire.write(0x12);
 Wire.write(0x20); // use table 1.4 addressing
 Wire.endTransmission();
 sendValueToLatch(relayPort); // relay 6 defaults to on on my shield. This forces it off at boot.
 // Set I/O bank A to outputs
 Wire.beginTransmission(I2C_ADDR);
 Wire.write(0x00); // IODIRA register
 Wire.write(0x00); // Set all of bank A to outputs
 Wire.endTransmission();
 
 Serial.println("GSM shield ready");
 Serial.println("Relay8 shield ready.");
}


void SIM900power()
// software equivalent of pressing the GSM shield "power" button
{
 digitalWrite(9, HIGH);
 delay(1000);
 digitalWrite(9, LOW);
 delay(7000);
}


void loop() 
{
 if(SIM900.available() >0)
 {
   inchar=SIM900.read(); 
   if (inchar=='#')
   {
     delay(10);

     inchar=SIM900.read(); 
     if (inchar=='r')
     {
       
       delay(10);
       inchar=SIM900.read();
       if (inchar=='0'){
       sendValueToLatch(relayPort);
       sendSMS("All relays are off");}
       delay(10);
       
       if (inchar=='1'){
       sendValueToLatch(B1);
       sendSMS("Flipping relay 1");}
       delay(10);
         
       if (inchar=='2'){
       sendValueToLatch(B10);
       sendSMS("Flipping relay 2");}
       delay(10);
        
       if (inchar=='3'){
       sendValueToLatch(B100);
       sendSMS("Flipping relay 3");}
       delay(10);
       
       if (inchar=='4'){
       sendValueToLatch(B1000);
       sendSMS("Flipping relay 4");}
       delay(10);
       
       if (inchar=='5'){
       sendValueToLatch(B10000);
       sendSMS("Flipping relay 5");}
       delay(10);
       
       if (inchar=='6'){
       sendValueToLatch(B100000);
       sendSMS("Flipping relay 6");}
       delay(10);  
       
       if (inchar=='7'){
       sendValueToLatch(B1000000);
       sendSMS("Flipping relay 7");}
       delay(10);
       
       if (inchar=='8'){
       sendValueToLatch(B10000000);
       sendSMS("Flipping relay 8");}
       
       delay(10);
       
       SIM900.println("AT+CMGD=1,4"); // delete all SMS
     }
   }
 }
}


void sendSMS(String sendSMSContent)
{
 SIM900.print("AT+CMGF=1\r");// AT command to send SMS message
 delay(100);
 SIM900.println("AT + CMGS = \"+0000000000\"");// recipient's mobile number, in international format
 delay(100);
 SIM900.println(sendSMSContent);// message to send
 delay(100);
 SIM900.println((char)26);// End AT command with a ^Z, ASCII code 26
 delay(100); 
 SIM900.println();
}


void sendValueToLatch(int latchValue)
{
 relayPort=relayPort^latchValue; //XOR toggles desired port
 Wire.beginTransmission(I2C_ADDR);
 Wire.write(0x12); // Select GPIOA
 Wire.write(relayPort); // Send value to bank A
 relayState();
 Wire.endTransmission();
}


void relayState() 
{
 for (byte i=0;i<8;i++)
 {
   Serial.print ("Relay ");
   Serial.print (i+1);
   if bitRead(relayPort,i) {Serial.println(" is ON");} 
   else {Serial.println(" is OFF");}
 }
}

The thing I’m trying to do is get the arduino to send me the status on all the relays via SMS whenever I flip one of the relays. I have managed to get the program to print it to serial using the void relayState(). But I can’t figgure out how to get the functionality of void relayState() into one SMS.

I have experimented with Appending Strings, but with no luck. Instead of printing to serial in the for loop…

for (byte i=0;i<8;i++)
 {
   Serial.print ("Relay ");
   Serial.print (i+1);
   if bitRead(relayPort,i) {Serial.println(" is ON");} 
   else {Serial.println(" is OFF");}
 }

I tried to append it to a String, but could not get it to work.

Does anyone know a soultion to this?

Thank you in advance!

GSM_Relaycontrol_test.ino (3.59 KB)

Welcome to the Forum. You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question …

aarg:
Welcome to the Forum. You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question …

Done. I completely forgot about that. Sorry

But I can't figgure out how to get the functionality of void relayState() into one SMS.

You mean the voidality of the function realyState()?

Could you not do something like this?

void relayState()
{
 for (byte i=0;i<8;i++)
 {
   Serial.print ("Relay ");
   Serial.print (i+1);
   if bitRead(relayPort,i) {

   Serial.println(" is ON");
   sendSMSContent = "Relay is On";
   sendSMS(sendSMSContent);

   }
   else {Serial.println(" is OFF");
   sendSMSContent = "Relay is Off";
   sendSMS(sendSMSContent);

   }


 }



}

Just off the top of my head.

As a side note… the rules on posting around here are pretty frustrating. Trying to edit a typo and I have to wait 5 minutes.: You have exceeded the number of posts you can make in a 5 minutes period. Please try again later.

Trying to post a full sketch with a 9000 character limit is also interesting :confused:

s a side note… the rules on posting around here are pretty frustrating. Trying to edit a typo and I have to wait 5 minutes.: You have exceeded the number of posts you can make in a 5 minutes period. Please try again later.

That restriction is removed after you have made a certain number of posts but I don’t know what the cutoff is.

Trying to post a full sketch with a 9000 character limit is also interesting :confused:

Hence the ability to attach files if necessary.

If you really want to control your device, why toggle the relays? Wouldn't it be more desirable to just direct the device to the desired state instead?

SMS "#1:ON" to turn on
SMS "#1:OFF" to turn off

or just:

#1:1 to turn On
#1:0 to turn Off
#1:2 to Toggle

if you are not sure, request the state via SMS:

#1:GET

PaulS:
You mean the voidality of the function realyState()?

bambamduino:
Could you not do something like this?

void relayState()

{
for (byte i=0;i<8;i++)
{
  Serial.print ("Relay ");
  Serial.print (i+1);
  if bitRead(relayPort,i) {

Serial.println(" is ON");
  sendSMSContent = “Relay is On”;
  sendSMS(sendSMSContent);

}
  else {Serial.println(" is OFF");
  sendSMSContent = “Relay is Off”;
  sendSMS(sendSMSContent);

}

}

}




Just off the top of my head.

*As a side note.. the rules on posting around here are pretty frustrating. Trying to edit a typo and I have to wait 5 minutes.*: You have exceeded the number of posts you can make in a 5 minutes period. Please try again later. 

Trying to post a full sketch with a 9000 character limit is also interesting :/

Well, I could, but I would like to get the status of all the relays sent to me in one SMS

BulldogLowell:
If you really want to control your device, why toggle the relays? Wouldn't it be more desirable to just direct the device to the desired state instead?

SMS "#1:ON" to turn on
SMS "#1:OFF" to turn off

or just:

#1:1 to turn On
#1:0 to turn Off
#1:2 to Toggle

if you are not sure, request the state via SMS:

#1:GET

How would you set that up? I'm not the best at I2C communication, but I'm learning as I go.

You can compose one string with all the relay information in it like this:

  char all[200],one[30];

  all[0] = 0; // Init a zero length null-terminated string

  for (int i = 0;i < 8;i++)
  {
    if (i) // add a space if this is not the first relay
      strcat(all," ");

    // format report for one relay
    sprintf(one,"Relay %d is %s.",i,bitRead(relayPort,i)  ? "ON" : "OFF");
    // add it to the total
    strcat(all,one);
  }
  // send out the combined report
  sendSMS(all);

Jobi-Wan:
You can compose one string with all the relay information in it like this:

  char all[200],one[30];

all[0] = 0; // Init a zero length null-terminated string

for (int i = 0;i < 8;i++)
  {
    if (i) // add a space if this is not the first relay
      strcat(all," ");

// format report for one relay
    sprintf(one,“Relay %d is %s.”,i,bitRead(relayPort,i)  ? “ON” : “OFF”);
    // add it to the total
    strcat(all,one);
  }
  // send out the combined report
  sendSMS(all);

Wow, very slick solution. Works perfectly! Thanks alot :smiley:
+karma for you!