Strange output, but why and how to solve it

Hello,

i have an arduino nano with a network shield.
when i compile the program, i get a warning about stability .
but that is not the problem i think.

my program, setup the network card, read the one wire temperature sensor and output it to a server.
the first sensor is read good, but the second sensor is always 0.

i know it is connected good, because when i load one of the examples sketches for the dallas temp sensor it shows, 2 sensors and displays the correct values.

What could be wrong, please need some help on how to find out what is wrong.

// Demo using DHCP and DNS to perform a web client request.
// 2011-06-08 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php
#include <EtherCard.h>
#include <OneWire.h>
#include <DallasTemperature.h>
char payload[90];

void(* resetFunc) (void) = 0; //declare reset function @ address 0

// ethernet interface mac address, must be unique on the LAN
// and other stuff used for the network interface
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[700];

char website[] PROGMEM = "82.72.232.147";//destination website

// Stuff used for one wire sensors
// Data wire is plugged into pin 7 on the Arduino
#define ONE_WIRE_BUS 7


// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// Some random stuff used in this program
static uint32_t timer;
byte wd_counter;

// called when the client request is complete
static void my_callback (byte status, word off, word len) {
  Ethernet::buffer[off+300] = 0;
  // get_reply_data gets the data part of the reply and puts it in the line_buf char a
  
  // Controleer of de data juist is ontvangen.
  if (strcmp((const char*) Ethernet::buffer,"HTTP/1.1 200 OK"))
  {
    wd_counter=0;
    Serial.println(" recieved ok.");
  } else
  {
    Serial.println(" !!! ERROR !!!.");
    Serial.println(">>>");
    Serial.print((const char*) Ethernet::buffer+off);
    Serial.println("<<<");
  }
}

//Setup the board
void setup () {

  
  //Start serial port
  Serial.begin(9600);
  Serial.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  Serial.println("\nNANO EMONCMS Temperatuur sender\n\n\n");
  
  //Start ethernet port
  if (ether.begin(sizeof Ethernet::buffer, mymac,10) == 0) 
    Serial.println( "Failed to access Ethernet controller");
  //Get own ip adress
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");
  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  
  //Look ip destination website
  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
  ether.printIp("SRV: ", ether.hisip);

  // Startup one wire sensors
  sensors.begin();
}


// Main program
void loop () {


  ether.packetLoop(ether.packetReceive());
  
  if (wd_counter>=10) {
    Serial.println("Resetting device");
    resetFunc();
  }
  

  
  
  if (millis() > timer) {
    wd_counter++;
    timer = millis() + 25000;
    Serial.println(" ");
    Serial.print("Attempt number ");
    Serial.print(wd_counter);
    Serial.print(" of 10, data send to emoncms: ");
    
    sensors.requestTemperatures();
        
    sprintf(payload,"{Temp_buiten_camping:%d,Temp_binnen_camping:%d}",round(sensors.getTempCByIndex(1)*10),round(sensors.getTempCByIndex(0)*10));
    Serial.print(payload);
    ether.browseUrl(PSTR("/emoncms/input/post.json?apikey=913f557706d069745bfe8dac70040261&json="), payload, website, my_callback);

  }
}

and of course the output from arduino.

Sketch uses 17.258 bytes (56%) of program storage space. Maximum is 30.720 bytes.
Global variables use 1.565 bytes (76%) of dynamic memory, leaving 483 bytes for local variables. Maximum is 2.048 bytes.
Weinig geheugen beschikbaar, er kunnen zich stabiliteitsproblemen voordoen

There is nothing wrong, it only warns that the sketch uses most of the memory so there is little room for runtime allocation of memory (especially function calls) and local variables.

You can save a lot of RAM by using the F() macro. Find all your string constants, and put them in an F() macro..

So instead of

    Serial.println(" recieved ok.");

you would use

    Serial.println( F(" recieved ok.") );

But would that solve my problem of not having the second temperature value.
is this becaus of low ram ?

I'd be inclined to try testing it with a sketch that just has what it needs to compile this:

    sensors.requestTemperatures();
        
    sprintf(payload,"{Temp_buiten_camping:%d,Temp_binnen_camping:%d}",round(sensors.getTempCByIndex(1)*10),round(sensors.getTempCByIndex(0)*10));
    Serial.print(payload);

what do you mean wild bill ?

I'm suggesting that you might want to prove to yourself that that code reads the sensors as you expect. There's no need for all the ethernet stuff until you can. I'm assuming that the serial output is showing zero though - is it?

yep serial.print is sending 0.

now i altered the program a bit, i made 2 integer temp1 and temp2.

and made this at the bottom of the sketch.

    sensors.requestTemperatures();
    temp1 = round(sensors.getTempCByIndex(0)*10);
    temp2 = round(sensors.getTempCByIndex(1)*10);      
    
    sprintf(payload,"{Temp_buiten_camping:%d,Temp_binnen_camping:%d}",temp1,temp2);
    Serial.print(payload);
    ether.browseUrl(PSTR("/emoncms/input/post.json?apikey=913f557706d069745bfe8dac70040261&json="), payload, website, my_callback);

round returns a float. Your sprintf is expecting an int. Make those temp variables int instead. Don't try to fix it with %f, sprintf doesn't support it. You may have to use dtostrf to get around this.

round returns a float.

"C:\Users\pjs9486\Documents\arduino-1.0.5\hardware\arduino\cores\arduino\Arduino.h"
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))

Doesn't look to me like it "returns" a float.

PaulS:
Doesn't look to me like it "returns" a float.

Apparently not - odd, masking the gcc round. Solution now then would appear to be to use %ld rather than %d.

ageurtse:
yep serial.print is sending 0.

now i altered the program a bit, i made 2 integer temp1 and temp2.

and made this at the bottom of the sketch.

And are you going to tell us if it worked or not?

You misunderstand wildbill. He is telling you that you should make a new sketch that ONLY reads the sensors and ONLY prints them to Serial. Tacking something onto the end of the sketch will not help you solve your problem if it happens to be a memory shortage.

But would that solve my problem of not having the second temperature value.
is this becaus of low ram ?

You won't know until you make some changes and do some testing. It is entirely possible that you are runningout of RAM.When you run out of RAM, storing to variables can clobber other variables.

Well,

i did test it with a new sketch see my first post

i know it is connected good, because when i load one of the examples sketches for the dallas temp sensor it shows, 2 sensors and displays the correct values.

after altering my sketch with the temp1 and temp2 variabels set.
it now run's oke.

But why it didn't run in the first time i don't know.
for now i'm happy that my sketch is up and running.

ageurtse:
But why it didn't run in the first time i don't know.
for now i'm happy that my sketch is up and running.

I would still recommend cutting down on the RAM usage. You are using about 75% of available RAM, and that only leaves 25% for local variables and such. You have a LOT of string constants that could be put into F() macros.

yes, i know i now placed the string constants between F().

leaving me with these output readings

Sketch uses 17.372 bytes (56%) of program storage space. Maximum is 30.720 bytes.
Global variables use 1.297 bytes (63%) of dynamic memory, leaving 751 bytes for local variables. Maximum is 2.048 bytes.

and this is the final sketch, some more things to do but so far so good.

// Demo using DHCP and DNS to perform a web client request.
// 2011-06-08 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php
#include <EtherCard.h>
#include <OneWire.h>
#include <DallasTemperature.h>
char payload[50];

void(* resetFunc) (void) = 0; //declare reset function @ address 0

// ethernet interface mac address, must be unique on the LAN
// and other stuff used for the network interface
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[700];

char website[] PROGMEM = "82.72.232.147";//destination website

// Stuff used for one wire sensors
// Data wire is plugged into pin 7 on the Arduino
#define ONE_WIRE_BUS 7


// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
  int temp1;
  int temp2;

// Some random stuff used in this program
static uint32_t timer;
byte wd_counter;

// called when the client request is complete
static void my_callback (byte status, word off, word len) {
  Ethernet::buffer[off+300] = 0;
  // get_reply_data gets the data part of the reply and puts it in the line_buf char a
  
  // Controleer of de data juist is ontvangen.
  if (strcmp((const char*) Ethernet::buffer,"HTTP/1.1 200 OK"))
  {
    wd_counter=0;
    Serial.println( F(" recieved ok."));
  } else
  {
    Serial.println( F(" !!! ERROR !!!."));
    Serial.println( F(">>>"));
    Serial.print((const char*) Ethernet::buffer+off);
    Serial.println( F("<<<"));
  }
}

//Setup the board
void setup () {

  
  //Start serial port
  Serial.begin(9600);
  Serial.println( F("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"));
  Serial.println( F("\nNANO EMONCMS Temperatuur sender\n\n\n"));
  
  //Start ethernet port
  if (ether.begin(sizeof Ethernet::buffer, mymac,10) == 0) 
    Serial.println(  F("Failed to access Ethernet controller"));
  //Get own ip adress
  if (!ether.dhcpSetup())
    Serial.println( F("DHCP failed"));
  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  
  //Look ip destination website
  if (!ether.dnsLookup(website))
    Serial.println( F("DNS failed"));
  ether.printIp("SRV: ", ether.hisip);

  // Startup one wire sensors
  sensors.begin();
}


// Main program
void loop () {


  ether.packetLoop(ether.packetReceive());
  
  if (wd_counter>=10) {
    Serial.println( F("Resetting device"));
    resetFunc();
  }
  

  
  
  if (millis() > timer) {
    wd_counter++;
    timer = millis() + 25000;
    Serial.println(" ");
    Serial.print( F("Attempt number "));
    Serial.print(wd_counter);
    Serial.print( F(" of 10, data send to emoncms: "));
    
    sensors.requestTemperatures();
    temp1 = round(sensors.getTempCByIndex(0)*10);
    temp2 = round(sensors.getTempCByIndex(1)*10);      
    
    sprintf(payload,"{Temp_buiten_camping:%d,Temp_binnen_camping:%d}",temp1,temp2);
    Serial.print(payload);
    ether.browseUrl(PSTR("/emoncms/input/post.json?apikey=913f557706d069745bfe8dac70040261&json="), payload, website, my_callback);

  }
}
char payload[50];

It's not very likely, but you could be writing 55 characters to this array.