Sending AT commands to a 3G modem module

Hi All - VERY new to Arduino and struggling somewhat as I am from a BASIC background (Picaxe etc!)

Looking for guidance on how to do this a better way - I have it working but its extremely messy and convoluted at the moment.

Background on the project is to collect certain weather data (Temp, RH & windspeed) and send a warning via email when certain conditions are met.
I have the data collection working and displayed on LCD . Temp & Humidity from a RHT03 sensor using the DHT library and a Davis Anemometer for the windspeed.

I have the 3G module (SIM5216J) working (ie power control, checking network registration etc) and sending SMS initially but the code is very messy as the commands are not nice to send via serial and cause errors in the compiling.

The command I needed to send was as follows:

AT+CMGS="0418xxxxxx"

This caused compiler errors due to the quotes inside the quotes:

gsmserial.println ("AT+CMGS="0418xxxxxx"");

So I had to do it this way (bigger code snippet with more of the commands)

sendATcommand("AT+CMGF=1","OK",500);
Serial.println(F("3G : SMS Mode"));
  gsmSerial.print("AT+CMGS=");
  gsmSerial.write(34);
  gsmSerial.print("0418xxxxxx");   //mobile number edited for privacy
  gsmSerial.write(34);
 gsmSerial.write(13);
 delay(500);
 gsmSerial.print("SIMCOM MODULE TEST");
 gsmSerial.write(26);
 delay(500);
 Serial.println(F("3G : SMS Sent"));

This works but is not very tidy - mainly due to my lack of understanding on Ardunio & C!

Now my issue grows larger as I need to send a much more complicated set of commands to send an email!
The "@" and the quotes inside the message strings cause compiler issues and having to do it the way I did the SMS will get crazy messy!
See below for commands that have to be sent:

AT+CGSOCKCONT=1,"IP","3GAPN","0.0.0.0",0,0
AT+SMTPSRV="smtp.server.com",25
AT+SMTPAUTH=0
AT+SMTPFROM="username@server.com"
ATSMTPRCPT=0,0,"toaddr@server.com"
AT+SMTPSUB="email subject"
AT+SMTPBODY="message body"
AT+SMTPSEND

Can anyone suggest a better way to handle sending these serial strings? Maybe point me at a good tutorial or appnote that makes this clear and is not too generic - I've read the arduino pages on serial.print etc and still no clearer to me!

Cheers and sorry for such a long post :slight_smile:

Stocky :astonished:

Haven't looked at the code at all, but the first problem you mention of wanting to put double quotes inside a string literal can be solved by prefixing them with an escape character:

"AT+CMGS=\"0418xxxxxx\""

Thank you for your help PeterH....such a simple thing but I cant seem to find that explained anywhere (obviously not looking in the right place!)
That makes the code look a lot better and allows one line to replace my 5 lines!
Now to apply that to the other problematic lines of code and see if I can condense them the same way :slight_smile:

Regards

Stocky