Help please! Reading multiple HX711's

Hey there

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…

  1. 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.
  2. 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); 

}

Is this actually the code you are building???

There are variables with “mutating” case (e.g. Scale2Weight vs. scale2Weight) and “unknown” objects (e.g. foodScale and catScale).

Your wgtStr[10] is too short for sprintf(wgtStr, "{\"scale1\":%u,\"scale2\":%u}", scale1Weight, scale2Weight);

You can also just write

  Particle.publish("wgtStr", wgtStr, 100, PUBLIC); 

You say it works, but I’d rather use a dedicated variable instead of “exposing” the object as Particle.variable()

Particle.variable("scale1", scale1);  // scale1 is the object

Hey ScruffR

Thank you for your comments. I had edited some of the variable names to make it more useful for anyone else with this issue, but was a bit sloppy.

Your tips worked. I’ve edited the above code so it’s functional for anyone with the same issues.

Thanks again.

J

1 Like

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.

1 Like

Thanks ScruffR.

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?

This number rather looks like a negative number assigned to a unsigned type.

You are using %u in your sprintf() telling the function to treat the number you pass in as an unsigned int.

1 Like

Perfect. %u to %d solved this too. THANK YOU.

2 Likes