-
Notifications
You must be signed in to change notification settings - Fork 0
/
codigo_test_pings.ino
136 lines (127 loc) · 2.68 KB
/
codigo_test_pings.ino
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <SoftwareSerial.h>
#include <TimeLib.h>
SoftwareSerial mySerial(10, 11); // RX, TX
#define arraySize 128
int counter = 0;
int success_counter = 0;
int error_counter = 0;
int i;
int temp;
void setup(){ // Open serial communications and wait for port to open:
Serial.begin(9600);
mySerial.begin(9600);
initGPRS(); // inicia el modulo GPRS
}
void initGPRS(){
connectGPRS("AT+CIPSHUT","SHUT OK");
delay(2000);
connectGPRS("AT+CREG=0","OK");
delay(2000);
connectGPRS("AT+CREG=1","OK");
delay(2000);
connectGPRS("AT+CGATT=1","OK");
delay(2000);
connectGPRS("AT+CIPSHUT","SHUT OK");
delay(2000);
connectGPRS("AT+CSTT=\"CMNET\"","OK");
delay(2000);
connectGPRS_CIICR("AT+CIICR","OK");
delay(5000);
connectGPRS_CIFSR("AT+CIFSR");
delay(1000);
connectGPRS("AT+CENG=4","OK");
delay(1000);
}
void connectGPRS(String cmd, char *res){
while(1){
Serial.println(cmd);
mySerial.println(cmd);
delay(500);
while(mySerial.available()>0){
if(mySerial.find(res)){
Serial.println(res);
delay(2000);
return;
}
}
delay(2000);
}
}
void connectGPRS_CIICR(String cmd, char *res){
while(1){
Serial.println(cmd);
mySerial.println(cmd);
delay(10000);
while(mySerial.available()>0){
if(mySerial.find(res)){
Serial.println(res);
delay(1000);
return;
}
}
}
}
void connectGPRS_CIFSR(String cmd){
static char buffer[256];
static size_t pos;
mySerial.println(cmd);
delay(500);
while(mySerial.available() && pos < sizeof buffer - 1){
char c = mySerial.read();
buffer[pos++] = c;
if(c == '\n'){
buffer[pos] = '\0';
Serial.print(buffer);
pos = 0;
}
}
}
bool connectGPRS_PING(){
bool ping_ok = true;
char inData[arraySize];
mySerial.println("AT+CIPPING=\"192.168.216.119\",1,1024\r");
delay(10000);
int c = 0;
while(mySerial.available()){
inData[c] = mySerial.read();
Serial.print(inData[c]);
c++;
}
Serial.println();
if(strstr(inData, "600")){
ping_ok = false;
}
else{
ping_ok = true;
}
return ping_ok;
}
void connectCENG(){
char inData2[arraySize];
mySerial.println("AT+CENG?\r");
delay(1000);
int c = 0;
while(mySerial.available()){
inData2[c] = mySerial.read();
Serial.print(inData2[c]);
c++;
}
Serial.println();
}
void loop(){// run over and over
Serial.println( String(hour()) + ":" + String(minute())+":"+String(second()));
connectCENG();
bool value = connectGPRS_PING();
if (value == true){
success_counter++;
}
else{
error_counter++;
}
counter = success_counter + error_counter;
Serial.println((String)"Total paquetes enviados: " + counter);
Serial.println((String)"Ping errados: " + error_counter);
Serial.println((String)"Ping correctos: " + success_counter);
temp = random(10,50)*100;
delay(temp);
}