0

I want to check the water level and have an SMS sent via a GSM module. Reed switches are connected to four Arduino analog inputs.

I am unable to add a string and tank level (TKLVL) in program. The program is sending some garbage value via SMS. But TKLVL variable prints the right value.

What is wrong with my code?

    #include <SoftwareSerial.h>
    SoftwareSerial SIM900(10, 11);
    String textMessage;
    #define LED1 2
    #define IRLED 3
    //#define rf433DATA 4
    //D4 IS CONNECTED TO RF433MHZ RECEIVER CHANGE ACC TO LIBRARY
    #define PIRpower 5
    int receiver = 6;
    #define VALVE1 7
    #define GSMRESET 8
    #define pumpONdetect 9
    #define VALVE2 12
    //#define TANK_SENSPOWER 13 //
    #define TANK_SENS4 A0  //full tank wigh buzzer
    #define TANK_SENS3 A1
    #define TANK_SENS2 A2
    #define TANK_SENS1 A3  //low tank
    int TKLVL= 0;
    int LEVEL;

    void setup()
    { pinMode(LED1, OUTPUT);
      pinMode(IRLED, OUTPUT);
      pinMode(PIRpower, OUTPUT);
      //CHECK RECEIVER PIN
      pinMode(GSMRESET, OUTPUT);
      pinMode(VALVE2, OUTPUT);
      // pinMode(TANK_SENSPOWER, OUTPUT); //DRIVE THIS PIN LOW TO ON TANK POWER(PNP TRANSISTOR)
      pinMode(TANK_SENS4, INPUT); //FULL TANK WITH BEEP BUZZER
      pinMode(TANK_SENS3, INPUT);
      pinMode(TANK_SENS2, INPUT);
      pinMode(TANK_SENS1, INPUT); //LOW TANK WATER
      // digitalWrite(TANK_SENSPOWER, HIGH); //turns OFF tanksenspower OFFvia PNP transistor..
      Serial.begin(9600);
      SIM900.begin(9600);
      Serial.print("SIM900 ready...");
      SIM900.println("AT+CMGD=1,4\r");
      delay(1000);
      // AT command to set SIM900 to SMS mode
      SIM900.println("AT+CMGF=1\r");
      delay(400);
      // Set module to send SMS data to serial out upon receipt
      SIM900.println("AT+CNMI=2,2,0,0,0\r");
      delay(400);
      digitalWrite(VALVE1, LOW);

    }

    void loop()
    {
      if (SIM900.available() > 0) {
        textMessage = SIM900.readString();
        Serial.print(textMessage);
        delay(10);
      }
      if (textMessage.indexOf("Von") >= 0)
      {
        G_valve1On();
        Serial.println("on loop");
        textMessage = "";
      }
      if (textMessage.indexOf("Voff") >= 0)
      {
        G_valve1Off();
        Serial.println("off loop");
        textMessage = "";
      }

      if (textMessage.indexOf("Tanklvl") >= 0)
      {
        Tanklvl_calc();
         Serial.print("TKLVL is=");
         Serial.println(TKLVL);
        //  TKLVL=LEVEL;
        // Serial.print("level is=");
        //Serial.println(LEVEL);
        String msg ="TANK IS " + TKLVL; 
        Serial.println(msg);   
        sendSMS(msg);
        textMessage = "";
      }

    }
    //SEND SMS function
    void sendSMS(String lvlmsg)
    {
      SIM900.print("AT+CMGF=1\r");
      delay(1000);
      SIM900.println("AT+CMGS=\"+9182375*****\"\r");
      delay(100);
      SIM900.println(lvlmsg);
      delay(100);
      SIM900.println((char)26);
      delay(1000);
      SIM900.println();
      delay(5000);
    }



    void G_valve1On()
    {
      digitalWrite(VALVE1, HIGH);
      Serial.println("...turning valve1 on...");
    }
    void G_valve1Off()
    {
      digitalWrite(VALVE1, LOW);
      Serial.println("...turningvalve1 off...");
    }


    int Tanklvl_calc()
    {
      if (digitalRead(TANK_SENS4) == HIGH)
      {    
        TKLVL = 100;
        Serial.println("TANK LEVEL 100%");
      }
      else if (digitalRead(TANK_SENS3) == HIGH)
      {    
        TKLVL = 75;
        Serial.println("TANL LEVEL 75%");
      }
      else if (digitalRead(TANK_SENS2) == HIGH)
      {    
        TKLVL = 50;
        Serial.println("TANK LEVEL 50%");
      }
      else if (digitalRead(TANK_SENS1) == HIGH)
      {
        TKLVL = 25;
        Serial.println("TANK LEVEL 25%");
      }
      else
      { 
        TKLVL = 0;
        Serial.println("TANK LEVEL IS LESS THAN 25%");
      }  
      return(TKLVL);
    }
  • Are you receiving some sms from UNO ? Did serial monitor print correct value? – Mitu Raj Nov 23 '17 at 17:23
  • Garbage values received in sms – Nikhil P Nov 23 '17 at 17:27
  • @MITURAJ plz chk string msg command,I think it nor combining statement +level variable. – Nikhil P Nov 23 '17 at 17:30
  • Serial.println(msg); <-- Did this work? I doubt if it prints junk value cz TKLVL is int type. Convert it to string using ---> String msg = "TANK IS " + String(TKLVL) – Mitu Raj Nov 23 '17 at 17:34
  • @MITURAJ No this line doesnt print any value. – Nikhil P Nov 23 '17 at 17:38
0

TKLVL is integer type. Convert it to String object and add :-

String msg = "TANK IS " + String(TKLVL)

0

Using Strings in a microcontroller is a no-no. First, try to send a test SMS, to see if everything works OK on SIM800-Arduino etc.

If you receive it correctly, then try to play with char arrays. First, declare a char array big enough for your data to fit in. Let's use a demo array, but keep in mind that it's your responsibility to keep things tidy.

byte sizeOfBuffer = 10;
char smsString[sizeOfBuffer] = {'\0'};

What we did is create an smsString array of 10 bytes, 9 bytes for data and 1 byte for our special end of sting character. Since we don't want Arduino to handle things with Strings, we have to tell our microcontroller where does that String end. That's what '\0' does.

Then, modify the send function to accept that data type.

//SEND SMS function with char array
void send_SMS_charArray(char *lvlmsg, byte sizeOfsms)
{
  SIM900.print("AT+CMGF=1\r");
  delay(1000);
  SIM900.println("AT+CMGS=\"+918237504767\"\r");
  delay(100);
  for (byte i=0; i<sizeof(sizeOfsms); i++){
    SIM900.print(lvlmsg[i]);
  }
  Serial.println();
  delay(100);
  SIM900.println((char)26);
  delay(1000);
  SIM900.println();
  delay(5000);
}

Finally, to call the new function you would do something like

sprintf_P(smsString, PSTR("TANK IS: %i"), TKLVL);
send_SMS_charArray(smsString, sizeOfBuffer);

Notice sprintf_P when using static messages such as "TANK IS". It will save your life in a few months.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.