/* Web client This sketch connects to a website (http://www.google.com) using an Arduino Wiznet Ethernet shield. Circuit: * Ethernet shield attached to pins 10, 11, 12, 13 created 18 Dec 2009 by David A. Mellis modified 9 Apr 2012 by Tom Igoe, based on work by Adrian McEwen */ #include <SPI.h> #include <Ethernet.h> #include <LiquidCrystal.h> #define ResetPIN 7 // 初始化引脚 LiquidCrystal lcd(9, 8, 5, 4, 3, 6); // Enter a MAC address for your controller below. // Newer Ethernet shields have a MAC address printed on a sticker on the shield byte mac[] = { 0xDE, 0xAA, 0xCE, 0xEA, 0xFF, 0xE1 }; // if you don‘t want to use DNS (and reduce your sketch size) // use the numeric IP instead of the name for the server: //IPAddress server(74,125,232,128); // numeric IP for Google (no DNS) char server[] = "192.168.9.6"; // name address for Google (using DNS) String clientPwd="-1"; int ReadFlag=0; // Set the static IP address to use if the DHCP fails to assign IPAddress ip(172,16,0,222); IPAddress gateway(172,16,0,1); IPAddress dns_server(60,191,134,196); IPAddress subnet(255,255,0,0); // Initialize the Ethernet client library // with the IP address and port of the server // that you want to connect to (port 80 is default for HTTP): EthernetClient client; int errCount=0; void setup() { //LCD // 设置行列值 lcd.begin(16, 2); pinMode(A0,INPUT); pinMode(A1,OUTPUT); digitalWrite(A1,LOW); pinMode(ResetPIN,OUTPUT); digitalWrite(ResetPIN,LOW); // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } // start the Ethernet connection: // give the Ethernet shield a second to initialize: if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); Ethernet.begin(mac, ip,dns_server,gateway,subnet); } delay(1000); Serial.println("Ready"); } void loop() { ReadFlag= digitalRead(A0); //analogRead(A0); digitalWrite(A1,ReadFlag); Serial.println(ReadFlag); Serial.println(clientPwd); // 打印字符串 lcd.setCursor(0,0); lcd.print(clientPwd); httpRequest(); delay(1000); } //================Http Request Code======================== void httpRequest(){ // if you get a connection, report back via serial: if(!client.connected()){ Serial.println("disconnecting."); client.stop(); if (client.connect(server, 9988)) { Serial.println("connected"); }else{ // kf you didn‘t get a connection to the server: Serial.println("connection failed"); errCount++; if(errCount>=3){ digitalWrite(ResetPIN,HIGH); delay(1000); } } } else{ // Make a HTTP request: client.println("GET /main.ashx?m=c&mil=" + String(millis()) + "&cpwd="+clientPwd+"&rf="+String(ReadFlag) +" HTTP/1.1"); client.println("Host:" +String(server)); //client.println("Connection: close"); client.println(); delay(10); String reply=""; // if there are incoming bytes available // from the server, read them and print them: while (client.available()) { char c = client.read(); reply+=c; } //Serial.print(reply); int index=reply.indexOf("\"Model\":\"8888"); if(index>=0){ index=index+9; clientPwd= reply.substring(index,index+8); Serial.println("Changed:" + clientPwd); } errCount=0; //wdt_reset(); } } //================End Http Request Code===============
原文:http://www.cnblogs.com/wdfrog/p/4886887.html