Particle.publish variable

photon
Tags: #<Tag:0x00007fe21c44ab00>

#1

HI -

I am trying to post ACS722 current sensor values to Particle cloud. As my coding skills are VERY limited, I am not sure whether the ‘null’ return I am receiving in Particle cloud is due to code or hardware error. Please see my code below:

int redPin = D2;       
int greenPin = D3;    
int bluePin = D1;

void setup() {
 
  Serial.begin(9600);
  
    pinMode(redPin, OUTPUT);
    pinMode(greenPin, OUTPUT);
    pinMode(bluePin, OUTPUT);

        digitalWrite(bluePin, LOW);
        digitalWrite(greenPin, HIGH);
        digitalWrite(redPin, LOW); 

  pinMode(A5, INPUT);

}
 
void loop() {
  
  float acsvalue = 0;
  for(int i = 0; i < 1000; i++) {
//    average = average + (.0264 * analogRead(A5) -13.51) / 1000;
    acsvalue = analogRead(A5);
    delay(5);
  }
 
  Serial.println(acsvalue);  
  Particle.publish("acsvalue", PRIVATE);
 
  digitalWrite(greenPin, LOW);
  digitalWrite(bluePin, HIGH);
  delay(250);
  digitalWrite(greenPin, HIGH);
  digitalWrite(bluePin, LOW);

Would help if I could check on serial monitor as this would eliminate whether the problem is in the publish part or thew actual sensor I suppose, but in this case, I no longer have access the the USB port on the Photon. If I had to, I would probably put my money on a coding error, hehe…

I would really appreciate some help. Thank you for your patience :slight_smile:

Regards
Friedl.


#2

Try calling it in the Particle.publish(“acsvalue”, PRIVATE), inside the loop.


#3

Hi @muneebr1

:blush::see_no_evil:

Great thanks. will give it a shot.

Regards,


#4

HI @muneebr1 -

I changed it to the below;

void loop() {
  
  float acsvalue = 0;
  for(int i = 0; i < 1000; i++) {
//    average = average + (.0264 * analogRead(A5) -13.51) / 1000;
    acsvalue = analogRead(A5);
    Serial.println(acsvalue);  
    Particle.publish("acsvalue", PRIVATE);
    delay(5);


  digitalWrite(greenPin, LOW);
  digitalWrite(bluePin, HIGH);
  delay(250);
  digitalWrite(greenPin, HIGH);
  digitalWrite(bluePin, LOW);


  }
  
}

Sadly, still the ‘null’ as per the image attached.


#5

Looking at the documentation, the function is overloaded.

With two parameters, you’re giving the event name. Which is what you’re seeing, however with two parameters you’re not giving in any data value.

Particle.publish(const char *eventName, PublishFlags flags);
Particle.publish(String eventName, PublishFlags flags);

In order to give the event name, as well as the data you need to use three parameters.


Particle.publish(const char *eventName, const char *data, PublishFlags flags);
Particle.publish(String eventName, String data, PublishFlags flags);

So the second parameter would be the data that you want to publish. I implemented like this in one of my projects.

int average = smoothingAverage(distance);
Particle.publish(“Sensor Output”, "Sensor Value: " + String(average), PRIVATE);

https://docs.particle.io/reference/device-os/firmware/photon/#particle-variable-


#6

Hi -

Thank you for the help, let me try this :slight_smile:


#7

No Problem :blush:


#8

Great!!

I changed it to there following;

  float acsvalue = 0;
  for(int i = 0; i < 1000; i++) {
//    acsvalue = acsvalue + (.0264 * analogRead(A5) -13.51) / 1000;
    acsvalue = analogRead(A5);
//    Serial.println(acsvalue);  
    Particle.publish("current", String(acsvalue), PRIVATE);
    delay(5000);

I am receiving values meaning my board is working at least. Using a custom PCB I designed so was nervous as to whether it was done correctly. Seem it is, pheewww. Now just calibrating the sensor and then all good.

Thank you for you help!!

Regards
Friedl


#9

Could you share the circuit schematic being used with the ACS722?
What sort of current (AC or DC) are you measuring?

float acsvalue = 0;
for(int i = 0; i < 1000; i++) {
// average = average + (.0264 * analogRead(A5) -13.51) / 1000;
acsvalue = analogRead(A5);
delay(5);
}

If you are reading alternating current then this code will never work. Try a search for reading and calculating RMS current before you think about how to write the code. The timing of the voltage reading on the output from the ACS722 is incredibly important and using delay(5) just will not work reliably.

If you are reading direct current then there are a few things wrong with this code, the output from analogRead(A5) is not of data type float. Have a look at the reference documentation

analogRead() returns an integer value ranging from 0 to 4095.

If you want to average the value I would suggest something like this;

int avInt = 0;

for (int i = 0; i < 1000; i++) {
     // some timing mechanism
     avInt += analogRead(A5);
}
float acsvalue =  0.0264 * ((float) avInt / 1000);     //C compiler experts may say this isn't necessary to convert an integer to a float - now you have the current as a floating point number

Next Particle.publish("acsvalue", PRIVATE); is not publishing the current reading as a data value - hence why you get ‘null’. What you need is something like this: Particle.publish("acsvalue", "0.213", PRIVATE); where 0.213 is the value of acsvalue as a c-string - so you need to convert the float type to a const char* - I will leave you to lookup how to do that!


#10

Hi @armor -

I am reading AC current indeed. I managed to get the publishing working with the assistance of @muneebr1. I am getting readings fluctuating between 2030 and 2044.

In the sample code the delay was sent to

delay(1)

I just changed it to 5 as I did not want to keep receiving so may readings whilst still trying to figure out the particle.Publish command. Reading through the documentation now to find out how to correctly configure the ACS722 to present accurate readings.

As I mentioned to @muneebr1, coding is not my forte, but the coder on this project is still busy with his code, I simply wanted to test and see whether the PCB I designed was working.

I only have the Eagle schematic of the entire PCB, but will gladly sent it if it will help. Is it safe to assume though that if I am getting readings, the circuit is working fine?

Many thanks.


#11

Is it safe to assume though that if I am getting readings, the circuit is working fine?

What was the AC current you were trying to measure?
That depends upon what accuracy you are looking for and what sort of load there is being measured.
The Allegro current sensors are only as accurate as the stability of the voltage supply - so using a LDO linear voltage regulator is fine as there will be no ripple but a switching DC:DC converter is not going to work so well.

On the load side AC is not a perfect sine wave unless you have a perfect resistive load. What would be more useful to understand is to read 100 values into an array and then output them via the Serial port - so you should then see something akin to a sine wave. A DSO on the output of the ACS722 would be a good idea for you to confirm that your readings are a good representation. If your software developer is expert with this stuff then fine.


#12

Hi -

Fortunately in this case we only need the current sensing to determine on/off (in use/not in use) status of a machine. The supply voltage to the machine is 230V with max current of 5A in this case. In other words, I do not need very 100% accurate readings.

It might be that some devices will draw 1A current whilst others might be operating at 4A. I am basically looking for a range of values resembling off or "not in use" status of a machine and then of course a range of values resembling on or in use status. I will then use these to send to Ubidots dashboard. where we will log all the data.


#13

You could also try using a bridge recitifier to convert the AC signal to DC toss in a capacitor to smooth things out and use an LTC2945 to read the voltage and send it out via I2C …You lose about .5VDC on the Bridge but you can add it later for a more accurate reading


#14

I have some pretty good code for the LTC2945


#15

hi @jade7272 -

Thank you for your reply. I just had a look at one of your previous posts. I wish I have seen this before I started :slight_smile:

I went with the ACS722 due to the good reviews it receives and the initially the code seemed simple enough. Of course I never realised all code examples was all done measuring DC and not AC. Nevertheless, I persisted and managed to get the results as per the image below:

I used a 1A light bulb to simulate load at 220V. I am improving my code to measure at increments of 1ms, taking 500 readings and then calculating the current. Finally posting every 1s as permissible in Particle Cloud.

I will post my project as soon as I am done. I tried several PCB manufacturers but for some reason they seem much more expensive, so I went with PCBWay again. Happy with their quality aso far, even with this PCB containing both HV and LV on the same (and quote small) board.

Thanks again for your reposes, it is much appreciated!

Regards,.
Friedl.


#16

Hey Freidi if you want a lower cost manufacture of total PCB build try freya@quint-tech.com freya@quint-tech.com;she makes our boards out of china for LoRa devices prices are the best ive seen so far


#17

Even lower that PCBWay?

I paid around US$35 for 10 boards this time. Much of the time high than usual price was due to the 2oz copper I chose for the 220V, 5A signals.

I will have a look, thanks!!


#18

If you are only trying to detect AC voltage present then you could try using an opto coupler - an MC400 or the LTC2945. The MC400 does not require rectification of the AC but you do need to be careful how you control the current through it. I replied on a topic recently if you search for either IC.


#19

HI @armor -

Thank you again for your reply and advice. I will most certainly look into this for future designs. As for now, I already made the PCB’s for the ACS722, so will have to stick to that. It seems be to working well so far, or from what I can see at least :smile:

I watched a couple of videos on the LTC2945 component. It seems that in my case it would not work as the current I am working with is 220V and up to 5A. It seems the max voltage is 80V but does seem easier to work with though :slight_smile:


#20

PCB way is the best price for Bare boards quin-tech is awesome for complete manufactured boards