极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11200|回复: 0

ENC28J60 ethercard库 xively示例收到的reply

[复制链接]
发表于 2014-11-27 21:17:52 | 显示全部楼层 |阅读模式
ethercard库里带了一个例子,是上传数据到xively的。
这个例子发送数据之后会用下面的语句读一遍。

    // send the packet - this also releases all stash buffers once done
    session = ether.tcpSend();         //发送数据
   
    const char* reply = ether.tcpReply(session);   //我的理解是读取发送完后返回的响应
   
    if (reply != 0)                      //不理解为什么是!=0,根据串口读取的数据,响应是http的报头。
     {res = 0;                           //重启计数器清零,否则如果接收不到响应就要重启enc28j60
     Serial.println(reply);          //响应是http的报头
     }

现在的问题是我不知道这句话
const char* reply = ether.tcpReply(session);
的意思。也不知道应该收到什么。
用这个示例程序可以收到reply报头。
我仿照这个例子写了一个我自己的,就没法收到任何反应。数据发送都没有问题。


下面是完整的示例,关键的地方加了自己的注释
// Simple demo for feeding some random data to Pachube.
// 2011-07-08 <[email protected]> http://opensource.org/licenses/mit-license.php

// Handle returning code and reset ethernet module if needed
// 2013-10-22 [email protected]

#include <EtherCard.h>

// change these settings to match your own setup
#define FEED "000"
#define APIKEY "xxx"

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

const char website[] PROGMEM = "api.xively.com";

byte Ethernet::buffer[350];
uint32_t timer;
Stash stash;
byte session;

//timing variable
int res = 0;         //设置重新启动计数器



void setup () {
  Serial.begin(57600);
  Serial.println("\n[Xively example]");

  //Initialize Ethernet
  initialize_ethernet();
}


void loop () {
  
  //if correct answer is not received then re-initialize ethernet module
  if (res > 220){                        //如果超过220就重启enc28j60
    initialize_ethernet();
  }
  
  res = res + 1;
  
  ether.packetLoop(ether.packetReceive());
  
  //200 res = 10 seconds (50ms each res)
  if (res == 200) {                                                 //200的时候发送随机数,我就把我自己的数据放在这里了

   
    //Generate random info
    float demo = random(0,500);
    word one = random(0,500);
    String msje;

    if (demo < 250){
      msje = "low";
    }
    else{
      msje = "high";
    }


    // generate two fake values as payload - by using a separate stash,
    // we can determine the size of the generated message ahead of time
    byte sd = stash.create();
    stash.print("demo,");
    stash.println(demo);
    stash.print("one,");
    stash.println(one);
    stash.print("mensaje,");
    stash.println(msje);
    stash.save();

    //Display data to be sent
    Serial.println(demo);
    Serial.println(one);


    // generate the header with payload - note that the stash size is used,
    // and that a "stash descriptor" is passed in as argument using "$H"
    Stash::prepare(PSTR("PUT http://$F/v2/feeds/$F.csv HTTP/1.0" "\r\n"
      "Host: $F" "\r\n"
      "X-PachubeApiKey: $F" "\r\n"
      "Content-Length: $D" "\r\n"
      "\r\n"
      "$H"),
    website, PSTR(FEED), website, PSTR(APIKEY), stash.size(), sd);

    // send the packet - this also releases all stash buffers once done
    session = ether.tcpSend();                                                               //发送数据
  }
  
   const char* reply = ether.tcpReply(session);                                       //我理解应该是读取反馈
   
   if (reply != 0) {                                                                                 //如果收到
     res = 0;                                                                                         //重启计数器归零
     Serial.println(reply);                                                                        //这个reply是http报头,可是我改写的程序就收不到reply.
   }
   delay(50);
}



void initialize_ethernet(void){  
  for(;;){ // keep trying until you succeed
    //Reinitialize ethernet module
    pinMode(5, OUTPUT);
    Serial.println("Reseting Ethernet...");
    digitalWrite(5, LOW);
    delay(1000);
    digitalWrite(5, HIGH);
    delay(500);

    if (ether.begin(sizeof Ethernet::buffer, mymac) == 0){
      Serial.println( "Failed to access Ethernet controller");
      continue;
    }
   
    if (!ether.dhcpSetup()){
      Serial.println("DHCP failed");
      continue;
    }

    ether.printIp("IP:  ", ether.myip);
    ether.printIp("GW:  ", ether.gwip);  
    ether.printIp("DNS: ", ether.dnsip);  

    if (!ether.dnsLookup(website))
      Serial.println("DNS failed");

    ether.printIp("SRV: ", ether.hisip);

    //reset init value
    res = 0;
    break;
  }
}
[/code]
回复

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-16 15:09 , Processed in 0.041469 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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