My Cart

How to Send a Temperature Emergency SMS Using Tracking Kit (GPRS + GPS)

Difficulty Level: Intermediate -

1. Introduction

This tutorial allows the user to measure the temperature of an object. When the temperature is 0ÂşC or less, a text message is sent to a mobile phone in which the temperature and the position (laltitude and longitude) of the module is indicated.

Ingredients:

    - 1 x Tracking Kit (GPRS + GPS):
    • 1x Platform
    • 1x GPRS+GPS Quadband Module (SIM908)
    • 1x GPRS antenna
    • 1x GPS antenna
    • 1x Temperature sensor (MCP9700A)
    • 1x Breadboard
    • 1x External power supply
    • 1x Programming cable
    • Jumper Wires

Preparation Time: 30 minutes

Buy now
NOTE: Depending on the platform chosen to develop the project, the ingredients and the schematics shown in this tutorial can vary.

This project can be developed with Arduino or Intel Galileo. It is also compatible with Raspberry Pi using the Raspberry Pi to Arduino shields connection bridge.

For further information about the GPRS+GPS (SIM908) Shield, consult the main tutorial.

Step 1: Connection

Connect the GPRS and GPS antenna to the shield and then, connect the shield to the Arduino or to Raspberry Pi connection bridge. Connect temperature sensor in the breadboard as you can see in the next diagram.

Connect two wires, red and black, to the two long rows on the side of the breadboard to provide access to the VCC supply (5V in Arduino and 3.3V in Raspberry Pi) and ground. Place the sensor with the flat part looking up like the diagram. Connect the left leg of the sensor to GND and the right to VCC. Connect the central leg to the first pin at bottom left of the shield (Analog 0).

Step 2: The Code

Arduino:

Code:
/*  
 *  GPRS+GPS Quadband Module (SIM908)
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  a
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Victor Boria
 */

//Enter here you data
const char pin_number[] = "****";        // Write the pin number of the SIM card
const char phone_number[] = "*********"; // Write the number to send the SMS

int onModulePin = 2;
char aux_string[30];
int8_t answer;
int flag = 0;

float temperature = 0.0;   // Stores the calculated temperature
int sample;                // Counts through ADC samples
float ten_samples = 0.0;   // Stores sum of 10 samples

char frame[200];
char latitude[15];
char longitude[15];
char altitude[6];
char date[16];
char time[7];
char satellites[3];
char speedOTG[10];
char course[10];

void setup() {
  pinMode(onModulePin, OUTPUT);
  Serial.begin(115200);

  Serial.println("Starting...");
  power_on();    // Powering the module

  delay(5000);
  
  //sets the PIN code
  sprintf(aux_string, "AT+CPIN=%s", pin_number);
  sendATcommand(aux_string, "OK", 2000);

  delay(3000);
  
  Serial.println("Connecting to the network...");
  
  //Check network registration
  while ( (sendATcommand("AT+CREG?", "+CREG: 0,1", 1000) ||
           sendATcommand("AT+CREG?", "+CREG: 0,5", 1000)) == 0 );

  sendATcommand("AT+CLIP=1", "OK", 1000);

  while ( start_GPS() == 0);
  
  Serial.println("Connected to the network!!");
  delay(1000);

  while (Serial.available() != 0)
  {
    Serial.read();
  }
}

void loop() {

  measure_temperature();

  //Send SMS if temperature is 0 or below 0
  //Only send one SMS because of the flag, to reset the flag, reset Arduino
  if (temperature <= 0 && flag == 0) {

    get_GPS();
    delay(500);
    sendSMS();
    delay(100);
    flag++; 
  }
}

/************************************************************************
 ****               Definition of functions                          ****
 ************************************************************************/
 
void measure_temperature()
{
  // take 10 samples from the MCP9700
  for (sample = 0; sample < 10; sample++) {
    // convert A0 value to temperature
    temperature = ((float)analogRead(A0) * 5.0 / 1024.0) - 0.5;
    temperature = temperature / 0.01;
    temperature = temperature - 5.3;
    // sample every 0.1 seconds
    delay(100);
    // sum of all samples
    ten_samples = ten_samples + temperature;
  }
  // get the average value of 10 temperatures
  temperature = ten_samples / 10.0;
  // send temperature out of serial port
  Serial.print(temperature);
  Serial.println(" deg. C");
  ten_samples = 0.0;
}

void power_on() {

  uint8_t answer = 0;

  // checks if the module is started
  answer = sendATcommand("AT", "OK", 2000);
  if (answer == 0)
  {
    // power on pulse
    digitalWrite(onModulePin, HIGH);
    delay(3000);
    digitalWrite(onModulePin, LOW);

    // waits for an answer from the module
    while (answer == 0) {   // Send AT every two seconds and wait for the answer
      answer = sendATcommand("AT", "OK", 2000);
    }
  }

}


int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout) {

  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;

  memset(response, '\0', 100);    // Initialice the string

  delay(100);

  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer

  if (ATcommand[0] != '\0')
  {
    Serial.println(ATcommand);    // Send the AT command
  }


  x = 0;
  previous = millis();

  // this loop waits for the answer
  do {
    if (Serial.available() != 0) {  // if there are data in the UART input buffer, reads it and checks for the asnwer
      response[x] = Serial.read();
      //Serial.print(response[x]);
      x++;
      if (strstr(response, expected_answer) != NULL)    // check if the desired answer (OK) is in the response of the module
      {
        answer = 1;
      }
    }
  } while ((answer == 0) && ((millis() - previous) < timeout));   // Waits for the asnwer with time out

  return answer;
}



int8_t start_GPS() {

  unsigned long previous;

  previous = millis();
  // starts the GPS
  sendATcommand("AT+CGPSPWR=1", "OK", 2000);
  sendATcommand("AT+CGPSRST=0", "OK", 2000);

  // waits for fix GPS
  while (( (sendATcommand("AT+CGPSSTATUS?", "2D Fix", 5000) ||
            sendATcommand("AT+CGPSSTATUS?", "3D Fix", 5000)) == 0 ) &&
         ((millis() - previous) < 90000));

  if ((millis() - previous) < 90000)
  {
    return 1;
  }
  else
  {
    return 0;
  }
}

int8_t get_GPS() {

  int8_t counter, answer;
  long previous;

  // First get the NMEA string
  // Clean the input buffer
  while ( Serial.available() > 0) Serial.read();
  // request Basic string
  sendATcommand("AT+CGPSINF=0", "AT+CGPSINF=0\r\n\r\n", 2000);

  counter = 0;
  answer = 0;
  memset(frame, '\0', 100);    // Initialize the string
  previous = millis();
  // this loop waits for the NMEA string
  do {

    if (Serial.available() != 0) {
      frame[counter] = Serial.read();
      counter++;
      // check if the desired answer is in the response of the module
      if (strstr(frame, "OK") != NULL)
      {
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < 2000));

  frame[counter - 3] = '\0';

  // Parses the string
  strtok(frame, ",");
  strcpy(longitude, strtok(NULL, ",")); // Gets longitude
  strcpy(latitude, strtok(NULL, ",")); // Gets latitude
  strcpy(altitude, strtok(NULL, ".")); // Gets altitude
  strtok(NULL, ",");
  strcpy(date, strtok(NULL, ".")); // Gets date
  strtok(NULL, ",");
  strtok(NULL, ",");
  strcpy(satellites, strtok(NULL, ",")); // Gets satellites
  strcpy(speedOTG, strtok(NULL, ",")); // Gets speed over ground. Unit is knots.
  strcpy(course, strtok(NULL, "\r")); // Gets course

  convert2Degrees(latitude);
  convert2Degrees(longitude);

  return answer;
}



void sendSMS() {
 
  Serial.print("Setting SMS mode...");
  sendATcommand("AT+CMGF=1", "OK", 1000);    // sets the SMS mode to text
  Serial.println("Sending SMS");

  sprintf(aux_string, "AT+CMGS=\"%s\"", phone_number);
  answer = sendATcommand(aux_string, ">", 2000);    // send the SMS number
  if (answer == 1)
  {
    Serial.print("Help me! My temperature is critical. Find me in:");
    Serial.print("Latitude: ");
    int i = 0;
    while (latitude[i] != 0) {
      Serial.print(latitude[i]);
      i++;
    }
    Serial.print(" / Longitude: ");
    i = 0;
    while (longitude[i] != 0) {
      Serial.print(longitude[i]);
      i++;
    }
    Serial.write(0x1A);
    answer = sendATcommand("", "OK", 20000);
    if (answer == 1)
    {
      Serial.print("Sent ");
    }
    else
    {
      Serial.print("error ");
    }
  }
  else
  {
    Serial.print("error ");
    Serial.println(answer, DEC);
  }

}


/* convert2Degrees ( input ) - performs the conversion from input
 * parameters in  DD°MM.mmm’ notation to DD.dddddd° notation.
 *
 * Sign '+' is set for positive latitudes/longitudes (North, East)
 * Sign '-' is set for negative latitudes/longitudes (South, West)
 *
 */
int8_t convert2Degrees(char* input) {

  float deg;
  float minutes;
  boolean neg = false;

  //auxiliar variable
  char aux[10];

  if (input[0] == '-')
  {
    neg = true;
    strcpy(aux, strtok(input + 1, "."));

  }
  else
  {
    strcpy(aux, strtok(input, "."));
  }

  // convert string to integer and add it to final float variable
  deg = atof(aux);

  strcpy(aux, strtok(NULL, '\0'));
  minutes = atof(aux);
  minutes /= 1000000;
  if (deg < 100)
  {
    minutes += deg;
    deg = 0;
  }
  else
  {
    minutes += int(deg) % 100;
    deg = int(deg) / 100;
  }

  // add minutes to degrees
  deg = deg + minutes / 60;


  if (neg == true)
  {
    deg *= -1.0;
  }

  neg = false;

  if ( deg < 0 ) {
    neg = true;
    deg *= -1;
  }

  float numeroFloat = deg;
  int parteEntera[10];
  int cifra;
  long numero = (long)numeroFloat;
  int size = 0;

  while (1) {
    size = size + 1;
    cifra = numero % 10;
    numero = numero / 10;
    parteEntera[size - 1] = cifra;
    if (numero == 0) {
      break;
    }
  }

  int indice = 0;
  if ( neg ) {
    indice++;
    input[0] = '-';
  }
  for (int i = size - 1; i >= 0; i--)
  {
    input[indice] = parteEntera[i] + '0';
    indice++;
  }

  input[indice] = '.';
  indice++;

  numeroFloat = (numeroFloat - (int)numeroFloat);
  for (int i = 1; i <= 6 ; i++)
  {
    numeroFloat = numeroFloat * 10;
    cifra = (long)numeroFloat;
    numeroFloat = numeroFloat - cifra;
    input[indice] = char(cifra) + 48;
    indice++;
  }
  input[indice] = '\0';
}
        

Raspberry Pi:

Code:
/*  
 *  GPRS+GPS Quadband Module (SIM908)
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  a
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Marcos MartĂ­nez
 */
 
//Include arduPi library
#include "arduPi.h"

int8_t sendATcommand(const char* ATcommand, const char* expected_answer, unsigned int timeout);
void power_on();
void sendSMS();
int8_t start_GPS();
void measure_temperature();
int8_t get_GPS();
int8_t convert2Degrees(char* input);

//Enter here you data
const char pin_number[] = "****";        // Write the pin number of the SIM card
const char phone_number[] = "*********"; // Write the number to send the SMS

int onModulePin = 2;
char aux_string[30];
int8_t answer;
int flag = 0;

float temperature = 0.0;   // Stores the calculated temperature
int sample;                // Counts through ADC samples
float ten_samples = 0.0;   // Stores sum of 10 samples

char frame[200];
char latitude[15];
char longitude[15];
char altitude[6];
char date[16];
char satellites[3];
char speedOTG[10];
char course[10];

void setup() {
	
  pinMode(onModulePin, OUTPUT);
  Serial.begin(115200);

  printf("Starting...\n");
  power_on();    // Powering the module
  
  delay(5000);
  
  //sets the PIN code
  sprintf(aux_string, "AT+CPIN=%s", pin_number);
  sendATcommand(aux_string, "OK", 2000);

  delay(3000);
  
  printf("Connecting to the network...\n");
  
  //Check network registration
  while ( (sendATcommand("AT+CREG?", "+CREG: 0,1", 1000) ||
           sendATcommand("AT+CREG?", "+CREG: 0,5", 1000)) == 0 );

  sendATcommand("AT+CLIP=1", "OK", 1000);

  while ( start_GPS() == 0);
  
  printf("Connected to the network!!\n");
  delay(1000);

  while (Serial.available() != 0)
  {
    Serial.read();
  }
}

void loop() {

  measure_temperature();

  //Send SMS if temperature is 0 or below 0
  //Only send one SMS because of the flag, to reset the flag, reset Arduino
  if (temperature <= 0.0 && flag == 0) {

    get_GPS();
    delay(500);
    sendSMS();
    delay(100);
    flag++; 
  }
}

/************************************************************************
 ****               Definition of functions                          ****
 ************************************************************************/
 
void measure_temperature()
{
  // take 10 samples from the MCP9700
  for (sample = 0; sample < 10; sample++) {
    // convert A0 value to temperature
    temperature = ((float)analogRead(0) * 5.0 / 1024.0) - 0.5;
    temperature = temperature / 0.01;
    // sample every 0.1 seconds
    delay(100);
    // sum of all samples
    ten_samples = ten_samples + temperature;
  }
  // get the average value of 10 temperatures
  temperature = ten_samples / 10.0;
  // send temperature out of serial port
  printf("temperature: %fÂşC\n", temperature);
  ten_samples = 0.0;
}

void power_on() {
  uint8_t answer = 0;

  // checks if the module is started
  answer = sendATcommand("AT", "OK", 2000);
  if (answer == 0)
  {
    // power on pulse
    digitalWrite(onModulePin, HIGH);
    delay(3000);
    digitalWrite(onModulePin, LOW);

    // waits for an answer from the module
    while (answer == 0) {   // Send AT every two seconds and wait for the answer
      answer = sendATcommand("AT", "OK", 2000);
    }
  }

}


int8_t sendATcommand(const char* ATcommand, const char* expected_answer, unsigned int timeout){

    uint8_t x=0,  answer=0;
    char response[100];
    unsigned long previous;

    memset(response, '\0', 100);    // Initialize the string

    delay(100);

    while( Serial.available() > 0) Serial.read();    // Clean the input buffer

    Serial.println(ATcommand);    // Send the AT command 


        x = 0;
    previous = millis();

    // this loop waits for the answer
    do{
        if(Serial.available() != 0){    
            // if there are data in the UART input buffer, reads it and checks for the asnwer
            response[x] = Serial.read();
            printf("%c",response[x]);
            x++;
            // check if the desired answer  is in the response of the module
            if (strstr(response, expected_answer) != NULL)    
            {
				printf("\n");
                answer = 1;
            }
        }
    }
    // Waits for the asnwer with time out
    while((answer == 0) && ((millis() - previous) < timeout));    

        return answer;
}


int8_t start_GPS() {

  unsigned long previous;

  previous = millis();
  // starts the GPS
  sendATcommand("AT+CGPSPWR=1", "OK", 2000);
  sendATcommand("AT+CGPSRST=0", "OK", 2000);

  // waits for fix GPS
  while (( (sendATcommand("AT+CGPSSTATUS?", "2D Fix", 5000) ||
            sendATcommand("AT+CGPSSTATUS?", "3D Fix", 5000)) == 0 ) &&
         ((millis() - previous) < 90000));

  if ((millis() - previous) < 90000)
  {
    return 1;
  }
  else
  {
    return 0;
  }
}

int8_t get_GPS() {

  int8_t answer,counter;
  long previous;

  // First get the NMEA string
  // Clean the input buffer
  while ( Serial.available() > 0) Serial.read(); 
  delay(100);
  // request Basic string
  sendATcommand("AT+CGPSINF=0", "AT+CGPSINF=0\r\n\r\n", 2000);

  counter = 0;
  answer = 0;
  memset(frame, '\0', 200);    // Initialize the string
  previous = millis();
  // this loop waits for the NMEA string
  do {

    if (Serial.available() != 0) {
      frame[counter] = Serial.read();
      counter++;
      // check if the desired answer is in the response of the module
      if (strstr(frame, "OK") != NULL)
      {
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < 2000));

  frame[counter - 3] = '\0';
  
  // Parses the string
  strtok(frame, ",");
  strcpy(longitude, strtok(NULL, ",")); // Gets longitude
  strcpy(latitude, strtok(NULL, ",")); // Gets latitude
  strcpy(altitude, strtok(NULL, ".")); // Gets altitude
  strtok(NULL, ",");
  strcpy(date, strtok(NULL, ".")); // Gets date
  strtok(NULL, ",");
  strtok(NULL, ",");
  strcpy(satellites, strtok(NULL, ",")); // Gets satellites
  strcpy(speedOTG, strtok(NULL, ",")); // Gets speed over ground. Unit is knots.
  strcpy(course, strtok(NULL, "\r")); // Gets course
 
  convert2Degrees(latitude);
  convert2Degrees(longitude);

  return answer;
}



void sendSMS() {
 
  printf("Setting SMS mode...\n");
  sendATcommand("AT+CMGF=1", "OK", 1000);    // sets the SMS mode to text
  printf("Sending SMS\n");

  sprintf(aux_string, "AT+CMGS=\"%s\"", phone_number);
  answer = sendATcommand(aux_string, ">", 2000);    // send the SMS number
  if (answer == 1)
  {
    Serial.print("Help me! My temperature is critical. Find me in ");
    Serial.print("Latitude: ");
    int i = 0;
    while (latitude[i] != 0) {
      Serial.print(latitude[i]);
      i++;
    }
    Serial.print(" / Longitude: ");
    i = 0;
    while (longitude[i] != 0) {
      Serial.print(longitude[i]);
      i++;
    }
    Serial.write(0x1A);
    answer = sendATcommand("", "OK", 20000);
    if (answer == 1)
    {
      printf("Sent \n");
    }
    else
    {
      printf("error \n");
    }
  }
  else
  {
    printf("error %i\n",answer);

}
}


/* convert2Degrees ( input ) - performs the conversion from input
 * parameters in  DD°MM.mmm’ notation to DD.dddddd° notation.
 *
 * Sign '+' is set for positive latitudes/longitudes (North, East)
 * Sign '-' is set for negative latitudes/longitudes (South, West)
 *
 */
int8_t convert2Degrees(char* input) {

  float deg;
  float minutes;
  boolean neg = false;

  //auxiliar variable
  char aux[10];
  
  memset(aux, '\0', sizeof(aux));

  if (input[0] == '-')
  {
    neg = true;
	strncpy(aux, input+1, strchr(input, '.') - input - 1);
  }
  else
  {
  strncpy(aux, input, strchr(input, '.') - input );
  }

  // convert string to integer and add it to final float variable
  deg = atof(aux);

  memset(aux, '\0', sizeof(aux));
  strncpy(aux, strchr(input, '.')+1, strlen(input) - (strchr(input, '.') - input) + 1);
  minutes = atof(aux);
  minutes /= 1000000;
  if (deg < 100)
  {
    minutes += deg;
    deg = 0;
  }
  
  else
  {
    minutes += int(deg) % 100;
    deg = int(deg) / 100;
  }

  // add minutes to degrees
  deg = deg + minutes / 60;

    

  if (neg == true)
  {
    deg *= -1.0;
  }

  neg = false;

  if ( deg < 0 ) {
    neg = true;
    deg *= -1;
  }

  float numeroFloat = deg;
  int parteEntera[10];
  int cifra;
  long numero = (long)numeroFloat;
  int size = 0;

  while (1) {
    size = size + 1;
    cifra = numero % 10;
    numero = numero / 10;
    parteEntera[size - 1] = cifra;
    if (numero == 0) {
      break;
    }
  }

  int indice = 0;
  if ( neg ) {
    indice++;
    input[0] = '-';
  }
  for (int i = size - 1; i >= 0; i--)
  {
    input[indice] = parteEntera[i] + '0';
    indice++;
  }

  input[indice] = '.';
  indice++;

  numeroFloat = (numeroFloat - (int)numeroFloat);
  for (int i = 1; i <= 6 ; i++)
  {
    numeroFloat = numeroFloat * 10;
    cifra = (long)numeroFloat;
    numeroFloat = numeroFloat - cifra;
    input[indice] = char(cifra) + 48;
    indice++;
  }
  input[indice] = '\0';
}


int main (){
    setup();
    while(1){
        loop();
    }
    return (0);
}

        

Intel Galileo:

Code:
/*  
 *  GPRS+GPS Quadband Module (SIM908)
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  a
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Jorge Casanova, Luis MartĂ­n
 */

//Enter here you data
const char pin_number[] = "****";        // Write the pin number of the SIM card
const char phone_number[] = "*********"; // Write the number to send the SMS

int onModulePin = 2;
char aux_string[30];
int8_t answer;
int flag = 0;

float temperature = 0.0;   // Stores the calculated temperature
int sample;                // Counts through ADC samples
float ten_samples = 0.0;   // Stores sum of 10 samples

char frame[200];
char latitude[15];
char longitude[15];
char altitude[6];
char date[16];

char satellites[3];
char speedOTG[10];
char course[10];

void setup() {
  pinMode(onModulePin, OUTPUT);
  Serial.begin(115200);
  Serial1.begin(115200);

  Serial.println("Starting...");
  power_on();    // Powering the module

  delay(5000);
  
  //sets the PIN code
  sprintf(aux_string, "AT+CPIN=%s", pin_number);
  sendATcommand(aux_string, "OK", 2000);

  delay(3000);
  
  Serial.println("Connecting to the network...");
  
  //Check network registration
  while ( (sendATcommand("AT+CREG?", "+CREG: 0,1", 1000) ||
           sendATcommand("AT+CREG?", "+CREG: 0,5", 1000)) == 0 );

  sendATcommand("AT+CLIP=1", "OK", 1000);

  while ( start_GPS() == 0);
  
  Serial.println("Connected to the network!!");
  delay(1000);

  while (Serial1.available() != 0)
  {
    Serial1.read();
  }
}

void loop() {

  measure_temperature();

  //Send SMS if temperature is 0 or below 0
  //Only send one SMS because of the flag, to reset the flag, reset Arduino
  if (temperature <= 0 && flag == 0) {

    get_GPS();
    delay(500);
    sendSMS();
    delay(100);
    flag++; 
  }
}

/************************************************************************
 ****               Definition of functions                          ****
 ************************************************************************/
 
void measure_temperature()
{
  // take 10 samples from the MCP9700
  for (sample = 0; sample < 10; sample++) {
    // convert A0 value to temperature
    temperature = ((float)analogRead(A0) * 5.0 / 1024.0) - 0.5;
    temperature = temperature / 0.01;
    temperature = temperature - 5.3;
    // sample every 0.1 seconds
    delay(100);
    // sum of all samples
    ten_samples = ten_samples + temperature;
  }
  // get the average value of 10 temperatures
  temperature = ten_samples / 10.0;
  // send temperature out of serial port
  Serial.print(temperature);
  Serial.println(" deg. C");
  ten_samples = 0.0;
}

void power_on() {

  uint8_t answer = 0;

  // checks if the module is started
  answer = sendATcommand("AT", "OK", 2000);
  if (answer == 0)
  {
    // power on pulse
    digitalWrite(onModulePin, HIGH);
    delay(3000);
    digitalWrite(onModulePin, LOW);

    // waits for an answer from the module
    while (answer == 0) {   // Send AT every two seconds and wait for the answer
      answer = sendATcommand("AT", "OK", 2000);
    }
  }

}


int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout) {

  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;

  memset(response, '\0', 100);    // Initialice the string

  delay(100);

  while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer

  if (ATcommand[0] != '\0')
  {
    Serial1.println(ATcommand);    // Send the AT command
  }


  x = 0;
  previous = millis();

  // this loop waits for the answer
  do {
    if (Serial1.available() != 0) {  // if there are data in the UART input buffer, reads it and checks for the asnwer
      response[x] = Serial1.read();
      Serial.print(response[x]);
      x++;
      if (strstr(response, expected_answer) != NULL)    // check if the desired answer (OK) is in the response of the module
      {
        answer = 1;
      }
    }
  } while ((answer == 0) && ((millis() - previous) < timeout));   // Waits for the asnwer with time out

  return answer;
}



int8_t start_GPS() {

  unsigned long previous;

  previous = millis();
  // starts the GPS
  sendATcommand("AT+CGPSPWR=1", "OK", 2000);
  sendATcommand("AT+CGPSRST=0", "OK", 2000);

  // waits for fix GPS
  while (( (sendATcommand("AT+CGPSSTATUS?", "2D Fix", 5000) ||
            sendATcommand("AT+CGPSSTATUS?", "3D Fix", 5000)) == 0 ) &&
         ((millis() - previous) < 90000));

  if ((millis() - previous) < 90000)
  {
    return 1;
  }
  else
  {
    return 0;
  }
}

int8_t get_GPS() {

  int8_t counter, answer;
  long previous;

  // First get the NMEA string
  // Clean the input buffer
  while ( Serial1.available() > 0) Serial1.read();
  // request Basic string
  sendATcommand("AT+CGPSINF=0", "AT+CGPSINF=0\r\n\r\n", 2000);

  counter = 0;
  answer = 0;
  memset(frame, '\0', 100);    // Initialize the string
  previous = millis();
  // this loop waits for the NMEA string
  do {

    if (Serial1.available() != 0) {
      frame[counter] = Serial1.read();
      counter++;
      // check if the desired answer is in the response of the module
      if (strstr(frame, "OK") != NULL)
      {
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < 2000));

  frame[counter - 3] = '\0';

  // Parses the string
  strtok(frame, ",");
  strcpy(longitude, strtok(NULL, ",")); // Gets longitude
  strcpy(latitude, strtok(NULL, ",")); // Gets latitude
  strcpy(altitude, strtok(NULL, ".")); // Gets altitude
  strtok(NULL, ",");
  strcpy(date, strtok(NULL, ".")); // Gets date
  strtok(NULL, ",");
  strtok(NULL, ",");
  strcpy(satellites, strtok(NULL, ",")); // Gets satellites
  strcpy(speedOTG, strtok(NULL, ",")); // Gets speed over ground. Unit is knots.
  strcpy(course, strtok(NULL, "\r")); // Gets course

  convert2Degrees(latitude);
  convert2Degrees(longitude);

  return answer;
}



void sendSMS() {
  
  Serial.print("Setting SMS mode...");
  sendATcommand("AT+CMGF=1", "OK", 1000);    // sets the SMS mode to text
  Serial.println("Sending SMS");

  sprintf(aux_string, "AT+CMGS=\"%s\"", phone_number);
  answer = sendATcommand(aux_string, ">", 2000);    // send the SMS number
  if (answer == 1)
  {
    Serial.print("Help me! My temperature is critical. Find me in:");
    Serial.print("Latitude: ");
    Serial1.print("Help me! My temperature is critical. Find me in:");
    Serial1.print("Latitude: ");
    int i = 0;
    while (latitude[i] != 0) {
      Serial.print(latitude[i]);
      Serial1.print(latitude[i]);
      i++;
    }
    Serial.print(" / Longitude: ");
    Serial1.print(" / Longitude: ");
    i = 0;
    while (longitude[i] != 0) {
      Serial.print(longitude[i]);
      Serial1.print(longitude[i]);
      i++;
    }
    Serial1.write(0x1A);
    answer = sendATcommand("", "OK", 20000);
    if (answer == 1)
    {
      Serial.print("Sent ");
    }
    else
    {
      Serial.print("error ");
    }
  }
  else
  {
    Serial.print("error ");
    Serial.println(answer, DEC);
  }

}


/* convert2Degrees ( input ) - performs the conversion from input
 * parameters in  DD°MM.mmm’ notation to DD.dddddd° notation.
 *
 * Sign '+' is set for positive latitudes/longitudes (North, East)
 * Sign '-' is set for negative latitudes/longitudes (South, West)
 *
 */
int8_t convert2Degrees(char* input) {

  float deg;
  float minutes;
  boolean neg = false;

  //auxiliar variable
  char aux[10];

  if (input[0] == '-')
  {
    neg = true;
    strcpy(aux, strtok(input + 1, "."));

  }
  else
  {
    strcpy(aux, strtok(input, "."));
  }

  // convert string to integer and add it to final float variable
  deg = atof(aux);

  strcpy(aux, strtok(NULL, "\0"));
  minutes = atof(aux);
  minutes /= 1000000;
  if (deg < 100)
  {
    minutes += deg;
    deg = 0;
  }
  else
  {
    minutes += int(deg) % 100;
    deg = int(deg) / 100;
  }

  // add minutes to degrees
  deg = deg + minutes / 60;


  if (neg == true)
  {
    deg *= -1.0;
  }

  neg = false;

  if ( deg < 0 ) {
    neg = true;
    deg *= -1;
  }

  float numeroFloat = deg;
  int parteEntera[10];
  int cifra;
  long numero = (long)numeroFloat;
  int size = 0;

  while (1) {
    size = size + 1;
    cifra = numero % 10;
    numero = numero / 10;
    parteEntera[size - 1] = cifra;
    if (numero == 0) {
      break;
    }
  }

  int indice = 0;
  if ( neg ) {
    indice++;
    input[0] = '-';
  }
  for (int i = size - 1; i >= 0; i--)
  {
    input[indice] = parteEntera[i] + '0';
    indice++;
  }

  input[indice] = '.';
  indice++;

  numeroFloat = (numeroFloat - (int)numeroFloat);
  for (int i = 1; i <= 6 ; i++)
  {
    numeroFloat = numeroFloat * 10;
    cifra = (long)numeroFloat;
    numeroFloat = numeroFloat - cifra;
    input[indice] = char(cifra) + 48;
    indice++;
  }
  input[indice] = '\0';
}