How to send SMS using GPRS from an Mduino
Sending Temperature data via SMS using GPRS module from an Mduino PLC
17 noviembre, 2021 por
How to send SMS using GPRS from an Mduino
Boot & Work Corp. S.L., Amil Shrivastava

Introduction

In this tutorial we will see how to send an SMS using an Mduino GPRS  54ARA + with the information of a Dallas temperature sensor attached to the PLC.

The principle idea is to set up the temperature sensor with the PLC, collect temperature data and once it reaches a certain threshold, an automatic SMS is sent to a designated phone number with the temperature reading and some information. 

To see how Dallas sensor is connected and controlled using and Mduino PLC, you can go to this link and check it out:

How to control Dallas Sensor with Mduino PLC

Libraries

Incase the arduino IDE does not recognize certain commands from the library, make sure you update the board libraries. You can do that by going to the board manager and updating the GPRS library.


Code

#include <DallasTemperature.h>
#include <GPRS.h>
#define TEMP_SET_POINT 25      // Celsius
#define TEMP_READ_PERIOD 1000  // ms
#define TEMP_RESOLUTION 12     // bits
#define ONEWIRE_PIN 3         // Pin for the OneWire
#define HEATER_PIN Q0_0        // Digital Output pin to ON the heater
//Not configurable constants
#define INVALID_SENSOR_INDEX 0xff
#define INVALID_TEMP 0x7fff
#define ADDRESS_LEN 8 // bytes
boolean gprsReady = false;
static const char* simPin = "";
//Initiating Libraries
OneWire oneWire(ONEWIRE_PIN);
DallasTemperature sensors(&oneWire);
uint8_t tempIndex = INVALID_SENSOR_INDEX;
double temp = INVALID_TEMP;
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(9600L);
  Serial.println("Starting Temp & SMS services");
  initTempSensor();
  initGPRS();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  updateTemp_SMS();
  updateGprsConnection();
  delay(1000);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void printAddress(const uint8_t *address) {
  for (uint8_t i = 0; i < ADDRESS_LEN; ++i) {
    if (i > 0) {
      Serial.print('-');
    }
    if (address[i] < 0x10) {
      Serial.print('0');
    }
    Serial.print(address[i], HEX);
  }
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void initTempSensor() {
  // Begin sensors library
  sensors.begin();
  // Get temperature sensor
  uint8_t address[ADDRESS_LEN];
  uint8_t numDevices = sensors.getDeviceCount();   //number of devices connected
  for (uint8_t i = 0; i < numDevices; ++i) {       //loop for check all the devices connected
    if (sensors.getAddress(address, i)) {          //verification of correct address
      if (sensors.validFamily(address)) {          //verification of valid family
        tempIndex = i;                             //tempIndex different from INVALID_SENSOR_INDEX, flag
        Serial.print("Sensor address: ");
        printAddress(address);
        Serial.print(" (");
        Serial.print(tempIndex);
        Serial.print(")");
        Serial.println();
        break;
      }
    }
  }
  // Set sensors parameters
  sensors.setResolution(TEMP_RESOLUTION);        //This temperature resolution is of 12bits
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void initGPRS() {
  if (!GPRS.begin()) {
    Serial.println("GPRS module not found");
    while (true);
  }
  int pinRequired = GPRS.isPINRequired();
  if (pinRequired == 1) {
    if (!GPRS.unlockSIM(simPin)) {
      Serial.println("Invalid PIN");
      while (true);
    }
  } else if (pinRequired != 0) {
    Serial.println("Blocked SIM");
    while (true);
  }
  Serial.println("GPRS Initiated");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void updateTemp_SMS() {
  static uint32_t lastUpdate = millis();
  static boolean smsSent = false;
  const char* buf;
  if (tempIndex == INVALID_SENSOR_INDEX) {      //If the sensor index is invalid, we initialize the temperature sensor again.
    initTempSensor();
  }
  else {                                       //If it is correct, we read the actual temperature from the sensor
    if (millis() - lastUpdate > TEMP_READ_PERIOD) {
      if (sensors.requestTemperaturesByIndex(tempIndex)) {
        temp = sensors.getTempCByIndex(tempIndex);
        Serial.println(temp);
        String string_temp =  String("Temperature Alert: ")+String(temp);
        buf = string_temp.c_str();
        if (temp > 25.0) {
          GPRS.sendSMS("+34xxxxxxxxx", buf); // Put the reciever's phone number here.
          Serial.println("Message Sent!!");
          Serial.println(buf);
          delay(10000);
        }
      }
    }
  }
}
////////////////////////////////////////////////////////////////////////////////////////////////////
boolean updateGprsConnection() {
  int networkStatus = GPRS.getNetworkStatus();
  gprsReady = (networkStatus == 1) || (networkStatus == 5);
}
///////////////////////////////////END//////////////////////////////////////////////////////////////

How to send SMS using GPRS from an Mduino
Boot & Work Corp. S.L., Amil Shrivastava
17 noviembre, 2021
Compartir
Archivar

¿Buscas tu controlador lógico programable ideal?

Echa un vistazo a esta comparativa de producto de varios controladores industriales basados en Arduino.

Comparamos entradas, salidas, comunicaciones y otras especificaciones con las de los equipos de otras marcas destacadas.


Comparación PLC industrial>>>

¿Quieres más información?

¡Rellena el formulario!

¡Cuéntame más!