Sending Load Cell (Weight) data using HX11 to MQTT Server using ESP32 with Arduino IDE
top of page

Sending Load Cell (Weight) data using HX11 to MQTT Server using ESP32 with Arduino IDE

Updated: Jun 27, 2020

This article covers the understading of Load Cell Sensor, HX11, ESP32 Microcontroller, MQTT Server, Integration of all these three blocks using Arduino IDE.



Load Cell Sensor


A load cell (or loadcell) is a transducer which converts force into a measurable electrical output. Although there are many varieties of force sensors, strain gauge load cells are the most commonly used type. (Reference Link)


HX711

HX711 is a precision 24-bit analogto-digital converter (ADC) designed for weigh scales and industrial control applications to interface directly with a bridge sensor. (Technical Datasheet)



ESP32


ESP32 is a single 2.4 GHz Wi-Fi-and-Bluetooth combo chip designed with the TSMC ultra-low-power 40 nm technology. (Technical Datasheet)




We will be using ESP32-WROOM32 development kit for our experiment which we can power-up using USB 5V power source.





MQTT Server

MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium. (Reference Link)



As you observe in the above picture,

- Device/Sensor in our application will be ADXL335 Acceleromter Sensor with ESP32

- MQTT Broker is where all the sensor data will be posted

- Mobile/Laptop is where you will see the data recieved on your MQTT broker


Design Approach for the System


For quick prototype system, most of the components are purchased off-the shelf which reduces the time to develop and print PCBs but increases the cost a bit.


Load Cell:

1. Standard off the shelf load cell (10Kg variant) with plant cut-out is bought to save time and

complexity.

2. HX711 is a 24bit Analog to Digital converter for weigh scale is used which helps us to have 24-bit ADC resolution


Micro-controller with built-in Wi-Fi:

1. Ideal choice with this requirement is ESP32 which has multiple GPIOs with Wi-Fi Antenna on the module itself which serves both purposes.

2. ESP32 EVB is used for the development where the programming can be done using Arduino IDE which makes it more user-friendly to write the business logic.

3. RGB LED is connected to the ESP32 module for understanding of the state of system and

debug if any failure occurring.


Power Block:

1. Since independent power required, Li-po battery is good choice which meets the capacity and voltage specification.

2. Li-Po battery with 3S configuration is chosen for the design which gives nominal voltage of 11.1V with 1500mAh capacity.

3. XT60 end connector Is used between Li-Po Battery & the USB Adapter converter board for high current contact & ease in removal of battery source to replace/charge.

4. 2S-6S input rage is feasible for the USB Adapter board which makes it safer in terms of any fluctuations on the battery side should not impact the USB power supply.

5. USB A type is the output connector of the USB Adapter board, so we need to use a micro-USB standard cable to power the ESP32 module via battery source


Block Diagram



Schematic Diagram


Schematic is designed in Autodesk Eagle platform. Link to download the editor.


HX711 Block

ESP32 WROOM32 Block




Bill of Materials (BOM)




MQTT Server Setup Steps


Step 1: Visit http://www.hivemq.com/demos/websocket-client/ which will act as broker for our data server


Step 2: Do the setup at hivemq end. (Note: Following image shows connected connection)


Step 3: Subscribe the channel where we are publishing our data from ESP32

Part A – mytopic/kitchenMotion



Step 4: You will see your messages on the MQTT Subscribed channel.



Demo Video



Source Code


/*
 * This code sends weight of the object placed on weighing scale using ESP32 WROOM32 EVB. 
 * 
 */

#include "HX711.h"
#include "EspMQTTClient.h"
#include <String>

HX711 scale(21, 22);		
const int redled = 17;
const int greenled = 16;
int device_id = 1122;

EspMQTTClient client(
  "Virendra",
  "virendra17_",
  "broker.hivemq.com",  // MQTT Broker server ip
  "TestClientTest",     // Client name that uniquely identify your device
  1883              // The MQTT port, default to 1883. this line can be omitted
);

void setup() {
  Serial.begin(115200);
  Serial.println("HX711 Test");
  Serial.println(scale.read());			// print a raw reading from the ADC
  pinMode(greenled, OUTPUT);
  pinMode(redled, OUTPUT);

  digitalWrite(greenled, HIGH);
  digitalWrite(redled, HIGH);

  scale.set_scale(1901.f);    // this value is obtained by calibrating the scale with known weights;
  scale.tare();				        // reset the scale to 0

  client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
  client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overrited with enableHTTPWebUpdater("user", "password").
}
void onConnectionEstablished()
{
//  client.subscribe("mytopic/kitchenWeighingScale", [](const String & payload) {
//    Serial.println(payload);
//  });


  client.publish("mytopic/kitchenWeighingScale", "Device Booted"); // You can activate the retain flag by setting the third parameter to true
}

int weight_status = 0;
int last_millis = 0;
void loop() {

  float weight = scale.get_units(10) * 10;
  if (weight > 10) {
    Serial.printf("Weight: %.1f, Present\n", weight);
    digitalWrite(greenled, HIGH);
    digitalWrite(redled, HIGH);
    weight_status = 1;
  }
  else {
    Serial.printf("Weight: %.1f, Picked up\n", weight);
    digitalWrite(greenled, LOW);
    digitalWrite(redled, LOW);
    weight_status = 0;

  }

  if (millis() - last_millis >= 5000) {
    String message_string = "{\"TS\":" + String(millis()) + "," 
    + "\"Device ID\":" + String(device_id) + "," 
    + "\"Status\":" +  String(weight_status) + "," 
    + "\"Weight(gm)\":" + String(weight) + "}" ;
    client.publish("mytopic/kitchenWeighingScale", message_string);
    last_millis = millis();
  }

  scale.power_down();			        // put the ADC in sleep mode
  delay(100);
  scale.power_up();
  client.loop();
}

For more details, Reach out to us. You can fill the form available on the website (www.allaboutprojects.in) or directly call/WhatsApp us on +91-9545954177.


-

Technical Team Member

2,367 views1 comment

Recent Posts

See All
bottom of page