var cmd="";
var issued_command="";
Serial4.setup(9600,{rx:C11,tx:C10});
//Parse response v2
function parse(buffer){
var line = buffer.toString().substring(0, buffer.length - 1);
//Remove anything remotely blank or echoey
if(line.length < 2 || line.indexOf(">")===0 || issued_command===line){
return;
}
issued_command="";
//SMS Received
if(line.indexOf("+")>-1){
var d = line.substr(line.indexOf("+")+1,line.indexOf(":")-1);
switch(d) {
case "CMTI":
tnum = line.split(",");
console.log("SMS received #", tnum[1]);
setTimeout(function(){send_command("at+cmgr="+tnum[1]+"\r");},1000);
break;
//read incoming SMS
case "CSQ":
sgnl = line.slice(5);
sgnl = sgnl.split(",");
console.log("signal strength "+sgnl[0]);
//console.log(line);
break;
//Read cell signal
case "CMGL":
//console.log("all text messages in memory ");
break;
//View all text message in memory
case "CMGR":
console.log("text message "+line);
break;
//read individual text message in memory
case "COPS":
ntwk = line.slice(5);
ntwk = ntwk.split(",");
console.log("network "+ ntwk[2]);
break;
//Read network carrier
case "CBC":
chg = line.split(",");
console.log("battery charge " + chg[1] + "% + " + chg[2] + "mv's");
break;
//read battery charge
}
}
//Turn on LED's if command sent!
else if (line.indexOf("Lights on")>-1) {
console.log("do something");
LED.write(1);
LED2.write(1);
}
//Turn off LED's if command sent.
else if (line.indexOf("lights off")>-1) {
LED.write(0);
LED2.write(0);
}
else {
//Response Received
console.log("Parsed: " + line);
}}
//Serial on data v2
Serial4.on('data',function(data){
cmd += data;
var id = cmd.indexOf("\n");
while (id>=0){
var line = cmd.substr(0,id);
cmd=cmd.substr(id+1);
parse(line);
id=cmd.indexOf("\n");
}
});
// get network carier
function get_network() {
setTimeout(function(){send_command("at+cops?\r");},200);
}
//get signal strength
function get_signal() {
setTimeout(function(){send_command("at+csq\r");},200);
}
//read all texts in memory
function read_all_text() {
setTimeout(function(){send_command("at+cmgl=\"all\"\r");},200);
}
//bet battery % charge and voltage
function get_voltage() {
setTimeout(function(){send_command("at+cbc\r");},200);
}
function Send_SMS(text)
{
Start_GSM();
setTimeout(function(){send(text);},1000);
}
//Start the GSM Module
function Start_GSM() {
console.log("GSM Starting...");
pinMode(C0,"output");
digitalPulse(C0,1,2000);
setTimeout(function(){send_command("at+cmgf=1\r");},1000);
}
function Stop_GSM(){
pinMode(C0,"output");
digitalPulse(C0,1,2000);
}
function send(input){
setTimeout(function(){send_command("AT+CMGS=\"xxxxxxxxxx\"\r");},2000);
setTimeout(function(){send_command(input);},3000);
setTimeout(function(){send_command("\x1A\r");},3500);
setTimeout(function(){Stop_GSM();},10000);
}
function send_command(command){
//Set the command that we are sending to issue_command for later parse out
issued_command=command;
//Send the command to serial port connected to GSM
Serial4.write(command);
//Output what we sent to console
console.log("Command Sent:" + command);
}
function onInit(){
// send SMS when BTN pressed
setWatch(function(e) { Send_SMS("Saying hello! "); }, BTN, { repeat:true, edge:'falling', debounce:20 });
//Send_SMS("Saying hello ");
}
onInit();
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.