【问题标题】:Send data to webserver from Arduino using Ethercad lib使用 Ethercad lib 从 Arduino 向网络服务器发送数据
【发布时间】:2016-02-21 20:43:33
【问题描述】:

我正在尝试使用 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.:至于现在,我正在强制 register[0],register[1] 值,如您在上面的代码中所见,但想法是稍后这些值来自外包。

【问题讨论】:

  • 我不认为 PHP 标记在这里是相关的。无论如何,首先检查 tempRegister 是否包含您认为它包含的内容。另外,尝试在循环中添加一些睡眠时间。我真的没有这方面的经验,但它可能会有所帮助。
  • 我认为 tempRegister 运行良好,因为它返回一个没有问题,但我会在它之后使用 serial.println 以确保。我还尝试在 register[i].toCharArray(tempRegister, 80) 和 ether.browserUrl(PTSR("/log.php"), tempRegister , browser_callback) 之间添加延迟(3000),但它也没有用。
  • 尝试将Serial.println(tempRegister); 添加到循环中,以便您可以看到里面的数据。您还需要更好地解释发生了什么,您说如果它循环两次,它只发送第二条数据。它会打印两次Data sent,还是只打印一次?您可能还想发布您的 PHP 脚本,并解释您是如何注意到它只发送第二个数据的。
  • 现在就去做,看看结果。不,它只返回一次“数据发送”。我的 php 脚本非常简单,它只是一个 mysqli_query($link, "INSERT INTO Log (NUMBER) VALUES ('$NUMBER')"); $NUMBER 是 $_GET['NUMBER'];我知道这是因为,例如,如果我使用 register[2] = "?NUMBER=1234, ?NUMBER=5678" 并运行程序,则只有 5678 会发送到我的数据库。
  • 刚刚测试过它,tempRegister 的工作就像一个魅力。我真的不知道为什么它只发送最新的。

标签: php arduino


【解决方案1】:

简而言之,browseurl 是异步工作的。来看看

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

【讨论】:

  • 该死的......我正在尝试修改他编写的代码之类的东西,但没有运气。这将比我想象的要困难得多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-09
  • 2012-10-03
相关资源
最近更新 更多