Hi,
I have the following hardware; Siemens TC35 GSM, DS18B20 1-wire temperate sensor and an RGB LED hooked up to an Atmega328. This will, when code right!, allow me to interrogate the temperature of my home and allow me to text to switch my heating off/on. RGB LED implemented to test function of TC35.
As per the code below, I can send a text message from my mobile with either; Red, Green, Blue or Off and this controls the state of the RGB LED (which works ok). I can also text "Temp", as in temperature, and the program should respond by texting back the temperature (which it does), however, something in the program is making it send multiple text messages, any ideas what could be causing this? (not all of this is my own work, and mega-credit goes to those who wrote the original code)
#include <OneWire.h>
int DS18S20_Pin = 4; //DS18B20 Signal pin on digital 4
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 4
#include <SoftwareSerial.h>
SoftwareSerial gsmSerial(2,3);
//-------- TC35 GSM ---------------
int SMS_location_number;
const unsigned int MAX_INPUT = 165; // 160 characters for SMS plus a few extra
static unsigned int input_pos = 0;
const int commandDelay = 250;
int redLED = 5;
int greenLED = 6;
int blueLED = 7;
int LEDstate = 0; //0 = off, 1 = red, 2 = green, 3 = blue
float temperature = 99.99;
void setup() {
Serial.begin(9600); // set serial monitor to baud 9600
gsmSerial.begin(9600); // set comms with TC35 to baud 9600
// set RGB LED digital pins
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(8, INPUT); // turns on/resets TC35 by taking pin 8 (IGT on TC35) low for 100ms
digitalWrite(8, LOW);
pinMode(8, OUTPUT);
delay(100);
pinMode(8, INPUT);
temperature = getTemp(); // get temperature to initialise temp sensor
delay(1000);
temperature = getTemp(); // re-read temperature sensor
Serial.println(temperature);
gsmSerial.println("AT+CMGF=1"); // for txt mode
delay(commandDelay);
gsmSerial.println("AT+CNMI=2,1,0,0,1"); // message indication
delay(commandDelay);
gsmSerial.println("AT^SMGO=1"); // SMS full indication
delay(commandDelay);
gsmSerial.println("AT&W"); // store to memory
delay(commandDelay);
gsmSerial.println("AT&V"); //display current configuration
delay(commandDelay);
}//------ End setup -------
void loop() {
readTC35();
}//------ End loop --------
//---------------------------- Read TC35 ------------------------------------
// Read data from the TC35, When a linefeed is read the data is processed
void readTC35(){
static char input_line [MAX_INPUT];
//static unsigned int input_pos = 0;
if (gsmSerial.available () > 0)
{
while (gsmSerial.available () > 0) {
char inByte = gsmSerial.read ();
switch (inByte)
{
case '\n': // end of text
input_line [input_pos] = 0; // terminating null byte
// terminator reached! process input_line here ...
process_data (input_line);
// reset buffer for next time
input_pos = 0;
break;
case '\r': // discard carriage return
break;
default:
// keep adding if not full ... allow for terminating null byte
if (input_pos < (MAX_INPUT - 1))
input_line [input_pos++] = inByte;
break;
} // end of switch
} // end of while incoming data
} // end of if incoming data
} // end of readTC35
//---------------------------- process_data --------------------------------
void process_data (char * data){
// display the data
Serial.println (data);
if(strstr(data, "^SMGO: 2")){ // SIM card FULL
delete_All_SMS(); // delete all SMS
}
if(strstr(data, "+CMTI:")){ // An SMS has arrived
char* copy = data + 12; // Read from position 12 until a non ASCII number to get the SMS location
SMS_location_number = (byte) atoi(copy); // Convert the ASCII number to an int
gsmSerial.print("AT+CMGR=");
gsmSerial.println(SMS_location_number); // Print the SMS in Serial Monitor
} // this SMS data will go through this process_data function again
// any true if statements will execute
if(strstr(data, "Red")){ // If data contains Red
Serial.println("LED: Red");
LEDstate = 1;
delete_one_SMS();
}
if(strstr(data, "Green")){ // If data contains Green
Serial.println("LED: Green");
LEDstate = 2;
delete_one_SMS();
}
if(strstr(data, "Blue")){ // If data contains Blue
Serial.println("LED: Blue");
LEDstate = 3;
delete_one_SMS();
}
if(strstr(data, "Off")){ // If data contains Off
Serial.println("LED: Off");
LEDstate = 0;
delete_one_SMS();
}
if(strstr(data, "Temp")){ // If data contains Temp
float temperature = getTemp(); // read temperature sensor
gsmSerial.print("AT+CMGF=1\r");
delay(400);
gsmSerial.println("AT+CMGS=\"+447914527888\""); //AT command to send SMS
//Serial.println("AT+CMGS=\"+447914527888\""); //AT command to send SMS
delay(500); //the length of this delay is very important. 400 is too low
gsmSerial.print("Livingroom Temperature: ");
gsmSerial.print(temperature); //Print the message
gsmSerial.print("\x1A");
delay(15);
delete_one_SMS();
}
// Set RGB LED to colour
if (LEDstate == 0) {
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
digitalWrite(blueLED, LOW);
}
if (LEDstate == 1) {
digitalWrite(greenLED, LOW);
digitalWrite(blueLED, LOW);
digitalWrite(redLED, HIGH);
}
if (LEDstate == 2) {
digitalWrite(redLED, LOW);
digitalWrite(blueLED, LOW);
digitalWrite(greenLED, HIGH);
}
if (LEDstate == 3) {
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
digitalWrite(blueLED, HIGH);
}
}
//--------------------------- end of process_data ---------------------------
void delete_one_SMS(){
Serial.print("deleting SMS ");
Serial.println(SMS_location_number);
gsmSerial.print("AT+CMGD=");
gsmSerial.println(SMS_location_number);
}
void delete_All_SMS(){
for(int i = 1; i <= 20; i++) {
gsmSerial.print("AT+CMGD=");
gsmSerial.println(i);
Serial.print("deleting SMS ");
Serial.println(i);
delay(500);
}
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte dataTemp[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
dataTemp[i] = ds.read();
}
ds.reset_search();
byte MSB = dataTemp[1];
byte LSB = dataTemp[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}