My Sensorized Weighing scale - Need help in code modification



  • I have been working on creating a Weighing scale using a 5 Kg Load Cell and HX711 Amp connected to a MySensor node. I plan to have a dynamic weighing scale for reporting weight of anything kept on the Load cell sensor (Aluminium Bar form factor load cell) . Ideal use case can be a Weight scale node kept in a refrigerator measuring the weight of a Milk bottle. The system will send out notification in case the milk bottle starts running low, which can be a trigger to go shop for groceries when your away from home.

    Initial code for calibration of Load cell was taken from this website and then factored into My Sensor code. I have since been able to make a working node in MySensor and able to display weight data in Domoticz. I would like to further refine the code and make it more practical.

    Source webiste: [http://circuits4you.com/2016/11/25/hx711-arduino-load-cell/](link url)

    I am beginner in MySensor and C/C++ coding so thought it would be best if the My Sensor community can help me out in below objectives:

    1. Ideally, I would want the sensor to be battery powered so I have ensured that I run a barebones Atmega 328p on 8 Mhz internal clock. Is it possible that
      the sensor node is always in deep sleep consuming min battery power and only wake up if only requested from the Controller (in my case Domoticz). This way
      the sensor will only report weight data when asked for and result in minimal battery usage and user can "Query" the node for the weight. Not sure if this will use
      an Interrupt function. The readings from the Load Cell sampled every 3 seconds in my code below vary by a very small amount say by .001 gms.

    2. Send a keep alive message say every 1 hour and update controller on the Weight.

    Any help suggestions are welcome and really appreciate the MySensors Team and Community for being awwsum every time!
    Thanks....

    Source Code created for the Weight node

    #define MY_DEBUG 
    #define MY_RADIO_NRF24
    #define MY_NODE_ID 10    
    
    #include <MySensors.h>
    #include <SPI.h>
    
    #include "HX711.h" 
    
    #define DOUT  3
    #define CLK  2
    
    HX711 scale(DOUT, CLK);
    
    float calibration_factor = -465000;   //calcuated by trial and error; goal was to ensure Zero weight reading when no weight on the load cell
    
    #define CHILD_ID_WEIGHT 1
    
    unsigned long waitTime = 3000;          // delay in milliseconds to set time between data readings 
    float weight_read;    
                         
    MyMessage msgVolume(CHILD_ID_WEIGHT, V_LEVEL);             
    
    void setup() {
    
    scale.set_scale();
    scale.tare();
    
    }
    
    void presentation()  {
    
      sendSketchInfo("Weight Sensor", "1.0");
      present(CHILD_ID_WEIGHT, S_DUST,"Weight");
      
    }
     
     void loop()
     { 
     
       data_calc();      
     
     
      {          
      send(msgVolume.set(weight_read, 3));
    
      }   
     
      wait(waitTime);  //Wait then back to loop
      
    }
    
     void data_calc() {
     scale.set_scale(calibration_factor); 
    
    #ifdef MY_DEBUG
    
      Serial.print("Reading: ");
      Serial.print(scale.get_units(), 3);
      Serial.print(" kg");
    
    weight_read =  scale.get_units();
    
      Serial.print(" Weight Read by sensor: ");
      Serial.print(weight_read, 3);
      Serial.println();
      
    #endif
     }
    
    

    Output in Serial monitor

    Starting sensor (RNNNA-, 2.0.0)
    TSM:INIT
    TSM:RADIO:OK
    TSP:ASSIGNID:OK (ID=10)
    TSM:FPAR
    TSP:MSG:SEND 10-10-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSP:MSG:READ 0-0-10 s=255,c=3,t=8,pt=1,l=1,sg=0:0
    TSP:MSG:FPAR RES (ID=0, dist=0)
    TSP:MSG:PAR OK (ID=0, dist=1)
    TSM:FPAR:OK
    TSM:ID
    TSM:CHKID:OK (ID=10)
    TSM:UPL
    TSP:PING:SEND (dest=0)
    TSP:MSG:SEND 10-10-0-0 s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=ok:1
    TSP:MSG:READ 0-0-10 s=255,c=3,t=25,pt=1,l=1,sg=0:1
    TSP:MSG:PONG RECV (hops=1)
    TSP:CHKUPL:OK
    TSM:UPL:OK
    TSM:READY
    TSP:MSG:SEND 10-10-0-0 s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=ok:0100
    TSP:MSG:SEND 10-10-0-0 s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=ok:2.0.0
    TSP:MSG:SEND 10-10-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=ok:0
    TSP:MSG:READ 0-0-10 s=255,c=3,t=6,pt=0,l=1,sg=0:M
    TSP:MSG:SEND 10-10-0-0 s=255,c=3,t=11,pt=0,l=13,sg=0,ft=0,st=ok:Weight Sensor
    TSP:MSG:SEND 10-10-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=ok:1.0
    TSP:MSG:SEND 10-10-0-0 s=1,c=0,t=24,pt=0,l=6,sg=0,ft=0,st=ok:Weight
    Request registration...
    TSP:MSG:SEND 10-10-0-0 s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=ok:2
    TSP:MSG:READ 0-0-10 s=255,c=3,t=27,pt=1,l=1,sg=0:1
    Node registration=1
    Init complete, id=10, parent=0, distance=1, registration=1
    Reading: 0.000 kg Weight Read by sensor: -0.000
    TSP:MSG:SEND 10-10-0-0 s=1,c=1,t=37,pt=7,l=5,sg=0,ft=0,st=ok:-0.000
    Reading: 0.001 kg Weight Read by sensor: 0.002
    TSP:MSG:SEND 10-10-0-0 s=1,c=1,t=37,pt=7,l=5,sg=0,ft=0,st=ok:0.002
    Reading: 0.229 kg Weight Read by sensor: 0.229
    TSP:MSG:SEND 10-10-0-0 s=1,c=1,t=37,pt=7,l=5,sg=0,ft=0,st=ok:0.229
    Reading: 0.229 kg Weight Read by sensor: 0.228
    TSP:MSG:SEND 10-10-0-0 s=1,c=1,t=37,pt=7,l=5,sg=0,ft=0,st=ok:0.228
    Reading: 0.229 kg Weight Read by sensor: 0.228
    TSP:MSG:SEND 10-10-0-0 s=1,c=1,t=37,pt=7,l=5,sg=0,ft=0,st=ok:0.228
    Reading: 0.228 kg Weight Read by sensor: 0.227
    TSP:MSG:SEND 10-10-0-0 s=1,c=1,t=37,pt=7,l=5,sg=0,ft=0,st=ok:0.227
    Reading: 0.229 kg Weight Read by sensor: 0.228
    TSP:MSG:SEND 10-10-0-0 s=1,c=1,t=37,pt=7,l=5,sg=0,ft=0,st=ok:0.228
    Reading: 0.227 kg Weight Read by sensor: 0.228
    TSP:MSG:SEND 10-10-0-0 s=1,c=1,t=37,pt=7,l=5,sg=0,ft=0,st=ok:0.228
    Reading: 0.229 kg Weight Read by sensor: 0.227
    
    

    0_1492935786673_Weight Output.JPG


  • Admin

    It is not possible to wake up a sleeping node from the controller side.

    I suggest you just send in weight once every hour by replacing the wait()-statement in the loop-section with a sleep(1000*60*60) which represent 1 hour.



  • @hek Out of curiosity, is this what the interrupt line on an nRF24 module would be for? If so, is there any talk of implementing it in a future MySensors release?


  • Admin

    The interrupt can already be used (for gateway or high throughput nodes) to de-queue the radios FIFO buffer. If you don't do this fast enough they will be lost and the IRQ helps to do this in the "background".

    IRQ debuffering in enabled by:

    #define MY_RX_MESSAGE_BUFFER_FEATURE
    

    Keeping arduino at sleep and radio in listening state (to wake up the arduino) won't help much.... Radio still consumes too much battery.



  • @hek Good point.



  • Instead of time triggering (using sleep) you might consider putting a switch in the door of the fridge. Waking up when the door is opened/closed.



  • @kontrollable Thanks, that looks like a better option.. I will try this option and come back with how it went....Thanks All for ur suggestion as awlays



  • Would be great to see some photo of hardware you have to get this working?


Log in to reply
 

Suggested Topics

  • 8
  • 3
  • 7
  • 2
  • 1
  • 44

62
Online

11.4k
Users

11.1k
Topics

112.6k
Posts