Help with Strings. Its giving out a null value

Guys I'm quite new to Arduino and I'm having a problems with Strings, here is the snippet of the code where the problems occurs. The comments shouldtry to explain the problems. TIA! :slight_smile:

while (alarmTriped) { //this loop triggers if the variable "alarmTriped" is true.This is where my main problem is, just read through the comments that i wrote below.

        unsigned long currentMillis = millis();
        String lat, longi, speedo;
        String message;

        if (sim808.getGPS()) { //gets GPS data 
          lat = String(sim808.GPSdata.lat); // converts the numbers to string and stores it in variables
          longi = String(sim808.GPSdata.lon);
          speedo = String(sim808.GPSdata.speed_kph);

        }
        else { //if no GPS data received this is what it does
          lat = "No GPS data received.";
          longi = "No GPS data received.";
          speedo = "No GPS data received.";

        }

        message = "Latitude: " + lat + "\nLongitude: " + longi + "\nSpeed: " + speedo; /*here is my main problem, this should generate the message for sending(sends through phone text message) and stores it to "message" variable. But it does not generate the message it only gives me a null value*/

        Serial.println(message); //prints out the message in serial monitor to check if there is a message generated.

        char charbuff[message.length()]; //declares a char variable for something to store the converted message(which is a String) to char.

        message.toCharArray(charbuff, message.length() + 1); /*this line converts the message from String to Char to prepare it for sending. (since the function that im using to send the text messages only accept char values for the message.) */

        Serial.println(charbuff); // displays the converted String message to char on Serial monitor.

        if (currentMillis - prevMillis >= interval) { //this part is where it sends the message at a defined interval 

          prevMillis = currentMillis;
          sim808.sendSMS(ph_num, charbuff); /*this is the line where it sends the message which was coverted from string to char.*/

          Serial.println("sent message"); //displays confirmation that the message was sent.
        }

        if (rfid.PICC_IsNewCardPresent()) {  /*this part checks if there is a new RFID present and verifies the RFID if it matches the desired UID but we wont be focusing here (since this is not my main problem)*/
          readPass = readCard();
          verify_rfid(readPass);
          break;
        }



      }

Your replies would be very much appreciated! :smiley:

I do not have a GPS setup, but try this:

Add this line to setup()

Serial.begin(9600);

and add these lines to call the Serial object to your code:

        if (sim808.getGPS()) { //gets GPS data 
          lat = String(sim808.GPSdata.lat); // converts the numbers to string and stores it in variables
Serial.print("lat = ");
Serial.print(sim808.GPSdata.lat);

          longi = String(sim808.GPSdata.lon);

Serial.print("   longi = ");
Serial.print(sim808.GPSdata.lon);

          speedo = String(sim808.GPSdata.speed_kph);
Serial.print("   speedo = ");
Serial.printlnsim808.GPSdata.speed_kph);

        }

and then open up the Serial monitor to observe the data. This should at least tell you if you're reading the data correctly.

Oh .. that's not what i meant .... it entirely prints out nothing even if i assign a constant value to the string. Notice the "serial.print(message")" i put that in to check for any values stored on the "message" variable, but when it executes nothing gets stored into the "message" variable. It just prints out nothing.

If you don't have a Serial.begin before you use Serial.print (e.g. in your setup), it will never show anything on the serial monitor.

As you did not show the complete code, it's impossible for us to check that (or any other possible problems.

If message equals null as you state, it means more than likely that you're out of memory. Get rid of String with capital S

Yes I did put in "Serial.begin()" inside void setup() with a baud rate of 9600 . Here is the source code. Replies would be much appreciated! :smiley:

Basically what I'm trying to make is a Bike Theft Notifier and Tracker. It has two states the armed state and disarmed state.

In the armed state it senses for motion coming from the motion sensor(SW-420) while the user is away, if it detects motion from the sensor it sends a notification to the user through text message with the location of the bike.

In the disarmed state basically it just does nothing. To arm and disarm the system you just use a valid RFID card with its UID registered in the system. To disarm you just insert the RFID card near the RFID module and to arm just take out the RFID card away from the RFID module.

My main problem here for now is the actual message that is to be sent to the user. It always sends out an empty text message whenever it's triggered.

#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>
#include <MFRC522.h>

#define SS_PIN_RFID 10
#define RST_PIN_RFID 9
#define ph_num "09271806021" //put phone number here
#define PIN_TX_808    2
#define PIN_RX_808    3
#define buzz_pin 5
#define EP 4

MFRC522 rfid(SS_PIN_RFID, RST_PIN_RFID); // Instance of the class
MFRC522::MIFARE_Key key;
SoftwareSerial mySerial(PIN_TX_808, PIN_RX_808);
DFRobot_SIM808 sim808(&mySerial); //Connect RX,TX,PWR,


String pass_string = "240 190 103 77"; //UID of the RFID card
String pass_string2 = "82 192 48 91"; // UID of the RFID card
String uidString; 
String readPass;
String googlePrefix = "http://maps.google.com/maps?q=";


const long interval = 20000;

//this is for alarm periphirals
bool alarmArmed = false;
bool disarmNotif = false;
bool armNotif = false ;
bool alarmTriped = false;
bool sent1stmsg = false;


void setup() {
  Serial.begin(9600); //init serial 9600
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522
  mySerial.begin(9600); //begin serial communications for sim808


  pinMode(EP, INPUT); //set EP input for measurment
  pinMode(buzz_pin, OUTPUT);

  while (!sim808.init()) {
    delay(1000);
    Serial.print("Sim808 init error\r\n");
  }
  Serial.print("Sim808 init success!\r\n");

  //turns on GPS power
  while (!sim808.attachGPS()) {
    Serial.println("Waiting for GPS to turn on");
  }

  Serial.println("Open the GPS power success");




}

void loop() {

  if (rfid.PICC_IsNewCardPresent()) { //looks for a card and if a card is present it verifies whether it matches the defined UID above

    readPass = readCard();
    verify_rfid(readPass);


  }

  else if (!rfid.PICC_IsNewCardPresent()) { //if there is no RFID card present this is executed and arms the alarm


    if (!armNotif) {
      armNotif = true;
      digitalWrite(buzz_pin, HIGH);
      delay(750);
      digitalWrite(buzz_pin, LOW);

      //arms alarm
      alarmArmed = true;
      disarmNotif = false;

    }


    if (alarmArmed) { //if the alarm is armed it waits and detects for motion coming from the motion sensor
      long vibra_read = 0;
      unsigned long prevMillis = 0;
      vibra_read = vibra_out(); // read the vibration sensor for motion to trigger the alarm

      Serial.print("Vibra Reading: "); // displays the reading of the sensor
      Serial.println(vibra_read);

      if (!alarmTriped) {

        if (vibra_read >= 30000) { //if the reading from the motion sensor exceeds 30k it triggers the system to send the user a notification through text message.
          alarmTriped = true;
        }
      }

      while (alarmTriped) { //this loop triggers if the variable "alarmTriped" is true.This is where my main problem is, just read through the comments that i wrote below.
                            // this loop will just go on until a matching RFID card is present then the loop stops.

        unsigned long currentMillis = millis();
        String lat, longi, speedo;
        String message;

        if (sim808.getGPS()) { //gets GPS data 
          lat = String(sim808.GPSdata.lat); // converts the numbers to string and stores it in variables
          longi = String(sim808.GPSdata.lon);
          speedo = String(sim808.GPSdata.speed_kph);

        }
        else { //if no GPS data received this is what it does
          lat = "No GPS data received.";
          longi = "No GPS data received.";
          speedo = "No GPS data received.";

        }

        message = "Latitude: " + lat + "\nLongitude: " + longi + "\nSpeed: " + speedo; /*here is my main problem, this should generate 
        the message for sending(sends through phone text message) and stores it to "message" variable. But it does not generate the message it only gives me a null value*/
        Serial.println(message); //prints out the message in serial monitor to check if there is a message generated.
        char charbuff[message.length()]; //declares a char variable for something to store the converted message(which is a String) to char.
        message.toCharArray(charbuff, message.length() + 1); /*this line converts the message from String to Char to prepare it for sending. (since the function that im using to send the 
        text messages only accept char values for the message.) */
        Serial.println(charbuff); // displays the converted String message to char on Serial monitor.

        if (currentMillis - prevMillis >= interval) { //this part is where it sends the message at a defined interval 
          prevMillis = currentMillis;
          sim808.sendSMS(ph_num, charbuff); /*this is the line where it sends the message which was coverted from string to char.*/
          Serial.println("sent message"); //displayes confirmation that the message was sent.
        }

        if (rfid.PICC_IsNewCardPresent()) {  /*this part checks if there is a new RFID present and verifies the RFID, if it matches the desired UID it breaks the loop and
          disarms the alarm*/
          readPass = readCard();
          verify_rfid(readPass);
          break;
        }



      }


    }


  }

  Serial.print("Alarm Armed: "); //this is just to display if the state of the alarm. To show that if it is armed or not
  Serial.println(alarmArmed);
  Serial.print("Alarm Triped: "); //this is also just to displayed if the alarm is triped or not
  Serial.println(alarmTriped);

  delay(333);

}//Ending of void loop


//////////////////////////////////////////// CUSTOM FUNCTIONS //////////////////////////////////////////////////////////

long vibra_out() {
  long measurement = pulseIn (EP, HIGH); //wait for the pin to get HIGH and returns measurement
  return measurement;
}

String readCard() {
  rfid.PICC_ReadCardSerial();
  uidString = String(rfid.uid.uidByte[0]) + " " + String(rfid.uid.uidByte[1]) + " " + String(rfid.uid.uidByte[2]) + " " + String(rfid.uid.uidByte[3]);
  return uidString;

}

void verify_rfid(String readPass) {
  if (readPass == pass_string || pass_string2) {

    if (!disarmNotif) {
      disarmNotif = true;
      digitalWrite(buzz_pin, HIGH);
      delay(200);
      digitalWrite(buzz_pin, LOW);
      delay(50);
      digitalWrite(buzz_pin, HIGH);
      delay(200);
      digitalWrite(buzz_pin, LOW);

      //resets evertyhing
      alarmArmed = false;
      armNotif = false;
      alarmTriped = false;
      sent1stmsg = false;


    }
  }
  else {
    Serial.print("Invalid RFID!\n");

  }
}
  if (rfid.PICC_IsNewCardPresent()) { //looks for a card and if a card is present it verifies whether it matches the defined UID above

    readPass = readCard();
    verify_rfid(readPass);


  }

  else if (!rfid.PICC_IsNewCardPresent()) { //if there is no RFID card present this is executed and arms the alarm

If there is not a card present, what is the chance that there will not not be a card present? The second if is pointless. If the first is false, the second one MUST be true, If the first if true, the second won't even be evaluated.

Your serial output seems to be missing.