I need your help. Please help me save my sanity and my hair…
I have two load sensors that I’m trying to read in a loop with a Photon. I’m using 2 HX711’s, and the HX711ADC library to do it. Individually, they work well, and the variables seen in the dashboard are great. However, I have two issues that I just can’t figure out. I’m sure it’s something really obvious…
When I try to read two, as below, I get nonsense back. Particle
variables also alternate from 0 to massive readings. I’ve tried
various things including turning the sensors off alternately (as
below) but just can’t get it to work.
Particle variable returns the right values, but publish doesn’t.
My code is below. Please help me figure what am I doing wrong?
Thanks!
// This #include statement was automatically added by the Particle IDE.
#include "HX711ADC/HX711ADC.h"
int scale1Weight = 0;
int scale2Weight = 0;
char wgtStr[20];
int ledPin = D7;
HX711ADC scale1(A4, A3); // parameter "gain" is ommited; the default value 128 is used by the library
HX711ADC scale2(A2,A1);
void setup() {
scale1.power_up();
scale1.set_scale(-673);
scale1.tare();
scale1.power_down();
delay(1000);
scale2.power_up();
scale2.set_scale(-19.45);
scale2.tare();
scale2.power_down();
delay(1000);
Particle.variable("scale1", scale1Weight);
Particle.variable("scale2", scale2Weight);
}
void loop() {
scale1.power_up();
scale1Weight = scale1.get_units(5);
scale1.power_down();
delay(1000);
scale2.power_up();
scale2Weight = scale2.get_units(5);
scale2.power_down();
delay(1000);
sprintf(wgtStr, "{\"scale1\":%u,\"scale2\":%u}", scale1Weight, scale2Weight);
Particle.publish("wgtStr", wgtStr, 100, PUBLIC);
delay(15000);
}
Your char array is still too short. Your string literals - even without any added values - are already 22 char!
Always add some extra space otherwise you’ll find yourself tracking intermittent errors and give yourself a hard time reproducing them.
I’ve increased this as you’ve said. But I’m still getting occasional erratic behaviour. Sometimes one or both of the scale readings goes nuts, I have a current scale 2 reading of 4294967279!
After reading through the HX711 library, the get_units() function returns a float value, so I’ve changed scale1Weight to match, then converted to an integer before publishing. This hasn’t helped.
Everything is soldered and neat, I’ve even swapped out the HX711 boards and yet this still happens. Any further ideas pls?
thanks
J
EDIT: I have an inkling it may be when the scale returns zero. Is there something in the publish function that I’m missing here that sends it haywire?