Example of code which receives SMS, input changes and blink RG led
[js]
var sms = require('sms');
var sys = require('system');
var gpio = require('gpio');
var File = require('file');
sys.println("Program is starting!");
//file access
var file = new File('a:/program.js');
file.openForRead();
sys.println(file.readByte());
//read file, max 5000B
var text = file.readString(5000);
sys.println("text length:"+(text.length));
sys.println(text);
sys.println("bytes available:"+file.available());
file.close();
//incoming SMS - event from modem
sms.smsReceive = function(from, text, date){
sys.println("received sms:"+from+" "+text+" "+date.toUTCString());
}
//IO change - event from modem
gpio.inputChanged = function(id,value){
sys.println("pin:"+id+" value:"+value);
//set output 1 to value from input
gpio.set(1,value);
}
sys.releasemem();
sys.println("free mem:"+sys.freemem());
//object blinker - control RG LED
var Blikac = function(interval){
this.interval=interval;
this.run = function(){
while(true){
gpio.set(8,1);
gpio.set(7,0);
sys.sleep(this.interval);
gpio.set(8,0);
gpio.set(7,1);
sys.sleep(this.interval);
}
};
//method for interval changing
this.setInterval = function(interval){
this.interval=interval;
};
};
//GPIO settings
var pins = [gpio.INPUT,gpio.OUTPUT,gpio.UNUSED,gpio.UNUSED,gpio.UNUSED,gpio.UNUSED,gpio.UNUSED,gpio.OUTPUT,gpio.OUTPUT,gpio.UNUSED];
gpio.init(pins);
//new blinker with 2s interval
var blik = new Blikac(2000);
//run blinker as new thread
sys.runAsThread("blik",blik);
//sys.runAsThread("blik",function(){ while(true){ .. another blinking solution... }});
//timers test
// one time
sys.setTimeout(function(){
sys.println("tik")
},2000);
//forever
sys.setTimeout(function(){
sys.println("tik2")
},2000,0);
//repeat 5x
sys.setTimeout(function(){
sys.println("tik3")
},1000,5);
//main thread
sys.println("main thread is done");
//all sms or input changes will be processed forever, only main thread is done
[/js]