-
Notifications
You must be signed in to change notification settings - Fork 27
/
Eth_GSM.cpp
119 lines (95 loc) · 2 KB
/
Eth_GSM.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
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
#include <Arduino.h>
#include <Dhcp.h>
#include <String.h>
#include "IoTgo.h"
#include "Eth_GSM.h"
#ifdef NET_USE_GSM
/* Create a object of InetGSM class */
static InetGSM inet;
static InetGSM *gsm_inet = &inet;
/**
* Initialize the GSM module
*
* @retval true - success.
* @retval false - failed.
*/
bool Gsm::intialGSM()
{
if(gsm.begin(9600))
{
DebugSerial.println("Gsm ready OK!");
//GPRS attach, put in order APN, username and password.
if(gsm_inet->attachGPRS("internet.wind", "", ""))
{
DebugSerial.println("attachGPRS status=ATTACHED");
}
else
{
DebugSerial.println("attachGPRS status=ERROR");
return false;
}
delay(1000);
//Read IP address.
gsm.SimpleWriteln("AT+CIFSR");
delay(5000);
gsm.WhileSimpleRead();
return true;
}
else
{
DebugSerial.println("gsm.begin err!");
return false;
}
}
int32_t Gsm::createTCPConnection(String host, uint32_t port)
{
char msg[64] = {0};
const char *server_name = host.c_str();
bool connected = false;
int i;
for (i = 0; i < 10; i++)
{
//DebugSerial.print("Try ");
//DebugSerial.println(i);
if(gsm_inet->connectTCP(server_name, (int)port))
{
//DebugSerial.println("TCP connect ok!");
return 0;
}
delay(1000);
}
DebugSerial.println("TCP connect err!");
return ERR_TCP_CONN_FAILED;
}
int32_t Gsm::send(String data)
{
int32_t ret = 0;
char end_c[2];
end_c[0]=0x1a;
end_c[1]='\0';
gsm.SimpleWrite(data.c_str());
gsm.SimpleWrite(end_c);
switch(gsm.WaitResp(10000, 10, "SEND OK"))
{
case RX_TMOUT_ERR:
ret = ERR_TCP_SEND_FAILED;
DebugSerial.println("send timeout!");
break;
case RX_FINISHED_STR_NOT_RECV:
ret = ERR_TCP_SEND_FAILED;
//DebugSerial.println("send no response!");
break;
//default:
//DebugSerial.println("send ok!");
}
return ret;
}
int32_t Gsm::recv(char * buffer, uint32_t length)
{
return gsm.read(buffer, length);
}
int32_t Gsm::releaseTCPConnection()
{
return gsm_inet->disconnectTCP() == 1 ? 0 : ERR_TCP_DISCONN_FAILED;
}
#endif /*NET_USE_GSM*/