This repository has been archived by the owner on Apr 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Ibelium.cpp
95 lines (73 loc) · 1.87 KB
/
Ibelium.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "Arduino.h"
#include "Ibelium.h"
//Each function returns 1 if successful, 0 otherwise.
Ibelium::Ibelium() {
//Empty Constructor
}
int Ibelium::init() {
//Initializes and tests the module.
Serial.begin(115200);
delay(2000);
pinMode(led, OUTPUT);
pinMode(onModulePin, OUTPUT);
switchModule(); //Switches the module on
return sendATCmd("AT");
}
int Ibelium::sendATCmd(char* cmd) {
//Send an AT Command without returning the response
char* resp;
int s;
s = sendATQuery(cmd, resp);
free(resp);
return s;
}
int Ibelium::sendATQuery(char* cmd, char* response) {
//Send an AT command, and get the response from the board.
//free(response); //Avoid creating a memory leak.
_error=0;
_watchdog=0;
int size=10;
char* buff = (char*) calloc(size, sizeof(char)); //Allocate a 10 character buffer for the response
int x=0;
Serial.print(cmd);
Serial.print((char)0xD);
Serial.print((char)0xA);
Serial.flush();
delay(2000);
//By now, the command should be sent completely.
do {
_watchdog=0;
while(Serial.available()==0 && _watchdog<1000) _watchdog++;
if(_watchdog>=1000) //No response received after a certain length of time
{
Serial.println("Watchdog timer gone");
_error=2;
break;
}
buff[x]=Serial.read(); //Read the next character
x++;
if(x>=size) { //Reallocate memory if message is getting longer.
size *= 2;
buff = (char*) realloc(buff, size);
}
if(size>256){ //Reponse too long
_error = 1;
Serial.println(size);
break;
}
} while(x<2 || !(buff[x-1]=='K' && buff[x-2]=='O') );//Keep Waiting for a response until the last two characters are 'OK'
buff[x]=0x0;
response = buff;
if(_error==0)
return x;
else
return 0;
}
int Ibelium::sendSMS(char* message, char* response) {
return 1;
}
void Ibelium::switchModule() {
digitalWrite(onModulePin,HIGH);
delay(2000);
digitalWrite(onModulePin,LOW);
}