So after a few tweets back and forth with @Gordon regarding a GSM library I thought I would first make a small attempt at producing something which will send a SMS and then build from there.
Little did I know I have naff all javascript skills so began the learning process. After a few hours I have ended up with the following code:
var cmd="";
var global_response=0;
var answer="OK";
//Function handles data coming in from Serial.
Serial4.on('data',function (data) {
if(cmd.indexOf(answer) > 0){
console.log(cmd);
cmd="";
global_response=1;
}
else
{
cmd+=data;
}
});
//Function handles sending AT commands to GSM device.
function Send_Command(command,time,caller){
var AT = setInterval(function (f) { Serial4.write(command); }, time);
var timey = setInterval(function(){
if(global_response==1){
clearInterval(AT);
global_response=0;
clearInterval(timey);
caller();
}
},300);
}
//Start the GSM Module
function Start_Gsm() {
answer="OK";
console.log("GSM Starting..");
pinMode(C0,"output");
digitalPulse(C0,1,2000);
Send_Command("AT\r\n",500,function(){Set_SMS_Mode();
});
}
function Stop_GSM() {
pinMode(C0,"output");
digitalPulse(C0,1,2000);
console.log("GSM Stopped...");
}
//Set the SMS Mode
function Set_SMS_Mode() {
answer="OK";
console.log("Setting SMS Mode..");
Send_Command("AT+CMGF=1\r\n",1000,function(){Set_SMS_Number();});
}
//Set the SMS number to send text message to
function Set_SMS_Number() {
answer=">";
console.log("Setting Phone Number..");
Send_Command('AT+CMGS=\"INSERT A NUMBER HERE IN 44 for uk format\"\r',1000,function(){Send_SMS();});
}
//Send the text message
function Send_SMS(){
console.log("Send Message");
answer="OK";
Send_Command("testing\x1A\r",3000,function(){Stop_GSM();});
}
//Set up the Serial port for the GSM
Serial4.setup(9600,{rx:C11,tx:C10});
Start_Gsm();
I'm not going to overly explain what is going on as you can all read but the stop and start GSM functions are specific to the GSM2 Click board which is based on the M95 GSM chip. Everything else should hopefully be quite generic.
A few things I know need improving which I would love some help on:
Puppies have probably died due to my poor use of callbacks. I'm not used to them but Gordon had suggested making some changes to utilise chaining.
The Send Command function currently sets a timeout to wait for a response, there are probably much better ways to do this.
The response I define within the Send Command function is given when I do the call, there may be cases where the response received is not the one expected. Once again probably much better ways to do this.
Currently using console.log to view what is going on when I run this code, which assumes you are using USB. Obviously I have to remove this when running on the espruino from battery without USB to laptop which is a bit of a pain. Any better ways to show status? Maybe the Led blinks?
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.
So after a few tweets back and forth with @Gordon regarding a GSM library I thought I would first make a small attempt at producing something which will send a SMS and then build from there.
Little did I know I have naff all javascript skills so began the learning process. After a few hours I have ended up with the following code:
https://gist.github.com/Hardware-Hacks/2fffb92cb3b277585cd9#file-gistfile1-js
I'm not going to overly explain what is going on as you can all read but the stop and start GSM functions are specific to the GSM2 Click board which is based on the M95 GSM chip. Everything else should hopefully be quite generic.
A few things I know need improving which I would love some help on:
Any help points/leg work much appreciated =]