Internet Connectivity and Embedded Systems
by Al Williams


Figure 3:

      sendString("@TDM1");
      if (waitStatus(90)!='C' ||
          waitStatus(30)!='P' ||
          waitStatus(60)!='H' ||
          waitStatus(30)!='M')
            System.out.println("Can't connect!");
     System.out.println("Done");


Listing One
import stamp.core.*;
import stamp.peripheral.sensor.temperature.DS1620;

/** Handler for iModem, @version 1.0 -- @author Al Williams  */
public class iModemDDJ {
  final static int SERIAL_RX_PIN = CPU.pin0;
  final static int SERIAL_TX_PIN = CPU.pin1;

  static Uart rxUart = new Uart( Uart.dirReceive, SERIAL_RX_PIN, 
                          Uart.dontInvert, Uart.speed2400, Uart.stop1 );
  static Uart txUart = new Uart( Uart.dirTransmit, SERIAL_TX_PIN, 
                          Uart.dontInvert, Uart.speed2400, Uart.stop1 );
static StringBuffer t=new StringBuffer(81);
static Timer waittimer = new Timer();

public static void sendString(String s) {
   for (int i=0;i<s.length();i++) {
     txUart.sendByte(s.charAt(i));
     CPU.delay(500);     // modem needs delay between characters
     }
   txUart.sendByte('\r');
   waitStatus(5);   // read echoed line
 }
 public static boolean waitOK() {
   return waitStatus(2)=='O';
   }
  public static char waitStatus(int timeout) {
  Timer to=waittimer;
   char cr='\0',c=' ';
   to.mark();
   do {
     if (rxUart.byteAvailable()) {
       c=(char)rxUart.receiveByte();
       System.out.print(c);
       if (c!='\r' && c!='\n' && cr=='\0') cr=c;  // save first character
       }
     } while ((cr=='\0' || c!='\n') && !to.timeoutSec(timeout));
     if (c!='\n') System.out.println("Timeout!");
  return cr;
  }
public static boolean findModem() {
  sendString("");
  sendString("AT");
  if (!waitOK()) {
    System.out.println("Can't find modem");
    return false;
    }
  return true;
  }
  public static void main() {
  // careful... the iModem takes a few seconds to "wake up"
   Timer clock=new Timer();
   Timer level0timer = new Timer();
   DS1620 sensor=null;
   int cnt=-1;
   int temp;
   int i;
   sensor=new DS1620(CPU.pin4,CPU.pin5,CPU.pin6);
   // let's try to see if the modem is awake and clear
   clock.mark();
   out:
    do {
     txUart.sendByte('\r');
     do {
       if (rxUart.byteAvailable() && rxUart.receiveByte()=='\r') break out;
       }  while (!clock.timeout(4000));
    } while (true);
  mainloop:
    while (true) {
     if (rxUart.byteAvailable()) System.out.print((char)rxUart.receiveByte());
     if (cnt!=-1 && !clock.timeoutSec(30)) continue;  // only check every 30s
     clock.mark();
     cnt++;
     if (cnt%10!=0) continue;  // only every 5 minutes
     cnt=0; // stop future roll over
     temp=sensor.getTempF();
     if (temp<75) continue;  // only report if >= 75F
     t.clear();
     t.append(Integer.toString(temp));
     if (!findModem()) continue; // no modem
     do
       sendString("@TS1=Temp Report");
     while (!waitOK());
     do
       sendString("@TA1=alw@al-williams.com");
     while (!waitOK());
     do {
       sendString("@TM1=Greetings from Javelin!");
       CPU.delay(100);  // pause for message line processing
       sendString("The temperature is ");
       CPU.delay(100);
       sendString(t.toString());
       CPU.delay(100);
       sendString(".");  // end of message
     } while (!waitOK());
     CPU.delay(1000);
     sendString("@TDM1");
     if (waitStatus(90)!='C' ||
         waitStatus(30)!='P' ||
         waitStatus(60)!='H' ||
         waitStatus(30)!='M')
           System.out.println("Can't connect!");
    System.out.println("Done");
    }
  }
}






3


