Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more
Diff: cellular/Cellular.cpp
- Revision:
- 0:563b70517320
- Child:
- 4:6561c9128c6f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/cellular/Cellular.cpp Mon Dec 09 15:29:35 2013 +0000
@@ -0,0 +1,164 @@
+#ifndef CELLULAR_CPP
+#define CELLULAR_CPP
+
+#include "Cellular.h"
+
+Cellular::Cellular(MTSBufferedIO* io) : io(io)
+{
+}
+
+Cellular::~Cellular()
+{
+}
+
+Cellular::Code Cellular::ATTest()
+{
+ return sendBasicCommand("AT", 1000);
+}
+
+Cellular::Code Cellular::echoOff(bool state)
+{
+ if (state) {
+ return sendBasicCommand("ATE0", 1000);
+ } else {
+ return sendBasicCommand("ATE1", 1000);
+ }
+}
+
+int Cellular::getSignalStrength()
+{
+ string response = sendCommand("AT+CSQ", 1000);
+ if (response.find("OK") == string::npos) {
+ return -1;
+ }
+ int start = response.find(':');
+ int stop = response.find(',', start);
+ string signal = response.substr(start + 2, stop - start - 2);
+ int value;
+ sscanf(signal.c_str(), "%d", &value);
+ return value;
+}
+
+Cellular::Registration Cellular::getRegistration()
+{
+ string response = sendCommand("AT+CREG?", 1000);
+ if (response.find("OK") == string::npos) {
+ return UNKNOWN;
+ }
+ int start = response.find(',');
+ int stop = response.find(' ', start);
+ string regStat = response.substr(start + 1, stop - start - 1);
+ int value;
+ sscanf(regStat.c_str(), "%d", &value);
+ switch (value) {
+ case 0:
+ return NOT_REGISTERED;
+ case 1:
+ return REGISTERED;
+ case 2:
+ return SEARCHING;
+ case 3:
+ return DENIED;
+ case 4:
+ return UNKNOWN;
+ case 5:
+ return ROAMING;
+ }
+}
+
+int Cellular::connect(string host, int port)
+{
+ // Set the Server Address
+ string hostCmd = "AT#TCPSERV=1,\"";
+ hostCmd.append(host);
+ hostCmd.append("\"");
+ if (sendBasicCommand(hostCmd, 1000) != OK) {
+ return -1;
+ }
+
+ // Set the Server Port
+ string portCmd = "AT#TCPPORT=1,\"";
+ char tmp[7];
+ if (sprintf(tmp, "%d", port) < 0) {
+ return -1;
+ }
+ portCmd.append(string(tmp));
+ portCmd.append("\"");
+ if (sendBasicCommand(portCmd, 1000) != OK) {
+ return -1;
+ }
+
+ // Try and Connect
+ string response = sendCommand("AT#OTCP=1", 2000);
+ if (response.find("Ok_Info_WaitingForData") != string::npos) {
+ return 0;
+ } else {
+ return -1;
+ }
+}
+
+Cellular::Code Cellular::sendBasicCommand(string command, int timeoutMillis, ESC_CHAR esc)
+{
+ string response = sendCommand(command, timeoutMillis, esc);
+ if (response.size() == 0) {
+ return NO_RESPONSE;
+ } else if (response.find("OK") != string::npos) {
+ return OK;
+ } else if (response.find("ERROR") != string::npos) {
+ return ERROR;
+ } else {
+ return FAILURE;
+ }
+}
+
+Cellular::Code Cellular::sendSMS(string phoneNumber, string message)
+{
+ Code code = sendBasicCommand("AT+CMGF=1", 1000);
+ if (code != OK) {
+ return code;
+ }
+ string cmd = "AT+CMGS=\"+";
+ cmd.append(phoneNumber);
+ cmd.append("\"");
+ string response1 = sendCommand(cmd, 1000);
+ if (response1.find('>') == string::npos) {
+ return NO_RESPONSE;
+ }
+ wait(.2);
+ string response2 = sendCommand(message, 4000, CTRL_Z);
+ printf("SMS Response: %s\n", response2);
+ if (response2.find("+CMGS:") == string::npos) {
+ return FAILURE;
+ }
+ return OK;
+}
+
+string Cellular::sendCommand(string command, int timeoutMillis, ESC_CHAR esc)
+{
+ int size = command.size() + 1;
+ char cmd[size];
+ strcpy(cmd, command.c_str());
+ if (esc == CR) {
+ cmd[size -1] = '\r';
+ } else if (esc == CTRL_Z) {
+ cmd[size -1] = 0x1A;
+ }
+
+ io->rxClear();
+ io->txClear();
+ int status = io->write(cmd, size);
+ int available = io->rxAvailable();
+ int previous = -1;
+ int timer = 0;
+ while (available != previous && timer < timeoutMillis) {
+ wait(.1);
+ timer = timer + 100;
+ previous = available;
+ available = io->rxAvailable();
+ }
+ char tmp[available];
+ io->read(tmp, available);
+ return string(tmp);
+}
+
+#endif /* CELLULAR_CPP */
\ No newline at end of file
uIP Socket Modem Shield (Outdated - see below)