Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Load Cell Example Code
// Copyright (c) 2014 Electric Imp
// This file is licensed under the MIT License
// http://opensource.org/licenses/MIT
const PLOTLY_USER = "XXXXX";
const PLOTLY_KEY = "XXXXX";
// Name your plot
const PLOTLY_FILENAME = "Compensated Weight vs Temp Graph 3kg";
// How much historical data to show in hours (autorange must be disabled)
const GRAPH_WINDOW = 24;
firstRun <- true;
query <- {
un = PLOTLY_USER,
key = PLOTLY_KEY,
origin = "plot",
platform = "electricimp",
kwargs = http.jsonencode({
filename = PLOTLY_FILENAME
fileopt = "extend"
world_readable = "true"
layout = {
title = "Electric Imp Weight Logger 1kg"
xaxis = {
title = "Date"
type = "date"
autorange=true
//range = [format("%i000", time() - (GRAPH_WINDOW * 3600)), format("%i000", time())]
}
yaxis = {
title = "Weight (g)",
side = "left",
autorange=true,
}
yaxis2 = {
title = "Temperature (°C)",
side = "right",
overlaying="y"
autorange=true,
}
}
})
}
function updatePlotly(table) {
local t = time().tostring()+"000";
query.args <- "["+ //{\"x\": "+t+", \"y\": "+table.weight+", \"name\": \"Weight\"},"+
" {\"x\": "+t+", \"y\": "+table.comp+", \"name\": \"Compensated Weight\"},"+
" {\"x\": "+t+", \"y\": "+table.temp+", \"name\": \"Temp\", \"yaxis\": \"y2\"}]";
local query_encoded = http.urlencode(query);
//server.log(query_encoded);
local request = http.post("https://plot.ly/clientresp", {}, query_encoded);
local response = request.sendsync();
local reply = http.jsondecode(response.body);
// Display any responses we get from Plotly
if (reply["message"] != "") {
//server.log(reply["message"]);
}
if (reply["warning"] != "") {
server.log(reply["warning"]);
}
if (reply["error"] != "") {
server.log(reply["error"]);
}
if (firstRun) {
server.log("Graph available at " + reply.url);
firstRun = false;
}
}
// When we get data from the device, format it and send it to Plotly
device.on("data", function(data) {
updatePlotly(data);
});
// Copyright (c) 2014 Electric Imp
// This file is licensed under the MIT License
// http://opensource.org/licenses/MIT
const PULSE = "\x7F";
const ZERO_OFFSET = 161150; //Measured emperically
const GAIN = 1301.48; //LSBs/Gram Measured emperically
spi <- hardware.spi257;
data <- hardware.pin1;
ntc <- hardware.pin2;
w_sum <- 0.0;
t_sum <- 0.0;
cnt <- 0;
// Heavily compressed NTC formula
// for better explanation see:
// https://github.com/electricimp/reference/tree/master/hardware/thermistor
function read_temp() {
local vdda = hardware.voltage();
local v_therm = ntc.read() * (vdda / 65535.0);
return (298.15 * 3380.0) /
(3380.0 - 298.15 * math.log(10000.0 / ((vdda - v_therm) *
(10000.0 / v_therm)))) - 273.15;
}
function read_weight(){
//This is bit banged so we
//bind calls to local variables
//to speed things up.
//Takes about 2.2mS to do the read
local sw = spi.write.bindenv(spi);
local dr = data.read.bindenv(data);
local val = 0;
for(local i = 24; i >= 0; i--){
sw(PULSE);
val += dr() ? math.pow(2, i) : 0;
}
return (val-ZERO_OFFSET)/GAIN; //Value in grams
}
function sample(){
if( ! data.read() ){
data.configure(DIGITAL_IN);
w_sum += read_weight();
t_sum += read_temp();
cnt++;
data.configure(DIGITAL_IN, sample);
}else{
server.log("Data not ready");
}
}
function round(value, precision){
local n = math.pow(10, precision);
return (n * 1.0 * value).tointeger() / 100.0;
}
function loop(){
imp.wakeup(30, loop);
if(cnt == 0){
return
}
local weight = round(w_sum / cnt, 2);
local temp = round(t_sum / cnt, 2);
local comp = round(weight + 1.2*(temp - 25), 2);
//server.log("Weight: "+weight);
server.log("Compensated: "+comp);
server.log("Temp: "+temp);
agent.send("data", {weight = weight, temp = temp, comp = comp});
w_sum = 0.0;
t_sum = 0.0;
cnt = 0;
}
//This makes the clock pulse time 1.865us
spi.configure(SIMPLEX_TX, 3750);
data.configure(DIGITAL_IN, sample);
ntc.configure(ANALOG_IN);
loop();
@chrisjbremner

This comment has been minimized.

Copy link

@chrisjbremner chrisjbremner commented Jul 6, 2017

The HX711 outputs in two's compliment, so we need to account for this in the read_weight function:

function read_weight(){
    // This is bit banged so we bind calls to local variables
    // to speed things up. Takes about 2.2mS to do the read
    
    // The HX711 outputs negative numbers in two's complement,
    // so we check the highest bit and if it is high, we convert
    // to a negative by inverting the bits and adding 1, then negate
    
    local sw  = spi.write.bindenv(spi);
    local dr  = data.read.bindenv(data);
    local val = 0;
    local negative = false;
    local num_bits = 24;

    for(local i = num_bits; i >= 0; i--){
        sw(PULSE);
        local reading = dr();
        if (i == num_bits){
            negative = reading ? true : false;
        }
        
        if (negative){
            val += reading ? 0 : math.pow(2, i);
        }
        else{
            val += reading ? math.pow(2, i) : 0;
        }
    }

    if (negative){
        val = (val * -1) + 1;
    }

    return (val-ZERO_OFFSET)/GAIN;  //Value in grams
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.