Hello everyone
I have been on this code for like weeks now and its giving sleepless night, here is how far i could get it to work, am hook here. please i really need your help now.
I am using SIM900 and Arduino uno
I have been trying to see if I could remedy the situation but to no avail.
I only have knowledge for PHP and MYSQL and am new in C, don't know much in C
The code below can send information to a database and call it back using MySQL and PHP, INSERT and SELECT statement.
I will like to make a phone call with the number returned from the request.
I want to parse the result and make a call to it.
Sorry for the code is long, please have patient with me and read it, if you have a shorter code that can do the same thing, please help me with it. thank you very much.
#include <SoftwareSerial.h>
SoftwareSerial GSM(2, 3); // RX, TX
enum _parseState {
PS_DETECT_MSG_TYPE,
PS_IGNORING_COMMAND_ECHO,
PS_HTTPACTION_TYPE,
PS_HTTPACTION_RESULT,
PS_HTTPACTION_LENGTH,
PS_HTTPREAD_LENGTH,
PS_HTTPREAD_CONTENT,
PS_HTTPTERM,
PS_MAKECALL
};
byte parseState = PS_DETECT_MSG_TYPE;
char buffer[80];
byte pos = 0;
int contentLength = 0;
void resetBuffer() {
memset(buffer, 0, sizeof(buffer));
pos = 0;
}
void sendGSM(String msg, int waitMs = 500) {
GSM.println(msg);
delay(waitMs);
while(GSM.available()) {
parseATText(GSM.read());
}
}
void setup()
{
GSM.begin(2400);
Serial.begin(9600);
sendGSM("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
sendGSM("AT+SAPBR=3,1,\"APN\",\"9mobile\"");
//sendGSM("AT+SAPBR=3,1,\"USER\",\"FLAT\"");
//sendGSM("AT+SAPBR=3,1,\"PWD\",\"FLAT\"");
sendGSM("AT+SAPBR=1,1",3000);
sendGSM("AT+SAPBR=2,1");
sendGSM("AT+HTTPINIT");
sendGSM("AT+CGATT?");
sendGSM("AT+HTTPPARA=\"CID\",1");
sendGSM("AT+HTTPPARA=\"URL\",\"ourhelperlinks.com/webservice/data.php?network_id=4&senderNo=2348064996077&recMsg=ItHasWorkedWellNow&timeArrived=10/03/2017\"");
sendGSM("AT+HTTPACTION=0");
sendGSM("AT+HTTPTERM");
sendGSM("AT+SAPBR=0,1");
sendGSM("AT+CMGF=1");
void loop()
{
while(GSM.available()) {
parseATText(GSM.read());
}
}
void parseATText(char b) {
buffer[pos++] = b;
if ( pos >= sizeof(buffer) )
resetBuffer(); // just to be safe
switch (parseState) {
case PS_DETECT_MSG_TYPE:
{
if ( b == '\n' )
resetBuffer();
else {
if ( pos == 3 && strcmp(buffer, "AT+") == 0 ) {
parseState = PS_IGNORING_COMMAND_ECHO;
}
if (pos == 1 && strcmp(buffer, "AT+") == 0 ) {
//Serial.println("Sms Mode Activated");
parseState = PS_IGNORING_COMMAND_ECHO;
}
if ( strcmp(buffer, "+CMGF") == 1 ) {
Serial.println("Sms Mode Activated");
parseState = PS_MAKECALL;
}
else if ( b == ':' ) {
if ( strcmp(buffer, "+HTTPACTION:") == 0 ) {
Serial.println("Received HTTPACTION");
parseState = PS_HTTPACTION_TYPE;
}
else if ( strcmp(buffer, "+HTTPREAD:") == 0 ) {
Serial.println("Received HTTPREAD");
parseState = PS_HTTPREAD_LENGTH;
}
resetBuffer();
}
}
}
break;
case PS_IGNORING_COMMAND_ECHO:
{
if ( b == '\n' ) {
Serial.print("Ignoring echo: ");
Serial.println(buffer);
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
case PS_HTTPACTION_TYPE:
{
if ( b == ',' ) {
Serial.print("HTTPACTION type is ");
Serial.println(buffer);
parseState = PS_HTTPACTION_RESULT;
resetBuffer();
}
}
break;
case PS_HTTPACTION_RESULT:
{
if ( b == ',' ) {
Serial.print("HTTPACTION result is ");
Serial.println(buffer);
parseState = PS_HTTPACTION_LENGTH;
resetBuffer();
}
}
break;
case PS_HTTPACTION_LENGTH:
{
if ( b == '\n' ) {
Serial.print("HTTPACTION length is ");
Serial.println(buffer);
// now request content
GSM.print("AT+HTTPREAD=0,");
GSM.println(buffer);
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
case PS_HTTPREAD_LENGTH:
{
if ( b == '\n' ) {
contentLength = atoi(buffer);
Serial.print("HTTPREAD length is ");
Serial.println(contentLength);
Serial.print("HTTPREAD content: ");
parseState = PS_HTTPTERM;
resetBuffer();
}
}
break;
case PS_MAKECALL:
{
if ( b == '\n' ) {
sendGSM("ATD+" b ";");
//now request content
GSM.println("ATD +2348064996077;");
delay(20000);
GSM.println(buffer);
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
}
}
Am trying to upload the sketch but its telling me
Expression cannot be used as a function
(byte b)
in sendGSM("ATD+" b ";");
I want to parse the result and make a call to it.
If i put a number instead of the variable num, the phone call goes but not going with the variable, and i want it to make a call to the number gotten from the database
I made the SELECT statement returns only the number which I intend to call. But I dont know how place that final call from the Arduino sketch.
I hope you understand my issue, i will really appreciate your help cos this code has given me sleepless night for more than a week. thanks for understanding.
Please don't read my message and pass, please help me even if its just an idea. thanks
I will appreciate your help and swift response.
Thank you.