Go Down

Topic: Arduino Uno code is being ignored (Read 255 times) previous topic - next topic

Delta_G

#15
May 28, 2015, 06:28 pm Last Edit: May 28, 2015, 06:28 pm by Delta_G
you're running short of RAM.
The compiler is telling you that your global variables use a certain percentage of your RAM and it all looks OK.  But you are using the String class and also using the + operator from it a whole bunch.  Each time you use the + operator on a String it has to reallocate memory for the larger char array that the String class is hiding.  The result is often fragmented memory in the heap.  The String class is well known for not being very friendly in the memory usage department.

The first thing I would try would be to get rid of the String class entirely, make one char array big enough to hold the whole of the largest message, and read up on using sprintf to assemble the message.
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

bkenobi

Ok, I can give that a look.  Is there a way to print available memory to confirm your theory?  I had an issue with a pro mini that I was writing at the wrong clock speed accidentally and writing the CPU speed sussed the issue.

PaulS

Quote
Is there a way to print available memory to confirm your theory?
http://playground.arduino.cc/Code/AvailableMemory

bkenobi

#18
May 28, 2015, 07:11 pm Last Edit: May 28, 2015, 07:11 pm by bkenobi
I should have searched before posting.  I was just looking at that page and think one of the included codes should help.  Looks like the 6th option (freeRam) is quite compact so I'll try it first.

bkenobi

#19
May 29, 2015, 07:51 pm Last Edit: May 29, 2015, 07:57 pm by bkenobi
It appears that the memory may not be the issue (or I'm not looking at the data correctly).  I added the freeRam function and printed the it's output before and after calling the MQTTreport functions.  It returned the same value (~600-800 depending on the code at the time).

I modified the code (as attached) so that the MQTTreport function now returns a char array that is constructed using sprintf.  It seems to use approximately the same memory after the function call.  I assume that if I put the freeRam call inside the MQTTreport function when the "packet" variable still exists, free memory would be lower.

Either way, I have made changes such that the MQTT broker now sees the communication from the Arduino.  There is an issue with wind speed being reported as blank when it's zero, but I'm guessing this is some kind of formatting issue.  The wind speed sometimes reports 0 the first time but is blank every other time.

Code: [Select]
pi@raspberrypi ~ $ mosquitto_sub -h localhost -v -t '#'
ookDecoder online
blueline TotalEnergy=0, CurrentPower=1576, TempF=0, Battery=0
acurite5n1 Windspeed=, Winddir=112.50, Rainfall=0.00, TempF=55.80, Humidity=96, Battery=1
blueline TotalEnergy=0, CurrentPower=1576, TempF=0, Battery=0
acurite5n1 Windspeed=, Winddir=112.50, Rainfall=0.00, TempF=55.80, Humidity=96, Battery=1

PaulS

Code: [Select]
      char str_windspeedkph[6];
      dtostrf(windspeedkph,4,2,str_windspeedkph);
      sprintf(packet,"Windspeedkph=%s",str_windspeedkph);
      Serial.println(packet);

If the wind speed is 3.5 kph, what do you think str_windspeedkph is going to contain? If it is 10.6?

bkenobi

#21
May 29, 2015, 11:28 pm Last Edit: May 29, 2015, 11:46 pm by bkenobi
I'm sorry, I don't understand the question.  But, if the wind speed was reported as 3.5 kph, then windspeedkph should hold (float)3.5.  Then I would expect the value of str_windspeedkph to be "3.50".  If the value in windspeedkph was 10.6, I would expect str_windspeedkph to be "10.60".  When I run the code and watch the serial window, I see str_windspeedkph reported correctly so it appears that this code works:
Code: [Select]
char str_windspeedkph[6];
dtostrf(windspeedkph,4,2,str_windspeedkph);
sprintf(packet,"Windspeedkph=%s",str_windspeedkph);
Serial.println(packet);

The next line in the serial window should be the windspeed in mph but it the output is blank.  I only see "Windspeed=".  So this code appears to be an issue:
Code: [Select]
dtostrf(convKphMph(windspeedkph),4,2,str_winds);
...
sprintf(packet,"Windspeed=%s",str_winds);
Serial.println(packet);



NOTE:  I copied the serial window output to a file but did not copy it to my USB stick this morning so I don't have it with me.

PaulS

Quote
If the value in windspeedkph was 10.6, I would expect str_windspeedkph to be "10.60"
How can you expect that? You said that the field width was 4. "10.60" has a width of 5. At least, the way I count it does.

bkenobi

The documentation I read indicated that sprintf had 4 parameters.

  • float input
  • minimum length of output string (0.00 = 4)
  • precision (number of digits after decimal)
  • output variable


The issue seems to be something else though.  This is the output of the serial window:
Code: [Select]
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=0.00
765
Windspeed=0.00, Winddir=0.00, Rainfall=0.00, TempF=0.00, Humidity=0, Battery=0
Windspeed=0.00, Winddir=0.00, Rainfall=0.00, TempF=0.00, Humidity=0, Battery=0
connction failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=0.00
713
Windspeed=0.00, Winddir=0.00, Rainfall=0.00, TempF=56.80, Humidity=81, Battery=1
Windspeed=0.00, Winddir=0.00, Rainfall=0.00, TempF=56.80, Humidity=81, Battery=1
connction failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=0.00
713
Windspeed=0.00, Winddir=0.00, Rainfall=0.00, TempF=56.80, Humidity=81, Battery=1
Windspeed=0.00, Winddir=0.00, Rainfall=0.00, TempF=56.80, Humidity=81, Battery=1
connction failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=0.00
713
Windspeed=0.00, Winddir=0.00, Rainfall=0.00, TempF=56.80, Humidity=81, Battery=1
Windspeed=0.00, Winddir=0.00, Rainfall=0.00, TempF=56.80, Humidity=81, Battery=1
connction failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=
713
Windspeed=, Winddir=112.50, Rainfall=0.00, TempF=56.80, Humidity=81, Battery=1
Windspeed=, Winddir=112.50, Rainfall=0.00, TempF=56.80, Humidity=81, Battery=1
connction failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=
713
Windspeed=, Winddir=112.50, Rainfall=0.00, TempF=56.80, Humidity=81, Battery=1
Windspeed=, Winddir=112.50, Rainfall=0.00, TempF=56.80, Humidity=81, Battery=1
connction failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=
713
Windspeed=, Winddir=112.50, Rainfall=0.00, TempF=56.80, Humidity=81, Battery=1
Windspeed=, Winddir=112.50, Rainfall=0.00, TempF=56.80, Humidity=81, Battery=1
connction failed


Generated by the following code:
Code: [Select]
    char* MQTTreport_c (void) {
      char packet[100];
      float windspeed = convKphMph(windspeedkph);
      char str_temp[6];
      char str_winds[6];
      char str_windd[6];
      char str_rain[6];
     
      dtostrf(tempf,4,2,str_temp);
      dtostrf(windspeed,4,2,str_winds);
      dtostrf(winddir,4,2,str_windd);
      dtostrf(rainfall,4,2,str_rain);
      /*
      char str_windspeedkph[6];
      dtostrf(windspeedkph,4,2,str_windspeedkph);
      sprintf(packet,"Windspeedkph=%s",str_windspeedkph);
      Serial.println(packet);
      */
      Serial.print("Windspeed=");
      Serial.println(windspeed);
     
      sprintf(packet,"Windspeed=%s",str_winds);
      Serial.println(packet);
     
      Serial.println(freeRam());

      sprintf(packet,"Windspeed=%s, Winddir=%s, Rainfall=%s, TempF=%s, Humidity=%u, Battery=%u",
        str_winds, str_windd, str_rain, str_temp, humidity, batteryok);
     
      Serial.println(packet);
      return packet;
    }


In this run, I did not have the Uno hooked up to the network which is why it indicates "connction failed".  The integer reported is the output from freeRam at the end of the acurite5n1.MQTTreport_c function.  The interesting thing is that the first "windspeed=" statement (output from a println of the float value) always reports correct.  The second "windspeed=" is output from the sprintf line starts correct but breaks at some point.  However, the windspeed is always 0.00.

bkenobi

I tried changing the sprintf format to a length of 6.  The result is the same:
Code: [Select]
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=2.17
Windspeed=  2.17
711
Windspeed=  2.17, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=56, Battery=1
Windspeed=  2.17, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=56, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=2.17
Windspeed=  2.17
711
Windspeed=  2.17, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=56, Battery=1
Windspeed=  2.17, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=56, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=2.17
Windspeed=  2.17
711
Windspeed=  2.17, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=56, Battery=1
Windspeed=  2.17, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=56, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=2.17
Windspeed=  2.17
711
Windspeed=  2.17, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=56, Battery=1
Windspeed=  2.17, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=56, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=2.17
Windspeed=  2.17
711
Windspeed=  2.17, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=56, Battery=1
Windspeed=  2.17, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=56, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=2.17
Windspeed=  2.17
711
Windspeed=  2.17, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=56, Battery=1
Windspeed=  2.17, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=56, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=2.17
Windspeed=  2.17
711
Windspeed=  2.17, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=56, Battery=1
Windspeed=  2.17, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=56, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=  0.00
711
Windspeed=  0.00, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=57, Battery=1
Windspeed=  0.00, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=57, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=  0.00
711
Windspeed=  0.00, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=57, Battery=1
Windspeed=  0.00, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=57, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=  0.00
711
Windspeed=  0.00, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=57, Battery=1
Windspeed=  0.00, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=57, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=  0.00
711
Windspeed=  0.00, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=57, Battery=1
Windspeed=  0.00, Winddir=0.00, Rainfall=0.00, TempF=, Humidity=57, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=  0.00
711
Windspeed=  0.00, Winddir=67.50, Rainfall=0.00, TempF=, Humidity=57, Battery=1
Windspeed=  0.00, Winddir=67.50, Rainfall=0.00, TempF=, Humidity=57, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=  0.00
711
Windspeed=  0.00, Winddir=67.50, Rainfall=0.00, TempF=, Humidity=57, Battery=1
Windspeed=  0.00, Winddir=67.50, Rainfall=0.00, TempF=, Humidity=57, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=  0.00
711
Windspeed=  0.00, Winddir=67.50, Rainfall=0.00, TempF=, Humidity=57, Battery=1
Windspeed=  0.00, Winddir=67.50, Rainfall=0.00, TempF=, Humidity=57, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=  0.00
711
Windspeed=  0.00, Winddir=67.50, Rainfall=0.00, TempF=, Humidity=58, Battery=1
Windspeed=  0.00, Winddir=67.50, Rainfall=0.00, TempF=, Humidity=58, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=  0.00
711
Windspeed=  0.00, Winddir=67.50, Rainfall=0.00, TempF=, Humidity=58, Battery=1
Windspeed=  0.00, Winddir=67.50, Rainfall=0.00, TempF=, Humidity=58, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=  0.00
711
Windspeed=  0.00, Winddir=67.50, Rainfall=0.00, TempF=, Humidity=58, Battery=1
Windspeed=  0.00, Winddir=67.50, Rainfall=0.00, TempF=, Humidity=58, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=  0.00
711
Windspeed=  0.00, Winddir=67.50, Rainfall=0.00, TempF=, Humidity=58, Battery=1
Windspeed=  0.00, Winddir=67.50, Rainfall=0.00, TempF=, Humidity=58, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=
711
Windspeed=, Winddir=112.50, Rainfall=0.00, TempF=, Humidity=58, Battery=1
Windspeed=, Winddir=112.50, Rainfall=0.00, TempF=, Humidity=58, Battery=1
connection failed
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
TotalEnergy=0, CurrentPower=0, TempF=0, Battery=0
Windspeed=0.00
Windspeed=
711
Windspeed=, Winddir=112.50, Rainfall=0.00, TempF=, Humidity=58, Battery=1
Windspeed=, Winddir=112.50, Rainfall=0.00, TempF=, Humidity=58, Battery=1
connection failed

bkenobi

And this is not limited to just windspeed=0.0.  I parsed the data down for simplicity, but this is the output when the wind speed is greater than zero.
Code: [Select]
Windspeed=2.68
Windspeed=  2.68
connection failed
Windspeed=1.66
Windspeed=
connection failed
Windspeed=1.66
Windspeed=
connection failed
Windspeed=1.14
Windspeed=
connection failed
Windspeed=2.17
Windspeed=
connection failed

bkenobi

I'm sneaking up on the issue.  It looks like dtostrf is breaking for some reason.

Code: [Select]
      dtostrf(windspeed,10,2,str_winds);
     
      Serial.print("Windspeed float=|");
      Serial.print(windspeed);
      Serial.println("|");
     
      Serial.print("Windspeed dtostrf=|");
      Serial.print(str_winds);
      Serial.println("|");
     
      sprintf(packet,"Windspeed sprintf=|%s",str_winds);
      Serial.print(packet);
      Serial.println("|");


Code: [Select]
Windspeed float=|2.68|
Windspeed dtostrf=|      2.68|
Windspeed sprintf=|      2.68|

connection failed
Windspeed float=|2.17|
Windspeed dtostrf=|      2.17|
Windspeed sprintf=|      2.17|

connection failed
Windspeed float=|2.17|
Windspeed dtostrf=||
Windspeed sprintf=||

connection failed
Windspeed float=|1.66|
Windspeed dtostrf=||
Windspeed sprintf=||

connection failed


At some point, the value stored in "str_winds" becomes empty for no apparent reason.  I haven't found any known issues with dtostrf, so I'm not sure what the issue could be.

bkenobi

I stripped the code out of the MQTTreport_c function and put it into a new sketch.  It works correctly so there must be some interaction between my other code and dtostrf that is the issue.  I guess I'll just have to start commenting things out till it doesn't break...

PaulS

Code: [Select]
      return packet;
Returning a pointer to a variable that has gone out of scope is worse than useless.

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