这是因为<EtherCard.h>在以太网层,总是先向网关发数据,我的方法是内网服务器和网关设置成同一IP-
- #include <EtherCard.h>
- #include <EEPROM.h>
- #include <avr/wdt.h>
- #define REQUEST_RATE 5000 // milliseconds
- static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
- static byte myip[] = { 192,168,10,100 };
- static byte gwip[] = { 192,168,10,100 };
- static byte dnsip[] = { 192,168,10,100};
-
- // remote website name
- char website[] PROGMEM = "192.168.10.100";
- static byte hisip[] = { 192,168,10,100};
- char path[] PROGMEM = "/thcpu/uploaddata.jsp";
- char header[] PROGMEM = "U-ApiKey: a";
- static char andataBuf[80];
- char* zone = "A1";
- byte Ethernet::buffer[800]; // a very small tcp/ip buffer is enough here
- static long timer;
- static int errcount = 0; //
- static int okcount = 0; //
-
- // called when the client request is complete
-
- void Reboot()
- {
- wdt_disable() ;
- wdt_enable(WDTO_15MS);
- while(1)
- {
- }
- }
-
- void setup () {
- Serial.begin(115200);
- Serial.println("WebClient linjingmi");
- getconfig();
- if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
- Serial.println( "Failed to access Ethernet controller");
- ether.staticSetup(myip, gwip,dnsip);
- ether.copyIp(ether.hisip,hisip );
- ether.hisport = 8080;
- ether.printIp("IP Address:\t", ether.myip);
- ether.printIp("Gateway:\t", ether.gwip);
-
- timer = - REQUEST_RATE+2000; // start timing out right away
- //ether.persistTcpConnection(true); // if notset, only 512bytes return in callback
- initDns();
- //while (ether.clientWaitingGw()) ether.packetLoop(ether.packetReceive());
-
- wdt_enable(WDTO_8S);
-
-
- }
- void loop () {
- wdt_reset();
- ether.packetLoop(ether.packetReceive());
-
- if (millis() > timer + REQUEST_RATE) {
- if (millis() > 3600000) {Reboot();}
- if (errcount > 10) {Reboot();}
- timer = millis();
- send2server();
- }
- }
- void send2server() {
- Serial.println("\n>>> REQ");
- sprintf(andataBuf,"zone=%s&signal=%dA%dB%dC%dD%dE%dF%dG%d",zone,analogRead(0),analogRead(1),analogRead(2),analogRead(3),analogRead(4),analogRead(5),analogRead(6),analogRead(7));
- ether.httpPost (path, website,header ,andataBuf, my_result_cb);
- Serial.println(andataBuf);
- Serial.println("wait");
- errcount=errcount+1;
- }
- static void my_result_cb (byte status, word off, word len) {
- errcount=0;
- //okcount=okcount+1;
- Serial.print("<<< reply ");
- Serial.print(millis() - timer);
- Serial.println(" ms");
- Serial.println((const char*) Ethernet::buffer + off);
- /*
- if(strstr((char *)Ethernet::buffer + off, "zoneuploaddataisok") != 0) {
- Serial.println("Received ok command");
- }
- else Serial.println("Received no ok command");
- */
- }
-
- //dns提速
- void initDns(){
- long timer2;
- //waitting status
- timer2 = millis();
- while (!ether.isLinkUp() ) {
- if( millis() - timer2 < 10000 )
- ether.packetLoop(ether.packetReceive());
- else break;
- }
- timer2 = millis();
- while( ether.clientWaitingGw() ){
- if( millis() - timer2 < 10000 )
- ether.packetLoop(ether.packetReceive());
- else break;
- }
- }
-
- void getconfig() {
- int deviceID=EEPROM.read(0);
- if (deviceID <= 10) {
- Serial.print( "READ EEPROMINFO:");
- Serial.println( deviceID, DEC);
- myip[3] = myip[3] +deviceID;
- mymac[5] = mymac[5] +deviceID;
- }
- switch (deviceID) {
- case 1:
- zone="A1";
- break;
- case 2:
- zone="A2";
- break;
- case 3:
- zone="B2";
- break;
- case 4:
- zone="B11";
- break;
- case 5:
- zone="B12";
- break;
- default:
- zone="A1";
- }
-
-
- }
复制代码 |