-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGSMHat.jl
351 lines (280 loc) · 7.91 KB
/
GSMHat.jl
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
module GSMHat
using LibSerialPort
using Dates
using PiGPIO
using DelimitedFiles
using URIs
using StringEncodings
const ENCODING = "8859-1"
# using LoggingExtras
# const date_format = "yyyy-mm-dd HH:MM:SS"
# timestamp_logger(logger) = TransformerLogger(logger) do log
# merge(log, (; message = "$(Dates.format(now(), date_format)) $(log.message)"))
# end
# ConsoleLogger(stdout, Logging.Debug) |> timestamp_logger |> global_logger
function start_modem()
pi = Pi();
pin = 4; # mapping to port 7
PiGPIO.write(pi, pin, PiGPIO.ON);
sleep(4);
PiGPIO.write(pi, pin, PiGPIO.OFF)
end
function get(sp)
out = ""
if bytesavailable(sp) > 0
out *= decode(read(sp),"iso-$ENCODING")
#out *= String(read(sp))
end
return out
end
function cmd(sp,s,expect="OK")
write(sp, s * "\r\n")
return split(waitfor(sp,expect,command = s),"\r\n",keepempty=false)
end
function waitfor(sp,expect::Vector{String},maxpoll=Inf; command="")
out = ""
i = 0
while true
if bytesavailable(sp) > 0
out *= decode(read(sp),"iso-$ENCODING")
#out *= String(read(sp))
end
#@debug "wait for $expect in: $out"
if any([occursin(expect_,out) for expect_ in expect])
break
end
if occursin("ERROR",out)
@warn "out: $out"
throw(ErrorException(command * ":" * out))
end
i = i+1
if i > maxpoll
break
end
sleep(1)
end
return out
end
function waitfor(sp,expect::String,maxpoll=Inf; command = "")
return waitfor(sp,[expect],maxpoll,command = command)
end
"""
GSMHat.enable_gnss(sp)
Power GNSS (Global Navigation Satellite System) on. `sp` is connection object from `GSMHat.init`.
"""
enable_gnss(sp) = cmd(sp, "AT+CGNSPWR=1")
"""
GSMHat.disable_gnss(sp)
Power GNSS (Global Navigation Satellite System) off. `sp` is connection object from `GSMHat.init`.
"""
disable_gnss(sp) = cmd(sp, "AT+CGNSPWR=0")
"""
time,lon,lat = GSMHat.get_gnss(sp)
Get GNSS (Global Navigation Satellite System) coordinates and time. The function returns
`nothing` if when position and/or time are unknown.
"""
function get_gnss(sp)
time = longitude = latitude = nothing
info = cmd(sp, "AT+CGNSINF")
if length(info) >= 2
parts = split(info[1],",")
if length(parts) >= 5
time = tryparse(DateTime,parts[3],dateformat"yyyymmddHHMMSS.sss")
latitude = tryparse(Float64,parts[4])
longitude = tryparse(Float64,parts[5])
end
@debug "GNSS response $(info[1])"
end
return time,longitude,latitude
end
"""
GSMHat.send_message(sp,phone_number,local_SMS_service_center,message)
Send the GSM `message` to the number `phone_number` (including coutry code, e.g. 003242345678 for Belgium)
using the local SMS service center (also including country code).
"""
function send_message(sp,phone_number,local_SMS_service_center,message)
@debug "set local SMS service center"
cmd(sp, "AT+CSCA=\"$local_SMS_service_center\"")
@debug "set SMS text mode"
cmd(sp, "AT+CMGF=1")
@debug "set phone number"
cmd(sp, "AT+CMGS=\"$phone_number\"\r\n","\r\n> ")
@debug "send message $message"
write(sp, message)
@debug "terminate message"
write(sp, "\x1a\r\n")
waitfor(sp,"OK")
end
"""
GSMHat.get_messages(sp)
"""
function get_messages(sp)
write(sp,"AT+CMGL=\"ALL\"\r\n")
ret = waitfor(sp,"OK")
list = split(ret,"\r\n",keepempty=false)
if list[end] == "OK"
pop!(list)
end
list_sms = []
while !isempty(list)
status = popfirst!(list)
#a = split(split(status,"+CMGL: ")[2],",")
if startswith(status,"+CMGL: ")
a = replace(status,"+CMGL: " => "")
data = readdlm(IOBuffer(a),',')
if length(data) >= 5
index,message_status,address,address_text,service_center_time_stamp = data
sms_message_body = popfirst!(list)
sms = (; index,message_status,address,address_text,service_center_time_stamp,sms_message_body)
push!(list_sms,sms)
end
end
end
return list_sms
end
"""
GSMHat.delete_messages(sp, status=:received_read)
"""
function delete_messages(sp; status=:received_read)
if status == :received_read
flag = 1
elseif status == :all
flag = 4
else
error("unknown status $status for delete message")
end
cmd(sp,"AT+CMGD=1,$flag")
end
"""
GSMHat.delete_message(sp, 1)
"""
function delete_message(sp, index)
cmd(sp,"AT+CMGD=$index")
end
function unlook(sp,pin)
@info "query SIM"
write(sp, "AT+CPIN?\r\n")
out = waitfor(sp,"OK")
if !occursin("READY",out)
@info "unlock SIM"
write(sp, "AT+CPIN=\"$pin\"\r\n")
waitfor(sp,"SMS Ready")
else
@info "SIM already unlocked"
end
end
"""
GSMHat.reset(sp)
Reset the modem.
"""
function reset(sp)
cmd(sp, "AT+CFUN=0")
cmd(sp, "AT+CFUN=1")
end
"""
sp = GSMHat.init(portname, baudrate; pin=nothing)
Initialize the WaveShare GSM/GNSS/GPRS Hat pull pin 4 (mapping to port 7) high for 4 seconds and
unlock the SIM card if `pin` is provided.
"""
function init(portname, baudrate; pin=nothing, local_SMS_service_center = nothing)
# check if modem is on
sp = LibSerialPort.open(portname, baudrate)
write(sp,"AT\r\n")
out = waitfor(sp,"OK",20)
modem_on = occursin("OK",out)
if !modem_on
close(sp)
@info "start modem"
start_modem()
sleep(10)
sp = LibSerialPort.open(portname, baudrate)
sleep(2)
else
@info "modem already on"
end
@info "disable echo"
cmd(sp,"ATE0")
@info "test AT command"
cmd(sp,"AT")
if pin != nothing
unlook(sp,pin)
end
@info "selects the character set $(GSMHat.ENCODING)"
cmd(sp,"AT+CSCS=\"$(GSMHat.ENCODING)\"")
if local_SMS_service_center != nothing
@debug "set local SMS service center"
cmd(sp, "AT+CSCA=\"$local_SMS_service_center\"")
end
return sp
end
"""
GSMHat.enable_network(sp,APN)
"""
function enable_network(sp,APN)
# Attach to the network
write(sp,"AT+CGATT=1\r\n")
waitfor(sp,"OK")
# Wait for Attach
# Start task and set the APN
write(sp,"AT+CSTT=\"$APN\"\r\n")
waitfor(sp,"OK")
# Bring up the wireless connection
write(sp,"AT+CIICR\r\n")
waitfor(sp,"OK")
# Wait for bringup
# Get the local IP address
write(sp,"AT+CIFSR\r\n")
waitfor(sp,"\r\n") # no OK in output, just IP address
# like "\r\n10.197.161.140\r\n"
end
"""
# https://web.archive.org/web/20230121160033/https://docs.eseye.com/Content/ELS61/ATCommands/ELS61CREG.htm
"""
function registration_status(sp)
reg_status = cmd(sp,"AT+CREG?")
modus,status = parse.(Int,strip.(split(split(reg_status[1],':')[2],',')))
@info "registration status" reg_status
return modus,status
end
"""
GSMHat.http(sp,method,url,data)
"""
function http(sp,method,url,data)
url2 = URI(url)
@assert url2.scheme == "http"
ip = url2.host
urlpath = url2.path
# port 80 is the default HTTP port
port = if isempty(url2.port)
80
else
parse(Int,url2.port)
end
if method == "get"
url_ = URI(url2, query = data)
msg = "GET $url_ HTTP/1.0\r\n\r\n"
else
urlencoded = string(URI(;query = data))[2:end]
#Host: httpbin.org
#Host: $(ip)
#Connection: close
msg = """POST $urlpath HTTP/1.1
Connection: close
Host: $(ip)
Content-Type: application/x-www-form-urlencoded
Content-length: $(length(urlencoded))
$(urlencoded)
"""
end
# Start a TCP connection to remote address
cmd(sp,"AT+CIPSTART=\"TCP\",\"$ip\",$port","CONNECT OK")
write(sp,"AT+CIPSEND\r\n")
waitfor(sp,">")
write(sp,msg)
write(sp, "\x1a\r\n")
# out = waitfor(sp,"SEND OK")
# write(sp,"AT+CIPCLOSE\r\n")
ret = waitfor(sp,"CLOSED")
return ret
end
end