package main;
import util.Utility;
import com.siemens.icm.io.ATCommand;
import com.siemens.icm.io.ATCommandFailedException;
import com.siemens.icm.misc.Watchdog;
public class test {
void programLoop() throws InterruptedException {
ATCommand mainCommand = null;
try {
mainCommand = new ATCommand(false);
System.out.println("at commands created");
} catch (ATCommandFailedException atcfe) {
handleError("Unable to instantiate atCommands" + atcfe);
return;
}
mainCommand.addListener(this);
// Create sensors instance
sensors = new Sensors();
String result;
int PORT1 =0;
int PORT2 =1;
int PORT3 =2;
try {
// Activate the GPIO bus
result = mainCommand.send("at^spio=1\r");
result = mainCommand.send("at^scpin=1," + PORT1 + ",1\r");
result = mainCommand.send("at^scpin=1," + PORT2 + ",1\r");
result = mainCommand.send("at^scpin=1," + PORT3 + ",1\r");
result = mainCommand.send("at^scport=" + PORT1 +"," + PORT2 + "," + PORT3 + "\r");
sensors.portNumber = sensors.parsePort(result); //Just extract the port nbr that is returned.
} catch(ATCommandFailedException atcfe) {
System.out.println("Sensors::configurePort: " + atcfe);
}
System.out.println("Port configured!");
try {
mainCommand.send("AT^SCPIN=1,8,1,1\r\n");
/* enable watchdog */
String res = mainCommand.send("AT^SCFG=\"Userware/Watchdog\", 1\r\n");
Watchdog.start(5 * 60);
String imei = mainCommand.send("at+gsn\r\n");
imei = imei.substring(9, imei.length() - 8);
System.out.println("Start OK: " + VERSION + " imei:" + imei);
} catch (ATCommandFailedException atcfe) {
handleError("Unable to start - " + atcfe);
return;
}
try {
/* Setup pulsecounter */
mainCommand.send("AT^SCCNT=1,0\r\n");
//Get the static ADC constants for calculating the real Voltage when doing the real ADC measurements.
String result = command.send("at^saadc?"+ "\r");
// result is e.g. ^SRADC: 3,8533,2,8000
/* Locate the first ',' */
int start = result.indexOf(",");
/* Locate the second ',' */
start = result.indexOf(",", start+1);
int end = result.indexOf(",", start+1);
String partString = result.substring(start+1, end);
ofs2 = Integer.parseInt(partString);
start = end+1;
end = Utility.findLastDigit(result.substring(start));
end += start;
partString = result.substring(start, end);
amp2 = Integer.parseInt(partString);
}catch(ATCommandFailedException atcfe) {
handleError("Unable to setup pulsecounter - " + atcfe);
}
while (isRunning) {
try {
/* First calculate the amount of pulses received on the pulsecounter. (To calculate windspeed)*/
//Turn on GPIO 9 to pull up the pulsecounter.
try {
result = mainCommand.send("AT^SSIO=8,1\r\n");
Thread.sleep(200); // Sleep 10 sec.
/* Reset pulsecounter and start counting */
mainCommand.send("AT^SSCNT=0\r\n");
mainCommand.send("AT+CFUN=0\r\n"); // Sleep
Thread.sleep(10000); // Sleep 10 sec.
mainCommand.send("AT+CFUN=1\r\n");
/*
* Now get the amount of pulses. Display the amount of pulses. The result
* will be received as an ATEvent
*/
/*result =*/ mainCommand.send("AT^SSCNT=3\r\n");
Thread.sleep(200); // Sleep 10 sec.
result = mainCommand.send("AT^SSIO=8,0\r\n");
/*
* Now this is where the ADC reading is done (To read the other sensors, humidity, temperature etc). The ADC2 port is connected to an analogue mux which is controlled by three of the GPIO signals (as seen above)
* when changing the value of the GPIO:s I get the ADC connected to different sensor. */
/* Read from ADC2. This is configured to contain up to 8 sensors */
System.out.print("Reading sensors: ");
for(int i = 0; i < sensors.NBR_OF_SENSORS; i++) {
if(!selectSensor(mainCommand, sensors)) {
handleError("Unable to open sensor port");
}
try {
Thread.sleep(500); //Just to make sure that the GPIO signals have had time to change and the external circuit is stable
readDataADC2(mainCommand);
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Increment sensor index
sensors.getNextSensor();
}
System.out.println();
Watchdog.kick();
}
} catch (ATCommandFailedException atcfe) {
handleError("Unable to read data - " + atcfe);
}
}
}
/**
* Read data from ADC2. Store the value from the read in the sensor associated
* with the current sensor
*/
private void readDataADC2(ATCommand command) throws ATCommandFailedException {
String result, partString;
int measuredVoltage, actualVoltage, start, end;
result = command.send("at^sradc=1\r");
if(result.indexOf("ERROR") >= 0) {
return;
}
/* result e.g. ^SRADC: 0,1,433 */
start = result.indexOf(",");
start = result.indexOf(",", start+1);
start += 1;
end = Utility.findLastDigit(result.substring(start));
end += start;
partString = result.substring(start, end);
measuredVoltage = Integer.parseInt(partString);
/* Value = (<value> from AT^SRADC - <ofs1>) * <amp1> / 4096 */
actualVoltage = (measuredVoltage - ofs2) * amp2 / 4096;
System.out.print(actualVoltage + "V ");
sensorValues.addToSensor(sensors.getCurrentSensor(), actualVoltage);
}
Hope this may help somebody, and that somebody may help me too :-)
kind regards
Thomas