Arduino pull data from emoncms

I think you need to add some debugging statements to prove the truth of that. (I don’t know whether it is true or not, but that is the first step to finding out what is happening.)

[quote=“copderoc, post:20, topic:3742”]
I could try to update emoncms every 0,5s?[/quote]

Don’t try to guess a solution - it is much better to follow logic and prove the fault, then you can find the correct solution.

What is this - “pg.toInt() == 1”
Why do you need “toInt()” What are the possible values of “pg”? I know it’s a string, but it seems peculiar to convert string to integer and then compare to 1.

Thanks Robert,

I have been testing it again and I think that you are right.
Arduino is on a loop asking with the string for the emoncms values, but when it don’t have this values the string is in a indeterminated status, so the leds are blinking.

I am searching a method to stabilize this values. For now i “solved” it adding a delay(5000);, but i would like to get a working stable code. I think I know what could work but I don’t know how to code that. Perhaps somthing like this:

Arduino ask for emoncms data and save this values into some arduino “X” variable. Ex: x=1, this variable must be saved stable on arduino until another value form emoncms comes, Ex x=0. The if comparision to do some action on arduino must be with the x saved varaible, not with the direct string instatly got from emoncms.

Could you help me in that, please?

PD: I have been searching internet and I have used .toInt() to convert the 0 or 1 string value into a number, in order to compare it with the == 1. Perhaps it’s wrong.

Thanks.

Thanks.

Your answer is in there! What you must do is nothing when the string does not contain good data. What you have here
//LEDS
if(pg.toInt() == 1) {
      digitalWrite(ledvermell, HIGH);
      digitalWrite(ledverd, LOW);

  }
  else{                                   
    digitalWrite(ledverd, HIGH);
    digitalWrite(ledvermell, LOW);       
  }

is a test that says if pg is “1”, turn the green LED on, but if pg is anything else - not necessarily “0”, turn it off. But for “anything else”, you will always get zero. The explanation is in the Arduino documentation for toint():

Returns

long
If no valid conversion could be performed because the string doesn’t start with a integer number, a zero is returned.

Can you give examples of good and bad data?

It might be the explanation for your problem. I would simply have checked that the string pg was equal to the string “1” or string “0”, or if the only possible values are 0 and 1 (not 01, 02, 11, 12,…) just test the first character of the string pg equals the character ‘1’ : if (pg[0] == '1')
That way, anything not “0” or “1” would be ignored, and the LEDs would not change.

Ok Robert… Now I am understanding a lot of things… We are near, but the led is still blinking :slight_smile:

Now I have:
Door closed, string is: 0 0 0 0 0 0 0, sometimes 0, and sometimes 0 for the invalid data, 0 returned. So in this case, led don’t blinks.

Door open, string is: 1 1 0 1 01 01 01 01, 1 when string has content and 0 for the invalid data, so the led will allways blink.

I have been trying all formulas, the latest one:

if(pg[0] == '1') {  
    digitalWrite(ledvermell, HIGH);      #blinking
  }
 else{                                   
   digitalWrite(ledvermell, LOW);    #notblinking
  }

I think this is not the way… I have another arduino at the door sending the 0 and the 1 data to the emoncms server, I was thinking to change this values to OFF and ON, to isolate them of the bad content 0, but the problem will we the same.

Closed door: 0 0 0 OFF 0 OFF 0 OFF
Open door: 0 0 0 0 ON 0 ON 0 ON

Will blink in all cases. Brrrrr…

There is your problem. You do not know the difference between invalid data and door open. You must find a way to send something different for ‘open’, or something different for invalid data.

Thanks! I got it.

I reflashed my garage arduino to post to emoncms:

Open door = 1
Closed door = 2

Now it’s working well:

if(pg[0] == '1') {  
    digitalWrite(ledvermell, HIGH);       
  }
if(pg[0] == '2') {  
    digitalWrite(ledvermell, LOW);       
  } 

And the led isn’t blinking :slight_smile: the 0 invalid data isn’t considered.

Now I am thinking that I want to do more things with analog feed data, like power, for example disable something when power > 2500 watts. I will have 0 data here too?

I wrote earlier

If you find that you get a zero for invalid power, then I think you must go back to where you receive the string and arrange to either do nothing, or you can not change the string and use the old data again, until your receive valid data.

I have been testing with a led that power on when power > than 500 watts, and yes the led is blinking because is getting 0’s from invalid data. 0 is < than 500 so the led is off, blinking on/off again for the invalid data.

Yes, I understand you, but how to know when the string is invalid, or how to proceed only when the string contains valid data? Or why I receive invalid data on the string? :roll_eyes:
The solution will be to always have a valid data on the string, but I don’t know how to do that. No more 0’s please!

I do not have your system here, so I cannot see exactly what is happening. All I have done so far is teach you how to parse the string. I didn’t design the total system, so I don’t know how the data is generated, nor how it is transmitted (because I’ve never used that Ethernet library). You should be asking yourself questions like these:

  • What is invalid data, and where is it coming from?
  • If I am filling the string with invalid data, why am I doing that?
  • If it’s not data that I’m interested in, can I put an unique signature at the beginning of my data so that I can know that it is mine?
  • Can I put a checksum on the end so that I know that it is complete?
  • Am I always sending valid data?

Look at how the emonTx sends to emonHub using JeeLib, if you want an example. A Jeelib packet of data contains an address, either where it’s come from or where it’s going to, a count of the amount of data that’s coming, and a checksum on the end to detect errors. EmonHub only uses the data if it comes from the right place and the checksum is good. I am not saying you should do exactly that, but you can get some ideas from there.

Thanks Robert, you helped me very much.

Today I got it working, as you said yesterday if data it’s not 0 (valid data) go on, if data is 0 (invalid) do nothing. It works.

//led vermell porta garatge oberta
if(pg != 0) {  
  if(pg[0] == '1') {  
      digitalWrite(ledvermell, HIGH);       
    }
  if(pg[0] == '2') {  
      digitalWrite(ledvermell, LOW);       
    }       
}


//led verd potència superior a x watts
if(potencia != 0) {  
  if(potencia.toInt() > 370) {  
      digitalWrite(ledverd, HIGH);       
  }
  else {
    digitalWrite(ledverd, LOW); 
  } 
}

Of course, it would be better if I don’t have invalid data, but for now filtering the data it’s working.
In the future I will try to improve the program, but for now it’s working. I am a noob in arduino, thanks for your help!

1 Like