chipKIT® Development Platform

Inspired by Arduino™

Cerobot MX7 ck Ethernet

Created Fri, 27 Jan 2012 15:21:19 +0000 by andreibadilacg


andreibadilacg

Fri, 27 Jan 2012 15:21:19 +0000

I use CEREBOT mx7 ck, ethernet libraries network shield arduino. With the library, I can write HTML code, and see web page in browser. My problem is that the HTML I press a button on your browser and to drive a LED on the board. An idea? I worked on the code is below. #include <chipKITEthernet.h>

// Enter a MAC address and IP address for your controller below. // A zero MAC address means that the chipKIT MAC is to be used byte mac[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

// !!MODIFY THIS!! // The IP address will be dependent on your local network:

byte ip[] = { 192,168,137,2 }; byte gateway[] = { 192,168,137,1 };//getway calc byte subnet[] = { 255,255,255,0 }; byte dns1[] = { 0,0,0,0}; byte dns2[] = { 24,113,32,29}; // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): Server server(80);

void setup() { // start the Ethernet connection and the server: Ethernet.begin(mac, ip); //Ethernet.begin(); // this will use DHCP server.begin(); }

void loop() { int sensorValue = analogRead(0); // citesc senzorul de lumina
// map it to the range of the analog out: int outputValue = map(sensorValue, 500, 1000, 0, 255); // fac maparea outputValue = 255 - outputValue;// extrac ce lumina imi trebuie ca sa ajung la lumina ideala analogWrite(1,outputValue);// output pe led // listen for incoming clients Client client = server.available(); if (client) { // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c == '\n' && currentLineIsBlank) { // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println();

      // output the value of each analog input pin
      for (int analogChannel = 0; analogChannel &lt; 1; analogChannel++) {
        client.print("analog input ");
        client.print(analogChannel);
        client.print(" is ");
        client.print(analogRead(analogChannel));
        client.println("&lt;br /&gt;");
       

      }
      break;
    }
    if (c == '\n') {
      // you're starting a new line
      currentLineIsBlank = true;
    } 
    else if (c != '\r') {
      // you've gotten a character on the current line
      currentLineIsBlank = false;
    }
  }
}

// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();

} }


Jacob Christ

Fri, 27 Jan 2012 16:20:53 +0000

You need to parse out the post message that comes back to the board rather than just looking for the '\n' character.

Jacob


andreibadilacg

Tue, 14 Feb 2012 15:09:22 +0000

Can you be more specific? I understand the method. Tank you


Jacob Christ

Wed, 15 Feb 2012 19:47:39 +0000

Print out what comes back from this command:

char c = client.read();

to the serial port and you will see what the browser is sending to the chipKIT board.

Jacob


andreibadilacg

Sat, 18 Feb 2012 16:28:17 +0000

ok, thanks. My problem is, if change longer time to delay, browser respond conection time out. If change short time, the data appear partially. Can you help me to for a middle way. I use this for project in my school.


Jacob Christ

Fri, 30 Mar 2012 07:46:22 +0000

What are your delay values? I have about 1K web page and I changed the delay to 100. When it was 1 it was truncated as well.

I wonder how something like Apache works? It seems like there should be a way on the chipKIT web server to know that all transmitted packets have been received prior to closing the connection. Which I guess poses another question, if the receiver goes away before the whole message is received how long do you wait before closing the connection.

Jacob