HTTP GET MALFORMED COMMAD AFTER 5 SEND (ARDUINO+ETHERNET)

GET /dataino.it/api/add_value.php?api-KEY=41uX-u50e-84d4-8f0u-kP0s&Temp=22.75 HTTP/1.0" 200 6 "-" "-"
GET /dataino.it/api/add_value.php?api-KEY=41uX-u50e-84d4-8f0u-kP0s&Temp=22.75 HTTP/1.0" 200 6 "-" "-"
GET /dataino.it/api/add_value.php?api-KEY=41uX-u50e-84d4-8f0u-kP0s&Temp=22.75 HTTP/1.0" 200 6 "-" "-"
GET /dataino.it/api/add_value.php?api-KEY=41uX-u50e-84d4-8f0u-kP0s&Temp=22.75 HTTP/1.0" 200 6 "-" "-"
GET /dataino.it/api/add_value.php?api-K\xc4\x06=41uX-u50e-84d4-8f0u-kP0s&Temp=22.75 HTTP/1.0" 200 983 "-" "-"

why ??????????
programm hang !!!

for test online

IPAddress server(94,23,64,3); //dataino.it
String API_key = "68bS-4p6r-r8FR-3W0X-bZ68";

DS18b20Dataino.ino (9.7 KB)

Looks like out of memory, or memory corruption.

What arduino model are you using?

Would you please post your code inside code tags? More people will look at it if you do.

Thanks,

-br

Whether or not this is your bug, it's not a good idea to do this much string concatenation on an embedded system, because memory fragmentation can cause silent failures.

    command = cmd_add + API_key + "&" + var[0] + val0 + "\0";
    command.toCharArray(pageAdd, 128);

You'd be better off building the command using C char arrays, and ditching the use of String altogether.

-br

Try F() on all strings. Also don't use the String class.

the model is arduino UNO

#include <Ethernet.h>
#include <SPI.h>
// DS18B20
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
// Assign the addresses of your 1-Wire temp sensors.
DeviceAddress insideThermometer = { 
  0x28, 0x67, 0xE7, 0x47, 0x04, 0x00, 0x00, 0x2B };

//dataino.it
// remember to chang mac!!!
// this must be unique
byte mac[] = {
  0x90,0xA2,0xDA,0x0D,0x82,0x39};
// change to your network settings
IPAddress ip(192,168,6,91);
IPAddress gateway(192, 168, 6, 90);
IPAddress subnet(255, 255, 255, 0);

// LOCAL HOST SERVER
//IPAddress server(192,168,6,90); //localhost
//String API_key = "41uX-u50e-84d4-8f0u-kP0s";

//~ ONLINE SERVER !!!
IPAddress server(94,23,64,3); //dataino.it
String API_key = "68bS-4p6r-r8FR-3W0X-bZ68";

String cmd_find = "/dataino.it/api/find_value.php?api-KEY=";
String cmd_add = "/dataino.it/api/add_value.php?api-KEY=";
String cmd_read = "/dataino.it/api/read_value.php?api-KEY=";
String cmd_delete = "/dataino.it/api/delete_value.php?api-KEY=";
String cmd_set = "/dataino.it/api/set_value.php?api-KEY=";
String command = "";
String var[]={
  "Temp="};
int totalCount = 0;
int loopCount = 0;
char pageAdd[128];
EthernetClient client;
// S E T U P
void setup(void)
{
  // start serial port
  Serial.begin(9600);
  
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(insideThermometer, 10);
    
  // Start ethernet
  Serial.println(F("Starting ethernet..."));
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  // If using dhcp, comment out the line above 
  // and uncomment the next 2 lines

  // if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  // else Serial.println(F("ok"));
  //digitalWrite(10,HIGH);

  Serial.println(Ethernet.localIP());

  delay(2000);
  Serial.println(F("Ready"));
}
// PRINT TEMP
void printTemperature(DeviceAddress deviceAddress)
{
// DS18B20
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } 
  else {
    Serial.print("C: ");
    Serial.print(tempC);
    Serial.print(" F: ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
    // dataino.it
    String val0= ftoa(tempC,2,5);
    
    command = cmd_add + API_key + "&" + var[0] + val0 + "\0";
    command.toCharArray(pageAdd, 128);
    
  }
}
// DS18B20
void loop(void)
{
  if(loopCount < 50)
  {
    // if loopCount is less than 50, just delay a 100 millisecond
    delay(100);
    Serial.print(".");
  }
  else
  {
    // every tree seconds this runs
    loopCount = 0;
    Serial.print("\n\r");
    // DS18B20
    Serial.print("Getting temperatures...\n\r");
    sensors.requestTemperatures();
    Serial.print("Inside temperature is: ");
    printTemperature(insideThermometer);
    Serial.print("\n\r");

    //================================================================================
    if(!getPage(server,pageAdd)) Serial.print(F("Fail "));
    else Serial.print(F("Pass "));
    totalCount++;
    Serial.println(totalCount,DEC);
  }    

  loopCount++;

}
//===============================================================================
// dataino.it
//===============================================================================
byte getPage(IPAddress ipBuf,char *page)

{
  int inChar;
  char outBuf[128];
  Serial.print(F("connecting..."));
  if(client.connect(ipBuf,80))
  {
    Serial.println(F("connected"));
    sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
    client.write(outBuf);
    Serial.println(F("outBuf"));
  } 
  else
  {
    Serial.println(F("failed"));
    return 0;
  }

  // connectLoop controls the hardware fail timeout

  int connectLoop = 0;
  //Unsigned long variables are extended size variables for number storage, 
  //and store 32 bits (4 bytes). Unlike standard longs unsigned longs 
  //won't store negative numbers, making their range from 0 to 4,294,967,295
  long VAL[16];
  while(client.connected())
  {
    while(client.available())
    {
      
      inChar = client.read();

      if (inChar==char('#'))
      {
        inChar = client.read();
        
        while (inChar!='#'){
          int count_val =0 ;
          unsigned long i = 1;
          VAL[count_val]=0; 
          
          do {
            VAL[count_val] = VAL[count_val] + (inChar-48) * i;
            i=i*10;
            inChar = client.read();
          }
          while ((inChar!=char(';')));
          
         inChar = client.read();
          //Serial.print(inChar);
          
          if (inChar==char('*')){ 
            Serial.print("Query=");
            Serial.print(VAL[count_val]);
            break;             
          }
          count_val=count_val+1;
          
        }
        
        
      }
      
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }
    connectLoop++;
  }
  // if more than 10000 milliseconds since the last packet
  if(connectLoop > 10000)
  {
    // then close the connection from this end.
    Serial.println();
    Serial.println(F("Timeout"));
    client.stop();
  }
  // this is a delay for the connectLoop timing
  delay(1);


  Serial.println();

  Serial.println(F("disconnecting."));
  // close client end
  client.stop();

  return 1;
}

String ftoa(float number, uint8_t precision, uint8_t size) {


  String s = "";

  // Negative 
  if (number < 0.0)  {
    s = "-";
    number = -number;
  }

  double rounding = 0.5;
  for (uint8_t i = 0; i < precision; ++i)    rounding /= 10.0;

  number += rounding;
  s += String(uint16_t(number));  // prints the integer part

  if(precision > 0) {
    s += ".";                // prints the decimal point
    uint32_t frac;
    uint32_t mult = 1;
    uint8_t padding = precision -1;
    while(precision--)     mult *= 10;

    frac = (number - uint16_t(number)) * mult;

    uint32_t frac1 = frac;
    while(frac1 /= 10)    padding--;
    while(padding--)      s += "0";
    s += String(frac,DEC) ;  // prints the fractional part
  }
  if (size>0)                // checks size
    if (s.length()>size)        return("#");
    else while(s.length()<size) s = " "+s;
  return s;
}

There are 6 more strings that can use F(). Once you get those, tell us if the problem is gone. UNO only has 2KB memory so the more you put in FLASH with F() the more you have left to run your program.

RESOLVED thx

add F()

convert in char

char API_key[] = "41uX-u50e-84d4-8f0u-kP0s";
char cmd_find[] = "find_value";
char cmd_add[] = "add_value";
char cmd_read[] = "read_value";
char cmd_delete[] = "delete_value";
char cmd_set[] = "set_value";
char var[] = "&Temp=";
char pageAdd[128];

and use

sprintf(pageAdd ,"/dataino.it/api/%s.php?api-KEY=%s%s%s" ,cmd_add, API_key, var,VALUE);

to form get command

one question about convert

floa >to> string >to> char

Serial.print(DallasTemperature::toFahrenheit(tempC));
//===============================================================================
// dataino.it
//===============================================================================
String val = ftoa(tempC,2,5);
char VALUE[10];
val.toCharArray(VALUE,10);

is the best way?

Hahaha, yes it is best to use C-string when you run programs on small embedded systems such as Arduino. If you take the code and run to Raspberry Pi system, you might indulge yourself with dynamic memory management again :slight_smile:

wath about the conversion of the float temp in strng and than in char is correct ?

String val = ftoa(tempC,2,5);
char VALUE[10];
val.toCharArray(VALUE,10);

maremosso68:
wath about the conversion of the float temp in strng and than in char is correct ?

String val = ftoa(tempC,2,5);
char VALUE[10];
val.toCharArray(VALUE,10);

You could use the dtostrf since you are going to avoid String class:

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga060c998e77fb5fc0d3168b3ce8771d42

Fantastic !!!

//dtostrf(FLOAT,WIDTH,PRECSISION,BUFFER);
char buffer[10];
dtostrf(tempC, 5, 2, buffer);

+3k of memory free now

thx again

please register to my site www.dataino.it and give me a feedback

No problem. There are lots of little tricks, which makes me think I can write a chapter of a book over the summer. :slight_smile: