arduino + enc28j60(数据上传+反向控制LED)

2013-01-07 10:28

 

 

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

采用 修改以后的ethercard 库函数(添加了一个udpreply 方法用于自动ip寻址)

etercard 修改后的库: userfiles/arduino/ethercard-lewei.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>

byte Ethernet::buffer[700];

// 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";

const int ledPin = 8;

 

 

char* on = "ON";

char* off = "OFF";

char* statusLabel;

char* buttonLabel;

char*udpReply="{"deviceName":"01","username":"laoliu1982","port":80,"api":"api","requestType":"get"}";

 

 

 

#define gPB ether.buffer 

#define UDP_DP_HIGH 36

#define UDP_DP_LOW 37

#define DSTPORT_HIHG 0x26

#define DSTPORT_LOW 0xe7

 

 

int x=1;

int sensorValue = 0;

int sensorPin = A0; 

 

 

uint32_t timer;

Stash stash;

 

void setup () {

  Serial.begin(9600);

  Serial.println(" [webClient]");

 

  pinMode(ledPin, OUTPUT);

  digitalWrite(ledPin, LOW);

  buttonLabel = "0"; 

 

   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");

}

 

 

void loop () {

 word len = ether.packetReceive();

 word pos = ether.packetLoop(len);

 

  if(len)

  {

 

      if(gPB[IP_PROTO_P]==IP_PROTO_UDP_V)

      {

 

        if((gPB[UDP_DP_HIGH]==DSTPORT_HIHG)&&(gPB[UDP_DP_LOW]==DSTPORT_LOW))

        {

          Serial.println("---Received an udp 9959 packet---");

       //  ether.sendUdp(udpReply,strlen(udpReply),60000,(gPB + IP_SRC_P),9960);

         

          ether.leweiUdpReply(udpReply,strlen(udpReply),9960);   

             

        }

      

         

      }

   

   }

  if(pos) 

  {

    Serial.print("len:");

    Serial.println(len);

    

     if(strstr((char *)Ethernet::buffer + pos, "&f=getAllSensors") != 0) 

     {

       Serial.println("Received GetAllSensor command");

       BufferFiller bfill = ether.tcpOffset();

       bfill.emit_p(PSTR("HTTP/1.0 200 OK "

        "Content-Type: text/html Pragma: no-cache "

      "{"successful":true,"message":null,"data":[{"id":"1","type":"jdq","name":"s1","value":"$S","status":"ok"}]}"),buttonLabel);

     ether.httpServerReply(bfill.position());

     }

     if(strstr((char *)Ethernet::buffer + pos, "&f=updateSensor&p1=1&p2=0") != 0) 

     {

       Serial.println("Received OFF command");

       BufferFiller bfill = ether.tcpOffset();

       bfill.emit_p(PSTR("HTTP/1.0 200 OK "

        "Content-Type: text/html Pragma: no-cache "

      "{"data":{"id":"1","type":"jdq","name":"s1","value":"0","status":"ok"},"successful":true,"message":null}"));

     ether.httpServerReply(bfill.position());

           digitalWrite(ledPin, LOW);

           buttonLabel="0";

     }  

      if(strstr((char *)Ethernet::buffer + pos, "&f=updateSensor&p1=1&p2=1") != 0) 

     {

       Serial.println("Received ON command");

       BufferFiller bfill = ether.tcpOffset();

       bfill.emit_p(PSTR("HTTP/1.0 200 OK "

        "Content-Type: text/html Pragma: no-cache "

      "{"data":{"id":"1","type":"jdq","name":"s1","value":"1","status":"ok"},"successful":true,"message":null}"));

     ether.httpServerReply(bfill.position());

           digitalWrite(ledPin, HIGH);

           buttonLabel="1";

     }     

 }

  

  if (millis() > timer) {

    //timer = millis() + 10000;

    timer = millis() + 3000;

     sensorValue = analogRead(sensorPin);   

    //Serial.println(x);

    Serial.println(sensorValue);

    // 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":"s1","Value":");

      //stash.print((word)x);

      stash.print((word)sensorValue);

         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();

  }

}