极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5797|回复: 1

enc28j60有访问局域网IP的办法吗

[复制链接]
发表于 2016-5-5 20:46:18 | 显示全部楼层 |阅读模式
enc28j60我现代码访问域名外网是正常,但改为局域网IP,就不行,有解决办法吗
回复

使用道具 举报

 楼主| 发表于 2016-5-5 20:47:28 | 显示全部楼层
  1. #include <EtherCard.h>
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>

  4. #define SEND_INTERVAL  30000
  5. #define TIMEOUT        5000
  6. #define ONE_WIRE_BUS   2
  7. #define STATUS_IDLE    0
  8. #define STATUS_SENT    1

  9. static byte mymac[] = {0xDD,0xDD,0xDD,0x00,0x00,0x01};
  10. byte Ethernet::buffer[700];

  11. char website[] PROGMEM = "192.168.1.18";
  12. char password[] PROGMEM = "password";

  13. OneWire oneWire(ONE_WIRE_BUS);
  14. DallasTemperature sensors(&oneWire);

  15. unsigned long previousMillis = 0;
  16. static byte session_id;
  17. byte actual_status;

  18. void setup () {

  19.   Serial.begin(57600);
  20.   Serial.println("WebTemperature demo");
  21.   Serial.println();

  22.   if (!ether.begin(sizeof Ethernet::buffer, mymac, 10)) {
  23.     Serial.println( "Failed to access Ethernet controller");
  24.     while(1);
  25.   } else Serial.println("Ethernet controller initialized");

  26.   if (!ether.dhcpSetup()) {
  27.     Serial.println("Failed to get configuration from DHCP");
  28.     while(1);
  29.   } else Serial.println("DHCP configuration done");

  30.   if (!ether.dnsLookup(website)) {
  31.     Serial.print("Unable to resolve Website IP");
  32.     while(1);
  33.   } else Serial.println("Website IP resolved");
  34.   
  35.   Serial.println();
  36.   ether.printIp("IP Address:\t", ether.myip);
  37.   ether.printIp("Netmask:\t", ether.netmask);
  38.   ether.printIp("Gateway:\t", ether.gwip);
  39.   ether.printIp("Website IP:\t", ether.hisip);
  40.   Serial.println();
  41. }
  42.   
  43. void loop() {

  44.   ether.packetLoop(ether.packetReceive());
  45.   unsigned long currentMillis = millis();
  46.   
  47.   switch(actual_status) {
  48.     case STATUS_IDLE:
  49.       if(currentMillis - previousMillis > SEND_INTERVAL) {
  50.         previousMillis = currentMillis;
  51.         sendTemperature();        
  52.       }
  53.       break;
  54.     case STATUS_SENT:
  55.       if(currentMillis - previousMillis > TIMEOUT) {
  56.         Serial.println("No response");
  57.         previousMillis = currentMillis;
  58.         actual_status = STATUS_IDLE;
  59.       }
  60.       checkResponse();
  61.   }
  62. }   
  63.   
  64. void sendTemperature() {
  65.   
  66.   sensors.requestTemperatures();
  67.   float float_temp = sensors.getTempCByIndex(0);
  68.   
  69.   char string_temp[7];
  70.   dtostrf(float_temp, 4, 2, string_temp);
  71.   
  72.   Stash stash;
  73.   byte sd = stash.create();
  74.   stash.print(string_temp);
  75.   stash.save();

  76.   Stash::prepare(PSTR("GET /demo/saveTemp.php?temp=$H&pwd=$F HTTP/1.0" "\r\n"
  77.     "Host: $F" "\r\n" "\r\n"),
  78.     sd, password, website);
  79.   session_id = ether.tcpSend();
  80.   Serial.print("Temperature ");
  81.   Serial.print(string_temp);
  82.   Serial.print(" sent to website... ");
  83.   actual_status = STATUS_SENT;
  84. }

  85. void checkResponse() {
  86.   
  87.   const char* reply = ether.tcpReply(session_id);
  88.   if(reply > 0) {
  89.     if(strstr(reply, "KO - ") != 0) Serial.println(strstr(reply, "KO - "));
  90.     else Serial.println("OK");
  91.     actual_status = STATUS_IDLE;  
  92.   }
  93. }
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-14 19:45 , Processed in 0.036724 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表