使用Ethercad lib从{Arduino发送数据到网络服务器

时间:2016-02-21 20:43:33

标签: php arduino

我正在尝试使用Ethercard库将多个数据发送到网络服务器,但出于任何原因,一次只发送一个数据。

以下是我正在使用的代码:

#include <enc28j60.h>
#include <EtherCard.h>
#include <net.h>

#define HTTP_HEADER_OFFSET 163 
#define MAX_STRINGS 100 
#define MAX_STRING_LENGTH 8 


/* Setup for Ethernet Library */
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
const char website[] PROGMEM = "www.arksecurity.net16.net"; 
const char device[] = "0004"; 
char test[MAX_STRINGS][MAX_STRING_LENGTH+1];
unsigned long timer;
unsigned long tempo = 5000;
String register[20];
unsigned int sentOK = 0;
char* localID[MAX_STRINGS];
int countRegister = 2;
//int countRegister = 1;
int countID;

byte Ethernet::buffer[700];


void setup() 
{
  Serial.begin(57600);
  register[0] = "empty";

  if (!ether.begin(sizeof Ethernet::buffer, mymac, 53))
  {
    Serial.println("Failed to access Ethernet controller");
    while(1);
  }
  else
    Serial.println("Ethernet controller initialized");
    Serial.println();

  if (!ether.dhcpSetup())
  {
    Serial.println("Failed to get configuration from DHCP");
    while(1);
  }
  else
    Serial.println("DHCP configuration done");

  if (!ether.dnsLookup(website))
  {
    Serial.println("DNS failed");
    while(1);
  }
  else 
    Serial.println("DNS resolution done"); 

  ether.printIp("SRV IP:\t", ether.hisip);
  Serial.println();

  timer = millis() - tempo;
}

void loop()
{

  char tempRegister[80];

  ether.packetLoop(ether.packetReceive());

  if (millis() - timer > tempo)
  {
    timer += tempo;
    ether.browseUrl(PSTR("/DevicesQuery.php?device="), device , website, response_callback);


    if(test[0][0] != 0)
    {
      int missing = 0;    
      Serial.println("Data Received");

      for(int i = 0; i < countID ; i++)
      {
        for(int x = 0; x < 8 ; x++)
        {
          if (test[i][x] == '\0')
          {
          missing = 1;             
          }
        }
      }

      if(missing)
      {
        Serial.println("Data Missing");
        tempo = 5000;
      }
      else
      {
        for(int g; g < countID ; g++)
        {
            register[g] = String(test[g]);   
        }
        tempo = 40000;
      }
    }    
    else{
      Serial.println("Waiting for server response...");
      tempo = 5000;
     }


    //register[0] = "?ID=1234";
    //register[1] = "?ID=5678"; 

   if(register[0] != "empty")
    {
      for(int i = 0; i < countRegister ; i++)
      { 
        register[i].toCharArray(tempRegister, 80);
        //delay(5000);
        //Serial.println(tempRegister);
        ether.browseUrl(PSTR("/log.php"), tempRegister , website, browser_callback);
        //while(sentOK == 0);    
        //sentOK = 0;
      }
      countRegister = 0;
      register[0] = "empty";
    }
  }
}


static void response_callback (byte status, word off, word len) {
    for(int i=0;i<MAX_STRINGS;i++)
        for(int j=0;j<=MAX_STRING_LENGTH;j++)
            test[i][j] = 0;

    int i_string = 0;
    int i_char = 0;
    int i_ethBuff = off + HTTP_HEADER_OFFSET;
    char carat;
    countID = 0;
    for (i_ethBuff = off + HTTP_HEADER_OFFSET; (carat = (char)Ethernet::buffer[i_ethBuff]) != 0; i_ethBuff++)
    {
        if (carat == '\n')
        { // New line char = new string
            if (i_string < MAX_STRINGS - 1){
                i_string++;
                i_char = 0;
                }
            else
                break; // Limite de memória do Arduino
        }
        else
        {
            if (i_char < MAX_STRING_LENGTH)
            {
                test[i_string][i_char] = carat;
                i_char++;
            } // otherwise discard the char (max length of string reached)
        }
    }
} 


static void browser_callback (byte status, word off, word len)
{
  Serial.println("Data sent");
  //sentOK = 1;
}

countRegister是一个变量,当它等于1时,它工作得很好,但是,如果他等于2,他只会将第二个数据发送到服务器并忽略第一个数据。谁知道为什么会这样?

PS.:至于现在,我正在强制注册[0],注册[1]值,正如你在上面的代码中看到的那样,但是想法是后来这些值来自外包。

1 个答案:

答案 0 :(得分:0)

缺点是browseurl异步工作。看看

http://forum.jeelabs.net/node/1477.html