I have started with a project to control some functions in my Trailer using UNO with GSM shield and Relay shield. Klick ! Klick !
The Idea is to turn on lights and heating, check temperature and the voltage in the trailer.
To control the Relays works fine. The next steep to check the status of the Relays with “digitalRead” doesn’t work.
// Control of a Ralay shield - Last update: AndreasVan 2015-04-04 Version 1.01
// Micro controller = Arduino UNO - GSM Shield
// this code is public domain, enjoy!
#include <SoftwareSerial.h>
char inchar; //Will hold the incoming character from the Serial Port.
SoftwareSerial SIM900(7,8); // Software Serial Pin7 is Rx pin, pin 8 is Tx pin.
int relay01 = 10;
int relay02 = 11;
int relay03 = 12;
int relay04 = 13;
int led = 2;
int val1 = 0; // variable to store the relay1 value
void setup()
{
Serial.begin(19200);
Serial.print(" Start ");
pinMode(relay01, OUTPUT); // prepare the digital output pins
pinMode(relay02, OUTPUT);
pinMode(relay03, OUTPUT);
pinMode(relay04, OUTPUT);
pinMode(led, OUTPUT);
digitalWrite(relay01, LOW);
digitalWrite(relay02, LOW);
digitalWrite(relay03, LOW);
digitalWrite(relay04, LOW);
pinMode(9, OUTPUT ); // software equivalent of pressing the GSM shield "power" button
digitalWrite(9, HIGH);
delay(2000);
digitalWrite(9, LOW);
Serial.print(" Wait for network ");
SIM900.begin(19200); // Initialize GSM module serial port for communication.
delay(30000); // give time for GSM module to register on network etc.
SIM900.print("AT+CMGF=1\r"); // set SMS mode to text
delay(200);
SIM900.print("AT+CNMI=2,2,0,0,0\r"); // set module to send SMS data to serial out upon receipt
delay(200);
digitalWrite(led, HIGH); // GSM Shield ready
delay(500);
digitalWrite(led, LOW);
delay(500);
digitalWrite(led, HIGH);
Serial.print(" Ready ! ");
}
void loop() {
//If a character comes in from the SIM900ular module...
if(SIM900.available() >0)
{
inchar=SIM900.read();
if (inchar=='#')
{
delay(10);
inchar=SIM900.read();
if (inchar=='a')
Serial.print(" Read Relay1 ");
{
delay(10);
inchar=SIM900.read();
if (inchar=='0')
{
digitalWrite(relay01, LOW);
}
else if (inchar=='1')
{
digitalWrite(relay01, HIGH);
}
delay(10);
inchar=SIM900.read();
if (inchar=='b')
{
inchar=SIM900.read();
if (inchar=='0')
{
digitalWrite(relay02, LOW);
}
else if (inchar=='1')
{
digitalWrite(relay02, HIGH);
}
delay(10);
inchar=SIM900.read();
if (inchar=='c')
{
inchar=SIM900.read();
if (inchar=='0')
{
digitalWrite(relay03, LOW);
}
else if (inchar=='1')
{
digitalWrite(relay03, HIGH);
}
delay(10);
inchar=SIM900.read();
if (inchar=='d')
{
delay(10);
inchar=SIM900.read();
if (inchar=='0')
{
digitalWrite(relay04, LOW);
}
else if (inchar=='1')
{
digitalWrite(relay04, HIGH);
}
delay(10);
inchar=SIM900.read();
Serial.print(" Read state ");
if (inchar=='s')
{
inchar=SIM900.read();
if (inchar=='0')
{
sendSMS();
do {} while (1);
}
else if (inchar=='1')
{
sendSMS();
}
delay(10); }
}
SIM900.println("AT+CMGD=1,4"); // delete all SMS
}
}
}
}
}
}
void sendSMS(){
val1 = digitalRead(relay01); // read the relay pin
Serial.print("relay01, val1");
Serial.print(" Send SMS ");
SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(100);
SIM900.println("AT + CMGS = \"+4612345678\""); // recipient's mobile number, in international format
delay(100);
SIM900.println("relay01, val1"); // message to send
Serial.print("relay01, val1");
delay(100);
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(100);
SIM900.println();
delay(5000); // give module time to send SMS
}
The commands #a1 #a0 and so on works fine. When I send #s1 or #s0 I don’t get anything.
I have already testet to send SMS with the shield without problems.
Does anybody have a solution for this?