Go Down

Topic: Problem sending data multiple times to webserver using Ethercard (Read 105 times) previous topic - next topic

Hasmodye

Hello all!

This is the first time I'm posting here and I really need help to solve this problem as quickly as possible.I'm trying to send some data to my online database using and php script( which is just a simple INSERT INTO with $_GET) but when I try to send multiple data(countRegister > 1) it simple doesn't send anything at all or just the last one. When countRegister = 1 it works fine.

Here is the code I'm using for this:

Code: [Select]

#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;

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;
}



What I've already tried:
  • Add delay(5000); before calling ether.browseUrl
  • Add Serial.println(tempRegister); to see if it is working properly(it is)


Do you guys have any idea how to solve this?

PaulS

Quote
Here is the part of the code I'm using for this
Part of the answer is to quit using Strings.

Another part is to stop wasting our time with snippets. For all we can tell with that code, you are running out of the bounds of one or more of your arrays.


Hasmodye

Part of the answer is to quit using Strings.

Another part is to stop wasting our time with snippets. For all we can tell with that code, you are running out of the bounds of one or more of your arrays.


Sorry about that, I thought that this part would be enough. As for right now, I'm forcing register[0],register[1]  values as you can see in the code above(now it is the entire code) but the idea is that later this values come from an outsource.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy