极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

楼主: 弘毅

arduino学习笔记26 - ENC28J60以太网模块实验

  [复制链接]
发表于 2012-8-31 05:20:32 | 显示全部楼层
这个库已经很久没人更新了,现在更新比较快的是ethercard库。目前最新最全的enc28j60的库,支持最新arduino ide。和原ethernet库不冲突。
回复 支持 反对

使用道具 举报

发表于 2012-8-31 07:34:14 | 显示全部楼层
参考一下张老师的《第二弹》?那里面做的比较成功
回复 支持 反对

使用道具 举报

发表于 2012-8-31 14:24:56 | 显示全部楼层
刚买的ENC28J60小卡,在新中发地下层,用EncEthernet库能用,但只有一个Examples中只有一个程序可试,成功,楼主这个一直未能成功。
回复 支持 反对

使用道具 举报

发表于 2012-8-31 15:19:19 | 显示全部楼层
JUST_DO_IT 发表于 2012-8-30 22:11
我这个已经做成功,但想传到yeelink上面去,能帮我看看代码吗/*
* Web Server
*

http://geek-workshop.com/thread-1747-1-1.html

参考上面的帖子代码,是张老师做的硬件
回复 支持 反对

使用道具 举报

发表于 2012-8-31 15:19:21 | 显示全部楼层
JUST_DO_IT 发表于 2012-8-30 22:11
我这个已经做成功,但想传到yeelink上面去,能帮我看看代码吗/*
* Web Server
*

http://geek-workshop.com/thread-1747-1-1.html

参考上面的帖子代码,是张老师做的硬件
回复 支持 反对

使用道具 举报

发表于 2012-8-31 17:39:32 | 显示全部楼层
但还是不行
代码:
  1. /*
  2. Yeelink sensor client
  3. by zcb
  4. 2012.07.17
  5. */
  6. #include <EtherCard.h>
  7. #include <OneWire.h>

  8. //
  9. //18B20
  10. #define ONEWIRE_PIN 7

  11. OneWire ds(ONEWIRE_PIN);
  12. ///////////////////////////////////////////////////////////
  13. ///////////////////////////////////////////////////////////
  14. char SensorID[] PROGMEM = "626"; // replace your sensor ID
  15. const uint8_t MyMac[6] = {
  16.   0x74,0x69,0x69,0x2D,0x32,0x06};
  17. const uint8_t MyIP[4] = {
  18.   222,16,228,228 };
  19. ////////////////////////////////////////////////////////////

  20. // for yeelink api
  21. char APIKey[] PROGMEM = "5c137bc47b1fe0d3d7854509d7783c78"; // replace your yeelink api key here
  22. char DeviceID[] PROGMEM = "500"; / replace your device ID


  23. // 分配一个MAC IP网关的以太网控制器的DNS assign a MAC IP gateway dns for the ethernet controller.

  24. const uint8_t MyGateway[4] = {
  25.   222,16,228,15 };
  26. const uint8_t yeelinkIP[4] = {
  27.   202,136,60,231};


  28. const uint16_t PostingInterval = 15000;// 2个数据点之间的延迟时间delay between 2 datapoints, 30s

  29. // 一些全局变量Some global variables
  30. char sensorData[20];
  31. uint8_t dataLength;
  32. uint8_t Ethernet::buffer[300];
  33. uint32_t lastConnectionTime = 0;          //你最后一次连接到服务器,以毫秒为单位 last time you connected to the server, in milliseconds

  34. Stash stash;



  35. void setup() {
  36.   Serial.begin(4800);
  37.   //启动串行端口 start serial port:
  38.   // 启动以太网连接start the Ethernet connection:
  39.   ether.begin(sizeof Ethernet::buffer, MyMac);
  40.   //
  41.   ether.staticSetup(MyIP,MyGateway);

  42. }

  43. void loop() {
  44.    Serial.print("yeelink:");
  45.    Serial.println(get_Current_Temp());
  46.    
  47.   ether.packetLoop(ether.packetReceive());
  48.   if(millis() - lastConnectionTime > PostingInterval){
  49.     get_Send_String();   
  50.     send_Data();
  51.    
  52.   }

  53. }

  54. void send_Data() {
  55.   // 创建一个发表yeelink服务器Create a Post for yeelink server,
  56.   // 发送请求保存会话IDand send request saving sessionID
  57.   Stash::prepare(PSTR("POST /v1.0/device/$F/sensor/$F/datapoints HTTP/1.1" "\r\n"
  58.     "Host: api.yeelink.net" "\r\n"
  59.     "U-ApiKey: $F" "\r\n"
  60.     "Content-Length: $D" "\r\n"
  61.     "Content-Type: application/x-www-form-urlencoded" "\r\n" "\r\n"                       
  62.     "$S"),
  63.   DeviceID,SensorID,APIKey,dataLength,sensorData);
  64.   ether.copyIp(ether.hisip, yeelinkIP);
  65.   ether.tcpSend();
  66.   lastConnectionTime = millis();
  67. }
  68. ///////////////////////////////////////////////////////////////////////////
  69. //从温度传感器中获取数据 get data from temperature sensor
  70. // 您的传感器,你可以替换此代码you can replace this code for your sensor
  71. void get_Send_String(){
  72.   uint16_t Tc_100 = get_Current_Temp();
  73.   uint8_t i,whole, fract;
  74.   whole = Tc_100/10 ;  // 分开的整数和小数部分separate off the whole and fractional portions
  75.   fract = Tc_100 % 10;
  76.   dataLength = sprintf(sensorData,"{"value":%d.%d}",whole,fract);  
  77. }

  78. uint16_t get_Current_Temp(){ //0.1C
  79.   //returns the temperature from one DS18S20 in DEG Celsius
  80.   byte data[12];
  81.   byte addr[8];
  82.   if ( !ds.search(addr)) {
  83.     //no more sensors on chain, reset search
  84.     ds.reset_search();
  85.     return 0;
  86.   }
  87.   if ( OneWire::crc8( addr, 7) != addr[7]) {
  88.     return 1000;
  89.   }
  90.   if ( addr[0] != 0x28) {
  91.     return 1000;
  92.   }
  93.   ds.reset();
  94.   ds.select(addr);
  95.   ds.write(0x44,1); // start conversion, with parasite power on at the end
  96.   delay(1000);
  97.   ds.reset();
  98.   ds.select(addr);  
  99.   ds.write(0xBE); // Read Scratchpad

  100.   for (int i = 0; i < 9; i++) { // we need 9 bytes
  101.     data[i] = ds.read();
  102.   }
  103.   ds.reset_search();
  104.   uint16_t tempRead = (data[1] << 8) | data[0]; //using two's compliment
  105.   return tempRead*10/16;
  106. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2012-9-6 22:33:40 | 显示全部楼层
有着片子的51板,不知能不能改
回复 支持 反对

使用道具 举报

发表于 2012-9-25 20:20:02 | 显示全部楼层
楼主你好~我入了一块28j60,想用他和arduino通信,比如结合labview来控制舵机,应该怎么去实现?
现在看用的都是ethercard库,基本上没注释看不懂,也不知道该怎么调用啊
回复 支持 反对

使用道具 举报

发表于 2012-9-30 15:10:57 | 显示全部楼层
不知道为什么   不管按照大哥你的程序还是zcbzjx的方法  我这边都不行.... 难道是我的ENC28J60有问题? 串口返回时一串乱码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-9-30 17:50:20 | 显示全部楼层
茕兔 发表于 2012-9-30 15:10
不知道为什么   不管按照大哥你的程序还是zcbzjx的方法  我这边都不行.... 难道是我的ENC28J60有问题? 串口 ...

调整串口波特率试试,把那几个波特率都试一次。因为有时候因为电脑原因,会导致不是所有波特率都能用
回复 支持 反对

使用道具 举报

发表于 2012-10-5 02:00:56 | 显示全部楼层
太好了,卖家一直没有提供库,害的我自己编写,累死...
回复 支持 反对

使用道具 举报

发表于 2012-10-11 21:10:24 | 显示全部楼层
救命,程序烧进去了,可是,打开那个IP网页确不成功,求解答
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-10-12 08:08:07 | 显示全部楼层
hk386 发表于 2012-10-11 21:10
救命,程序烧进去了,可是,打开那个IP网页确不成功,求解答

这个库有点老了,看张老师写的新版本的库的介绍吧,内容全很多。。。
http://www.geek-workshop.com/thread-2049-1-1.html
回复 支持 反对

使用道具 举报

发表于 2012-10-15 09:38:44 | 显示全部楼层
yeguiren773 发表于 2011-12-24 12:31
**** 作者被禁止或删除 内容自动屏蔽 ****

能否给出nec28j60的电路图,我有这个芯片,想跳线试试
回复 支持 反对

使用道具 举报

发表于 2013-3-24 22:36:22 | 显示全部楼层
本帖最后由 洛克王国专用号 于 2013-3-24 22:39 编辑

网页无法访问http://192.168.1.123,怎么也弄不通?求解答,路由器和ping 截图如下:


代码:
/*
* Web Server
*
* A simple web server that shows the value of the analog input pins.
*/

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 123 };

Server server(80);

void setup()
{
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  Client client = server.available();
  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();
         
          // output the value of each analog input pin
          for (int i = 0; i < 6; i++) {
            client.print("analog input ");
            client.print(i);
            client.print(" is ");
            client.print(analogRead(i));
            client.println("<br />");
          }
          break;
        }
        if (c == '\n') {
          // we're starting a new line
          current_line_is_blank = true;
        } else if (c != '\r') {
          // we've gotten a character on the current line
          current_line_is_blank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    client.stop();
  }
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 21:12 , Processed in 0.049933 second(s), 26 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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