Telit Cinterion IoT Developer Community
EHS8 and GPS receiver
Tutorial, October 14, 2016 - 4:01pm, 4084 views
This article is for those working with EHS8 and want to make sure GPS is always ON.
One of the power saving features of EHS8 switches OFF the GPS after 1.5 min with no FIX
Here is the small JAVA MidLet (code snippet) which you can install to run in the background which will check for the status of your GPS receiver and keep *** running. The Midlet can run in parallel to your java code, or even if you do not use java at all.
This shows how great java is and how you can move some parts of the non-time critical code from your microcontroler to the Java Virtual Maschine.
***************************************************************************
import javax.microedition.io.CommConnection;
import javax.microedition.io.Connector;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import java.io.*;
import com.cinterion.io.*;
public class GPSService extends MIDlet {
CommConnection commConn;
InputStream inStream;
OutputStream outStream;
private ATCommand m_Cmd;
private Listener m_Listener = new Listener();
static int TIME_WAIT = 10000; //time to wait at StartUP
static int TIME_PULL = 500; //time to wait in between Query
static boolean Debug=true; //Switch for Debug PrintOUt
static String strQueryGPS = "AT^SGPSC?\r\n";
static String strLookFor="^SGPSC: \"Engine\",\"0\"\r\n"; //AT Command Response Line to look for to "AT^SGPSC?"
static String strStartGPS="AT^SGPSC=\"Engine\",\"3\"\r\n";
/**
* Implementation of ATCommandListener to receive asynchronous events
*/
private class Listener implements ATCommandListener {
public void ATEvent(String Event) {
if(Debug)
System.out.println("ATEvent " + Event);
}
public void RINGChanged(boolean SignalState) {
if(Debug)
System.out.println("RINGChanged " + SignalState);
}
public void DCDChanged(boolean SignalState) {
if(Debug)
System.out.println("DCDChanged " + SignalState);
}
public void DSRChanged(boolean SignalState) {
if(Debug)
System.out.println("DSRChanged " + SignalState);
}
public void CONNChanged(boolean SignalState) {
if(Debug)
System.out.println("CONNChanged " + SignalState);
}
}
/**
* Implementation of ATCommandResponseListener to receive asynchronous responses
*/
/*
private class RspListener implements ATCommandResponseListener {
public void ATResponse(String Response) {
if(Debug){
System.out.println("Received asynchronous response:");
System.out.println(Response);
}//end if
}//End Function
}//End Class
*/
public GPSService() {
// TODO Auto-generated constructor stub
if(Debug){
System.out.println("GPSService: Constructor");
System.out.println("Available COM-Ports: " + System.getProperty("microedition.commports"));
}
try {
String strCOM = "comm:COM0;blocking=off;baudrate=115200";
commConn = (CommConnection)Connector.open(strCOM);
if(Debug){
System.out.println("CommConnection(" + strCOM + ") opened");
System.out.println("Real baud rate: " + commConn.getBaudRate());
}
inStream = commConn.openInputStream();
outStream = commConn.openOutputStream();
if(Debug)
System.out.println("InputStream and OutputStream opened");
} catch(IOException e) {
if(Debug)
System.out.println(e);
notifyDestroyed();
}
try {
m_Cmd = new ATCommand(false);
m_Cmd.addListener(m_Listener);
} catch (Exception e) {
if(Debug)
System.out.println(e);
}
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
// TODO Auto-generated method stub
if(Debug)
System.out.println("GPSService: destroyApp(" + unconditional + ")");
try {
if(Debug)
outStream.write(("GPSService: destroyApp(" + unconditional + ")").getBytes());
inStream.close();
outStream.close();
commConn.close();
if(Debug)
System.out.println("Streams and connection closed");
} catch(IOException e) {
if(Debug)
System.out.println(e);
}
try {
m_Cmd.release();
} catch (IOException e) {
if(Debug)
System.out.println(e);
}
notifyDestroyed();
}
protected void pauseApp() {
// TODO Auto-generated method stub
if(Debug)
System.out.println("GPSService: pauseApp()");
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
if(Debug){
System.out.println("GPSService: startApp");
System.out.println("Looping back received data, leave with 'Q'...");
}
try {
if(Debug)
outStream.write(("GPSService: startApp").getBytes());
//*************************************************
//Fist give the modem Time for it's Configuration
//Time is defined by TIME_WAIT
//Pause for Receiving
Thread.sleep(TIME_WAIT);
if(Debug)
outStream.write(("GPSService: StartUP TimeOut elapsed").getBytes());
//**************************************
//Handle User input Serial Com
//handle is as a String
//String strInput="";
//int ch = -1;
//while(ch != 'Q') {
while(true) {
//ch = inStream.read();
//*********************************
//Query if GPS Engine is running
String strResponse1 = m_Cmd.send(strQueryGPS);
//****************************************
//Query if GPS Engine is not running
//if(Response.startsWith(strLookFor)){
if(strResponse1.indexOf(strLookFor)>0){
//if(Debug) outStream.write(("GPS Engine Off\r\n").getBytes());
//****************************
//Start GPS Engine
strResponse1 = m_Cmd.send(strStartGPS);
if(Debug){
if(strResponse1.indexOf("ERROR")>0){
outStream.write(("ERROR Starting GPS Engine\r\n").getBytes());
}
}//End If Debug
}
//*************************
//Timeout in between Query
Thread.sleep(TIME_PULL);
/*
//Char with int 10 == Line feed
if (ch != 10) {
strInput = strInput +(char)ch;
//System.out.print((char)ch1);
}else{
if(Debug)
System.out.println("String finished");
if (strInput!=""){
if(Debug){
//System.out.print(strInput);
//Test output to serial com
outStream.write(("Input received : " + strInput).getBytes());
System.out.println("GPSService: sending: " + strInput);
}
try {
if(Debug)
System.out.println("Sending synchronous ");
strInput= strInput +13;
if(Debug)
System.out.println(strInput);
String Response = m_Cmd.send(strInput);
if(Debug){
System.out.println("Received synchronous response:");
System.out.println(Response);
outStream.write(("--------------\n").getBytes());
outStream.write(Response.getBytes());
outStream.write(("--------------\n").getBytes());
}
}catch (Exception e) {
if(Debug)
System.out.println(e);
}
strInput="";
}//end if
}//End if
*/
}//End while
/*
}catch(IOException e) {
System.out.println(e);
*/
}catch (Exception e)
{
System.out.println(e);
}
System.out.println();
destroyApp(true);
}
}
**********************************************************************
Basic config rules are at the beggining of the code.
Code provided as example. No support on it :)
Special thanks to Sasha!
Hi!
Good work! The GPS off has been an annoying feature for some customers.
Now they can get rid of it quite easily.
This idea really shows the power of Java.
Best Regards, Antero
Antero Markkula
Communication and Mechatronics
Enkom Active Oy – www.enkom-active.fi
Upseerinkatu 3 A, 02600 Espoo, Finland
Mobile: +358 400 411368
Office: +358 10 204 0000
Fax: +358 10 204 0010
E-mail: antero.markkula@enkom-active.fi