Need Advice with Trailer project

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?

Don't try to read the relay pin.
Instead set val1 = 1 when the relay is activated and set it = 0 when the relay is deactivated.

Now I coming a little bit longer … :wink:

// Control of a Ralay shield - Last update: AndreasVan 2015-04-07 Version 6.01    
// Micro controller = Arduino UNO - GSM Shield 
// this code is public domain, enjoy!

#include <SoftwareSerial.h>
#include "DHT.h"
#define DHTPIN A5    
#define DHTTYPE DHT22  
DHT dht(DHTPIN, DHTTYPE);
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 = 1;     // variable to store the relay1 value
int val2 = 1;     // variable to store the relay2 value
int val3 = 1;     // variable to store the relay3 value
int val4 = 1;     // variable to store the relay4 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 ! ");  
  dht.begin();
}

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();
                   } 
                 else if (inchar=='1')
                 {
                 sendSMS1();
                 }
                 delay(10);           }
                }
            SIM900.println("AT+CMGD=1,4"); // delete all SMS
        }
      }
    }
  }
}
}

void sendSMS(){
  val1 = digitalRead(relay01);   // read the relay pin
  val2 = digitalRead(relay02);   // read the relay pin 
  val3 = digitalRead(relay03);   // read the relay pin
  val4 = digitalRead(relay04);   // 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("Relay1:");
  SIM900.println(val1);                                         // message to send
  SIM900.println("Relay2:");
  SIM900.println(val2);
  SIM900.println("Relay3:");
  SIM900.println(val3);
  SIM900.println("Relay4:");
  SIM900.println(val4);
  delay(100); 
  SIM900.println((char)26);                                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(2000);                                                    // give module time to send SMS

}

void sendSMS1(){
  // Wait a few seconds between measurements.
  delay(2000);
    // Reading temperature or humidity
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
   Serial.println("Failed to read from DHT sensor!");
    return;
    }
     float hi = dht.computeHeatIndex(f, h);
  
  Serial.print(t);
  Serial.print(h);  
  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("Temp:");  
  SIM900.println(t);                                                // message to send
  SIM900.println("Humidity:"); 
  SIM900.println(h); 
  delay(100); 
  SIM900.println((char)26);                                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(2000);                                                    // give module time to send SMS

}

I can switch the Relays on and off and I can read status and temperature.
When I try to send only #s1 or #s0 to read status and temperature it dosen’t work. :slightly_frowning_face:

The nesting of your code is hurting you - you can only get to the code for 's' if you've received a 'd' and other predecessor characters. It might be better to read the whole command (from # to carriage return) and then parse it.

Why does delay(10) appear under 'a' and 'd' but not under 'b' or 's'? (I bet 'b' doesn't work either.)

delay(10);
inchar=SIM900.read();

This is not the right way to read serial. I suggest you read Robin's Serial Input Basics

The best way is for a major reorganization of your program. Make the main loop run continuously, with no delays and no waiting for serial data. It needs to know where it's up to with the serial input - are we waiting for a '#'? Have we seen a '#' and we're waiting for the a/b/d/s? Once it has this ability to remember where it's up to in the input then it never has to wait. It only does some action after a new piece of data arrives.

If it receives something it doesn't recognise, then it discards the rest of the input until it gets back in sync - finds another '#'.

wildbill:
The nesting of your code is hurting you - you can only get to the code for 's' if you've received a 'd' and other predecessor characters. It might be better to read the whole command (from # to carriage return) and then parse it.

Yes it hurts, can't see the wood for the trees anymore :o

MorganS:
Why does delay(10) appear under 'a' and 'd' but not under 'b' or 's'? (I bet 'b' doesn't work either.)

You loose :wink:

MorganS:
This is not the right way to read serial. I suggest you read Robin's Serial Input Basics

The best way is for a major reorganization of your program. Make the main loop run continuously, with no delays and no waiting for serial data.

Thanks for the link, i'll think it over again.

Thanks a lot for all your Feedback!
The project is in place with some changes and works fine on the trailer.
Now I have to send the whole string (for example #a0b1c0d1) and I get the answer directly incl. temperature and humidity and onboard voltage.

// Control of a Ralay shield - Last update: AndreasVan 2015-04-11 Version 13.01    
// Micro controller = Arduino UNO - GSM Shield 
// this code is public domain, enjoy!
#include <SoftwareSerial.h>
#include <DHT.h>
#define DHTPIN A5    
#define DHTTYPE DHT22  
DHT dht(DHTPIN, DHTTYPE);
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 = 1;                // variable to store the relay1 value - Light 1
int val2 = 1;                // variable to store the relay2 value - Light 2
int val3 = 1;                // variable to store the relay3 value - Heater 
int val4 = 1;                // variable to store the relay4 value - Reserve
const int doorPin = 3;       // the number of the pushbutton pin
int door = 1;                // variable for reading the pushbutton status
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(doorPin, INPUT);   

  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 ! ");  
  dht.begin();
  sendSMS0();
}

void loop() {

  if(SIM900.available() >0)
  {
    inchar=SIM900.read(); 
    if (inchar=='#')
    {
      delay(10);
      inchar=SIM900.read(); 
      if (inchar=='a')
      {
        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);
            }
          }
          SIM900.println("AT+CMGD=1,4"); // delete all SMS
          sendSMS();
        }
      }
    }
  }
}


void sendSMS(){
  val1 = digitalRead(relay01);   // read the relay pin
  val2 = digitalRead(relay02);   // read the relay pin 
  val3 = digitalRead(relay03);   // read the relay pin
  val4 = digitalRead(relay04);   // read the relay pin
  door = digitalRead(doorPin);
  // Wait a few seconds between measurements.
  delay(3000);
    // Reading temperature or humidity
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);
  // Read Voltage (12V)
  float v = analogRead(A4) * 17.8 / 1023; // read voltage
  delay(2000);
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
   Serial.println("Failed to read from DHT sensor!");
    return;
    }
     float hi = dht.computeHeatIndex(f, h);
  Serial.print(" Send SMS ");  
  Serial.print(" Temp: ");
  Serial.print(t);
  Serial.print(" Humidity: ");  
  Serial.print(h);  
  Serial.print(" Voltage ");  
  Serial.print(v);  
  SIM900.print("AT+CMGF=1\r");                                    // AT command to send SMS message
  delay(100);
  SIM900.println("AT + CMGS = \"+461234567\"");          // recipient's mobile number, in international format
  delay(100);
  SIM900.println("Indoor temp:");                                 // message to send                         
  SIM900.println(t);  
  SIM900.println("Indoor humidity:"); 
  SIM900.println(h); 
  SIM900.println("Board voltage:"); 
  SIM900.println(v);  
  if (door == 0) {      
   SIM900.println("Door is open");  }
  else  {
  SIM900.println("Door is closed");  }
  if (val1 == 0) {      
  SIM900.println("Kitchen light is off"); }
  else  {
  SIM900.println("Kitchen light is on");  }
  if (val2 == 0) {      
  SIM900.println("Door light is off"); }
  else  {
  SIM900.println("Door light is on");  }
  if (val3 == 0) {      
  SIM900.println("Heater is off"); }
  else  {
  SIM900.println("Heater is on");  }
  if (val4 == 0) {      
  SIM900.println("Reserve is off"); }
  else  {
  SIM900.println("Reserve is on");  }
  delay(100); 
  SIM900.println((char)26);                                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(2000);                                                    // give module time to send SMS

}
void sendSMS0(){
  Serial.print(" Send SMS ");  
  SIM900.print("AT+CMGF=1\r");                                    // AT command to send SMS message
  delay(100);
  SIM900.println("AT + CMGS = \"+461234567\"");                 // recipient's mobile number, in international format
  delay(100);
  SIM900.println("Trailer System online!");
  delay(100); 
  SIM900.println((char)26);                                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(2000);                                                    // give module time to send SMS

}

Now I thinking about how it could be possible to read the calling-number from the incoming SMS and send the answer back to this number !?

I have it running one year without problems with some some slightly changes.

// Control of a Ralay shield - Last update: AndreasVan 2016-04-13 Version 19.05    
// Micro controller = Arduino UNO - GSM Shield 
// this code is public domain, enjoy!

#include <SoftwareSerial.h>
#include <DHT.h>
#define DHTPIN A5    
#define DHTTYPE DHT22  
DHT dht(DHTPIN, DHTTYPE);
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 = 1;                         // variable to store the relay1 value - Light 1
int val2 = 1;                         // variable to store the relay2 value - Light 2
int val3 = 1;                         // variable to store the relay3 value - Heater 
int val4 = 1;                         // variable to store the relay4 value - Reserve
const int powerPin = 3;               // the number of the power_on pin
int power = 1;                        // variable for reading the pushbutton status
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(powerPin, INPUT);   

  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 ! ");  
  dht.begin();
  sendSMS0();
}

void loop() {
  power = digitalRead(powerPin);
  if (power == 0)
  { 
    Serial.print("Power offline ?");
    delay(500);   
    power = digitalRead(powerPin);
    if (power == 0)
    sendSMS2(); } 
  //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')
      {
        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);
            }
          }
          SIM900.println("AT+CMGD=1,4"); // delete all SMS
          sendSMS();
        }
      }
    }
  }
}


void sendSMS(){
  val1 = digitalRead(relay01);           // read the relay pin
  val2 = digitalRead(relay02);           // read the relay pin 
  val3 = digitalRead(relay03);           // read the relay pin
  val4 = digitalRead(relay04);           // read the relay pin
  power = digitalRead(powerPin);
  // Wait a few seconds between measurements.
  delay(3000);
    // Reading temperature or humidity
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);
  // Read Voltage (12V)
  float v = analogRead(A4) * 17.8 / 1023; // read voltage
  delay(2000);
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
   Serial.println("Failed to read from DHT sensor!");
    return;
    }
     float hi = dht.computeHeatIndex(f, h);
  Serial.print(" Status Send SMS ");  
  Serial.print(" Temp: ");
  Serial.print(t);
  Serial.print(f);
  Serial.print(" Humidity: ");  
  Serial.print(h);  
  Serial.print(" Voltage ");  
  Serial.print(v);  
  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("Indoor temp:");                                 // message to send                         
  SIM900.println(t);  
  SIM900.println("Indoor humidity:"); 
  SIM900.println(h); 
  SIM900.println("Board voltage:"); 
  SIM900.println(v);  
  if (power == 0) {      
  SIM900.println("Extern power is off");  }
  else  {
  SIM900.println("Extern power is on");  }
  if (val1 == 0) {      
  SIM900.println("Kitchen light is off"); }
  else  {
  SIM900.println("Kitchen light is on");  }
  if (val2 == 0) {      
  SIM900.println("Door light is off"); }
  else  {
  SIM900.println("Door light is on");  }
  if (val3 == 0) {      
  SIM900.println("Heater is off"); }
  else  {
  SIM900.println("Heater is on");  }
  if (val4 == 0) {      
  SIM900.println("Miner is off"); }
  else  {
  SIM900.println("Miner is on");  }
  delay(100); 
  SIM900.println((char)26);                                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(2000);                                                    // give module time to send SMS
}

void sendSMS0(){
  power = digitalRead(powerPin);
  Serial.print(" Status Send SMS 0");  
  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("Trailer System Status!");
  if (power == 0) {      
  SIM900.println("Extern power is off");  }
  else  {
  SIM900.println("Extern power is on");  }
  delay(100); 
  SIM900.println((char)26);                                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(2000);                                                    // give module time to send SMS
}

void sendSMS2(){
  power = digitalRead(powerPin);
  Serial.print(" Offline Send SMS 2 ");  
  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("Trailer Offline! Restart in 3min!");
  if (power == 0) {      
  SIM900.println("Extern power is off");  }
  else  {
  SIM900.println("Extern power is on");  }
  delay(100); 
  SIM900.println((char)26);                                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(60000);
  Serial.print("1. Minute");
  delay(60000);
  Serial.print("2. Minute");
  delay(60000);
  Serial.print("3. Minute");
  delay(60000);

  sendSMS0();
}

Now I wanted to include a second sensor and it does not work. :neutral_face: :slightly_frowning_face:

// Control of a Ralay shield - Last update: AndreasVan 2016-04-13 Version 20.01    
// Micro controller = Arduino UNO - GSM Shield 
// this code is public domain, enjoy!

#include <SoftwareSerial.h>
#include <DHT.h>
#define DHTPIN A5                     // DHT22 Indoor
#define DHTPIN2 A0                    // DHT22 Outdoor
#define DHTTYPE DHT22  
DHT dht(DHTPIN, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);

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 = 1;                         // variable to store the relay1 value - Light 1
int val2 = 1;                         // variable to store the relay2 value - Light 2
int val3 = 1;                         // variable to store the relay3 value - Heater 
int val4 = 1;                         // variable to store the relay4 value - Reserve
const int powerPin = 3;               // the number of the power_on pin
int power = 1;                        // variable for reading the pushbutton status
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(powerPin, INPUT);   

  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 ! ");  
  dht.begin();
  sendSMS0();
}

void loop() {
  power = digitalRead(powerPin);
  if (power == 0)
  { 
    Serial.print("Power offline ?");
    delay(500);   
    power = digitalRead(powerPin);
    if (power == 0)
    sendSMS2(); } 
  //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')
      {
        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);
            }
          }
          SIM900.println("AT+CMGD=1,4"); // delete all SMS
          sendSMS();
        }
      }
    }
  }
}


void sendSMS(){
  val1 = digitalRead(relay01);           // read the relay pin
  val2 = digitalRead(relay02);           // read the relay pin 
  val3 = digitalRead(relay03);           // read the relay pin
  val4 = digitalRead(relay04);           // read the relay pin
  power = digitalRead(powerPin);
  // Wait a few seconds between measurements.
  delay(3000);
    // Reading temperature or humidity
  float h = dht.readHumidity();
  float h2 = dht2.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  float t2 = dht2.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);
  float f2 = dht2.readTemperature(true);
  // Read Voltage (12V)
  float v = analogRead(A4) * 17.8 / 1023; // read voltage
  delay(2000);
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
   Serial.println("Failed to read from DHT sensor!");
    return;
    }
     float hi = dht.computeHeatIndex(f, h);
     float hi2 = dht.computeHeatIndex(f2, h2);
  Serial.print(" Status Send SMS ");  
  Serial.print("Indoor temp:");
  Serial.print(t);
//  Serial.print(f);
  Serial.print("Indoor humidity:");  
  Serial.print(h);  
  Serial.print("Outdoor temp:");
  Serial.print(t2);
//  Serial.print(f2); 
  Serial.print("Outdoor humidity:");  
  Serial.print(h2); 
  Serial.print("Voltage");  
  Serial.print(v);  
  Serial.print("start 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("Indoor temp:");                                 // message to send                         
  SIM900.println(t);  
  SIM900.println("Indoor humidity:"); 
  SIM900.println(h); 
  SIM900.println("Outoor temp:");                                 // message to send                         
  SIM900.println(t2);  
  SIM900.println("Outdoor humidity:"); 
  SIM900.println(h2); 
  SIM900.println("Board voltage:"); 
  SIM900.println(v);  
  if (power == 0) {      
  SIM900.println("Extern power is off");  }
  else  {
  SIM900.println("Extern power is on");  }
  if (val1 == 0) {      
  SIM900.println("Kitchen light is off"); }
  else  {
  SIM900.println("Kitchen light is on");  }
  if (val2 == 0) {      
  SIM900.println("Door light is off"); }
  else  {
  SIM900.println("Door light is on");  }
  if (val3 == 0) {      
  SIM900.println("Heater is off"); }
  else  {
  SIM900.println("Heater is on");  }
  if (val4 == 0) {      
  SIM900.println("Miner is off"); }
  else  {
  SIM900.println("Miner is on");  }
  delay(100); 
  SIM900.println((char)26);                                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(2000);                                                    // give module time to send SMS
}

void sendSMS0(){
  power = digitalRead(powerPin);
  Serial.print(" Status Send SMS 0");  
  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("Trailer System Status!");
  if (power == 0) {      
  SIM900.println("Extern power is off");  }
  else  {
  SIM900.println("Extern power is on");  }
  delay(100); 
  SIM900.println((char)26);                                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(2000);                                                    // give module time to send SMS
}

When I check with the Serial monitor everything locks fine until SMS has to send.

Serial.print(" Status Send SMS ");  
  Serial.print("Indoor temp:");
  Serial.print(t);
  Serial.print(f);
  Serial.print("Indoor humidity:");  
  Serial.print(h);  
  Serial.print("Outdoor temp:");
  Serial.print(t2);
  Serial.print(f2); 
  Serial.print("Outdoor humidity:");  
  Serial.print(h2); 
  Serial.print("Voltage");  
  Serial.print(v);  
  Serial.print("start SMS");
  SIM900.print("AT+CMGF=1\r");                                    // AT command to send SMS message
  delay(100);
  SIM900.println("AT + CMGS = \"+46123456789\"");                 // recipient's mobile number, in international format
  delay(100);
  SIM900.println("Indoor temp:");                                 // message to send                         
  SIM900.println(t);  
  SIM900.println("Indoor humidity:"); 
  SIM900.println(h); 
  SIM900.println("Outoor temp:");                                 // message to send                         
  SIM900.println(t2);  
  SIM900.println("Outdoor humidity:"); 
  SIM900.println(h2); 
  SIM900.println("Board voltage:"); 
  SIM900.println(v);

I see both temperatures ( inside, outside ) but the SMS never send.

Any help will be greatly appreciated !

Why have you all those delay()s in your program. The delay() function should only be used for quick-and-dirty demo programs. Use millis() to manage your timing as illustrated in Several Things at a Time

Why is this line (just as one example)

float h = dht.readHumidity();

in a function called sendSMS() ?

It should only be in a function called readHumidity() or similar

If you organize your code into single-purpose functions it will be much easier to debug. Have a look at Planning and Implementing a Program

...R

Robin2:
Why have you all those delay()s in your program. The delay() function should only be used for quick-and-dirty demo programs.

Many thanks for the prompt reply.
In the beginning it was a quick-and-dirty test program. But the trailer is far away and I have not so much time when I'm there. Anyway I will organize my code a little bit better :slight_smile:

But I can't understand what the problem is with the second sensor. If I read and send only one temperature , everything works fine. It dosen't mater which one I use.... :fearful: :neutral_face: :slightly_frowning_face: :angry:

Finally realized I had reached the limit of the number of character in one sms. :neutral_face:
That was the reason why it doesn't work.

Actual code :

// Control of a Ralay shield - Last update: AndreasVan 2016-04-23 Version 19.05b    
// Micro controller = Arduino UNO - GSM Shield 
// this code is public domain, enjoy!

#include <SoftwareSerial.h>
#include <DHT.h>
#define DHT2PIN A0  
#define DHT1PIN A5    
#define DHTTYPE DHT22  
DHT dht1(DHT1PIN, DHTTYPE);
DHT dht2(DHT2PIN, DHTTYPE);
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 gsm = 9;                          // GSM shield "power" button
int val1 = 1;                         // variable to store the relay1 value - Light 1
int val2 = 1;                         // variable to store the relay2 value - Light 2
int val3 = 1;                         // variable to store the relay3 value - Heater 
int val4 = 1;                         // variable to store the relay4 value - Reserve
const int powerPin = 3;               // the number of the power_on pin
int power = 1;                        // variable for reading the pushbutton status
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(powerPin, INPUT);   
  pinMode(gsm, OUTPUT );                  // software equivalent of pressing the GSM shield "power" button 
  digitalWrite(gsm, HIGH);
  delay(2000);
  digitalWrite(gsm, 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 ! ");  
  dht1.begin();
  dht2.begin();
  sendSMS0();
}

void loop() {
  power = digitalRead(powerPin);
  if (power == 0)
  { 
   Serial.print("Power offline ?");
   delay(500);   
   power = digitalRead(powerPin);
   if (power == 0)
   sendSMS2(); } 
    //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')
      {
        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);
            }
          }
          SIM900.println("AT+CMGD=1,4"); // delete all SMS
          sendSMS();
        }
      }
    }
  }
}


void sendSMS(){
  val1 = digitalRead(relay01);           // read the relay pin
  val2 = digitalRead(relay02);           // read the relay pin 
  val3 = digitalRead(relay03);           // read the relay pin
  val4 = digitalRead(relay04);           // read the relay pin
  power = digitalRead(powerPin);
  // Wait a few seconds between measurements.
  delay(3000);
    // Reading temperature or humidity
  float h1 = dht1.readHumidity();
  float h2 = dht2.readHumidity();
  // Read temperature as Celsius
  float t1 = dht1.readTemperature();
  float t2 = dht2.readTemperature();
  // Read temperature as Fahrenheit
  float f1 = dht1.readTemperature(true);
  float f2 = dht2.readTemperature(true);
  // Read Voltage (12V)
  float v = analogRead(A4) * 17.8 / 1023; // read voltage
  delay(2000);
  // Check if any reads failed and exit early (to try again).
  if (isnan(h1) || isnan(t1) || isnan(f2)) {
   Serial.println("Failed to read from DHT sensor!");
    return;
    }
   float hi = dht1.computeHeatIndex(f1, h1);
   Serial.print(" Status Send SMS ");  
   Serial.print("Indoor Temp:");
   Serial.print(t1);
//   Serial.print(f);
   Serial.print("Outdoor temp:");
   Serial.print(t2);
   Serial.print("Indoor Humidity:");  
   Serial.print(h1);
   Serial.print("Outdoor humidity:");  
   Serial.print(h2); 
   Serial.print(" Voltage ");  
   Serial.print(v);  
   delay(2000);
   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("Indoor:");                                 // message to send                         
   SIM900.println(t1);  
   SIM900.println(h1); 
   SIM900.println("Outdoor:");                                 // message to send                         
   SIM900.println(t2);  
   SIM900.println(h2); 
   SIM900.println("Voltage:"); 
   SIM900.println(v);  
   if (power == 0) {      
   SIM900.println("Extern power off");  }
   else  {
   SIM900.println("Extern power on");  }
   if (val1 == 0) {      
   SIM900.println("Kitchen light off"); }
   else  {
   SIM900.println("Kitchen light on");  }
   if (val2 == 0) {      
   SIM900.println("Door light off"); }
   else  {
   SIM900.println("Door light on");  }
   if (val3 == 0) {      
   SIM900.println("Heater off"); }
   else  {
   SIM900.println("Heater on");  }
   if (val4 == 0) {      
   SIM900.println("Miner off"); }
   else  {
   SIM900.println("Miner on");  }
   delay(100); 
   SIM900.println((char)26);                                       // End AT command with a ^Z, ASCII code 26
   delay(100); 
   SIM900.println();
   delay(2000);                                                    // give module time to send SMS
}

void sendSMS0(){
  power = digitalRead(powerPin);
  Serial.print(" Status Send SMS 0");  
  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("Trailer System Status!");
  if (power == 0) {      
  SIM900.println("Extern power is off");  }
  else  {
  SIM900.println("Extern power is on");  }
  delay(100); 
  SIM900.println((char)26);                                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(2000);                                                    // give module time to send SMS
}

void sendSMS2(){
  power = digitalRead(powerPin);
  Serial.print(" Offline Send SMS 2 ");  
  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("Trailer Offline! Restart in 3min!");
  if (power == 0) {      
  SIM900.println("Extern power is off");  }
  else  {
  SIM900.println("Extern power is on");  }
  delay(100); 
  SIM900.println((char)26);                                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(60000);
  Serial.print("1. Minute");
  delay(60000);
  Serial.print("2. Minute");
  delay(60000);
  Serial.print("3. Minute");

  sendSMS0();
}

Please get rid of the irritating animated image

...R