Hello I am writing a code that is able to command the GSM module (SIM900A) to send an SMS once I have send the appropriate text message to it. I got this code from a YouTube tutorial I watched, I have rewritten it a bit to match the one I need in my project.
this is the code
#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 // Data wire is plugged into digital pin 2 on the Arduino
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire device
DallasTemperature sensors(&oneWire); // Pass oneWire reference to DallasTemperature library
SoftwareSerial myGSM(8, 9);
float calibration_value = 21.34 + 1.1;
int phval = 0;
unsigned long int avgval;
int buffer_arr[10], temp;
float ph_act;
#define MPLa 4 //left peristaltic motor 1st pin
#define MPLb 5 //left peristaltic motor 2nd pin
#define MPRa 6 //right peristaltic motor 1st pin
#define MPRb 7 //right peristaltic motor 2nd pin
#define MWa 3 //right water motor 1st pin
#define MWb 10 //right water motor 2nd pin
char incomingChar;
int readData;
int SMSRequest;
int SendMessageph;
//pin used, 2-temp, 8,9-gsm, a0-ph, hydropump-3, waterpump-4
void setup() {
sensors.begin();
myGSM.begin(19200);
Serial.begin(19200); // initialize serial monitor with 19200 baud
delay(100);
myGSM.println("AT+CMGF=1");
delay(100);
// Set module to send SMS data to serial out upon receipt
myGSM.print("AT+CMGF=1,1,0,0,0");
delay(100);
pinMode(MPLa, OUTPUT); // Set Motor Pins As O/P
pinMode(MPLb, OUTPUT);
pinMode(MPRa, OUTPUT);
pinMode(MPRb, OUTPUT);
pinMode(MWa, OUTPUT);
pinMode(MWb, OUTPUT);
digitalWrite(MWa, LOW);
digitalWrite(MWb, HIGH);
}
void loop() {
sensors.requestTemperatures();
float t = sensors.getTempCByIndex(0); //print the temperature in Celsius
Serial.print("Temperature: ");
Serial.print(t); //Serial.print((char)176);//shows degrees character
Serial.print("C");
delay(100);
for (int i = 0; i < 10; i++) {
buffer_arr[i] = analogRead(A0);
delay(30);
}
for (int i = 0; i < 9; i++) {
for (int j = i + 1; j < 10; j++) {
if (buffer_arr[i] > buffer_arr[j]) {
temp = buffer_arr[i];
buffer_arr[i] = buffer_arr[j];
buffer_arr[j] = temp;
}
}
}
avgval = 0;
for (int i = 2; i < 8; i++)
avgval += buffer_arr[i];
float volt = (float)avgval * 5.0 / 1024 / 6;
float ph_act = -5.70 * volt + calibration_value;
Serial.println(" ");
delay(100);
Serial.print("PH Val: ");
delay(100);
Serial.print(ph_act);
delay(100);
if (t > 30) {
{
float t = sensors.getTempCByIndex(0);
myGSM.println("AT+CMGF=1"); //AT command that sets the gsm module to text mode
delay(100);
myGSM.println("AT+CMGS=\"09193280950\"\r");
delay(100);
myGSM.print("TEMPERATURE ALERT:"); //sms you want to send
delay(100);
myGSM.print(t); //sms you want to send
delay(100);
//myGSM.print((char)176); //sms you want to send
//delay(100);
myGSM.print("C"); //sms you want to send
delay(100);
myGSM.println((char)26); //ascii code of ctrl z
delay(100);
}
delay(4000);
}
if (ph_act < 8.5) {
myGSM.println("AT+CMGF=1"); //AT command that sets the gsm module to text mode
delay(100);
myGSM.println("AT+CMGS=\"09193280950\"\r");
delay(100);
myGSM.println("PH LEVEL ALERT:"); //sms you want to send
delay(100);
myGSM.println(ph_act); //sms you want to send
delay(100);
myGSM.println((char)26); //ascii code of ctrl z
delay(100);
}
else if (ph_act > 20) {
digitalWrite(MWa, LOW);
digitalWrite(MWb, LOW);
delay(4000);
{
myGSM.println("AT+CMGF=1"); //AT command that sets the gsm module to text mode
delay(100);
myGSM.println("AT+CMGS=\"09193280950\"\r");
delay(100);
myGSM.println("Water Tank: Empty"); //sms you want to send
delay(100);
myGSM.println((char)26); //ascii code of ctrl z
delay(100);
}
delay(4000);
}
if (SMSRequest ()) {
if (readData ()) {
delay(10);
{
delay(10);
myGSM.println("AT+CMGF=1");
delay(100);
myGSM.println("AT+CMGS=\"09193280950\"\r");
delay(100);
// REPLACE WITH YOUR OWN SMS MESSAGE CONTENT
String dataMessage = ("Temperature: " + String(t) + "*C " + " pH: " + String(ph_act) + "level");
// Uncomment to change message with farenheit temperature
// String dataMessage = ("Temperature: " + String(f) + "*F " + " Humidity: " + String(h) + "%");
// Send the SMS text message
myGSM.println(dataMessage);
delay(100);
// End AT command with a ^Z, ASCII code 26
myGSM.println((char)26);
delay(100);
myGSM.println();
// Give module time to send SMS
delay(5000);
}
delay(10);
}
void readData() {
//Read temperature
float t = sensors.getTempCByIndex(0);
//Read pH
float ph_act = -5.70 * volt + calibration_value
// Check if any reads failed and exit early (to try again).
if (isnan(t) || isnan(ph_act)) {
Serial.println("Failed to read!");
return false;
}
Serial.print("pH Level: ");
Serial.print(ph_act);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
return true;
}
void SMSRequest() {
if (myGSM.available() > 0) {
incomingChar = myGSM.read();
if (incomingChar == 'S') {
delay(10);
Serial.print(incomingChar);
incomingChar = myGSM.read();
if (incomingChar == 'T') {
delay(10);
Serial.print(incomingChar);
incomingChar = myGSM.read();
if (incomingChar == 'A') {
delay(10);
Serial.print(incomingChar);
incomingChar = SIM900.read();
if (incomingChar == 'T') {
delay(10);
Serial.print(incomingChar);
incomingChar = SIM900.read();
if (incomingChar == 'E') {
delay(10);
Serial.print(incomingChar);
Serial.print("...Request Received \n");
return true;
}
}
}
}
}
}
return false;
}
}
}
I have four error message I keep getting, I am unsure how to solve them.
This is the error message
C:\Users\JIZELL AIRA\Downloads\sketch_oct07c\sketch_oct07c.ino: In function 'void loop()':
C:\Users\JIZELL AIRA\Downloads\sketch_oct07c\sketch_oct07c.ino:128:19: error: 'SMSRequest' cannot be used as a function
if (SMSRequest ()) {
^
C:\Users\JIZELL AIRA\Downloads\sketch_oct07c\sketch_oct07c.ino:129:19: error: 'readData' cannot be used as a function
if (readData ()) {
^
C:\Users\JIZELL AIRA\Downloads\sketch_oct07c\sketch_oct07c.ino:154:21: error: a function-definition is not allowed here before '{' token
void readData() {
^
C:\Users\JIZELL AIRA\Downloads\sketch_oct07c\sketch_oct07c.ino:176:23: error: a function-definition is not allowed here before '{' token
void SMSRequest() {
^
exit status 1
Compilation error: 'SMSRequest' cannot be used as a function
I'm a newbie and this is for a school project. I hope someone can help me! Any tips to become better would also be appreciated. Thank you in advance!
SMSRequest is declared as an int and a function. Change the name of one of them
To add to the problems the closing brace for the loop() function is missing or in the wrong place, as is a second one Presumably it should end before the definition of the readData() function
If you Auto format the code in the IDE the problem with the loop() function shows up immediately because the definition of readData() does not start on the left margin of the editor and the closing brace of the loop() function shows its position at the end of the code with 2 closing braces on the same line
I have fixed the issue about the missing closing brace for the loop(), thanks! But, I still keep getting issues about "a function-definition is not allowed here before '{' token void SMSRequest() {
^ " same with the void readData(), what does that mean? Thanks!
I changed the "if (SMSrequest())" to "if (SMSrequestStatus())" same with readData, I changed the name in the void too, now it says "SMSrequest Status was not declared in this scope" same with readData, and I have yet to figure out how to solve the "a function-definition is not allowed here before'{' token "
here is the revised code
#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 // Data wire is plugged into digital pin 2 on the Arduino
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire device
DallasTemperature sensors(&oneWire); // Pass oneWire reference to DallasTemperature library
SoftwareSerial myGSM(8, 9);
float calibration_value = 21.34 + 1.1;
int phval = 0;
unsigned long int avgval;
int buffer_arr[10], temp;
float ph_act;
#define MPLa 4 //left peristaltic motor 1st pin
#define MPLb 5 //left peristaltic motor 2nd pin
#define MPRa 6 //right peristaltic motor 1st pin
#define MPRb 7 //right peristaltic motor 2nd pin
#define MWa 3 //right water motor 1st pin
#define MWb 10 //right water motor 2nd pin
char incomingChar;
int readData;
int SMSRequest;
int SendMessageph;
//pin used, 2-temp, 8,9-gsm, a0-ph, hydropump-3, waterpump-4
void setup() {
sensors.begin();
myGSM.begin(19200);
Serial.begin(19200); // initialize serial monitor with 19200 baud
delay(100);
myGSM.println("AT+CMGF=1");
delay(100);
// Set module to send SMS data to serial out upon receipt
myGSM.print("AT+CMGF=1,1,0,0,0");
delay(100);
pinMode(MPLa, OUTPUT); // Set Motor Pins As O/P
pinMode(MPLb, OUTPUT);
pinMode(MPRa, OUTPUT);
pinMode(MPRb, OUTPUT);
pinMode(MWa, OUTPUT);
pinMode(MWb, OUTPUT);
digitalWrite(MWa, LOW);
digitalWrite(MWb, HIGH);
}
void loop() {
sensors.requestTemperatures();
float t = sensors.getTempCByIndex(0); //print the temperature in Celsius
Serial.print("Temperature: ");
Serial.print(t); //Serial.print((char)176);//shows degrees character
Serial.print("C");
delay(100);
for (int i = 0; i < 10; i++) {
buffer_arr[i] = analogRead(A0);
delay(30);
}
for (int i = 0; i < 9; i++) {
for (int j = i + 1; j < 10; j++) {
if (buffer_arr[i] > buffer_arr[j]) {
temp = buffer_arr[i];
buffer_arr[i] = buffer_arr[j];
buffer_arr[j] = temp;
}
}
}
avgval = 0;
for (int i = 2; i < 8; i++)
avgval += buffer_arr[i];
float volt = (float)avgval * 5.0 / 1024 / 6;
float ph_act = -5.70 * volt + calibration_value;
Serial.println(" ");
delay(100);
Serial.print("PH Val: ");
delay(100);
Serial.print(ph_act);
delay(100);
if (t > 30) {
{
float t = sensors.getTempCByIndex(0);
myGSM.println("AT+CMGF=1"); //AT command that sets the gsm module to text mode
delay(100);
myGSM.println("AT+CMGS=\"09193280950\"\r");
delay(100);
myGSM.print("TEMPERATURE ALERT:"); //sms you want to send
delay(100);
myGSM.print(t); //sms you want to send
delay(100);
//myGSM.print((char)176); //sms you want to send
//delay(100);
myGSM.print("C"); //sms you want to send
delay(100);
myGSM.println((char)26); //ascii code of ctrl z
delay(100);
}
delay(4000);
}
if (ph_act < 8.5) {
myGSM.println("AT+CMGF=1"); //AT command that sets the gsm module to text mode
delay(100);
myGSM.println("AT+CMGS=\"09193280950\"\r");
delay(100);
myGSM.println("PH LEVEL ALERT:"); //sms you want to send
delay(100);
myGSM.println(ph_act); //sms you want to send
delay(100);
myGSM.println((char)26); //ascii code of ctrl z
delay(100);
}
else if (ph_act > 20) {
digitalWrite(MWa, LOW);
digitalWrite(MWb, LOW);
delay(4000);
{
myGSM.println("AT+CMGF=1"); //AT command that sets the gsm module to text mode
delay(100);
myGSM.println("AT+CMGS=\"09193280950\"\r");
delay(100);
myGSM.println("Water Tank: Empty"); //sms you want to send
delay(100);
myGSM.println((char)26); //ascii code of ctrl z
delay(100);
}
delay(4000);
}
if (SMSRequestStatus()) {
if (readDataStatus()) {
delay(10);
{
delay(10);
myGSM.println("AT+CMGF=1");
delay(100);
myGSM.println("AT+CMGS=\"09193280950\"\r");
delay(100);
// REPLACE WITH YOUR OWN SMS MESSAGE CONTENT
String dataMessage = ("Temperature: " + String(t) + "*C " + " pH: " + String(ph_act) + "level");
// Uncomment to change message with farenheit temperature
// String dataMessage = ("Temperature: " + String(f) + "*F " + " Humidity: " + String(h) + "%");
// Send the SMS text message
myGSM.println(dataMessage);
delay(100);
// End AT command with a ^Z, ASCII code 26
myGSM.println((char)26);
delay(100);
myGSM.println();
// Give module time to send SMS
delay(5000);
}
delay(10);
}
}
void readDataStatus() {
//Read temperature
float t = sensors.getTempCByIndex(0);
//Read pH
float ph_act = -5.70 * volt + calibration_value;
// Uncomment to compute temperature values in Fahrenheit
//f = dht.computeHeatIndex(f,h,false);
// Check if any reads failed and exit early (to try again).
if (isnan(t) || isnan(ph_act)) {
Serial.println("Failed to read!");
return false;
}
Serial.print("pH Level: ");
Serial.print(ph_act);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
return true;
}
void SMSRequestStatus() {
if (myGSM.available() > 0) {
incomingChar = myGSM.read();
if (incomingChar == 'S') {
delay(10);
Serial.print(incomingChar);
incomingChar = myGSM.read();
if (incomingChar == 'T') {
delay(10);
Serial.print(incomingChar);
incomingChar = myGSM.read();
if (incomingChar == 'A') {
delay(10);
Serial.print(incomingChar);
incomingChar = SIM900.read();
if (incomingChar == 'T') {
delay(10);
Serial.print(incomingChar);
incomingChar = SIM900.read();
if (incomingChar == 'E') {
delay(10);
Serial.print(incomingChar);
Serial.print("...Request Received \n");
return true;
}
}
}
}
}
}
return false;
}
}
and here is the new error message
C:\Users\JIZELL AIRA\Downloads\sketch_oct07c\sketch_oct07c.ino: In function 'void loop()':
C:\Users\JIZELL AIRA\Downloads\sketch_oct07c\sketch_oct07c.ino:128:7: error: 'SMSRequestStatus' was not declared in this scope
if (SMSRequestStatus()) {
^~~~~~~~~~~~~~~~
C:\Users\JIZELL AIRA\Downloads\sketch_oct07c\sketch_oct07c.ino:128:7: note: suggested alternative: 'SMSRequest'
if (SMSRequestStatus()) {
^~~~~~~~~~~~~~~~
SMSRequest
C:\Users\JIZELL AIRA\Downloads\sketch_oct07c\sketch_oct07c.ino:129:9: error: 'readDataStatus' was not declared in this scope
if (readDataStatus()) {
^~~~~~~~~~~~~~
C:\Users\JIZELL AIRA\Downloads\sketch_oct07c\sketch_oct07c.ino:129:9: note: suggested alternative: 'readData'
if (readDataStatus()) {
^~~~~~~~~~~~~~
readData
C:\Users\JIZELL AIRA\Downloads\sketch_oct07c\sketch_oct07c.ino:155:27: error: a function-definition is not allowed here before '{' token
void readDataStatus() {
^
C:\Users\JIZELL AIRA\Downloads\sketch_oct07c\sketch_oct07c.ino:177:29: error: a function-definition is not allowed here before '{' token
void SMSRequestStatus() {
^
exit status 1
Compilation error: 'SMSRequestStatus' was not declared in this scope
Oh my God I finally was able to fixed it! I changed the void to bool since it was recommended in the error message and it worked! Although, I'm not sure if that's correct, is it? Anyways, I also was able to fixed the missing brace to end the loop()! Thank you so much for the help! I'll test out the code to see if it's working as intended.
Yes! I understood it a bit. I realized my mistake of misplacing or missing brackets as they are very important in coding and the importance of knowing what type of function to use!