I have develop this code for send SMS whit LS300 serial por with AT+CMGS comand.
I have develop it because the command at*smsm2m… doesn´t admit the character ‘,’
Also I need LS300 work with legend AT because it is connected to a PLC that send legend command.
I have try it and send my SMS without problem usign AT command.
I am a begginner in LUA programing, and I have put a infinite loop while listenig serial port(I dont know if it is necesary, or is going to trigger wachtdog or similar problem)
¿Somebody have code for send SMS with legend AT command?
¿Somebody have code for send email with another AT command or similar using gmail account?
i would be thankfull for it
---
-- SMS sample, (c) Sierra Wireless
-- May 2012
---
-- Serial Access API sample, (c) Sierra Wireless
-- Reserves the serial port, opens the port, writes a line, and reads a line
-- May 2012
local devicetree = require 'devicetree'
local system = require 'system'
local serial = require "serial" -- Serial lib
local sms = require 'sms'
local sched = require "sched" -- Lua scheduling and synchronization lib
local os = require "os" -- Operating system lib
--------------------------------------------------------------------------------
-- Variable path to be used
local RESERVE_SER0 = "system.aleos.reserve.ser0" --puerto serie 0
local send_sms_to -- = '+34650564875' -- phone number to send SMS msgs to
local recv_sms_from --= '+34650564875' -- phone number filter for receiving
local recv_pattern = 'Delivered' -- pattern filter for receiving
--------------------------------------------------------------------------------
-- Check if the serial port has been reserved for our use
-- if not, reserve it and reboot to gain control
function example_reserve_serial_port (asset)
local serial_available = assert (devicetree.get(RESERVE_SER0))
if serial_available ~= 1 then
print ("WARNING","serial port not available, reserving it now...")
local result = assert (devicetree.set(RESERVE_SER0, 1))
print ("WARNING","Serial port Reserved ok, rebooting now...")
system.reboot()
sched.wait(30) -- stop here until the reset occurs
end
print ("Serial Port has been reserved for our use.")
end
--------------------------------------------------------------------------------
-- open and configure the serial port
function example_open_serial_port (asset)
local portname = "/dev/ttyS0"
local serial_config =
{
baudRate=9600,
flowControl = 'none',
numDataBits=8,
parity='none',
numStopBits=1
}
local serialdev = assert (serial.open(portname, serial_config))
return serialdev
end
--------------------------------------------------------------------------------
-- Send an SMS message
function example_send_sms ()
local smsPhone = send_sms_to
local sendmsg = 'Message from Lua app...'
local msgFormat_7bit = '7bits'
print ('Sending SMS message to ', smsPhone)
assert (sms.send(smsPhone, sendmsg, msgFormat_7bit))
end
--------------------------------------------------------------------------------
-- Write to the serial port
function example_write_to_serial_port (sdevice, sdata)
local write_cnt = assert (sdevice:write (sdata))
print ('Number of characters written to serial port: ', write_cnt)
end
--------------------------------------------------------------------------------
-- Callback function for SMS messages received
function when_sms_msg_received(sender, message)
print ("Received SMS msg from: ", sender)
print ("Msg: ", message)
end
--------------------------------------------------------------------------------
-- Register to receive all SMS messages
-- from any number, with any message content
function example_register_to_receive_all_sms_msgs ()
print ('Register to receive all SMS messages')
local reg_sms_id = assert (sms.register(when_sms_msg_received))
return (reg_sms_id) -- ID is needed to unregister
end
--------------------------------------------------------------------------------
-- Register to receive SMS messages from a specific phone number
function example_register_for_sms_msgs_from_a_sender ()
print ('Register to receive messages from a sender')
-- Note the country code may not be in the received message
local reg_sms_id = assert (sms.register(when_sms_msg_received, recv_sms_from))
return (reg_sms_id) -- ID is needed to unregister
end
--------------------------------------------------------------------------------
-- Register to receive SMS messages that have specific content in msg body
function example_register_for_msg_content ()
print ('Register to receive messages containing a pattern in msg body')
local reg_sms_id = assert (sms.register(when_sms_msg_received, '', recv_pattern))
return (reg_sms_id) -- ID is needed to unregister
end
--------------------------------------------------------------------------------
-- Connect to the ReadyAgent to handle SMS requests.
-- Register to receive SMS messages.
-- Send SMS message.
-- Receive SMS messages.
-- cleanup and exit.
local function main ()
assert(devicetree.init())
assert(system.init())
sched.wait(1)
example_reserve_serial_port ()
sched.wait(1)
local serdev = example_open_serial_port ()
sched.wait(1)
example_write_to_serial_port (serdev, 'Dispositivo Listo')
--------------------------------------------------------------------------------
local line
local temp
local ContenidoSMS
local entrada=""
local ContenidoSMS="0"
local EnvioSMS=false
local DisparoEnvioSMS=false
while true do
salida=false
entrada=""
while salida==false do
temp = assert (serdev:read(1)) -- read a line
if EnvioSMS==false then
if temp ~="\013" then entrada =entrada..temp
else salida=true
end
else
if temp ~="\026" then entrada =entrada..temp
else salida=true DisparoEnvioSMS=true
end
end
end
if EnvioSMS==false then
temp = string.sub(entrada, 0, 9)
temp=string.upper(temp)--se pasa cadena a mayusculas para no hacer distincion
if temp == "AT+CMGS=\""then
EnvioSMS=true
ContenidoSMS=""
send_sms_to = string.sub(entrada, 10, 18) --se extrae el numero de telefono
send_sms_to ="+34"..send_sms_to
end
else
ContenidoSMS=ContenidoSMS..entrada
ContenidoSMS=string.gsub(ContenidoSMS, "\013", "\010",string.len(ContenidoSMS))--se sustituye enter por salto de carro
if DisparoEnvioSMS==true then
print("Contenido:")print(ContenidoSMS)print("Telefono:")print(send_sms_to)
end
end
if DisparoEnvioSMS==true then
print ("Inicializando librerias SMS")
assert(sms.init())
sched.wait(1)
print ("Envio SMS")
assert (sms.send(send_sms_to, ContenidoSMS, '7bits'))
print ("Enviado SMS a:") print (send_sms_to)print ("con contenido:")print (ContenidoSMS)
DisparoEnvioSMS=false
EnvioSMS=false
sched.wait(30)
end
end
-- clean up and exit
-- serdev:close() -- close the serial instance
-- os.exit (0)
end
--------------------------------------------------------------------------------
-- Schedule the main function, and launch the scheduler main loop
sched.run(main)
sched.loop()