问题
I wrote this code to interface the gsm module, can anyone check the code and give me some propositions ?
hex1= '0x1A';
function delay_s(delay)
delay = delay or 1
local time_to = os.time() + delay
while os.time() < time_to do end
end
uart.alt(1);
uart.setup(0, 9600, 8, uart.PARITY_NONE, uart.STOPBITS_1, 1)
uart.write(0,"AT+IPR=9600\n")
for j = 1, 10 do
uart.write(0, "AT\n")
end
delay_s(1000)
uart.write(0, "AT\n")
delay_s(1000)
uart.write(0, 'AT+CSCS="GSM"\n')
delay_s(1000)
uart.write(0, 'AT+CMGF=1\n')
delay_s(1000)
uart.write(0, 'AT+CMGS="+21654102832"\n')
delay_s(1000)
uart.write(0, " Salut tout le mond !!!\n")
delay_s(1000)
uart.write(0, hex1)
delay_s(1000)
回答1:
This code won't even run on NodeMCU because the standard Lua os.time() will fail since the os
module is not available. I suggest you dig into http://nodemcu.readthedocs.io/en/latest/en/lua-developer-faq/#how-is-nodemcu-lua-different-to-standard-lua.
Besides, even if it were available os.time()
has 1 second resolution.
The returned value is a number, whose meaning depends on your system. In POSIX, Windows, and some other systems, this number counts the number of seconds since some given start time (the "epoch").
Hence, your delay_s(1000)
would delay execution for 1000 seconds. Doing that with busy-waiting is...not optimal.
You probably want to use the tmr module instead.
回答2:
uart.alt(x);
0 - standard pins
1 - alternate the pins
You havent specify which pins you are gonna use for the communication, or else use uart.alt(0);
for standard pins
来源:https://stackoverflow.com/questions/41961710/interfacing-gsm-module-with-nodemcu