Simple question regarding 1-wire temp sensor DS18B20 and SIM900 GPRS Shield

Hi experts,

I’ve done quite a bit of reading and googling, but need some support to figure out how to get my DS18B20 temp sensor reading to send via SMS on the GPRS Shield. I have already ran successful sketches to view the temperature readings through the Serial monitor, send an SMS with random text, and send an SMS with just the temperature reading. Now I am looking to combine them into one sketch.

This is my configuration:

  • Arduino Leonardo (currently using an UNO, but want to use the Leonardo I have here
  • Seeed Studio GPRS Shield V2.0, with unlocked SIM
  • Dallas/Maxim DS18B20
  • Connected via breadboard


The GPRS Shield is not pictured here, but it is on top of the Arduino, and the jumpers going into the headers on that.

What I would like the code to do is the following:

  • Send an SMS that says "The temp is currently " and insert the reading after that (see textForSMS below)
  • Perform this at an interval of every 5 minutes

I know that the string stuff associated with this line “textForSMS = textForSMS + (currentTemp)” in the code below is incorrect, but I can not quite figure out how to fix it.

 /*Demo for GPRS Shield to send SMS message
The microcontrollers Digital Pin 7 and hence allow unhindered communication with GPRS Shield using SoftSerial Library. 
IDE: Arduino 1.0 or later */

#include <SoftwareSerial.h>
#include <String.h>
#include <OneWire.h>
#include <DallasTemperature.h>
String textForSMS;
float currentTemp;
 
SoftwareSerial cellSerial(7,8);

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
 
void setup()
{
  cellSerial.begin(19200);               // the GPRS baud rate   
  Serial.begin(9600);    // temp sensor rate 
  delay(500);
  sensors.begin();
}

///SendTextMessage()
///this function is to send a sms message
void SendTextMessage(String message)
{
  cellSerial.print("AT+CMGF=1\r");    //Because we want to send the SMS in text mode
  delay(100);
  cellSerial.println("AT + CMGS = \"+1XXXXXX4470\"");//send sms message, be careful need to add a country code before the cellphone number
  delay(100);
  cellSerial.println(message);//the content of the message
  delay(100);
  cellSerial.println((char)26);//the ASCII code of the ctrl+z is 26
  delay(100);
  cellSerial.println();
}

void loop()
{
  currentTemp = sensors.getTempCByIndex(0);
  textForSMS = "Temp is ";
  Serial.println(currentTemp);
  textForSMS = textForSMS + (currentTemp)
  SendTextMessage(textForSMS);
  do {} while (1);
}
 
void ShowSerialData()
{
  while(cellSerial.available()!=0)
    Serial.write(cellSerial.read());
}

I am buried in tabs in Firefox of projects that are slightly different and don’t quite answer the questions above through digging through the code. Any ideas here? I want to be able to build this with some students in a STEM workshop next week so we would really appreciate any pointers.

Thanks!

  do {} while (1);

Huh? The purpose of this is?

Anyway, what is happening, compared to what you expect to happen?

currentTemp is a float, so I'm not certain

  textForSMS = textForSMS + (currentTemp)

will work?

Have a look at the function dtostrf().

I couldnt get it to compile. I basically wanted it to append the string with the text plus the sensor reading, then send it over SMS.

I actually got it to work this morning using this:

#include <SoftwareSerial.h>
#include <String.h>
#include <OneWire.h>
#include <DallasTemperature.h>
String textForSMS;
float currentTemp;
 
SoftwareSerial mySerial(7,8);

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
 
void setup()
{
  mySerial.begin(19200);               // the GPRS baud rate   
  Serial.begin(19200);    // the GPRS baud rate 
  Serial.println("test");
  delay(500);
  sensors.begin();
}

///SendTextMessage()
///this function is to send a sms message
void SendTextMessage(String message)
{
  mySerial.print("AT+CMGF=1\r");    //Because we want to send the SMS in text mode
  delay(100);
  mySerial.println("AT + CMGS = \"+1XXX5559970\"");//send sms message, be careful need to add a country code before the cellphone number
  delay(100);
  mySerial.println(message);//the content of the message
  delay(100);
  mySerial.println((char)26);//the ASCII code of the ctrl+z is 26
  delay(100);
  mySerial.println();
}

String AppendFloat(String oldString, float num)
{
	char buf[20];
	char* string = dtostrf(num, 4, 2, buf);
	oldString.concat(string);
        return oldString;
}


void GetTemp(float reading)
{
  sensors.requestTemperatures();
  currentTemp = sensors.getTempCByIndex(0);
  
}

void loop()
{
  Serial.println("Trying..."); 
  GetTemp(currentTemp);
  currentTemp = sensors.getTempCByIndex(0);
  textForSMS = "Temp Reading is";
  textForSMS = AppendFloat(textForSMS, currentTemp);
  Serial.println("Message:");
  Serial.println(textForSMS);
  SendTextMessage(textForSMS);
  delay(30000);
  Serial.println("Done waiting"); 
}
 
void ShowSerialData()
{
  while(mySerial.available()!=0)
    Serial.write(mySerial.read());
}