Go Down

Topic: Easy ethernet lookup macro's (Read 411 times) previous topic - next topic

qistoph

For maintainability and easier usage of the Ethernet library, I present to you, the hostToIp macro's.

I have added the, what I've called, DnsPreprocessor.
Just before compiling the DnsPreprocessor handles two macro's
- hostToIpString, and
- hostToIpBytes.

The DnsPreprocessor looks up the ip address of the given host and returns either a string, or a byte[].

An example:
Code: [Select]

char google[] =  hostToIpString("www.google.com");
byte googleB[] = hostToIpBytes("www.google.com");

void setup() {
 Serial.begin(9600);
 Serial.print("String: ");
 Serial.println(google);
 
 Client client(googleB, 80);
}


To use this, you'll have to download the Arduino IDE source.
Add the DnsPreprocessor to app/src/processing/app/preproc
Modify Sketch.java as described below.
And compile the Arduino IDE.

In Sketch.java in preprocess(String buildPath, PdePreprocessor preprocessor) change this
Code: [Select]
   StringBuffer bigCode = new StringBuffer();
   int bigCount = 0;
   for (SketchCode sc : code) {
     if (sc.isExtension("pde")) {
       sc.setPreprocOffset(bigCount);
       bigCode.append(sc.getProgram());
       bigCode.append('\n');
       bigCount += sc.getLineCount();
//        if (sc != code[0]) {
//          sc.setPreprocName(null);  // don't compile me
//        }
     }
   }

   /*
   String program = code[0].getProgram();
   StringBuffer bigCode = new StringBuffer(program);
   int bigCount = code[0].getLineCount();
   bigCode.append('\n');


To this
Code: [Select]
   StringBuffer bigCode = new StringBuffer();
   int bigCount = 0;
   for (SketchCode sc : code) {
     if (sc.isExtension("pde")) {
       sc.setPreprocOffset(bigCount);
       bigCode.append(sc.getProgram());
       bigCode.append('\n');
       bigCount += sc.getLineCount();
//        if (sc != code[0]) {
//          sc.setPreprocName(null);  // don't compile me
//        }
     }
   }

[glow]      try {
         bigCode = new DnsPreprocessor().process(bigCode.toString());
     } catch(java.net.UnknownHostException uhe) {
           uhe.printStackTrace();
           String msg = "Failed to lookup hostname";
           throw new RunnerException(msg);
     }[/glow]

   /*
   String program = code[0].getProgram();
   StringBuffer bigCode = new StringBuffer(program);
   int bigCount = code[0].getLineCount();
   bigCode.append('\n');


Just for clarity; the ip address are looked up at compile time. So the ip address and not the hostname is what ends up in the compiled sketch.

Any suggestions are more than welcome.

Go Up