Demo video
Source code
/* * Web Admin will let you control Arduino and get information from web, http request = GET * you must use W5100 Ethernet model. * 接法: * digital pin2:DC SWITCH * pin10:W5100 * pin11:W5100 * pin12:W5100 * pin13:W5100(SPI) * analog pin1:CT sersor * GET 命令: * b=00:get all information * b=01:DC on * b=02:DC off * b=03:AC on * b=04:AC off * b=05:change IP:192.168.1.251 * b=06:change IP:192.168.1.250 */ #include <Ethernet.h> //#include <NewSoftSerial.h> //#include <string.h> // W5100 configuration byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; byte ip[] = { 192, 168, 1, 250 }; Server server(80); // set pin 9 as RX //uint8_t ssRX = 6; // set pin 10 as TX //uint8_t ssTX = 7; // enable soft serial nss //NewSoftSerial nss(ssRX, ssTX); // initial #define id 1 // device ID #define DC_V 400 #define DC_read 0 #define DC_control 2 #define AC_V 110 #define AC_read 1 #define AC_control 3 int analog_val; // analog value read from UART void setup() { Ethernet.begin(mac, ip); server.begin(); Serial.begin(9600); //nss.begin(9600); pinMode(DC_control, OUTPUT); pinMode(AC_control, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); } void loop() { Client client = server.available(); // detect if current is the first line boolean current_line_is_first = true; // command code char command[3] = "\0"; if (client) { // an http request ends with a blank line boolean current_line_is_blank = true; while (client.connected()) { if (client.available()) { char c = client.read(); // if we've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so we can send a reply if (c == '\n' && current_line_is_blank) { // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); // java script auto reload client.println("<script language=\"javascript\">"); client.println("setTimeout(\"self.location.reload();\",4000);"); client.println("</script>"); // output the value of each digital and analog input pin for (int i = 0; i < 14; i++) { client.print("Digital Pin "); client.print(i); client.print(" is "); client.print(digitalRead(i)); client.println("<br />"); } for (int i = 0; i < 6; i++) { client.print("Analog Pin "); client.print(i); client.print(" is "); client.print(analogRead(i)); client.println("<br />"); } // AC and DC value for smart scoket /* client.print("DC VAL = "); client.print(analogRead(DC_read)); client.println("<br />"); client.print("AC VAL = "); client.print(analogRead(AC_read)); client.println("<br />"); */ client.println("<form method=get name=form>"); client.println("<button name=b value=01 type=submit>DC On</button>"); client.println("<button name=b value=02 type=submit>DC Off</button><br />"); client.println("<button name=b value=03 type=submit>AC On</button>"); client.println("<button name=b value=04 type=submit>AC Off</button>"); client.println("<button name=b value=05 type=submit>IP:251</button><br />"); client.println("<button name=b value=06 type=submit>IP:250</button>"); client.println("</form>"); break; } if (c == '\n') { // we're starting a new line current_line_is_first = false; current_line_is_blank = true; } else if (c != '\r') { // we've gotten a character on the current line current_line_is_blank = false; } // get the first http request if (current_line_is_first && c == '=') { for (int i = 0; i < 2; i++) { c = client.read(); command[i] = c; } // send all status if (!strcmp(command, "00") ) { client.print("ID="); client.print(id); client.print(","); client.print("DC_voltage"); client.print(DC_V); client.print(","); client.print("DC_val="); client.print(analogRead(DC_read)); client.print(";"); client.print("DC_control="); client.print(digitalRead(DC_control)); client.print(";"); client.print("AC_voltage"); client.print(AC_V); client.print(","); client.print("AC_val="); client.print(analogRead(AC_read)); client.print(";"); client.print("AC_control="); client.print(digitalRead(AC_control)); client.println(";"); } // DC control if (!strcmp(command, "01") ) { //Serial.println("high pin 2"); digitalWrite(DC_control, HIGH); //nss.print("A"); //nss.print(byte(0x00)); } if (!strcmp(command, "02") ) { //Serial.println("low pin 3"); digitalWrite(DC_control, LOW); //nss.print("A"); //nss.print(byte(0x02)); } // AC control if (!strcmp(command, "03") ) { //Serial.println("high pin 2"); digitalWrite(AC_control, HIGH); //nss.print("A"); //nss.print(byte(0x00)); } if (!strcmp(command, "04") ) { digitalWrite(AC_control, LOW); } // change IP if (!strcmp(command, "05") ) { byte ip[] = { 192, 168, 1, 251 }; Ethernet.begin(mac, ip); server.begin(); } if (!strcmp(command, "06") ) { byte ip[] = { 192, 168, 1, 250 }; Ethernet.begin(mac, ip); server.begin(); } } } } // give the web browser time to receive the data delay(1); client.stop(); // read from XBee /* if (nss.available()) { analog_val = nss.read(); Serial.print("I read analog_val "); Serial.println(analog_val); } */ } }
Last modified 14 years ago
Last modified on Aug 22, 2010, 3:42:41 PM