forked from projectsin/ruchebac2017
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ino
192 lines (157 loc) · 4.69 KB
/
server.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <GSM.h>
#include <MsTimer2.h>
#define PINNUMBER "1234"
char remoteNumber[20] = "0651769265";
char server[] = "88.162.164.62";
int port = 2000;
#define GPRS_APN "free"
#define GPRS_LOGIN "free"
#define GPRS_PASSWORD ""
String toSend;
long updateTime = millis();
boolean connectionActive = false;
GSMClient client;
GPRS gprs;
GSM gsmAccess;
GSMVoiceCall vcs;
GSM_SMS sms;
String xbeeData = "";
String clientData = "";
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
connectWithInternet();
MsTimer2::set(30000, ask);
MsTimer2::start();
}
void connectClient() {
Serial.println("Connecting to server...");
if (client.connect(server, port)) {
client.println("H");
Serial.println("Connected to server..");
connectionActive = true;
} else {
Serial.println("Not connected to server...");
connectionActive = false;
}
}
void loop() {
if ((millis() - updateTime) > (connectionActive ? 5 * 60000 : 60000)) {
checkConnection();
updateTime = millis();
}
if (Serial1.available() != 0) {
char c = Serial1.read();
//Serial.println("Receive char -> " + String(c)); //debug mode
if (c == '#') {
parseHiveData(xbeeData);
Serial.println("Receive X -> " + xbeeData); //debug mode
xbeeData = "";
} else {
xbeeData += String(c);
}
}
if (client.available() != 0) {
char c = client.read();
Serial.println("Receive char (client) -> " + String(c)); //debug mode
if (c == '#') {
Serial.println("Receive (client) -> " + clientData); //debug mode
parseClientData(clientData);
clientData = "";
} else
clientData += String(c);
}
}
void checkConnection() {
if (client.connected()) {
if (toSend.length() != 0)
client.print(toSend);
} else {
connectClient();
}
}
void parseClientData(String data) {
switch (data.charAt(0)) {
case 'N':
data.substring(1).toCharArray(remoteNumber, 20);
break;
case 'A':
Serial1.println(data + "#");
Serial.println("Envois xbee > " + data + "#");
break;
}
}
void parseHiveData(String data) {
if (data.startsWith("D")) {
data = data.substring(1);
String id = getValue(data, ';', 0);
String mass = getValue(data, ';', 1);
String temperature = getValue(data, ';', 2);
String humidity = getValue(data, ';', 3);
Serial.println("Hive " + id + " mass = " + mass + " temperature = " + temperature + " humidity = " + humidity);
client.print("D" + data);
} else if (data.startsWith("!")) {
switch (data.charAt(1)) {
case 'T':
sendSms("Alerte > la temperature de la ruche " + getValue(data.substring(2), ';', 0) + " est de "
+ getValue(data.substring(2), ';', 1), true);
break;
case 'V':
sendSms("Alerte > la ruche " + getValue(data.substring(2), ';', 0) + " subis un vol", true);
callGsm();
break;
case 'H':
sendSms("Alerte > la hygrometrie de la ruche " + getValue(data.substring(2), ';', 0) + " est de "
+ getValue(data.substring(2), ';', 1), true);
break;
case 'B':
sendSms("Alerte > la batterie de la ruche " + getValue(data.substring(2), ';', 0) + " est de "
+ getValue(data.substring(2), ';', 1) + "%", true);
break;
}
}
}
void ask() {
Serial1.println("?#");
Serial.println("Asking...");
}
String getValue(String data, char separator, int index) {
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void connectWithInternet() {
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) & (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
Serial.println("Sim cart connected");
connectClient();
} else
Serial.println("Not connected");
}
void connectWithoutInternet() {
if ((gsmAccess.begin(PINNUMBER) == GSM_READY)) {
Serial.println("Sim cart connected without internet");
} else
Serial.println("Not connected");
}
void sendSms(String message, boolean call) {
connectWithoutInternet();
sms.beginSMS(remoteNumber);
sms.print(message);
sms.endSMS();
if (call)
callGsm();
connectWithInternet();
}
void callGsm() {
if (vcs.voiceCall(remoteNumber)) {
vcs.hangCall();
}
}