使用以太网卡发送Mirf值

时间:2015-03-22 15:20:24

标签: arduino

我有项目,我通过nRF24L01获取数据并使用Mirf。现在我正在为Hub工作,需要将数据发送到我的webservice。对于以太网,我的选择是带有以太网卡库的ENC28j60。

问题:如何从Mirf等待数据并使用Ethercard browseUrl向前发送数据?我可以在没有Mirf的情况下发送数据,但是有一些我不理解的循环。

我的代码:

#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
#include <EtherCard.h>

// Set network settings
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
byte Ethernet::buffer[700];
static uint32_t timer;
// My webservice
const char website[] PROGMEM = "my.webservice.com";

// Mirf variables
int tmpVal1;

// Local components
const int Yellow = 6;
const int Blue = 5;

void setup() {
  Serial.begin(57600);

  // Setup leds
  pinMode(Yellow, OUTPUT);
  digitalWrite(Yellow, LOW);
  pinMode(Blue, OUTPUT);
  digitalWrite(Blue, LOW);

  setupMirf();
  setupEthernet();
}

void loop() {

  // Waiting to get date from Mirf
  while (!Mirf.dataReady()) {
   //ether.packetLoop(ether.packetReceive());
  }

  Mirf.getData((byte *)&tmpVal1);
  Serial.print(tmpVal1);
  Serial.println(F(" C"));

  // Receive responses
  ether.packetLoop(ether.packetReceive());

  if (millis() > timer) {
    timer = millis() + 5000;
    //Serial.println();
    Serial.println("Sending data to webservice : ");
    ether.browseUrl(PSTR("/sendingdata.asmx/sendingdata?"), "Device=100&DeviceValue=80", website, my_callback);
  }
  //ShowLedNotification();
}

// called when the client request is complete
static void my_callback (byte status, word off, word len) {
  Serial.println(">>>");
  Ethernet::buffer[off+300] = 0;
  Serial.print((const char*) Ethernet::buffer + off);
  Serial.println("...");
  digitalWrite(Blue,HIGH);
  delay(200);
  digitalWrite(Blue,LOW);
}

void ShowLedNotification() {
    if (tmpVal1 > 0 ) {
    digitalWrite(Yellow, HIGH);
    delay(1000);
    digitalWrite(Yellow, LOW);
  }
  else
  {
    digitalWrite(Blue, HIGH);
    delay(1000);
    digitalWrite(Blue, LOW);
  }
}


long readVcc() {
  long result;
  // Read 1.1V reference against AVcc
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = 1126400L / result; // Back-calculate AVcc in mV
  return result;
}

//Setting up network and getting DHCP IP
void setupEthernet() {
  Serial.println(F("Setting up network and DHCP"));
  Serial.print(F("MAC: "));
  for (byte i = 0; i < 6; ++i) {
    Serial.print(mymac[i], HEX);
    if (i < 5)
      Serial.print(':');
  }
  Serial.println();

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

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

   // Check network connection
  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
  ether.printIp("SRV: ", ether.hisip);

  }

void setupMirf() {
   //Initialize nRF24
  Serial.println(F("Initializing Mirf"));
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"serv1");
  Mirf.payload = sizeof(tmpVal1);
  // we use channel 90 as it is outside of WLAN bands
  // or channels used by wireless surveillance cameras
  Mirf.channel = 90;
  Mirf.config();
  }

1 个答案:

答案 0 :(得分:0)

完成了那项工作。现在使用if子句而不是Mirf.dataReady()

void loop() {

  if (Mirf.dataReady()) {

  Mirf.getData((byte *)&tmpVal1);
  Serial.print(tmpVal1);
  Serial.println(F(" C"));
  ShowLedNotification();

  // Send data to webservice
  if (millis() > timer) {
  timer = millis() + 5000;
  Serial.println("Sending data to webservice");

  String myVarsStr = "Device=";
  myVarsStr += myDeviceID;
  myVarsStr += "&DeviceValue=";
  myVarsStr += tmpVal1;
  char myVarsCh[40];
  myVarsStr.toCharArray(myVarsCh, 40);      

  ether.browseUrl(PSTR("/receivedata.asmx/ReceiveData?"), myVarsCh, website, my_callback);
  }
}
else
{
word pos = ether.packetReceive();
word len = ether.packetLoop(pos);
delay(200);
}
}