Close

Firmware

A project log for Reagent Tracker

Track reagent usage and get timely notifications to re-order.

stephen-harrisonStephen Harrison 06/11/2017 at 23:120 Comments

The flow chart below describes the main functionality of the firmware.


Most of the time the Photon will be in deep sleep to save battery power, waking only because of a RTC wake or tap interrupt when a bottle is placed on the scales and occasionally due to a system event (power cycle, firmware update, user reset).

Once the Photon is woken it checks the accelerometer to determine if any taps occurred that would indicate a bottle being placed on the scale, the sensitivity of tap sensing can be adjusted if needed.

Background (no tap wake) Activity:

If no taps were observed when the Photon was woken then it is most likely due to the RTC timer timeout. Currently the Photon will take an environment measurement (temperature, empty load cell weight, etc.) and then publish this to the cloud. This may change based on use case but can be used to monitor environmental storage of the reagents should this be required.

The current version (1.0.34) uses a fairly aggressive read and publish on the RTC clock which will cause high battery drain so this needs to be optimized for the battery version, but gives a nice indication of drift of the load cell baseline.

Normal Activity:

If the Photon was woken by a tap the system then goes into measurement mode. The NFC Tag Id is read (no other information is needed from the tag), the weight of the bottle is measured and the user promoted to remove the bottle by a double beep.

With normal scales you would tare the scale first to get a good zero baseline, however because we are woken by the placement of a bottle on the scale this is not possible, so the system waits for the bottle to be removed (making an annoying beep to prompt the user in the process).

Once the bottle is removed (indicated either by a weight reduction of more than 10g, or by the absence of the NFC tag) the tare (zero) weight is measured and the overall bottle weight corrected for this. This information is then published to the back-end service.

Reading the scales requires 1-2mA, reading the NFC requires more power due to the magnetic field needed. Ideally no NFC check would be used to reduce overall power usage, however it is possible that the weight of the bottle is <10g or that the bottle was removed before the weight could be properly measured and so a 10g difference between empty and empty is not possible, hence the use of the NFC check as a backup.

Once the bottle has been removed the Photon will stay awake for 30s waiting for a possible second, third, forth bottle to be placed on the scales. This is done to try and optimized for connection time as the Photon is required to connect to WiFi and then to the Particle Cloud after a deep sleep. If many bottles are to be measured it is better to connect once and wait for the next bottle.

Power Management:

The system is mainly optimized for deep sleep current consumption, no real optimization has been done for wake power usage, for example the NFC reader could be shutdown when not being used even in measuring mode.

The Deep sleep option is used for the Photon as this gives the lowest power consumption (0.08mA cf. 1mA for normal sleep) which will extend the battery life (or at-least reduce the time between charges). With normal sleep mode the Photon can be woken by a configurable interrupt source (e.g. a change on D1, rising edge on D2 etc.), however when in deep sleep the Photon can only be woken by a rising edge on the WKP pin.

When sleeping the Photon will only go into deep sleep if certain criteria are met, the Phonon will not sleep if any of the following happen:

Internet Connectivity:

Publishing to the internet is done via the Particle Cloud functions. Particle.publish() is used to publish either a "status" message or a "senml" message, these are well known message types used at Tinamous.com and will result in the message either being shown on the status timeline or set as a measured field value.

When publishing a senml message, (in the senml json format), we set the fieldname to be the bottle NFC tag id, this allows Tinamous to track that bottle, however it will not be able to track it across two different Reagent Trackers if more than one is in use.

The Photon is set to not connect to the Internet (or WiFi) by default so some extra work is needs to be done to connect when needed, we also include a timeout for connect so in the event of a problem the battery isn't drained just trying to get a WiFi or Internet connection. If the Photon fails to connect it will emit a series of buzzes via the Piezo buzzer.

The application also exposes a few Particle.function's. These are internet callable functions that are used to configure the unit on first use. The tare function sets the zero weight of the scale into a EEPROM. This isn't a true zero as the load cell will drift but it gives a good starting point and allows us to track long term drift of the load cell. For these functions to be accessible the Photon needs to be connected to the internet, so the KeepAwake pin should be set high to run these (or the firmware altered to prevent deep sleep, or a longer timeout).

Calibration:

The calibrate function is used to calibrate the load cell. Each load cell needs to be individually calibrated to determine its response to mass. This routine is hardcoded for a weight of 666g (yes I had a 666g weight I used - actually it was a mug of tea!). It is best done with a serial connection to the Photon to get feedback and should be run a few times to get the best factor. See the HX711 driver for more information on this. See also he SparkFun HX711 Github repository

Eventually you will end up with a scaleFactor for the load cell, this should be used in the scale.set_scale() function call in setupScale(). The current value of 400.8 works well for the load cell I have but will be different for yours.

Some optimization of this still needs to be done and ideally the scale will compute it itself and store in EEPROM rather than in the code.

User Interaction:

One of the aims of the project is to keep user interaction to a minimal, hence their is no display and no buttons on the system.

Currently a reverse wake tune is played when sleeping, however this is not needed (and it's annoying) so this will be remove in future versions, but it's handy when trying to debug issues to hear that the unit is sleeping.

If the Photon is unable to connect to the internet a series of beeps are played.

The Piezo buzzer is run at max volume (50% PWM duty cycle) and about 4kHz which is around the resonant frequency for the buzzer. The PWM may be altered to play different notes to make it a little more interesting.

The user does not need to switch on, or off the system.


Discussions