arduino + enc28j60(数据上传)-不推荐

2013-01-07 10:21

感谢 @zcbzjx 在  http://www.geek-workshop.com/forum.php?mod=viewthread&tid=2260 提供的资料

采用 ethercard 库函数 userfiles/arduino/ethercard.zip

 

 // Simple demo for feeding some random data to Pachube.

// 2011-07-08 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php
 
#include <EtherCard.h>
 
// change these settings to match your own setup
#define FEED    "5942"
#define APIKEY  "4c2a67e84aac4dbfb3e41821ca57a7c8"
 
// ethernet interface mac address, must be unique on the LAN
byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
 
char website[] PROGMEM = "open.lewei50.com";
 
byte Ethernet::buffer[700];
uint32_t timer;
Stash stash;
 
void setup () {
  Serial.begin(9600);
  Serial.println(" [webClient]");
 
   if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
     Serial.println( "Failed to access Ethernet controller");
  else
    Serial.println("Ethernet controller initialized");
 
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");
 
  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);
    Serial.println(" webClient setup end");
}
int x=1;
void loop () {
  ether.packetLoop(ether.packetReceive());
  
  if (millis() > timer) {
    timer = millis() + 10000;
    Serial.println(x);
    // generate two fake values as payload - by using a separate stash,
    // we can determine the size of the generated message ahead of time
    x=x+1;
    byte sd = stash.create();
      stash.print("[{"Name":"T1","Value":");
      stash.print((word)x);
       stash.println("}]");
 
    stash.save();
    
    // 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("POST /api/V1/Gateway/UpdateSensors/01 HTTP/1.1 " 
                      "Host: open.lewei50.com " 
                      "Content-Length: $D " 
                      "userkey: $F " 
                      " " 
                      "$H"),stash.size(), PSTR(APIKEY),sd);
              
    // send the packet - this also releases all stash buffers once done
    ether.tcpSend();
  }
}