-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArduino-SIM900-DHT22.ino
More file actions
323 lines (254 loc) · 7.01 KB
/
Copy pathArduino-SIM900-DHT22.ino
File metadata and controls
323 lines (254 loc) · 7.01 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <avr/wdt.h> //should be in any adruino IDE
#include <SoftwareSerial.h>
#include "DHT.h"
SoftwareSerial GPRS(2, 3); // RX, TX
#define GSM_ON 7 // connect GSM Module turn ON to pin 77 7 on my device
#define GSM_RESET 8 // connect GSM Module RESET to pin 35
#define DEBUG_EN true // enable debugging
#define DHTPIN 10 // what digital pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
// HTTPRequestInterval
unsigned long interval_1=60000; // the time we need to wait
unsigned long delayMillis_1=0; // millis() returns an unsigned long.
unsigned long delayMillisSuccess_1 = 0;
unsigned long resetMilis = 100000; //2147483647;
String apn = "gprs0.vipnet.hr"; // get device_key and
const char* device_key = "YOUR_DEVICE_KEY"; // get device_key and
const char* secret_key = "YOUR_DEVICE_PASSWORD"; // secret_key at http://loggingforest.com
String host = "loggingforest.com";
void setup()
{
GPRS.begin(9600);
if(DEBUG_EN)Serial.begin(9600);
if(DEBUG_EN)Serial.println("PROGRAM START ::::::::::");
blink(4);
// Power on SIM900 module
pinMode(GSM_ON, OUTPUT);
digitalWrite(GSM_ON, HIGH);
delay(500);
digitalWrite(GSM_ON, LOW);
delay(20000);
// end
//GPRS.write( 0x1a );
//delay(2000);
ifReceived("Call Ready", 20);
if(DEBUG_EN)Serial.println("TEST AT");
GPRS.write("AT\r");
if(ifReceived("OK", 30) == false)
softReset();
// [START] select mobile network manually
if(DEBUG_EN)Serial.println("SELECT NETWORK MANUALLY");
//print available networks
if(DEBUG_EN)
{
GPRS.write("AT+COPS=?\r");
ifReceived("OK", 20);
}
//GPRS.write("AT+COPS=1,2,\"21901\"\r"); // VIP=21910, BONBON=21901
//if(ifReceived("OK", 20) == false)
// softReset();
if(DEBUG_EN)Serial.println("NETWORK SELECTED");
// [END] select mobile network manually
/* VIP Mobile network PODACI
GPRS.write("AT+SAPBR=3,1,\"APN\",\"gprs0.vipnet.hr\"\r");
ifReceived("OK", 10);
GPRS.write("AT+SAPBR=3,1,\"USER\",\"38591\"\r");
ifReceived("OK", 10);
GPRS.write("AT+SAPBR=3,1,\"PWD\",\"38591\"\r");
ifReceived("OK", 10);
*/
// [START] t-mobile network data
//GPRS.write("AT+SAPBR=3,1,\"APN\",\"web.htgprs\"\r");
//GPRS.write("AT+SAPBR=3,1,\"APN\",\"gprs0.vipnet.hr\"\r");
String call_apn = "AT+SAPBR=3,1,\"APN\",\""+apn+"\"\r";
GPRS.print(call_apn);
ifReceived("OK", 10);
GPRS.write("AT+SAPBR=3,1,\"USER\",\"\"\r");
ifReceived("OK", 10);
GPRS.write("AT+SAPBR=3,1,\"PWD\",\"\"\r");
ifReceived("OK", 10);
// [END] t-mobile network data
GPRS.write("AT+SAPBR=0,1\r"); // ovo zna LOOP FAILED vratiti
ifReceived("OK", 10);
GPRS.write("AT+SAPBR=1,1\r"); // ovo zna LOOP FAILED vratiti
ifReceived("OK", 10);
// [START] DEL all sms messages
GPRS.write("AT+CMGDA=\"DEL ALL\"\r");
ifReceived("OK", 10);
// [END] DEL all sms messages
dht.begin();
}
void loop()
{
blink(1);
if(DEBUG_EN)Serial.println("LOOP START");
unsigned long currentMillis = millis(); // grab current time
// HANG CALL IF AVAILABLE
GPRS.write("ATH\r");
ifReceived("OK", 10);
// [SEND DATA INTERVAL]
if (millis() > delayMillis_1 )
{
sendHTTPData();
//if(DEBUG_EN)Serial.println("sendHTTPData()");
delayMillis_1 = millis()+interval_1;
}
else
{
if(DEBUG_EN)Serial.println((delayMillis_1-millis())
/1000);
}
// [/SEND DATA INTERVAL]
// RESET if 3 HTTP requests failed
if(delayMillisSuccess_1 < millis() &&
delayMillisSuccess_1 != 0)
softReset();
// RESET after half of millis() lifetime
if(millis() > resetMilis)
softReset();
if(DEBUG_EN)Serial.println(delayMillisSuccess_1/1000);
// Dump remaining data to clear buffers
dumpData();
if(DEBUG_EN)Serial.println("LOOP END");
blink(1);
delay(1000);
}
void sendHTTPData()
{
// [READ SENSOR]
float humd = dht.readHumidity();
float temp = dht.readTemperature();
long tmp_s = (long) millis() / 1000;
if(humd > 100 || temp > 100 || isnan(humd) || isnan(temp)){
if(DEBUG_EN)Serial.println("SENSOR FAILED");
}
else
{
if(DEBUG_EN)Serial.println(String(humd)+"% "+String(temp)+"C");
}
// [/READ SENSOR]
// [SEND DATA]
// We now create a URI for the request
String url = "/index.php/api";
url += "?device_key=";
url += device_key;
url += "&secret_key=";
url += secret_key;
url += "&milis=";
url += tmp_s;
url += "&temp=";
url += temp;
url += "&hum=";
url += humd;
//GET /api.php?data=dev:329;rt:10;h:10;t:10
String call = "AT+HTTPPARA=\"URL\",\""+host+url+"\"\r";
// send data
GPRS.write("AT+HTTPINIT\r");
ifReceived("OK", 10);
GPRS.write("AT+HTTPPARA=\"CID\",1\r");
ifReceived("OK", 10);
GPRS.print(call.substring(0, 50) );
GPRS.println(call.substring(50) );
ifReceived("OK", 10);
GPRS.write("AT+HTTPACTION=0 \r");
ifReceived("200", 130);
//read results
GPRS.write("AT+HTTPREAD=0,100 \r");
if(ifReceived("RESULT", 20) == true)
{
delayMillisSuccess_1 = millis()+interval_1*3;
blink(3);
}
//term connection
GPRS.write("AT+HTTPTERM\r");
ifReceived("OK", 10);
// [/SEND DATA]
}
void dumpData()
{
if(DEBUG_EN)Serial.println("DATA DUMP:");
while(GPRS.available())
{
char c = GPRS.read();
if(DEBUG_EN)Serial.print(c);
}
}
boolean ifReceived(String check, unsigned long timeout)
{
String received = String("");
boolean av=false;
timeout=1000*timeout;
if(timeout<10000)timeout=10000;
unsigned long dmilis=millis()+timeout;
while(dmilis > millis())
{
while(GPRS.available())
{
char c = GPRS.read();
received+=c;
av=true;
}
// ON WANTED OR ERROR brean immediately
if(received.indexOf(check) > 0 ||
received.indexOf("ERROR") > 0)break;
}
if(DEBUG_EN)Serial.println(received);
if(received.indexOf(check) > 0)
return true;
if(received.indexOf("ERROR") > 0 ||
received.indexOf("OK") > 0)
return false;
// serial buffer size fix
if(received.length() > 60)
{
if(DEBUG_EN)Serial.println("BUFFER OVERFLOW");
return false;
}
if(DEBUG_EN && false)
{
Serial.println("LOOP, FAILED");
softReset();
/*
while(true){
delay(500);
}
*/
}
return false;
}
void softReset(){
delay(5000);
GPRS.write("AT+CPOWD=1\r");
ifReceived("POWER DOWN", 20);
delay(5000);
if(DEBUG_EN)Serial.println("softReset");
wdt_enable(WDTO_1S); //enable it, and set it to 8s
while(1){}
//asm volatile (" jmp 0");
}
void blink(int n)
{
pinMode(13, OUTPUT);
for(int i=0; i<n; i++)
{
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
}
}
/*
// If you want to replace asterisks with ctrl+Z you can
// replace the second part above with this instead
while (Serial.available()) {
byte b = Serial.read();
if ( b == '*')
GPRS.write( 0x1a ); // replace * with ctrl+z
else
GPRS.write(b);
}
*/