-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim800.c
More file actions
106 lines (94 loc) · 2.5 KB
/
sim800.c
File metadata and controls
106 lines (94 loc) · 2.5 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
#include "sim800.h"
char waitForResponse(void){
char response;
while(1){
response = EUSART_Read();
if(response == 'O'){
if(EUSART_Read() == 'K') return 1;
} else if(response == 'E'){
if(EUSART_Read() == 'R') return 0;
}
}
}
char waitForConnection(void){
int seconds = 0;
EUSART_PrintLn("AT+CREG=1");
waitForResponse();
while(seconds < 10){
EUSART_PrintLn("AT+CREG?");
while(EUSART_Read() != ':');
EUSART_Read(); //Skip 1 char
if(EUSART_Read() == '1' || EUSART_Read() == '5'){
return 1;
}
__delay_ms(1000);
seconds++;
}
return 0;
}
char makeGPRSConnection(void){
EUSART_PrintLn("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
if(!waitForResponse()) return 0;
EUSART_PrintLn("AT+SAPBR=3,1,\"APN\",\"Internet\"");
if(!waitForResponse()) return 0;
EUSART_PrintLn("AT+SAPBR=1,1");
if(!waitForResponse()) return 0;
__delay_ms(100);
return 1;
}
void getBatteryLevel(char * batBuf){
int i=0;
EUSART_PrintLn("AT+CBC");
while(EUSART_Read() != ',');
do{
batBuf[i] = EUSART_Read();
i++;
} while(batBuf[i-1] != ',');
batBuf[i-1] = '\0';
}
void readHTTPRequest(char * RXBuffer){
uint8_t i=0;
EUSART_PrintLn("AT+HTTPREAD");
while(EUSART_Read() != '\n'); //Skip the echo
while(EUSART_Read() != '\n'); //Skip the length
do{
RXBuffer[i] = EUSART_Read();
i++;
}while(RXBuffer[i-1] != '\n');
RXBuffer[i-1] = '\0';
}
void makeHTTPRequest(char * lat, char * lon, char * battery, char * satellites, char * height){
EUSART_PrintLn("AT+HTTPINIT");
waitForResponse();
EUSART_Print("AT+HTTPPARA=\"URL\",\"http://api.erwincastricum.nl/test.php?lat=");
EUSART_Print(lat);
EUSART_Print("&lon=");
EUSART_Print(lon);
EUSART_Print("&bat=");
EUSART_Print(battery);
EUSART_Print("&sat=");
EUSART_Print(satellites);
EUSART_Print("&height=");
EUSART_Print(height);
EUSART_PrintLn("\"");
waitForResponse();
EUSART_PrintLn("AT+HTTPACTION=0");
__delay_ms(3000);
EUSART_PrintLn("AT+HTTPTERM");
waitForResponse();
}
void closeGPRSConnection(void){
EUSART_PrintLn("AT+SAPBR=0,1");
waitForResponse();
}
void simSleep(void){
EUSART_PrintLn("AT+CSCLK=2");
waitForResponse();
}
void simWakeUp(void){
EUSART_PrintLn("AT");
__delay_ms(500);
EUSART_PrintLn("AT+CSCLK=0");
waitForResponse();
__delay_ms(1000);
}