You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • Hi,

    About the console.log - it won't hurt to use it when running off a battery - you just won't see anything. I guess you could define a function like:

    function log(type, message) {
      if (type=="good") digitalWrite(LED2,1); // green
      if (type=="bad") digitalWrite(LED1,1); // red
      console.log(">"+message);
    }
    
    log("good", "It worked!");
    log("", "It's ok...");
    log("bad", "It's smoking");
    

    You could also try calling a callback for the response message, rather than using intervals...

    I've added a timeout so it won't just break if something goes wrong, but I'm not handling it very well at the moment :)

    var cmd="";
    var responseText = "OK";
    var responseCallback = undefined;
     
    //Function handles data coming in from Serial.
    Serial4.on('data',function (data) {
      cmd+=data;
      // maybe we should be looking for newline instead?
      if(cmd.indexOf(responseText) > 0){
        console.log(cmd);
        if (responseCallback) responseCallback();
        responseCallback = undefined;
        cmd="";
      }
    });
     
     
    //Function handles sending AT commands to GSM device.
    function Send_Command(command,response,time,calle­r){
      if (responseCallback) {
        console.log("we were already doing something!");
        return;
      }
      
      // if we didn't get anything within 'time' ms, 
      var timeout = setTimeout(function(){    
        console.log("Timeout for "+command);
        caller(false);
      },time);
      responseText = response;
      responseCallback = function() {
       // if we actually did want to repeatedly send the command, do it here
       clearInterval(timeout); // clear the timeout
       caller(true);
      };
      // send command
      Serial4.write(command);
    }
     
     
    //Start the GSM Module
    function Start_GSM(callback) {
      console.log("GSM Starting...");
      pinMode(C0,"output");
      digitalPulse(C0,1,2000);
      // this will send the AT command right after C0 has raised.
      // Not sure it's what you want? setTimeout might be better here
      Send_Command("AT\r\n","OK",500,function(­) {
        console.log("GSM Started.");
        callback();
      });
    }
    
    //Send the text message
    function Send_SMS(phoneNumber,message, callback){
     console.log("Setting SMS Mode..");
      Send_Command("AT+CMGF=1\r\n","OK",1000,f­unction(){
        answer=">";
        console.log("Setting Phone Number.."); 
        Send_Command('AT+CMGS=\""+phoneNumber+"\­"\r',">",1000,function(){
          console.log("Send Message"); 
          Send_Command(message+"\x1A\r","OK",3000,­function() {
           console.log("Message Sent"); 
           callback();
          });
        });
      });
    }
    
    // You can then chain the callbacks together to use the library
    Start_GSM(function() {
      Send_SMS("+441234567890","Hello There", function() {
         Send_SMS("+441234567890","Hello There Again", function() {
          Stop_GSM(function() {
             console.log("phew...");
          });
         });
       });
    });
    
    
About

Avatar for Gordon @Gordon started