How to Integrate HX711 with ESP32 and Azure SDK C for Weight Capture?

Vinu George 45 Reputation points
2024-10-05T17:22:58.6033333+00:00

I'm working on a project where I need to capture the weight from an HX711 load cell connected to an ESP32 board and integrate the data with Azure IoT Hub using the Azure SDK for C.

  • I have already configured the iot_configs.h file correctly with the necessary Azure IoT settings.
  • The goal is to send the weight data captured from the HX711 sensor to Azure IoT Hub.

I’ve successfully connected the HX711 to the ESP32 and can read the weight, but I'm struggling to combine this with the Azure SDK C code to send the weight_In_g data as telemetry to Azure IoT central. Could anyone guide integrating the weight capture program with Azure SDK C? Any code examples or references would be greatly appreciated.

Hardware:

  • ESP32
  • HX711

Software:

  • Azure IoT Central
  • Arduino IDE Code used for weight capture is below //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 03_Load_Cell_Sensor_5kg_Calibration // In this step, the calibration value will be stored in the ESP32 flash memory. // The calibration value in this step will be used in the next step. //----------------------------------------Including the libraries. #include "HX711.h" //----------------------------------------Defines the connected PIN between HX711 and ESP32. #define LOADCELL_DOUT_PIN 5 #define LOADCELL_SCK_PIN 18 //---------------------------------------- // Predefined calibration factor (set to 382 as per your requirement) #define CALIBRATION_FACTOR 382 // Bool variable to display the weighing results. bool show_Weighing_Results = false; int weight_In_g; // Int variable to hold the value of the weighing results in units of grams (g). float weight_In_oz; // Float variable to hold the value of the weighing results in units of ounce (oz). // Initialize the HX711 library as LOADCELL_HX711. HX711 LOADCELL_HX711; //________________________________________________________________________________VOID SETUP() void setup() { // put your setup code here, to run once: Serial.begin(115200); Serial.println(); delay(2000); Serial.println("Setup..."); delay(1000); Serial.println(); Serial.println("Do not place any object or weight on the scale."); delay(1000); Serial.println(); Serial.println("LOADCELL_HX711 begin."); LOADCELL_HX711.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); // Set the predefined calibration factor directly LOADCELL_HX711.set_scale(CALIBRATION_FACTOR); delay(1000); // Tare the scale LOADCELL_HX711.tare(); Serial.println("Scale has been tared."); show_Weighing_Results = true; Serial.println(); Serial.println("The scales are ready to use."); } //________________________________________________________________________________ //________________________________________________________________________________VOID LOOP() void loop() { // put your main code here, to run repeatedly: //----------------------------------------The condition for printing the results of weighing objects on the serial monitor after the calibration process is complete. if (show_Weighing_Results == true) {
    if (LOADCELL_HX711.is_ready()) {
    
      // The value 10 in get_units(10) means getting the average value of 10 readings.
    
      // For more details see in File -> Examples -> HX711 Arduino Library -> HX711_full_example
    
      // Get the reading of the object's weight in grams (g).
    
      weight_In_g = LOADCELL_HX711.get_units(10); 
    
      // Get the reading of the object's weight in ounces (oz).
    
      weight_In_oz = float(weight_In_g) / 28.34952;
    
    
    
      Serial.print("Weight : ");
    
      Serial.print(weight_In_g);
    
      Serial.print(" g");
    
      Serial.print(" | ");
    
      Serial.print(weight_In_oz, 2);
    
      Serial.println(" oz");
    
    } else {
    
      Serial.println("HX711 not found.");
    
    }
    
    } //---------------------------------------- delay(1000); }
Azure IoT Central
Azure IoT Central
An Azure hosted internet of things (IoT) application platform.
365 questions
Azure IoT SDK
Azure IoT SDK
An Azure software development kit that facilitates building applications that connect to Azure IoT services.
226 questions
Azure Startups
Azure Startups
Azure: A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.Startups: Companies that are in their initial stages of business and typically developing a business model and seeking financing.
482 questions
{count} votes

Accepted answer
  1. LeelaRajeshSayana-MSFT 16,786 Reputation points
    2024-10-08T15:59:53.3933333+00:00

    Hi @Vinu George Thank you for posting the question on this platform.

    The Arduino IDE provides a simpler way to integrate with Azure SDK C code and send telemetry data to Azure IoT Hub. Please refer the detailed steps provided in the GitHub repository - How to Setup and Run Azure SDK for Embedded C IoT Hub Client on Espressif ESP32 for more information on integration and set up.

    During the Set up step 4 requires you provide the configurations needed to establish the connection. The simplest way to perform a quick test is by using the Symmetric key. You can get the Primary key for your registered device in IoT Hub using the steps provided in the section retrieve device connection string and provide it to the IOT_CONFIG_DEVICE_KEY in the iot_configs.h file. This will set up connection between the ESP32 device and Azure IoT Hub.

    You can modify the method generateTelemetryPayload() in Azure_IoT_Hub_ESP32.ino file to push the weight telemetry to IoT Hub. Here is a sample implementation of the function to push the weight telemetry

    static void generateTelemetryPayload()
    {
      // You can generate the JSON using any lib you want. Here we're showing how to do it manually, for simplicity.
      // This sample shows how to generate the payload using a syntax closer to regular delevelopment for Arduino, with
      // String type instead of az_span as it might be done in other samples. Using az_span has the advantage of reusing the 
      // same char buffer instead of dynamically allocating memory each time, as it is done by using the String type below.
      if (show_Weighing_Results == true) {
         if (LOADCELL_HX711.is_ready()) {
    
            // The value 10 in get_units(10) means getting the average value of 10 readings.
    
            // For more details see in File -> Examples -> HX711 Arduino Library -> HX711_full_example
    
            // Get the reading of the object's weight in grams (g).
    
            weight_In_g = LOADCELL_HX711.get_units(10); 
    
            // Get the reading of the object's weight in ounces (oz).
    
            weight_In_oz = float(weight_In_g) / 28.34952;
    
            telemetry_payload = "{ \"Weight In oz\": " + String(weight_In_oz) + " }";
         }
         else {
           Serial.println("HX711 not found.");
         }
    }
    
    

    You still need to reference the #include "HX711.h" library in the Azure_IoT_Hub_ESP32.ino file and include the commands in your setup() method in the Azure_IoT_Hub_ESP32.ino file's setup() method to configure your sensor to read values from the ESP32 pin.

    Hope this helps! Please let us know if you need any additional assistance.



0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.