Problem with SIM900 shield

Hello everybody,

I'm trying to make weather-station which will send and receive SMS over SIM900 shield. I tested shield alone and it's working but when i incorporate code all together it's not operational and i dont know why. Shield is not responsive. Arduino and sensors are connected to 9V power supply and GSM shield on separate 9V power supply. Test code is working but weather-station code is not working for SMS. please please give me some advice. Thanks in advance.

 #include <SFE_BMP180.h>
#include "DHT.h"
#include <LiquidCrystal.h>
#include <Wire.h>
#include <SoftwareSerial.h>

SFE_BMP180 pressure;
SoftwareSerial SIM900(50, 51);
float temperature; //Temperature is number with decimal point so we are using floating point

#define ALTITUDE 20.0 // Altitude in Split, Croatia

#define DHTPIN 22     // what pin we're connected to with DHT sensor

#define DHTTYPE AM2301

const int analogInPin = A9;  //
const int soilPin = A11;  // Soil sensor output pin to Arduino analog A11 pin
const int gasPin = A10; //GAS sensor output pin to Arduino analog A10 pin

char inchar; // Will hold the incoming character from the GSM shield

int sensorValue = 0;        // value read from the pot
int sensorValue2 = 0;        // gas sensor value
int sensorValue3 = 0;       // soil humidity sensor value

int outputValue = 0;        // value output to the PWM (analog out)
int outputValue2 = 0;
int outputValue3 = 0;


int Rele = 30;
int led1 = 32;
int led2 = 36;
int led3 = 38;
int led4 = 40;
int Temp = 26;


float thickness;
int count, count1;

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


void setup() {

  lcd.begin(16, 2);
  lcd.print("Reading sensors");
  dht.begin();
  pressure.begin();
  pinMode(Rele, OUTPUT);
  //Serial.begin(9600);

  //Serial.begin(19200);
  //Serial.println("BEGIN LED INIT");
  //set up the digital pins to control
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  //Serial.println("END LED INIT");

  // wake up the GSM shield
  //Serial.println("Starting ...");
  //Serial.println("Start SIM power");
  //SIM900power();
  //Serial.println("END SIM power");
  SIM900.begin(19200);
  //Serial.println("Start of 20 sec");
  //delay(20000);  // give time to log on to network.
  //Serial.println("End of 20 sec");
  //Serial.println("BEGIN set SMS mode to text");
  SIM900.print("AT+CMGF=1\r");  // set SMS mode to text
  delay(100);
  SIM900.print("AT+CNMI=2,2,0,0,0\r");
  //Serial.println("END set SMS mode to text");
  //blurt out contents of new SMS upon receipt to the GSM shield's serial out
  delay(100);
  //Serial.println("Ready...");

}


void loop() {


  //If a character comes in from the cellular 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(led1, LOW);
          //Serial.println("SMS received for LED1 turn off");
        }
        else if (inchar == '1')
        {
          digitalWrite(led1, HIGH);
          //Serial.println("SMS received for LED1 turn on");
        }
        delay(10);
        inchar = SIM900.read();
        if (inchar == 'b')
        {
          inchar = SIM900.read();
          if (inchar == '0')
          {
            digitalWrite(led2, LOW);
          }
          else if (inchar == '1')
          {
            digitalWrite(led2, HIGH);
          }
          delay(10);
          inchar = SIM900.read();
          if (inchar == 'c')
          {
            inchar = SIM900.read();
            if (inchar == '0')
            {
              digitalWrite(led3, LOW);
            }
            else if (inchar == '1')
            {
              digitalWrite(led3, HIGH);
            }
            delay(10);
            inchar = SIM900.read();
            if (inchar == 'd')
            {
              delay(10);
              inchar = SIM900.read();
              if (inchar == '0')
              {
                digitalWrite(led4, LOW);
              }
              else if (inchar == '1')
              {
                digitalWrite(led4, HIGH);
              }
              delay(10);
            }
          }
          //SIM900.println("AT+CMGD=1,4"); // delete all SMS
          //Serial.println("All SMS deleted");
        }
      }
    }
  }


  float humidity, pressure;
  humidity = dht.readHumidity() + 10.0f;
  pressure = readPressureAndTemperature();
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  outputValue = map(sensorValue, 0, 1023, 0, 100);
  sensorValue2 = analogRead(gasPin);
  outputValue2 = map(sensorValue2, 0, 1023, 0, 100);
  sensorValue3 = analogRead(soilPin);
  outputValue3 = map(sensorValue3, 25, 727, 100, 0);
  // Serial.print("Gas");
  // Serial.println(sensorValue2);
  // Serial.print("Soil");
  // Serial.println(sensorValue3);
  // map it to the range of the analog out:


  if (temperature >= Temp) {
    digitalWrite(Rele, LOW);
    delay(1000);
  } else {
    digitalWrite(Rele, HIGH);
  }
  delay(1000);


  lcd.clear();

  char tempF[8];
  char humF[8];
  char pressF[7];

  dtostrf(temperature, 5, 1, tempF);
  dtostrf(humidity, 2, 0, humF);
  dtostrf(pressure, 7, 2, pressF);

  //Printing Temperature
  lcd.print("T:");
  lcd.print(tempF);
  lcd.print((char)223);
  lcd.print("C ");

  //Printing Humidity
  lcd.print("H: ");
  lcd.print(humF);
  lcd.print("%");

  //Printing Pressure
  lcd.setCursor(0, 1);
  lcd.print("P: ");
  lcd.print(pressF);
  lcd.print(" hPa");
  delay(3000);
  lcd.clear();

  lcd.print("Gas: ");
  lcd.print(outputValue2);
  lcd.print("%");

  lcd.setCursor(0, 1);
  lcd.print("Soil Hum.: ");
  lcd.print(outputValue3);
  lcd.print("%");
  delay(3000);

}

// -----------------------------------------------------------------------------------------------------------------------

float readPressureAndTemperature()
{
  char status;
  double T, P, p0, a;

  status = pressure.startTemperature();
  if (status != 0)
  {
    delay(status);
    status = pressure.getTemperature(T);
    if (status != 0)
    {
      temperature = T;
      status = pressure.startPressure(3);
      if (status != 0)
      {
        delay(status);
        status = pressure.getPressure(P, T);
        if (status != 0)
        {
          p0 = pressure.sealevel(P, ALTITUDE);
          return p0;
        }
      }
    }
  }
}



void SIM900power()
// software equivalent of pressing the GSM shield "power" button
{
  digitalWrite(9, HIGH);
  delay(1000);
  digitalWrite(9, LOW);
  delay(5000);
}
  if (SIM900.available() > 0)
  {
    inchar = SIM900.read();
    if (inchar == '#')
    {
      delay(10);

      inchar = SIM900.read();

Waiting 10 milliseconds does NOT guarantee that a character has arrived.

The code you posted does something. You expect the code to do something. You need to describe exactly what those two somethings are, so we can tell you why your expectations are wrong.

Thanks for the answer,

So my problem is that when i send SMS nothing happens and i'm not able to control anything over sms . All other sensors are working. Temperature humidity ... Also Relay is triggered when Temp is more than 26 *C.

So ... i installed this GSM SIM900 shield so i can turn off /on and on led's and relays over SMS.

When i install only code for SIM900 everything is working, when i add code for other sensors SMS is not working. This is my core problem. Should i introduce interrupts? Is there possibility that other sensor are interfering with SIM900? and how?

I will try to increase delay as you suggested, and will report results.

Thanks one more time.

BR.

Delay is increased on 100 and same behavior ...

I will try to increase delay as you suggested, and will report results.

I did NOT suggest that increase the time you have your head stuck in the sand.

You need to wait until there is another character to read, however long that takes, and ONLY wait that long.

   while(Serial.available() < 1) { // Do nothing }

What do your Serial.print()s tell you is happening?

I tried to introduce this while loop but results was: Nothing ... all other sensors are messed up, then.

I will try with interrupts but first of all i need to figure it out how to implement this.

It seems that all other sensors are interfering with loop for SMS ...

I hope that my head is little bit out of the sand now :slight_smile: or not? :confused: